]> git.meshlink.io Git - utcp/blob - test.c
Handle retransmissions in CLOSE_WAIT, CLOSING and LAST_ACK states.
[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 len;
45
46         ssize_t result = send(s, data, len, MSG_DONTWAIT);
47         if(result <= 0)
48                 fprintf(stderr, "Error sending UDP packet: %s\n", strerror(errno));
49         return result;
50 }
51
52 int main(int argc, char *argv[]) {
53         srand(time(NULL));
54         srand48(time(NULL));
55
56         if(argc < 2 || argc > 3)
57                 return 1;
58
59         bool server = argc == 2;
60         bool connected = false;
61
62         dropin = atof(getenv("DROPIN") ?: "0");
63         dropout = atof(getenv("DROPOUT") ?: "0");
64
65         struct addrinfo *ai;
66         struct addrinfo hint = {
67                 .ai_flags = server ? AI_PASSIVE : 0,
68                 .ai_socktype = SOCK_DGRAM,
69         };
70
71         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
72         if(!ai)
73                 return 1;
74
75         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
76         if(s < 0)
77                 return 1;
78
79         if(server) {
80                 if(bind(s, ai->ai_addr, ai->ai_addrlen))
81                         return 1;
82         } else {
83                 if(connect(s, ai->ai_addr, ai->ai_addrlen))
84                         return 1;
85                 connected = true;
86         }
87
88         freeaddrinfo(ai);
89
90         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
91         if(!u)
92                 return 1;
93
94         utcp_set_mtu(u, 1300);
95         utcp_set_user_timeout(u, 10);
96
97         if(!server)
98                 c = utcp_connect(u, 1, do_recv, NULL);
99
100         struct pollfd fds[2] = {
101                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
102                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
103         };
104
105         char buf[102400];
106         struct timeval timeout = utcp_timeout(u);
107
108         while(dir) {
109                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
110                 if(max > sizeof buf)
111                         max = sizeof buf;
112
113                 if((dir & 1) && max)
114                         poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
115                 else
116                         poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
117
118                 if(fds[0].revents) {
119                         fds[0].revents = 0;
120                         fprintf(stderr, "0");
121                         ssize_t len = read(0, buf, max);
122                         if(len <= 0) {
123                                 fds[0].fd = -1;
124                                 dir &= ~1;
125                                 if(c)
126                                         utcp_shutdown(c, SHUT_WR);
127                                 if(len < 0)
128                                         break;
129                                 else
130                                         continue;
131                         }
132                         if(c) {
133                                 ssize_t sent = utcp_send(c, buf, len);
134                                 if(sent != len)
135                                         fprintf(stderr, "PANIEK: %zd != %zd\n", sent, len);
136                         }
137                 }
138
139                 if(fds[1].revents) {
140                         fds[1].revents = 0;
141                         fprintf(stderr, "1");
142                         struct sockaddr_storage ss;
143                         socklen_t sl = sizeof ss;
144                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
145                         if(len <= 0) {
146                                 fprintf(stderr, "Error receiving UDP packet: %s\n", strerror(errno));
147                                 break;
148                         }
149                         if(!connected)
150                                 if(!connect(s, (struct sockaddr *)&ss, sl))
151                                         connected = true;
152                         if(drand48() >= dropin)
153                                 utcp_recv(u, buf, len);
154                 }
155
156                 timeout = utcp_timeout(u);
157         };
158
159         utcp_close(c);
160         utcp_exit(u);
161
162         return 0;
163 }