#include #include #include #include #include #include #include #include #include #include #include "connect.h" #include "buffer.h" int sd, if_sd; struct sockaddr_ll *bc; /* Return raw 802.2 socket */ int open_802_2(char *ifname) { sd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_802_2)); if(sd < 0) { perror("Failed to open socket: "); exit(EXIT_FAILURE); } if(create_broadcast(ifname) == NULL) { fputs("Failed to create ethernet broadcast.\n", stderr); exit(EXIT_FAILURE); } return sd; } /* Get interface ID for sockaddr_ll */ int get_interface(char *name) { struct ifreq ifr; if_sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if(if_sd == -1) { perror(name); exit(EXIT_FAILURE); } strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); if(ioctl(if_sd, SIOCGIFINDEX, &ifr) < 0) { perror("Failed ioctl"); exit(EXIT_FAILURE); } close(if_sd); return ifr.ifr_ifindex; } struct sockaddr_ll *create_broadcast(char *ifname) { bc = malloc(sizeof *bc); if(bc == NULL) return NULL; /* Setup broadcast */ bc->sll_family = AF_PACKET; bc->sll_protocol = 1; bc->sll_ifindex = get_interface(ifname); bc->sll_hatype = ARPHRD_IEEE802; bc->sll_pkttype = PACKET_BROADCAST; bc->sll_halen = IFHWADDRLEN; memset(bc->sll_addr, 0, sizeof bc->sll_addr); memset(bc->sll_addr, 0xFF, IFHWADDRLEN); return bc; } int send_buffer(Buffer *b) { if(b == NULL || b->data == NULL || b->used == 0) return -2; if(sendto(sd, b->data, b->used, 0, (struct sockaddr *) bc, sizeof *bc) == -1) { perror("Failed broadcast"); return -1; } return 1; }