vrmodel

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <arpa/inet.h>
8 #include <sys/time.h>
9 #include <sys/select.h>
11 #define MAGIC 0x6d0f
12 #define DISCOVER 1
13 #define DREPLY 2
15 struct message {
16 int magic;
17 int type;
18 char data[24];
19 };
21 int discover(struct sockaddr_in *client_sa);
23 int opt_dport = 0x6d0f; /* discover broadcast port */
24 unsigned int opt_timeout = 5000; /* 10 sec discovery timeout */
26 int main(int argc, char **argv)
27 {
28 }
30 int discover(struct sockaddr_in *client_sa)
31 {
32 int s, true = 1;
33 struct sockaddr_in sa;
34 unsigned int timeout = opt_timeout;
35 struct message msg;
36 struct timeval tv, tv_timeout;
38 if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
39 perror("failed to create discover datagram socket");
40 return -1;
41 }
42 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true);
44 memset(&sa, 0, sizeof sa);
45 sa.sin_family = AF_INET;
46 sa.sin_port = htons(opt_dport);
47 sa.sin_addr.s_addr = htonl(INADDR_ANY);
49 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
50 perror("failed to bind datagram socket");
51 close(s);
52 return -1;
53 }
55 do {
56 msg.magic = MAGIC;
57 msg.type = DISCOVER;
59 memset(&sa, 0, sizeof sa);
60 sa.sin_family = AF_INET;
61 sa.sin_port = htons(opt_dport);
62 sa.sin_addr.s_addr = 0xffffffff;
64 if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) {
65 perror("failed to send discovery bcast dgram");
66 close(s);
67 return -1;
68 }
69 } while(0);
71 close(s);
72 return 0;
73 }