-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinet_convertions.c
33 lines (30 loc) · 1.16 KB
/
inet_convertions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <arpa/inet.h>
int main(void) {
/*
* the struct in_addr is defined in /usr/include/netinet/in.h which is included
* by arpa/inet.h. This struct only has a single member which is in_addr_t which is
* defined in /usr/include/sys/_types/_in_addr_t.h
* struct in_addr {
* in_addr_t s_addr;
* }
*
* This is only a type def:
* typedef __uint32_t in_addr_t; // base type for internet address
*/
struct in_addr a;
// presentation to network byte ordered binary
char p[INET_ADDRSTRLEN];
if (inet_pton(AF_INET, "127.0.0.1", &a)) {
// inet_ntoa network byte ordered binary to ascii
printf("Network byte order: %d\n", a.s_addr);
inet_ntop(AF_INET, &a, p, sizeof(p));
printf("Presentation: %s\n", p);
}
struct sockaddr_in s = {.sin_family = AF_INET, .sin_port = 9999, .sin_addr = a};
printf("sockaddre_in.sin_family: %d\n", s.sin_family);
printf("sockaddre_in.sin_port: %d\n", s.sin_port);
printf("sockaddre_in.sin_addr.s_addr: %s\n", inet_ntop(AF_INET, &s.sin_addr, p, sizeof(p)));
struct sockaddr *sa = (struct sockaddr *) &s;
return 0;
}