Guest User

Untitled

a guest
Oct 6th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1.   #include <stdio.h>
  2.   #include <unistd.h>
  3.   #include <stdlib.h>
  4.   #include <errno.h>
  5.   #include <string.h>
  6.   #include <sys/types.h>
  7.   #include <sys/stat.h>
  8.   #include <sys/socket.h>
  9.   #include <netinet/in.h>
  10.   #include <arpa/inet.h>
  11.  
  12.   /*
  13.    * This saves lines of code later
  14.    */
  15.   static void
  16.   bail(const char *on_what) {
  17.       perror(on_what);     /* Report error */
  18.       exit(1);            /* Exit Program */
  19.   }
  20.  
  21.   /*
  22.    * This function accepts as input a socket
  23.    * for which a socket address must be
  24.    * determined for it. Then the address
  25.    * is converted into a string and returned.
  26.    *
  27.    * If an error occurs, NULL is returned.
  28.    */
  29.   char *
  30.   sock_addr(int s,char *buf,size_t bufsiz) {
  31.       int z;            /* Status return code */
  32.       struct sockaddr_in adr_inet;/* AF_INET */
  33.  
  34.       /*
  35.        * Obtain the address of the socket
  36.        */
  37. socklen_t  len_inet = sizeof adr_inet;
  38.       z = getsockname(s,
  39.           (struct sockaddr *)&adr_inet,
  40.           &len_inet);
  41.  
  42.       if ( z == -1 )
  43.           return NULL;    /* Failed */
  44.  
  45.       /*
  46.        * Convert address into a string
  47.        * form that can be displayed
  48.        */
  49.       snprintf(buf,bufsiz,
  50.       "%s:%lu:%u",
  51.           inet_ntoa(adr_inet.sin_addr),
  52.       adr_inet.sin_addr,
  53.           (unsigned)ntohs(adr_inet.sin_port));
  54.  
  55.       return buf;
  56.   }
  57.  
  58.   /*
  59.    * Main Program
  60.    */
  61.   int
  62.   main(int argc,char **argv,char **envp) {
  63.       int z;            /* Status return code */
  64.       int sck_inet;                /* Socket  */
  65.       struct sockaddr_in adr_inet;/* AF_INET */
  66.       int len_inet;                /* length  */
  67.       char buf[64];             /* Work buffer */
  68.  
  69.       /*
  70.        * Create an IPv4 Internet Socket
  71.        */
  72.       sck_inet = socket(AF_INET,SOCK_STREAM,0);
  73.  
  74.       if ( sck_inet == -1 )
  75.           bail("socket()");
  76.  
  77.       /*
  78.        * Create an AF_INET address
  79.        */
  80.       memset(&adr_inet,0,sizeof adr_inet);
  81.       adr_inet.sin_family = AF_INET;
  82.       adr_inet.sin_port = htons(0);
  83.       inet_aton("127.0.0.1",&adr_inet.sin_addr);
  84.       len_inet = sizeof adr_inet;
  85.  
  86.       /*
  87.        * Now bind the address to the socket
  88.        */
  89.       z = bind(sck_inet,
  90.           (struct sockaddr *)&adr_inet,
  91.           len_inet);
  92.       if ( z == -1 )
  93.           bail("bind()");
  94.  
  95.      /*
  96.       * Now test our sock_addr() function
  97.       */
  98.      if ( !sock_addr(sck_inet,buf,sizeof buf) )
  99.          bail("sock_addr()");
  100.  
  101.      printf("Address is '%s'\n",buf);
  102.  
  103.      close(sck_inet);
  104.     return 0;
  105.  }
Advertisement
Add Comment
Please, Sign In to add comment