]> git.meshlink.io Git - utcp/blob - test.c
38240db8fb2c04eec2012ca46e9584ce67b3f8cf
[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 int 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 0;
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 int 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 0;
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         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
86         if(!u)
87                 return 1;
88
89         utcp_set_user_timeout(u, 10);
90
91         if(!server)
92                 c = utcp_connect(u, 1, do_recv, NULL);
93
94         struct pollfd fds[2] = {
95                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
96                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
97         };
98
99         char buf[1024];
100         int timeout = utcp_timeout(u);
101
102         while(dir) {
103                 poll(fds, 2, timeout);
104
105                 if(fds[0].revents) {
106                         int len = read(0, buf, sizeof buf);
107                         if(len <= 0) {
108                                 fds[0].fd = -1;
109                                 dir &= ~1;
110                                 if(c)
111                                         utcp_shutdown(c, SHUT_WR);
112                                 if(len < 0)
113                                         break;
114                                 else
115                                         continue;
116                         }
117                         if(c)
118                                 utcp_send(c, buf, len);
119                 }
120
121                 if(fds[1].revents) {
122                         struct sockaddr_storage ss;
123                         socklen_t sl;
124                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
125                         if(len <= 0)
126                                 break;
127                         if(!connected)
128                                 if(!connect(s, (struct sockaddr *)&ss, sl))
129                                         connected = true;
130                         if(drand48() >= dropin)
131                                 utcp_recv(u, buf, len);
132                 }
133
134                 timeout = utcp_timeout(u);
135         };
136
137         utcp_close(c);
138         utcp_exit(u);
139
140         return 0;
141 }