# HG changeset patch # User John Tsiombikas # Date 1409327814 -10800 # Node ID affaad5fcd309e02e559042769c98b7ae79946e1 initial commit diff -r 000000000000 -r affaad5fcd30 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Aug 29 18:56:54 2014 +0300 @@ -0,0 +1,3 @@ +\.o$ +\.d$ +\.swp$ diff -r 000000000000 -r affaad5fcd30 inptools/test/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inptools/test/Makefile Fri Aug 29 18:56:54 2014 +0300 @@ -0,0 +1,12 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = test + +CFLAGS = -pedantic -Wall -g + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r affaad5fcd30 inptools/test/src/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inptools/test/src/main.c Fri Aug 29 18:56:54 2014 +0300 @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAGIC 0x6d0f +#define DISCOVER 1 +#define DREPLY 2 + +struct message { + int magic; + int type; + char data[24]; +}; + +int discover(struct sockaddr_in *client_sa); + +int opt_dport = 0x6d0f; /* discover broadcast port */ +unsigned int opt_timeout = 5000; /* 10 sec discovery timeout */ + +int main(int argc, char **argv) +{ +} + +int discover(struct sockaddr_in *client_sa) +{ + int s, true = 1; + struct sockaddr_in sa; + unsigned int timeout = opt_timeout; + struct message msg; + struct timeval tv, tv_timeout; + + if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { + perror("failed to create discover datagram socket"); + return -1; + } + setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true); + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(opt_dport); + sa.sin_addr.s_addr = htonl(INADDR_ANY); + + if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) { + perror("failed to bind datagram socket"); + close(s); + return -1; + } + + do { + msg.magic = MAGIC; + msg.type = DISCOVER; + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(opt_dport); + sa.sin_addr.s_addr = 0xffffffff; + + if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) { + perror("failed to send discovery bcast dgram"); + close(s); + return -1; + } + } while(0); + + close(s); + return 0; +}