Created
March 5, 2013 05:11
-
-
Save guyc/5088179 to your computer and use it in GitHub Desktop.
Testing UDP broadcast on unconfigured network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <net/if.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <netdb.h> | |
#define HCP_PORT 12345 | |
struct iaddr { int len; unsigned char iabuf[16]; }; | |
void send_announce(void) | |
{ | |
int result; | |
//struct sockaddr to; | |
struct addrinfo hints; | |
struct in_addr inaddr_any; | |
struct sockaddr_in sockaddr_broadcast; | |
sockaddr_broadcast.sin_family = AF_INET; | |
sockaddr_broadcast.sin_port = htons(HCP_PORT); | |
sockaddr_broadcast.sin_addr.s_addr = INADDR_BROADCAST; | |
inaddr_any.s_addr = INADDR_ANY; | |
//memset(&to, 0, sizeof(to)); | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_DGRAM; | |
hints.ai_flags = AI_PASSIVE; | |
hints.ai_protocol = 0; | |
hints.ai_canonname = NULL; | |
hints.ai_addr = NULL; | |
hints.ai_next = NULL; | |
char *port = "12345"; | |
printf("getaddrinfo\n"); | |
struct addrinfo *results; | |
int s = getaddrinfo(NULL, port, &hints, &results); | |
if (s != 0) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); | |
exit(EXIT_FAILURE); | |
} | |
struct addrinfo *rp; | |
for (rp = results; rp != NULL; rp = rp->ai_next) { | |
printf("Opening socket\n"); | |
// create a socket using this address | |
int sock = socket(rp->ai_family, | |
rp->ai_socktype, | |
rp->ai_protocol); | |
if (sock == -1) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(errno)); | |
} else { | |
int optval; | |
//int optlen = sizeof(optval); | |
result = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)); | |
if (result == -1) { | |
fprintf(stderr, "setsockopt: %s\n", strerror(errno)); | |
} | |
char *interface = "eth0"; | |
struct ifreq ifr; | |
memset(&ifr, 0, sizeof(ifr)); | |
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), interface); | |
result = setsockopt(sock, | |
SOL_SOCKET, | |
SO_BINDTODEVICE, | |
&ifr, | |
sizeof (ifr)); | |
if (result == -1) { | |
fprintf(stderr, "setsockopt bind: %s\n", strerror(errno)); | |
} | |
char *packet = "HELLONETWORK"; | |
int packet_length = strlen(packet); | |
struct sockaddr_in *to = &sockaddr_broadcast; | |
int result = sendto(sock, | |
packet, | |
packet_length, | |
0, | |
(struct sockaddr *)to, | |
sizeof(*to)); | |
fprintf(stderr, "sento return %d\n", result); | |
if (result == -1) { | |
fprintf(stderr, "sendto: %s\n", strerror(errno)); | |
} | |
close(sock); | |
} | |
} | |
freeaddrinfo(results); | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("hello world\n"); | |
// discover interfaces - actually only want to discover 1 interface, so use arg. | |
// send_announce - to announce we are here. | |
send_announce(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment