Skip to content

Instantly share code, notes, and snippets.

@brianredbeard
Last active May 16, 2020 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianredbeard/59fae5040d38361a47477f4928ddfcfd to your computer and use it in GitHub Desktop.
Save brianredbeard/59fae5040d38361a47477f4928ddfcfd to your computer and use it in GitHub Desktop.
Examples of name resolution (using gethostbyname() ) used in an explanation.
FROM scratch
ADD ghbn /ghbn
CMD ["/ghbn", "example.com"]
FROM scratch
ADD ghbn-go /ghbn
CMD ["/ghbn", "example.com"]
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
//
// gcc -static -ggdb -Wall -Wextra -Wpedantic -o ghbn ghbn.c
//
int main(int argc, char * argv[]) {
if (argc!=2) {
exit(1);
}
struct hostent *hosts;
struct in_addr *address;
hosts = gethostbyname(argv[1]);
address = (struct in_addr *) (hosts->h_addr);
printf("%s\n",inet_ntoa(*address));
}
package main
//
// go build -o ghbn-go ghbn.go
//
import (
"fmt"
"net"
"os"
)
func main() {
hostname := os.Args[1]
addrs, err := net.LookupHost(hostname)
if err != nil {
fmt.Print("Error resolving ", hostname)
os.Exit(1)
}
for _, v := range addrs {
fmt.Print(v)
fmt.Print("\n")
}
}
@brianredbeard
Copy link
Author

Compiled as:

$ gcc -static -ggdb -Wall -Wextra -Wpedantic -o ghbn ghbn.c 
/usr/bin/ld: /tmp/ccqvMHZq.o: in function `main':
/tmp/gethostbyname/ghbn.c:13: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared 
libraries from the glibc version used for linking
$ go build -o ghbn-go ghbn.go

Example execution (native host):

$ ./ghbn example.com
93.184.216.34
$ ./ghbn-go example.com
93.184.216.34
2606:2800:220:1:248:1893:25c8:1946

Example execution (Docker, C):

$ podman run -it ghbn-c
$ echo $?
139

Example execution (Docker, Go):

$ podman run -it ghbn-go 
93.184.216.34
2606:2800:220:1:248:1893:25c8:1946

@brianredbeard
Copy link
Author

$ podman export ghbn-c | tar tvf - 
drwxr-xr-t root/root         0 2020-05-15 19:47 dev/
drwxr-xr-x root/root         0 2020-05-15 19:47 etc/
-rwx------ root/root         0 2020-05-15 19:47 etc/hostname
-rwx------ root/root         0 2020-05-15 19:47 etc/hosts
-rwx------ root/root         0 2020-05-15 19:47 etc/resolv.conf
-rwxrwxr-x root/root   1843296 2020-05-15 18:15 ghbn
drwxr-xr-t root/root         0 2020-05-15 19:47 proc/
drwxr-xr-x root/root         0 2020-05-15 19:47 run/
-rwx------ root/root         0 2020-05-15 19:47 run/.containerenv
drwxr-xr-t root/root         0 2020-05-15 19:47 sys/
$ podman export ghbn-go | tar tvf - 
drwxr-xr-t root/root         0 2020-05-15 19:46 dev/
drwxr-xr-x root/root         0 2020-05-15 19:46 etc/
-rwx------ root/root         0 2020-05-15 19:46 etc/hostname
-rwx------ root/root         0 2020-05-15 19:46 etc/hosts
-rwx------ root/root         0 2020-05-15 19:46 etc/resolv.conf
-rwxrwxr-x root/root   2929560 2020-05-15 19:08 ghbn
drwxr-xr-t root/root         0 2020-05-15 19:46 proc/
drwxr-xr-x root/root         0 2020-05-15 19:46 run/
-rwx------ root/root         0 2020-05-15 19:46 run/.containerenv
drwxr-xr-t root/root         0 2020-05-15 19:46 sys/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment