home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / inetray / netinfo.c < prev    next >
C/C++ Source or Header  |  1992-06-28  |  3KB  |  93 lines

  1. /*======================================================================
  2.                     N E T I N F O . C 
  3.                     doc: Fri Jan 17 13:35:38 1992
  4.                     dlm: Sun Jun 28 18:23:47 1992
  5.                     (c) 1992 ant@julia
  6.                     uE-Info: 51 0 T 0 0 72 2 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. /*
  10.    Note: Most of the stuff done in here can be found in manpage if(4n)
  11.  */
  12.  
  13. #define ICBUFS    (1 + 10 * sizeof(struct ifreq))    /* interface request bufs */
  14.  
  15. #include    <stdio.h>
  16. #include     <sys/types.h>
  17. #include    <sys/socket.h>
  18. /*#include    <sys/sockio.h>*/        /* original SUN */
  19. #include    <sys/ioctl.h>            /* seems to be more portable */
  20. #include    <net/if.h>
  21. #include    <netinet/in.h>
  22. #include    <arpa/inet.h>
  23. #include    <stdio.h>
  24. #include    <string.h>
  25.  
  26. main ()
  27. {
  28.     int     sock,on=1,nif,i,found=0;
  29.     char    icBuf[ICBUFS];
  30.     struct    ifconf         ifc;
  31.     struct     ifreq        *ifr;
  32.     struct     sockaddr_in sin;
  33.  
  34.     /* get udp socket */
  35.     if ((sock = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
  36.         perror("socket");
  37.         exit(1);
  38.     }
  39.     /* prepare for broadcast */
  40.     if (setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&on,sizeof(int)) < 0) {
  41.         perror("setsockopt");
  42.         exit(1);
  43.     }
  44.     /* get ALL interface configs */
  45.     ifc.ifc_len = ICBUFS;
  46.     ifc.ifc_buf = icBuf;
  47.     if (ioctl(sock,SIOCGIFCONF,(char *)&ifc) < 0) {
  48.         perror("ioctl");
  49.         exit(1);
  50.     }
  51.     if (ifc.ifc_len == ICBUFS-1) 
  52.         printf("Warning: buffer full; some interfaces may not be reported!\n");
  53.     nif = ifc.ifc_len/sizeof(struct ifreq);
  54.     
  55.     /* now extract values */
  56.     for (i=0,ifr = ifc.ifc_req; i<nif; i++,ifr++) {
  57.         /* Only INET */
  58.         if (ifr->ifr_addr.sa_family != AF_INET) continue;
  59.         /* Get Flags */
  60.         if (ioctl(sock,SIOCGIFFLAGS,(char *)ifr) < 0) {
  61.             perror("ioctl");
  62.             exit(1);
  63.         }
  64.         /* Skip worthless interfaces */
  65.         if (((ifr->ifr_flags & IFF_UP) == 0) ||
  66.             ((ifr->ifr_flags & IFF_LOOPBACK) != 0) ||
  67.             ((ifr->ifr_flags &
  68.             (IFF_BROADCAST | IFF_POINTOPOINT)) == 0)) continue;
  69.         found++;
  70.         bcopy((char *)&ifr->ifr_addr,
  71.               (char *)&sin,
  72.               sizeof sin);
  73.         printf("Interface <%s>:\n",ifr->ifr_name);
  74.         printf("\tLocal Address: %s\n",
  75.             inet_ntoa(sin.sin_addr));
  76.         /* bcast interface */
  77.         if (ifr->ifr_flags & IFF_BROADCAST) {
  78.             if (ioctl(sock,SIOCGIFBRDADDR,(char *)ifr) < 0) {
  79.                 perror("ioctl");
  80.                 exit(1);
  81.             }
  82.             found++;
  83.                     bcopy((char *)&ifr->ifr_addr,
  84.                           (char *)&sin,
  85.                           sizeof sin);
  86.             printf("\tBroadcast Address: %s\n",
  87.                 inet_ntoa(sin.sin_addr));
  88.                 }
  89.     }
  90.     close(sock);
  91. }
  92.  
  93.