1 /*-
2  * Copyright (c) 2006, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/tools/tools/net80211/wesside/udps/udps.c,v 1.1 2006/08/07 00:05:03 sam Exp $
27  */
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <time.h>
40 #include <err.h>
41 
42 int poll_rate = 5;
43 int pps = 10;
44 
45 //#define INSANE
46 
47 void own(int s, struct sockaddr_in* s_in) {
48 	char buf[64];
49 	int times = 10;
50 	int i;
51 	int delay = 10*1000;
52 	unsigned int sent = 0;
53 	struct timeval start, end;
54 	struct timespec ts;
55 	int dont_sleep_times = 1;
56 	int dont_sleep;
57 
58 	delay = (int) ((double)1.0/pps*1000.0*1000.0);
59 
60 	if (delay <= 5000) {
61 		dont_sleep_times = 10;
62 /*
63 		printf("delay is %d... sleeping every %d packets\n",
64 			delay, dont_sleep_times);
65 */
66 		delay *= dont_sleep_times;
67 
68 		delay = (int) (0.90*delay);
69 	}
70 
71 	dont_sleep = dont_sleep_times;
72 	times = poll_rate*pps;
73 //	times *= dont_sleep;
74 
75 
76 
77 	ts.tv_sec = 0;
78 	ts.tv_nsec = delay*1000;
79 
80 //	printf("times=%d delay=%d\n", times, delay);
81 	if (gettimeofday(&start, NULL) == -1) {
82 		perror("gettimeofday()");
83 		exit(1);
84 	}
85 
86 	for(i = 0; i < times; i++) {
87 		if( sendto(s, buf, 6, 0, (struct sockaddr *)s_in, sizeof(*s_in)) != 6) {
88 			printf("messed up a bit\n");
89 			return;
90 		}
91 
92 #ifndef INSANE
93 
94 #if 0
95 		if (usleep(delay) == -1) {
96 			perror("usleep()");
97 			exit(1);
98 		}
99 #endif
100 		dont_sleep--;
101 
102 		if (!dont_sleep) {
103 			if (nanosleep(&ts, NULL) == -1) {
104 				perror("nanosleep()");
105 				exit(1);
106 			}
107 
108 			dont_sleep  = dont_sleep_times;
109 		}
110 
111 #endif
112 		sent++;
113 	}
114 
115 	if (gettimeofday(&end, NULL) == -1) {
116 		perror("gettimeofday()");
117 		exit(1);
118 	}
119 
120 	printf ("Sent %.03f p/s\n", ((double)sent)/(((double)end.tv_sec) - start.tv_sec));
121 
122 //	printf("Sent %d packets\n", i);
123 }
124 
125 int main(int argc, char* argv[]) {
126 	int port = 6969;
127 	struct sockaddr_in s_in;
128 	int s;
129 	int rd;
130 	int len;
131 	char buf[64];
132 	struct timeval tv;
133 	int do_it = 0;
134 	fd_set rfds;
135 	char ip[17];
136 
137 	if( argc > 1)
138 		pps = atoi(argv[1]);
139 
140 	printf("Packets per second=%d\n", pps);
141 
142 	s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
143 	if( s < 0)
144 		err(1, "socket()");
145 
146 	s_in.sin_family = PF_INET;
147 	s_in.sin_port = htons(port);
148 	s_in.sin_addr.s_addr = INADDR_ANY;
149 
150 	if( bind(s, (struct sockaddr*)&s_in, sizeof(s_in)) < 0) {
151 		perror("bind()");
152 		exit(1);
153 	}
154 
155 	while(1) {
156 		assert(do_it >= 0);
157 		len = sizeof(struct sockaddr_in);
158 
159 		memset(&tv, 0, sizeof(tv));
160 		tv.tv_usec = 1000*10;
161 		FD_ZERO(&rfds);
162 		FD_SET(s, &rfds);
163 		rd = select(s + 1, &rfds, NULL ,NULL ,&tv);
164 		if (rd == -1) {
165 			perror("select()");
166 			exit(1);
167 		}
168 		if (rd == 1 && FD_ISSET(s, &rfds)) {
169 			rd = recvfrom(s, buf, 64, 0, (struct sockaddr*)&s_in, &len);
170 
171 			if(rd < 0) {
172 				perror("read died");
173 				exit(1);
174 			}
175 
176 			if(rd == 5 && memcmp(buf, "sorbo", 5) == 0) {
177 				sprintf(ip, "%s", inet_ntoa(s_in.sin_addr));
178 				printf("Got signal from %s\n", ip);
179 #ifdef INSANE
180 				do_it = 10;
181 #else
182 				do_it = 2;
183 #endif
184 			}
185 		}
186 
187 		if (do_it) {
188 			printf("Sending stuff to %s\n", ip);
189 
190 			own(s, &s_in);
191 			do_it--;
192 
193 			if(do_it == 0)
194 			printf("Stopping send\n");
195 		}
196 	}
197 }
198