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