1 /*
2 * lws-minimal-raw-adopt-udp
3 *
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * This demonstrates integrating a connected udp
10 * socket into the lws event loop as a RAW wsi. It's interesting in
11 * the kind of situation where you already have a connected socket
12 * in your application, and you need to hand it over to lws to deal with.
13 *
14 * Lws supports "adopting" these foreign sockets, and also has a helper API
15 * to create, bind, and adopt them inside lws.
16 */
17
18 #include <libwebsockets.h>
19 #include <string.h>
20 #include <signal.h>
21 #if !defined(WIN32)
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <arpa/inet.h>
26 #endif
27 #include <sys/types.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #if !defined(WIN32)
32 #include <unistd.h>
33 #endif
34 #include <errno.h>
35
36 static uint8_t sendbuf[4096];
37 static size_t sendlen;
38 struct lws_udp udp;
39
40 static int
callback_raw_test(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)41 callback_raw_test(struct lws *wsi, enum lws_callback_reasons reason,
42 void *user, void *in, size_t len)
43 {
44 ssize_t n;
45 lws_sockfd_type fd;
46
47 switch (reason) {
48
49 /* callbacks related to raw socket descriptor */
50
51 case LWS_CALLBACK_RAW_ADOPT:
52 lwsl_user("LWS_CALLBACK_RAW_ADOPT\n");
53 break;
54
55 case LWS_CALLBACK_RAW_CLOSE:
56 lwsl_user("LWS_CALLBACK_RAW_CLOSE\n");
57 break;
58
59 case LWS_CALLBACK_RAW_RX:
60 lwsl_user("LWS_CALLBACK_RAW_RX (%d)\n", (int)len);
61 lwsl_hexdump_level(LLL_NOTICE, in, len);
62 /*
63 * Take a copy of the buffer and the source socket address...
64 */
65 udp = *(lws_get_udp(wsi));
66 sendlen = len;
67 if (sendlen > sizeof(sendbuf))
68 sendlen = sizeof(sendbuf);
69 memcpy(sendbuf, in, sendlen);
70 /*
71 * ... and we send it next time around the event loop. This
72 * can be extended to having a ringbuffer of different send
73 * buffers and targets queued.
74 *
75 * Note that UDP is ALWAYS writable as far as poll() knows
76 * because there is no mechanism like the tcp window to
77 * understand that packets are not being acknowledged. But
78 * this allows the event loop to share out the work.
79 */
80 lws_callback_on_writable(wsi);
81 break;
82
83 case LWS_CALLBACK_RAW_WRITEABLE:
84
85 if (!sendlen)
86 break;
87
88 fd = lws_get_socket_fd(wsi);
89 #if defined(WIN32)
90 if ((int)fd < 0)
91 break;
92 #else
93 if (fd < 0) /* keep Coverity happy: actually it cannot be < 0 */
94 break;
95 #endif
96
97 /*
98 * We can write directly on the UDP socket, specifying
99 * the peer the write is directed to.
100 *
101 * However the kernel may only accept parts of large sendto()s,
102 * leaving you to try to resend the remainder later. However
103 * depending on how your protocol on top of UDP works, that
104 * may involve sticking new headers before the remainder.
105 *
106 * For clarity partial sends just drop the remainder here.
107 */
108 n = sendto(fd,
109 #if defined(WIN32)
110 (const char *)
111 #endif
112 sendbuf,
113 #if defined(WIN32)
114 (int)
115 #endif
116 sendlen, 0, sa46_sockaddr(&udp.sa46),
117 sa46_socklen(&udp.sa46));
118 if (n < (ssize_t)len)
119 lwsl_notice("%s: send returned %d\n", __func__, (int)n);
120 break;
121
122 default:
123 break;
124 }
125
126 return 0;
127 }
128
129 static struct lws_protocols protocols[] = {
130 { "raw-test", callback_raw_test, 0, 0, 0, NULL, 0 },
131 LWS_PROTOCOL_LIST_TERM
132 };
133
134 static int interrupted;
135
sigint_handler(int sig)136 void sigint_handler(int sig)
137 {
138 interrupted = 1;
139 }
140
main(int argc,const char ** argv)141 int main(int argc, const char **argv)
142 {
143 struct lws_context_creation_info info;
144 struct lws_context *context;
145 struct lws_vhost *vhost;
146 const char *p;
147 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
148 /* for LLL_ verbosity above NOTICE to be built into lws,
149 * lws must have been configured and built with
150 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
151 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
152 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
153 /* | LLL_DEBUG */;
154
155 signal(SIGINT, sigint_handler);
156
157 if ((p = lws_cmdline_option(argc, argv, "-d")))
158 logs = atoi(p);
159
160 lws_set_log_level(logs, NULL);
161 lwsl_user("LWS minimal raw adopt udp | nc -u 127.0.0.1 7681\n");
162
163 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
164 info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
165
166 context = lws_create_context(&info);
167 if (!context) {
168 lwsl_err("lws init failed\n");
169 return 1;
170 }
171
172 info.port = CONTEXT_PORT_NO_LISTEN_SERVER;
173 info.protocols = protocols;
174
175 vhost = lws_create_vhost(context, &info);
176 if (!vhost) {
177 lwsl_err("lws vhost creation failed\n");
178 goto bail;
179 }
180
181 /*
182 * Create our own "foreign" UDP socket bound to 7681/udp
183 */
184 if (!lws_create_adopt_udp(vhost, NULL, 7681, LWS_CAUDP_BIND,
185 protocols[0].name, NULL, NULL, NULL, NULL,
186 "user")) {
187 lwsl_err("%s: foreign socket adoption failed\n", __func__);
188 goto bail;
189 }
190
191 while (n >= 0 && !interrupted)
192 n = lws_service(context, 0);
193
194 bail:
195 lws_context_destroy(context);
196
197 return 0;
198 }
199