home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / protocol / tcpip / 5068 < prev    next >
Encoding:
Text File  |  1992-11-06  |  3.8 KB  |  116 lines

  1. Newsgroups: comp.protocols.tcp-ip
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!sdd.hp.com!caen!destroyer!cs.ubc.ca!alberta!cpsc.ucalgary.ca!xenlink!newt.cuc.ab.ca!deraadt
  3. From: deraadt@newt.cuc.ab.ca (Theo de Raadt)
  4. Subject: Re: Sun Generic Broadcasting
  5. In-Reply-To: mc@math.math.mv.dupont.com's message of 4 Nov 92 16: 40:30 GMT
  6. Message-ID: <DERAADT.92Nov5144740@newt.newt.cuc.ab.ca>
  7. Sender: news@newt.cuc.ab.ca
  8. Nntp-Posting-Host: newt
  9. Organization: little lizard city
  10. References: <1004@math.math.mv.dupont.com>
  11. Date: Thu, 5 Nov 1992 21:47:40 GMT
  12. Lines: 102
  13.  
  14. [Distribution changed from "usa" to "world". Unix is the same in Europe, isn't it??]
  15.  
  16. In article <1004@math.math.mv.dupont.com> mc@math.math.mv.dupont.com (Matt Cunningham-Software Development) writes:
  17. > I am trying to find the proper broadcast IP address to send a packet (BOOTP 
  18. > packet to be specific - but the packet type is not important) to so that
  19. > everyone on the network will receive it.
  20. [deleted descriptions of numerous attempts to make it work...]
  21.  
  22. The bind() call will take INADDR_ANY to indicate that it will take a
  23. connect from any address. It's more difficult when you send packets
  24. out -- you must indicate _exactly_ where you want to send to. A
  25. unix machine may have multiple interfaces, with multiple addresses.
  26. You need to specify the broadcast address for the particular network
  27. you are interested in. If you wish to send on all the attached networks,
  28. you need to do multiple send()'s.
  29.  
  30. The program below will list the broadcast addresses for all of the
  31. networks that your machine is directly connected to. It will skip
  32. LOOPBACK and POINTTOPOINT links, simply because those do not have a
  33. broadcast address. For your problem, just sendto() each address that
  34. is discovered by this routine below..
  35.  
  36. Note that on some machines you'll need to enable broadcasting before
  37. packets will actually make it onto the wire. You don't need this on
  38. Suns, but it does now hurt. Other machines (ie. SGI) DO need it.
  39.  
  40.         if( (sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  41.                 perror("socket");
  42.                 return -1;
  43.         }
  44.     i = 1;
  45.         setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
  46.  
  47. On some operating system (Ultrix?) only root processes can broadcast,
  48. what a crock.
  49.  
  50. Hope this helps you.
  51.  <tdr.
  52.  
  53. ----------------------------------------------------------------------
  54. #include <sys/param.h>
  55. #include <sys/types.h>
  56. #include <sys/ioctl.h>
  57. #include <sys/signal.h>
  58. #include <sys/file.h>
  59. #include <sys/socket.h>
  60. #include <arpa/inet.h>
  61. #include <net/if.h>
  62. #include <netinet/in.h>
  63. #include <dirent.h>
  64. #include <netdb.h>
  65. #include <stdio.h>
  66. #include <string.h>
  67.  
  68. main()
  69. {
  70.     struct ifconf ifc;
  71.     struct ifreq ifreq, *ifr;
  72.     struct sockaddr_in *sin;
  73.     struct in_addr in;
  74.     struct net *n;
  75.     int i, sock;
  76.     char inbuf[1024];
  77.  
  78.     if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  79.         perror("socket");
  80.         return -1;
  81.     }
  82.  
  83.     ifc.ifc_len = sizeof inbuf;
  84.     ifc.ifc_buf = inbuf;
  85.     if( ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
  86.         close(sock);
  87.         perror("ioctl");
  88.         return -1;
  89.     }
  90.     ifr = ifc.ifc_req;
  91.     for(i=ifc.ifc_len/sizeof(struct ifreq); i>0; i--, ifr++) {
  92.         ifreq = *ifr;
  93.         if( ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  94.             perror("ioctl");
  95.             continue;
  96.         }
  97.         if( (ifreq.ifr_flags & IFF_BROADCAST) &&
  98.             (ifreq.ifr_flags & IFF_UP) &&
  99.             ifr->ifr_addr.sa_family == AF_INET) {
  100.             sin = (struct sockaddr_in *)&ifr->ifr_addr;
  101.             if( ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0 ) {
  102.                 in = inet_makeaddr(inet_netof(sin->sin_addr.s_addr),
  103.                     (int)INADDR_ANY);
  104.             } else { 
  105.                 in = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr;
  106.             }
  107.             printf("address %s\n", inet_ntoa(in));
  108.         }
  109.     }
  110.     close(sock);
  111. }
  112. ----------------------------------------------------------------------
  113. --
  114.  
  115. This space not left unintentionally unblank.        deraadt@newt.cuc.ab.ca
  116.