vrmodel

changeset 0:affaad5fcd30

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 29 Aug 2014 18:56:54 +0300
parents
children 76e75cbcb758
files .hgignore inptools/6droid/Makefile inptools/test/Makefile inptools/test/src/main.c src/main.cc
diffstat 3 files changed, 88 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Fri Aug 29 18:56:54 2014 +0300
     1.3 @@ -0,0 +1,3 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/inptools/test/Makefile	Fri Aug 29 18:56:54 2014 +0300
     2.3 @@ -0,0 +1,12 @@
     2.4 +src = $(wildcard src/*.c)
     2.5 +obj = $(src:.c=.o)
     2.6 +bin = test
     2.7 +
     2.8 +CFLAGS = -pedantic -Wall -g
     2.9 +
    2.10 +$(bin): $(obj)
    2.11 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.12 +
    2.13 +.PHONY: clean
    2.14 +clean:
    2.15 +	rm -f $(obj) $(bin)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/inptools/test/src/main.c	Fri Aug 29 18:56:54 2014 +0300
     3.3 @@ -0,0 +1,73 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <string.h>
     3.7 +#include <unistd.h>
     3.8 +#include <sys/types.h>
     3.9 +#include <sys/socket.h>
    3.10 +#include <arpa/inet.h>
    3.11 +#include <sys/time.h>
    3.12 +#include <sys/select.h>
    3.13 +
    3.14 +#define MAGIC		0x6d0f
    3.15 +#define DISCOVER	1
    3.16 +#define DREPLY		2
    3.17 +
    3.18 +struct message {
    3.19 +	int magic;
    3.20 +	int type;
    3.21 +	char data[24];
    3.22 +};
    3.23 +
    3.24 +int discover(struct sockaddr_in *client_sa);
    3.25 +
    3.26 +int opt_dport = 0x6d0f;	/* discover broadcast port */
    3.27 +unsigned int opt_timeout = 5000;	/* 10 sec discovery timeout */
    3.28 +
    3.29 +int main(int argc, char **argv)
    3.30 +{
    3.31 +}
    3.32 +
    3.33 +int discover(struct sockaddr_in *client_sa)
    3.34 +{
    3.35 +	int s, true = 1;
    3.36 +	struct sockaddr_in sa;
    3.37 +	unsigned int timeout = opt_timeout;
    3.38 +	struct message msg;
    3.39 +	struct timeval tv, tv_timeout;
    3.40 +
    3.41 +	if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
    3.42 +		perror("failed to create discover datagram socket");
    3.43 +		return -1;
    3.44 +	}
    3.45 +	setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true);
    3.46 +
    3.47 +	memset(&sa, 0, sizeof sa);
    3.48 +	sa.sin_family = AF_INET;
    3.49 +	sa.sin_port = htons(opt_dport);
    3.50 +	sa.sin_addr.s_addr = htonl(INADDR_ANY);
    3.51 +
    3.52 +	if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
    3.53 +		perror("failed to bind datagram socket");
    3.54 +		close(s);
    3.55 +		return -1;
    3.56 +	}
    3.57 +
    3.58 +	do {
    3.59 +		msg.magic = MAGIC;
    3.60 +		msg.type = DISCOVER;
    3.61 +
    3.62 +		memset(&sa, 0, sizeof sa);
    3.63 +		sa.sin_family = AF_INET;
    3.64 +		sa.sin_port = htons(opt_dport);
    3.65 +		sa.sin_addr.s_addr = 0xffffffff;
    3.66 +
    3.67 +		if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) {
    3.68 +			perror("failed to send discovery bcast dgram");
    3.69 +			close(s);
    3.70 +			return -1;
    3.71 +		}
    3.72 +	} while(0);
    3.73 +
    3.74 +	close(s);
    3.75 +	return 0;
    3.76 +}