]> git.meshlink.io Git - utcp/blob - test.c
Do port numbers properly.
[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, void *data, size_t len) {
21         if(!data || !len) {
22                 if(errno)
23                         fprintf(stderr, "Error: %s\n", strerror(errno));
24                 else {
25                         dir &= ~2;
26                         fprintf(stderr, "Connection closed by peer\n");
27                 }
28                 return 0;
29         }
30         return write(0, data, len);
31 }
32
33 void do_accept(struct utcp_connection *nc, uint16_t port) {
34         utcp_accept(nc, do_recv, NULL);
35         c = nc;
36 }
37
38 int do_send(struct utcp *utcp, void *data, size_t len) {
39         int s = *(int *)utcp->priv;
40         return send(s, data, len, MSG_DONTWAIT);
41 }
42
43 int main(int argc, char *argv[]) {
44         srand(time(NULL));
45
46         if(argc < 2 || argc > 3)
47                 return 1;
48
49         bool server = argc == 2;
50         bool connected = false;
51
52         struct addrinfo *ai;
53         struct addrinfo hint = {
54                 .ai_flags = server ? AI_PASSIVE : 0,
55                 .ai_socktype = SOCK_DGRAM,
56         };
57
58         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
59         if(!ai)
60                 return 1;
61
62         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
63         if(s < 0)
64                 return 1;
65
66         if(server) {
67                 if(bind(s, ai->ai_addr, ai->ai_addrlen))
68                         return 1;
69         } else {
70                 if(connect(s, ai->ai_addr, ai->ai_addrlen))
71                         return 1;
72                 connected = true;
73         }
74
75         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
76         if(!u)
77                 return 1;
78
79         if(!server)
80                 c = utcp_connect(u, 1, do_recv, NULL);
81
82         struct pollfd fds[2] = {
83                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
84                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
85         };
86
87         char buf[1024];
88
89         while(dir) {
90                 int r = poll(fds, 2, 1000);
91                 if(!r) {
92                         utcp_timeout(u);
93                         continue;
94                 }
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
125         utcp_close(c);
126         utcp_exit(u);
127
128         return 0;
129 }