2 * Copyright (c) 2006, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/tools/tools/net80211/wesside/udps/udps.c,v 1.1 2006/08/07 00:05:03 sam Exp $
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/select.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
47 void own(int s, struct sockaddr_in* s_in) {
52 unsigned int sent = 0;
53 struct timeval start, end;
55 int dont_sleep_times = 1;
58 delay = (int) ((double)1.0/pps*1000.0*1000.0);
61 dont_sleep_times = 10;
63 printf("delay is %d... sleeping every %d packets\n",
64 delay, dont_sleep_times);
66 delay *= dont_sleep_times;
68 delay = (int) (0.90*delay);
71 dont_sleep = dont_sleep_times;
72 times = poll_rate*pps;
73 // times *= dont_sleep;
78 ts.tv_nsec = delay*1000;
80 // printf("times=%d delay=%d\n", times, delay);
81 if (gettimeofday(&start, NULL) == -1) {
82 perror("gettimeofday()");
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");
95 if (usleep(delay) == -1) {
103 if (nanosleep(&ts, NULL) == -1) {
104 perror("nanosleep()");
108 dont_sleep = dont_sleep_times;
115 if (gettimeofday(&end, NULL) == -1) {
116 perror("gettimeofday()");
120 printf ("Sent %.03f p/s\n", ((double)sent)/(((double)end.tv_sec) - start.tv_sec));
122 // printf("Sent %d packets\n", i);
125 int main(int argc, char* argv[]) {
127 struct sockaddr_in s_in;
140 printf("Packets per second=%d\n", pps);
142 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
146 s_in.sin_family = PF_INET;
147 s_in.sin_port = htons(port);
148 s_in.sin_addr.s_addr = INADDR_ANY;
150 if( bind(s, (struct sockaddr*)&s_in, sizeof(s_in)) < 0) {
157 len = sizeof(struct sockaddr_in);
159 memset(&tv, 0, sizeof(tv));
160 tv.tv_usec = 1000*10;
163 rd = select(s + 1, &rfds, NULL ,NULL ,&tv);
168 if (rd == 1 && FD_ISSET(s, &rfds)) {
169 rd = recvfrom(s, buf, 64, 0, (struct sockaddr*)&s_in, &len);
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);
188 printf("Sending stuff to %s\n", ip);
194 printf("Stopping send\n");