]> git.meshlink.io Git - utcp/blob - test.c
Show total bytes sent/received in test program.
[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                 debug("\n");
136                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
137                 if(max > sizeof buf)
138                         max = sizeof buf;
139
140                 if((dir & DIR_READ) && max)
141                         poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
142                 else
143                         poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
144
145                 if(fds[0].revents) {
146                         fds[0].revents = 0;
147                         debug("stdin\n");
148                         ssize_t len = read(0, buf, max);
149                         if(len <= 0) {
150                                 fds[0].fd = -1;
151                                 dir &= ~DIR_READ;
152                                 if(c)
153                                         utcp_shutdown(c, SHUT_WR);
154                                 if(len == -1)
155                                         break;
156                                 else
157                                         continue;
158                         }
159                         if(c) {
160                                 ssize_t sent = utcp_send(c, buf, len);
161                                 if(sent != len)
162                                         debug("Short send: %zd != %zd\n", sent, len);
163                         }
164                 }
165
166                 if(fds[1].revents) {
167                         fds[1].revents = 0;
168                         debug("netout\n");
169                         struct sockaddr_storage ss;
170                         socklen_t sl = sizeof ss;
171                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
172                         if(len <= 0) {
173                                 debug("Error receiving UDP packet: %s\n", strerror(errno));
174                                 break;
175                         }
176                         if(!connected)
177                                 if(!connect(s, (struct sockaddr *)&ss, sl))
178                                         connected = true;
179                         inpktno++;
180                         if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
181                                 total_in += len;
182                                 utcp_recv(u, buf, len);
183                         }
184                 }
185
186                 timeout = utcp_timeout(u);
187         };
188
189         utcp_close(c);
190         utcp_exit(u);
191
192         debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
193
194         return 0;
195 }