I am trying to learn network programming from Beej's book on network programming. There is a piece of code in section 5.1
/*
** showip.c -- show IP addresses for a host given on the command line
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}
When I compile this inside Ubuntu terminal, I get the following error
showip.c: In function ‘main’:
showip.c:15:25: error: storage size of ‘hints’ isn’t known
15 | struct addrinfo hints, *res, *p;
| ^~~~~
showip.c:28:23: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
28 | if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
| ^~~~~~~~~~~
showip.c:29:54: warning: implicit declaration of function ‘gai_strerror’; did you mean ‘strerror’? [-Wimplicit-function-declaration]
29 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
| ^~~~~~~~~~~~
| strerror
showip.c:29:48: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
29 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
| ~^ ~~~~~~~~~~~~~~~~~~~~
| | |
| | int
| char *
| %d
showip.c:35:37: error: invalid use of undefined type ‘struct addrinfo’
35 | for(p = res;p != NULL; p = p->ai_next) {
| ^~
showip.c:41:22: error: invalid use of undefined type ‘struct addrinfo’
41 | if (p->ai_family == AF_INET) { // IPv4
| ^~
showip.c:42:75: error: invalid use of undefined type ‘struct addrinfo’
42 | struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
| ^~
showip.c:46:77: error: invalid use of undefined type ‘struct addrinfo’
46 | struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
| ^~
showip.c:52:28: error: invalid use of undefined type ‘struct addrinfo’
52 | inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
| ^~
showip.c:56:9: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
56 | freeaddrinfo(res); // free the linked list
| ^~~~~~~~~~~~
I checked the errata page given on his site and did not find it helpful. It seems that all required libraries are included here, so I don't see what could be the source of errors. If somebody can point it out, that would be nice. Thanks
As @stark mentioned, Storage size of addrinfo isn't known should help compile the code.
As I don't have enough reputation to comment, in regards to the runtime error getaddrinfo: Name or service not known
, you need to remove the protocol https://
and the terminating forward slash.
If you look at the manpages for getaddrinfo
:
node specifies either a
numerical network address (for IPv4, numbers-and-dots notation as
supported by inet_aton(3); for IPv6, hexadecimal string format as
supported by inet_pton(3)), or a network hostname
We only need the hostname and not the protocol. So you should run the program as follows:
$ ./a.out www.google.com
IP addresses for www.google.com:
IPv4: 142.251.163.106
...
In fact if you look at the page you linked, it gives you sample outputs:
$ showip www.example.net
IP addresses for www.example.net:
IPv4: 192.0.2.88
$ showip ipv6.example.com
IP addresses for ipv6.example.com:
IPv4: 192.0.2.101
IPv6: 2001:db8:8c00:22::171