2 /* broadcast a string on all interfaces, to udp_port(); */
6 #include <sys/socket.h>
9 #include <sys/sockio.h>
11 #include <netinet/in.h>
20 static struct ifconf iflist;
21 static int interfaces;
24 static int ifconfinit()
27 static char buf[2048];
29 iflist.ifc_len = sizeof(buf);
31 /* printf("ifc_len = %d\n", iflist.ifc_len); / **/
32 if (ioctl(sock, SIOCGIFCONF, (char *)&iflist)< 0) return -1;
33 /* printf("ifc_len = %d\n", iflist.ifc_len); / **/
34 interfaces = iflist.ifc_len / sizeof(struct ifreq);
36 /* printf("interfaces = %d\n",interfaces); / **/
37 /* for (in=0; in<interfaces ; in++) if (iflist.ifc_req[in].ifr_name[0]=='\0') break; */
38 /* interfaces = in; */
39 /* printf("interfaces = %d\n",interfaces); / **/
44 static void sendaddr(struct sockaddr_in addr, char *packet)
46 addr.sin_port = udp_port();
47 addr.sin_family = AF_INET;
49 sendto(sock, packet, strlen(packet), 0, (struct sockaddr *)&addr, sizeof(addr));
52 static void sendinter(int n, char *packet)
56 ifr = iflist.ifc_req[n];
57 /* printf("interface = %s\n", ifr.ifr_name); */
58 ioctl(sock, SIOCGIFBRDADDR, &ifr);
59 sendaddr(*(struct sockaddr_in*)&ifr.ifr_broadaddr, packet);
62 void broadcast(char *packet)
67 struct sockaddr_in myaddr;
71 sock = socket(AF_INET, SOCK_DGRAM, 0);
72 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&a, 4);
73 memset(&myaddr, 0, sizeof(myaddr));
74 myaddr.sin_family = AF_INET;
79 myaddr.sin_port = htons(port);
80 while (bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr))== -1) myaddr.sin_port = htons(--port);
81 if (ifconfinit()!= -1) /* gets list of interfaces */
82 for (n = 0 ; n < interfaces ; n++)