Advertisement
Guest User

Untitled

a guest
Jul 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.29 KB | None | 0 0
  1. / # cat test.c && ./test -v && strace ./test && ifconfig -a
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4.  
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8.  
  9. void print_ai( struct addrinfo *ai ) {
  10.  
  11.     unsigned family;
  12.     char addrs[ INET6_ADDRSTRLEN ];
  13.     unsigned port;
  14.     struct sockaddr_in *sai;
  15.     struct sockaddr_in6 *sai6;
  16.  
  17.     if ( ai->ai_addr ) {
  18.         switch( ai->ai_addr->sa_family ) {
  19.         case AF_INET:
  20.             sai = ai->ai_addr;
  21.             inet_ntop( AF_INET, & sai->sin_addr, addrs, sizeof( addrs ) );
  22.             break;
  23.         case AF_INET6:
  24.             sai6 = ai->ai_addr;
  25.             inet_ntop( AF_INET6, & sai6->sin6_addr, addrs, sizeof( addrs ) );
  26.             break;
  27.         default:
  28.             break;
  29.         }
  30.     }
  31.    
  32.     printf(
  33.         "struct addrinfo {" "\n"
  34.         "   ai_flags:        %x" "\n"
  35.         "   ai_family:       %u" "\n"
  36.         "   ai_socktype:     %u" "\n"
  37.         "   ai_protocol:     %u" "\n"
  38.         "   ai_addrlen:      %u" "\n"
  39.         "   ai_addr:           " "\n"
  40.         "      family:       %u" "\n"
  41.         "      addr:         %s" "\n"
  42.         "      port:         %u" "\n"
  43.         "   ai_canonname:    %s" "\n"
  44.         "   ai_next:         %p" "\n"
  45.         "}                     " "\n",
  46.         ai->ai_flags,
  47.         ai->ai_family,
  48.         ai->ai_socktype,
  49.         ai->ai_protocol,
  50.         ai->ai_addrlen,
  51.         NULL == ai->ai_addr ? "??" : ai->ai_addr->sa_family,
  52.         NULL == ai->ai_addr ? "??" : addrs,
  53.         NULL == ai->ai_addr ? "??" : port,
  54.         ai->ai_canonname ? ai->ai_canonname : "(null)",
  55.         ai->ai_next
  56.     );
  57. }
  58.  
  59. int main( int argc, char *argv[] ) {
  60.     int r;
  61.  
  62.     struct addrinfo hints = {
  63.         .ai_family = PF_UNSPEC,
  64.         .ai_socktype = SOCK_STREAM,
  65.         .ai_flags = AI_V4MAPPED | AI_ADDRCONFIG,
  66.     };
  67.     struct addrinfo *res;
  68.     struct addrinfo *it;
  69.  
  70.     bool verbose = argc > 1;
  71.  
  72.     r = getaddrinfo( "localhost", NULL, & hints, & res );
  73.     if ( 0 != r ) {
  74.         perror( "getaddrinfo" );
  75.         return r;
  76.     }
  77.  
  78.     if ( verbose ) {
  79.         for( it = res; NULL != it; it = it->ai_next ) {
  80.             print_ai( it );
  81.         }
  82.     }
  83.  
  84.     freeaddrinfo( res );
  85.  
  86.     return r;
  87. }
  88.  
  89. struct addrinfo {
  90.    ai_flags:        0
  91.    ai_family:       2
  92.    ai_socktype:     1
  93.    ai_protocol:     6
  94.    ai_addrlen:      16
  95.    ai_addr:          
  96.       family:       2
  97.       addr:         127.0.0.1
  98.       port:         0
  99.    ai_canonname:    localhost
  100.    ai_next:         0x55d919dce0d0
  101. }                    
  102. struct addrinfo {
  103.    ai_flags:        0
  104.    ai_family:       10
  105.    ai_socktype:     1
  106.    ai_protocol:     6
  107.    ai_addrlen:      28
  108.    ai_addr:          
  109.       family:       10
  110.       addr:         ::1
  111.       port:         0
  112.    ai_canonname:    localhost
  113.    ai_next:         0
  114. }                    
  115. execve("./test", ["./test"], 0x7ffcb6904280 /* 6 vars */) = 0
  116. arch_prctl(ARCH_SET_FS, 0x7fc81f0cdb88) = 0
  117. set_tid_address(0x7fc81f0cdbc0)         = 101
  118. mprotect(0x7fc81f0ca000, 4096, PROT_READ) = 0
  119. mprotect(0x55eccc5f2000, 4096, PROT_READ) = 0
  120. open("/etc/hosts", O_RDONLY|O_CLOEXEC)  = 3
  121. fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
  122. readv(3, [{iov_base="", iov_len=0}, {iov_base="127.0.0.1\tlocalhost\n::1\tlocalhos"..., iov_len=1024}], 2) = 174
  123. readv(3, [{iov_base="", iov_len=0}, {iov_base="", iov_len=1024}], 2) = 0
  124. close(3)                                = 0
  125. socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 3
  126. connect(3, {sa_family=AF_INET6, sin6_port=htons(65535), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = 0
  127. getsockname(3, {sa_family=AF_INET6, sin6_port=htons(53878), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, [28]) = 0
  128. close(3)                                = 0
  129. socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP) = 3
  130. connect(3, {sa_family=AF_INET6, sin6_port=htons(65535), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = -1 EADDRNOTAVAIL (Address not available)
  131. close(3)                                = 0
  132. exit_group(0)                           = ?
  133. +++ exited with 0 +++
  134. eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
  135.           inet addr:172.17.0.3  Bcast:172.17.255.255  Mask:255.255.0.0
  136.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  137.           RX packets:26909 errors:0 dropped:0 overruns:0 frame:0
  138.           TX packets:13292 errors:0 dropped:0 overruns:0 carrier:0
  139.           collisions:0 txqueuelen:0
  140.           RX bytes:39372973 (37.5 MiB)  TX bytes:720894 (703.9 KiB)
  141.  
  142. ip6tnl0   Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
  143.           NOARP  MTU:1452  Metric:1
  144.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  145.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  146.           collisions:0 txqueuelen:1
  147.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
  148.  
  149. lo        Link encap:Local Loopback  
  150.           inet addr:127.0.0.1  Mask:255.0.0.0
  151.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  152.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  153.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  154.           collisions:0 txqueuelen:1
  155.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
  156.  
  157. tunl0     Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-87-AF-00-00-00-00-00-00-00-00  
  158.           NOARP  MTU:1480  Metric:1
  159.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  160.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  161.           collisions:0 txqueuelen:1
  162.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement