/* udprecv.c * Simon Kirby , 2018-07-13 * * Test IP_MULTICAST_ALL functionality. * * Compile with: * * gcc -o udprecv udprecv.c -O2 -Wall */ #include #include #include #include #include #include #include #include #include #include #include #include /* Globals */ #define PAYLOAD_SIZE 65536 static char payload[PAYLOAD_SIZE]; /* Socket functions */ static int get_socket(){ int fd,option; fd = socket(AF_INET,SOCK_RAW,112); // 112 == VRRP // fd = socket(AF_INET,SOCK_DGRAM,0); if (fd == -1){ fprintf(stderr,"Unable to create datagram socket: %s\n", strerror(errno)); return -1; } option = 0; if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(void *)&option,sizeof(option))){ fprintf(stderr,"Unable to setsockopt() on udp listening socket: %s\n", strerror(errno)); return -1; } option = 0; setsockopt(fd,SOL_SOCKET,SO_BROADCAST,(void *)&option,sizeof(option)); option = 1; setsockopt(fd,SOL_IP,IP_HDRINCL,(void *)&option,sizeof(option)); option = 0; setsockopt(fd,SOL_IP,IP_MULTICAST_ALL,(void *)&option,sizeof(option)); struct sockaddr_in udp; udp.sin_addr.s_addr = htonl(INADDR_ANY); udp.sin_port = htons(8848); udp.sin_family = AF_INET; if (bind(fd,(struct sockaddr *)&udp,sizeof(udp))){ fprintf(stderr,"Unable to bind() udp socket: %s\n", strerror(errno)); return -1; } return fd; } int main(int argc,char *argv[]){ int fd, r; struct sockaddr_in remote; socklen_t addrlen = sizeof(remote); fd = get_socket(); if (fd == -1) exit(-1); while (1) { r = recvfrom(fd,payload,PAYLOAD_SIZE,0,(struct sockaddr *)&remote,&addrlen); if (r > 0) { fprintf(stderr, "recv %u\n", r); } } exit(0); }