]> git.meshlink.io Git - utcp/blob - test.c
Turn magic numbers into #defines.
[utcp] / test.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <time.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netdb.h>
12 #include <poll.h>
13
14
15 #include "utcp.h"
16
17 #define DIR_READ 1
18 #define DIR_WRITE 2
19
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
22 bool running = true;
23 long inpktno;
24 long outpktno;
25 long dropfrom;
26 long dropto;
27 double dropin;
28 double dropout;
29 long total_out;
30 long total_in;
31
32
33 void debug(const char *format, ...) {
34         struct timeval now;
35         gettimeofday(&now, NULL);
36         fprintf(stderr, "%lu.%lu ", now.tv_sec, now.tv_usec / 1000);
37         va_list ap;
38         va_start(ap, format);
39         vfprintf(stderr, format, ap);
40         va_end(ap);
41 }
42
43 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
44         if(!data || !len) {
45                 if(errno) {
46                         debug("Error: %s\n", strerror(errno));
47                         dir = 0;
48                 } else {
49                         dir &= ~DIR_WRITE;
50                         debug("Connection closed by peer\n");
51                 }
52                 return -1;
53         }
54         return write(1, data, len);
55 }
56
57 void do_accept(struct utcp_connection *nc, uint16_t port) {
58         utcp_accept(nc, do_recv, NULL);
59         c = nc;
60         utcp_set_accept_cb(c->utcp, NULL, NULL);
61 }
62
63 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
64         int s = *(int *)utcp->priv;
65         outpktno++;
66         if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout)
67                 return len;
68
69         total_out += len;
70         ssize_t result = send(s, data, len, MSG_DONTWAIT);
71         if(result <= 0)
72                 debug("Error sending UDP packet: %s\n", strerror(errno));
73         return result;
74 }
75
76 int main(int argc, char *argv[]) {
77         srand(time(NULL));
78         srand48(time(NULL));
79
80         if(argc < 2 || argc > 3)
81                 return 1;
82
83         bool server = argc == 2;
84         bool connected = false;
85
86         dropin = atof(getenv("DROPIN") ?: "0");
87         dropout = atof(getenv("DROPOUT") ?: "0");
88         dropfrom = atoi(getenv("DROPFROM") ?: "0");
89         dropto = atoi(getenv("DROPTO") ?: "0");
90
91         struct addrinfo *ai;
92         struct addrinfo hint = {
93                 .ai_flags = server ? AI_PASSIVE : 0,
94                 .ai_socktype = SOCK_DGRAM,
95         };
96
97         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
98         if(!ai)
99                 return 1;
100
101         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
102         if(s == -1)
103                 return 1;
104
105         if(server) {
106                 if(bind(s, ai->ai_addr, ai->ai_addrlen))
107                         return 1;
108         } else {
109                 if(connect(s, ai->ai_addr, ai->ai_addrlen))
110                         return 1;
111                 connected = true;
112         }
113
114         freeaddrinfo(ai);
115
116         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
117         if(!u)
118                 return 1;
119
120         utcp_set_mtu(u, 1300);
121         utcp_set_user_timeout(u, 10);
122
123         if(!server)
124                 c = utcp_connect(u, 1, do_recv, NULL);
125
126         struct pollfd fds[2] = {
127                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
128                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
129         };
130
131         char buf[102400];
132         struct timeval timeout = utcp_timeout(u);
133
134         while(!connected || utcp_is_active(u)) {
135                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
136                 if(max > sizeof buf)
137                         max = sizeof buf;
138
139                 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
140
141                 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
142                 if((dir & DIR_READ) && max)
143                         poll(fds, 2, timeout_ms);
144                 else
145                         poll(fds + 1, 1, timeout_ms);
146
147                 if(fds[0].revents) {
148                         fds[0].revents = 0;
149                         debug("stdin\n");
150                         ssize_t len = read(0, buf, max);
151                         if(len <= 0) {
152                                 fds[0].fd = -1;
153                                 dir &= ~DIR_READ;
154                                 if(c)
155                                         utcp_shutdown(c, SHUT_WR);
156                                 if(len == -1)
157                                         break;
158                                 else
159                                         continue;
160                         }
161                         if(c) {
162                                 ssize_t sent = utcp_send(c, buf, len);
163                                 if(sent != len)
164                                         debug("Short send: %zd != %zd\n", sent, len);
165                         }
166                 }
167
168                 if(fds[1].revents) {
169                         fds[1].revents = 0;
170                         debug("netin\n");
171                         struct sockaddr_storage ss;
172                         socklen_t sl = sizeof ss;
173                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
174                         if(len <= 0) {
175                                 debug("Error receiving UDP packet: %s\n", strerror(errno));
176                                 break;
177                         }
178                         if(!connected)
179                                 if(!connect(s, (struct sockaddr *)&ss, sl))
180                                         connected = true;
181                         inpktno++;
182                         if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
183                                 total_in += len;
184                                 utcp_recv(u, buf, len);
185                         }
186                 }
187
188                 timeout = utcp_timeout(u);
189         };
190
191         utcp_close(c);
192         utcp_exit(u);
193
194         debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
195
196         return 0;
197 }