Skip to content

Instantly share code, notes, and snippets.

@brianredbeard
Last active May 16, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@brianredbeard brianredbeard commented May 16, 2020

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

This comment has been minimized.

Copy link
Owner Author

@brianredbeard brianredbeard commented May 16, 2020

$ 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