vrmodel
diff inptools/test/src/main.c @ 0:affaad5fcd30
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 29 Aug 2014 18:56:54 +0300 |
parents | |
children | be91b72ce3f9 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/inptools/test/src/main.c Fri Aug 29 18:56:54 2014 +0300 1.3 @@ -0,0 +1,73 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <unistd.h> 1.8 +#include <sys/types.h> 1.9 +#include <sys/socket.h> 1.10 +#include <arpa/inet.h> 1.11 +#include <sys/time.h> 1.12 +#include <sys/select.h> 1.13 + 1.14 +#define MAGIC 0x6d0f 1.15 +#define DISCOVER 1 1.16 +#define DREPLY 2 1.17 + 1.18 +struct message { 1.19 + int magic; 1.20 + int type; 1.21 + char data[24]; 1.22 +}; 1.23 + 1.24 +int discover(struct sockaddr_in *client_sa); 1.25 + 1.26 +int opt_dport = 0x6d0f; /* discover broadcast port */ 1.27 +unsigned int opt_timeout = 5000; /* 10 sec discovery timeout */ 1.28 + 1.29 +int main(int argc, char **argv) 1.30 +{ 1.31 +} 1.32 + 1.33 +int discover(struct sockaddr_in *client_sa) 1.34 +{ 1.35 + int s, true = 1; 1.36 + struct sockaddr_in sa; 1.37 + unsigned int timeout = opt_timeout; 1.38 + struct message msg; 1.39 + struct timeval tv, tv_timeout; 1.40 + 1.41 + if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { 1.42 + perror("failed to create discover datagram socket"); 1.43 + return -1; 1.44 + } 1.45 + setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true); 1.46 + 1.47 + memset(&sa, 0, sizeof sa); 1.48 + sa.sin_family = AF_INET; 1.49 + sa.sin_port = htons(opt_dport); 1.50 + sa.sin_addr.s_addr = htonl(INADDR_ANY); 1.51 + 1.52 + if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) { 1.53 + perror("failed to bind datagram socket"); 1.54 + close(s); 1.55 + return -1; 1.56 + } 1.57 + 1.58 + do { 1.59 + msg.magic = MAGIC; 1.60 + msg.type = DISCOVER; 1.61 + 1.62 + memset(&sa, 0, sizeof sa); 1.63 + sa.sin_family = AF_INET; 1.64 + sa.sin_port = htons(opt_dport); 1.65 + sa.sin_addr.s_addr = 0xffffffff; 1.66 + 1.67 + if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) { 1.68 + perror("failed to send discovery bcast dgram"); 1.69 + close(s); 1.70 + return -1; 1.71 + } 1.72 + } while(0); 1.73 + 1.74 + close(s); 1.75 + return 0; 1.76 +}