1 struct addrinfo {
2 int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
3 int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
4 int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
5 int ai_protocol; // use 0 for "any"
6 size_t ai_addrlen; // size of ai_addr in bytes
7 struct sockaddr *ai_addr; // struct sockaddr_in or _in6
8 char *ai_canonname; // full canonical hostname
9 struct addrinfo *ai_next; // linked list, next node
10 };
11
12
2 int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
3 int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
4 int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
5 int ai_protocol; // use 0 for "any"
6 size_t ai_addrlen; // size of ai_addr in bytes
7 struct sockaddr *ai_addr; // struct sockaddr_in or _in6
8 char *ai_canonname; // full canonical hostname
9 struct addrinfo *ai_next; // linked list, next node
10 };
11
12
AI_PASSIVE 表示用自己的IP地址。
AF_INET 表示用ipv, AF_INET6表示用ipv6, AF_UNSPEC表示不确定是ipv4还是ipv6
SOCK_STREAM 表示TCP, SOCK_DGRAM 表示UDP
1 struct sockaddr {
2 unsigned short sa_family; // address family, AF_xxx
3 char sa_data[14]; // 14 bytes of protocol address
4 };
5
6 // (IPv4 only--see struct sockaddr_in6 for IPv6)
7 struct sockaddr_in {
8 short int sin_family; // Address family, AF_INET
9 unsigned short int sin_port; // Port number
10 struct in_addr sin_addr; // Internet address
11 unsigned char sin_zero[8]; // Same size as struct sockaddr
12 };
13
14 // (IPv4 only--see struct in6_addr for IPv6)
15 // Internet address (a structure for historical reasons)
16 struct in_addr {
17 uint32_t s_addr; // that's a 32-bit int (4 bytes)
18 };
19
20 // (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4)
21 struct sockaddr_in6 {
22 u_int16_t sin6_family; // address family, AF_INET6
23 u_int16_t sin6_port; // port number, Network Byte Order
24 u_int32_t sin6_flowinfo; // IPv6 flow information
25 struct in6_addr sin6_addr; // IPv6 address
26 u_int32_t sin6_scope_id; // Scope ID
27 };
28
29 struct in6_addr {
30 unsigned char s6_addr[16]; // IPv6 address
31 };
2 unsigned short sa_family; // address family, AF_xxx
3 char sa_data[14]; // 14 bytes of protocol address
4 };
5
6 // (IPv4 only--see struct sockaddr_in6 for IPv6)
7 struct sockaddr_in {
8 short int sin_family; // Address family, AF_INET
9 unsigned short int sin_port; // Port number
10 struct in_addr sin_addr; // Internet address
11 unsigned char sin_zero[8]; // Same size as struct sockaddr
12 };
13
14 // (IPv4 only--see struct in6_addr for IPv6)
15 // Internet address (a structure for historical reasons)
16 struct in_addr {
17 uint32_t s_addr; // that's a 32-bit int (4 bytes)
18 };
19
20 // (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4)
21 struct sockaddr_in6 {
22 u_int16_t sin6_family; // address family, AF_INET6
23 u_int16_t sin6_port; // port number, Network Byte Order
24 u_int32_t sin6_flowinfo; // IPv6 flow information
25 struct in6_addr sin6_addr; // IPv6 address
26 u_int32_t sin6_scope_id; // Scope ID
27 };
28
29 struct in6_addr {
30 unsigned char s6_addr[16]; // IPv6 address
31 };
sockaddr和sockaddr_in是一回事,可以相互cast,由于历史原因sockaddr_in里面有in_addr,in_addr保存的就是ip地址
1 struct sockaddr_in sa; // IPv4
2 struct sockaddr_in6 sa6; // IPv6
3 inet_pton(AF_INET, "192.0.2.1", &(sa.sin_addr)); // IPv4
4 inet_pton(AF_INET6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); // IPv6
5
2 struct sockaddr_in6 sa6; // IPv6
3 inet_pton(AF_INET, "192.0.2.1", &(sa.sin_addr)); // IPv4
4 inet_pton(AF_INET6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); // IPv6
5
inet_pton用于把ip转换成int形式,inet_addr已经不推荐使用了。
1 inet_ntop(AF_INET, &(sa.sin_addr), ip4, INET_ADDRSTRLEN);
2 printf("The IPv4 address is: %s\n", ip4);
3 // IPv6:
4 char ip6[INET6_ADDRSTRLEN]; // space to hold the IPv6 string
5 struct sockaddr_in6 sa6; // pretend this is loaded with something
6 inet_ntop(AF_INET6, &(sa6.sin6_addr), ip6, INET6_ADDRSTRLEN);
7 printf("The address is: %s\n", ip6);
8
2 printf("The IPv4 address is: %s\n", ip4);
3 // IPv6:
4 char ip6[INET6_ADDRSTRLEN]; // space to hold the IPv6 string
5 struct sockaddr_in6 sa6; // pretend this is loaded with something
6 inet_ntop(AF_INET6, &(sa6.sin6_addr), ip6, INET6_ADDRSTRLEN);
7 printf("The address is: %s\n", ip6);
8
inet_ntop把int形式转换成dot形式。
1 int main(int argc, char *argv[])
2 {
3 struct addrinfo hints, *res, *p;
4 int status;
5 char ipstr[INET6_ADDRSTRLEN];
6 if (argc != 2) {
7 fprintf(stderr,"usage: showip hostname\n");
8 return 1;
9 }
10 memset(&hints, 0, sizeof hints);
11 hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
12 hints.ai_socktype = SOCK_STREAM;
13 if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
14 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
15 return 2;
16 }
17 printf("IP addresses for %s:\n\n", argv[1]);
18 for(p = res;p != NULL; p = p->ai_next) {
19 void *addr;
20 char *ipver;
21 // get the pointer to the address itself,
22 // different fields in IPv4 and IPv6:
23 if (p->ai_family == AF_INET) { // IPv4
24 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
25 addr = &(ipv4->sin_addr);
26 ipver = "IPv4";
27 } else { // IPv6
28 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
29 addr = &(ipv6->sin6_addr);
30 ipver = "IPv6";
31 }
32 // convert the IP to a string and print it:
33 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
34 printf(" %s: %s\n", ipver, ipstr);
35 }
36 freeaddrinfo(res); // free the linked list
37 return 0;
38 }
39
2 {
3 struct addrinfo hints, *res, *p;
4 int status;
5 char ipstr[INET6_ADDRSTRLEN];
6 if (argc != 2) {
7 fprintf(stderr,"usage: showip hostname\n");
8 return 1;
9 }
10 memset(&hints, 0, sizeof hints);
11 hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
12 hints.ai_socktype = SOCK_STREAM;
13 if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
14 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
15 return 2;
16 }
17 printf("IP addresses for %s:\n\n", argv[1]);
18 for(p = res;p != NULL; p = p->ai_next) {
19 void *addr;
20 char *ipver;
21 // get the pointer to the address itself,
22 // different fields in IPv4 and IPv6:
23 if (p->ai_family == AF_INET) { // IPv4
24 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
25 addr = &(ipv4->sin_addr);
26 ipver = "IPv4";
27 } else { // IPv6
28 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
29 addr = &(ipv6->sin6_addr);
30 ipver = "IPv6";
31 }
32 // convert the IP to a string and print it:
33 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
34 printf(" %s: %s\n", ipver, ipstr);
35 }
36 freeaddrinfo(res); // free the linked list
37 return 0;
38 }
39
1 int getaddrinfo(const char *node, // e.g. "www.example.com" or IP
2 const char *service, // e.g. "http" or port number
3 const struct addrinfo *hints,
4 struct addrinfo **res);
5
2 const char *service, // e.g. "http" or port number
3 const struct addrinfo *hints,
4 struct addrinfo **res);
5
getaddrinfo自动填充hints,然后赋值给res,取代了gethostbyname之类。
struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};
// (IPv4 only--see struct sockaddr_in6 for IPv6)
struct sockaddr_in {
short int sin_family; // Address family, AF_INET
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // Same size as struct sockaddr
};
// (IPv4 only--see struct in6_addr for IPv6)
// Internet address (a structure for historical reasons)
struct in_addr {
uint32_t s_addr; // that's a 32-bit int (4 bytes)
};
// (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4)
struct sockaddr_in6 {
u_int16_t sin6_family; // address family, AF_INET6
u_int16_t sin6_port; // port number, Network Byte Order
u_int32_t sin6_flowinfo; // IPv6 flow information
struct in6_addr sin6_addr; // IPv6 address
u_int32_t sin6_scope_id; // Scope ID
};
struct in6_addr {
unsigned char s6_addr[16]; // IPv6 address
};
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};
// (IPv4 only--see struct sockaddr_in6 for IPv6)
struct sockaddr_in {
short int sin_family; // Address family, AF_INET
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // Same size as struct sockaddr
};
// (IPv4 only--see struct in6_addr for IPv6)
// Internet address (a structure for historical reasons)
struct in_addr {
uint32_t s_addr; // that's a 32-bit int (4 bytes)
};
// (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4)
struct sockaddr_in6 {
u_int16_t sin6_family; // address family, AF_INET6
u_int16_t sin6_port; // port number, Network Byte Order
u_int32_t sin6_flowinfo; // IPv6 flow information
struct in6_addr sin6_addr; // IPv6 address
u_int32_t sin6_scope_id; // Scope ID
};
struct in6_addr {
unsigned char s6_addr[16]; // IPv6 address
};