]> git.meshlink.io Git - utcp/blob - test.c
Start implementing timeout handling.
[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
20 int do_recv(struct utcp_connection *c, const void *data, size_t len) {
21         if(!data || !len) {
22                 if(errno) {
23                         fprintf(stderr, "Error: %s\n", strerror(errno));
24                         dir = 0;
25                 } else {
26                         dir &= ~2;
27                         fprintf(stderr, "Connection closed by peer\n");
28                 }
29                 return 0;
30         }
31         return write(1, data, len);
32 }
33
34 void do_accept(struct utcp_connection *nc, uint16_t port) {
35         utcp_accept(nc, do_recv, NULL);
36         c = nc;
37 }
38
39 int do_send(struct utcp *utcp, const void *data, size_t len) {
40         int s = *(int *)utcp->priv;
41         return send(s, data, len, MSG_DONTWAIT);
42 }
43
44 int main(int argc, char *argv[]) {
45         srand(time(NULL));
46
47         if(argc < 2 || argc > 3)
48                 return 1;
49
50         bool server = argc == 2;
51         bool connected = false;
52
53         struct addrinfo *ai;
54         struct addrinfo hint = {
55                 .ai_flags = server ? AI_PASSIVE : 0,
56                 .ai_socktype = SOCK_DGRAM,
57         };
58
59         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
60         if(!ai)
61                 return 1;
62
63         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
64         if(s < 0)
65                 return 1;
66
67         if(server) {
68                 if(bind(s, ai->ai_addr, ai->ai_addrlen))
69                         return 1;
70         } else {
71                 if(connect(s, ai->ai_addr, ai->ai_addrlen))
72                         return 1;
73                 connected = true;
74         }
75
76         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
77         if(!u)
78                 return 1;
79
80         utcp_set_connection_timeout(u, 10);
81
82         if(!server)
83                 c = utcp_connect(u, 1, do_recv, NULL);
84
85         struct pollfd fds[2] = {
86                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
87                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
88         };
89
90         char buf[1024];
91         int timeout = utcp_timeout(u);
92
93         while(dir) {
94                 poll(fds, 2, timeout);
95
96                 if(fds[0].revents) {
97                         int len = read(0, buf, sizeof buf);
98                         if(len <= 0) {
99                                 fds[0].fd = -1;
100                                 dir &= ~1;
101                                 if(c)
102                                         utcp_shutdown(c, SHUT_WR);
103                                 if(len < 0)
104                                         break;
105                                 else
106                                         continue;
107                         }
108                         if(c)
109                                 utcp_send(c, buf, len);
110                 }
111
112                 if(fds[1].revents) {
113                         struct sockaddr_storage ss;
114                         socklen_t sl;
115                         int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
116                         if(len <= 0)
117                                 break;
118                         if(!connected)
119                                 if(!connect(s, (struct sockaddr *)&ss, sl))
120                                         connected = true;
121                         utcp_recv(u, buf, len);
122                 }
123
124                 timeout = utcp_timeout(u);
125         };
126
127         utcp_close(c);
128         utcp_exit(u);
129
130         return 0;
131 }