]> git.meshlink.io Git - utcp/blob - test.c
Remove unconditional debug messages.
[utcp] / test.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netdb.h>
11 #include <poll.h>
12
13
14 #include "utcp.h"
15
16 struct utcp_connection *c;
17 int dir = 3;
18 bool running = true;
19 double dropin;
20 double dropout;
21
22 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
23         if(!data || !len) {
24                 if(errno) {
25                         fprintf(stderr, "Error: %s\n", strerror(errno));
26                         dir = 0;
27                 } else {
28                         dir &= ~2;
29                         fprintf(stderr, "Connection closed by peer\n");
30                 }
31                 return -1;
32         }
33         return write(1, data, len);
34 }
35
36 void do_accept(struct utcp_connection *nc, uint16_t port) {
37         utcp_accept(nc, do_recv, NULL);
38         c = nc;
39 }
40
41 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
42         int s = *(int *)utcp->priv;
43         if(drand48() >= dropout)
44                 return send(s, data, len, MSG_DONTWAIT);
45         else
46                 return len;
47 }
48
49 int main(int argc, char *argv[]) {
50         srand(time(NULL));
51         srand48(time(NULL));
52
53         if(argc < 2 || argc > 3)
54                 return 1;
55
56         bool server = argc == 2;
57         bool connected = false;
58
59         dropin = atof(getenv("DROPIN") ?: "0");
60         dropout = atof(getenv("DROPOUT") ?: "0");
61
62         struct addrinfo *ai;
63         struct addrinfo hint = {
64                 .ai_flags = server ? AI_PASSIVE : 0,
65                 .ai_socktype = SOCK_DGRAM,
66         };
67
68         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
69         if(!ai)
70                 return 1;
71
72         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
73         if(s < 0)
74                 return 1;
75
76         if(server) {
77                 if(bind(s, ai->ai_addr, ai->ai_addrlen))
78                         return 1;
79         } else {
80                 if(connect(s, ai->ai_addr, ai->ai_addrlen))
81                         return 1;
82                 connected = true;
83         }
84
85         freeaddrinfo(ai);
86
87         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
88         if(!u)
89                 return 1;
90
91         utcp_set_mtu(u, 1300);
92         utcp_set_user_timeout(u, 10);
93
94         if(!server)
95                 c = utcp_connect(u, 1, do_recv, NULL);
96
97         struct pollfd fds[2] = {
98                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
99                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
100         };
101
102         char buf[102400];
103         struct timeval timeout = utcp_timeout(u);
104
105         while(dir) {
106                 poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
107
108                 if(fds[0].revents) {
109                         int len = read(0, buf, sizeof buf);
110                         if(len <= 0) {
111                                 fds[0].fd = -1;
112                                 dir &= ~1;
113                                 if(c)
114                                         utcp_shutdown(c, SHUT_WR);
115                                 if(len < 0)
116                                         break;
117                                 else
118                                         continue;
119                         }
120                         if(c)
121                                 utcp_send(c, buf, len);
122                 }
123
124                 if(fds[1].revents) {
125                         struct sockaddr_storage ss;
126                         socklen_t sl = sizeof ss;
127                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
128                         if(len <= 0)
129                                 break;
130                         if(!connected)
131                                 if(!connect(s, (struct sockaddr *)&ss, sl))
132                                         connected = true;
133                         if(drand48() >= dropin)
134                                 utcp_recv(u, buf, len);
135                 }
136
137                 timeout = utcp_timeout(u);
138         };
139
140         utcp_close(c);
141         utcp_exit(u);
142
143         return 0;
144 }