1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/minimal-examples/raw/minimal-raw-adopt-udp
Andy Green c591e1adfc asynchronous dns for ipv4 and ipv6
This adds the option to have lws do its own dns resolution on
the event loop, without blocking.  Existing implementations get
the name resolution done by the libc, which is blocking.  In
the case you are opening client connections but need to carefully
manage latency, another connection opening and doing the name
resolution becomes a big problem.

Currently it supports

 - ipv4 / A records
 - ipv6 / AAAA records
 - ipv4-over-ipv6 ::ffff:1.2.3.4 A record promotion for ipv6
 - only one server supported over UDP :53
 - nameserver discovery on linux, windows, freertos

It also has some nice advantages

 - lws-style paranoid response parsing
 - random unique tid generation to increase difficulty of poisoning
 - it's really integrated with the lws event loop, it does not spawn
   threads or use the libc resolver, and of course no blocking at all
 - platform-specific server address capturing (from /etc/resolv.conf
   on linux, windows apis on windows)
 - it has LRU caching
 - piggybacking (multiple requests before the first completes go on
   a list on the first request, not spawn multiple requests)
 - observes TTL in cache
 - TTL and timeout use lws_sul timers on the event loop
 - ipv6 pieces only built if cmake LWS_IPV6 enabled
2019-09-19 06:54:53 +01:00
..
CMakeLists.txt asynchronous dns for ipv4 and ipv6 2019-09-19 06:54:53 +01:00
minimal-raw-adopt-udp.c asynchronous dns for ipv4 and ipv6 2019-09-19 06:54:53 +01:00
README.md minimal: raw udp 2018-04-06 10:38:03 +08:00

lws minimal ws server raw adopt udp

This example demonstrates echoing packets on a UDP socket in lws.

A "foreign" UDP socket is created, bound (so it can "listen"), and adopted into lws event loop. It acts like a tcp RAW mode connection in lws and uses the same callbacks.

Writing is a bit different for UDP. By default, the system has no idea about the receiver state and so asking for a callback_on_writable() always believes that the socket is writeable... the callback will happen next time around the event loop if there are no pending partials.

With UDP, there is no "connection". You need to write with sendto() and direct the packets to a specific destination. You can learn the source of the last packet that arrived at the LWS_CALLBACK_RAW_RX callback by getting a struct lws_udp * from lws_get_udp(wsi). To be able to send back to that guy, you should take a copy of the struct lws_udp * and use the .sa and .salen members in your sendto().

However the kernel may not accept to buffer / write everything you wanted to send. So you are responsible to watch the result of sendto() and resend the unsent part next time.

build

 $ cmake . && make

usage

 $ ./lws-minimal-raw-adopt-udp
$ ./lws-minimal-raw-adopt-udp 
[2018/03/24 08:12:37:8869] USER: LWS minimal raw adopt udp | nc -u 127.0.0.1 7681
[2018/03/24 08:12:37:8870] NOTICE: Creating Vhost 'default' (no listener), 1 protocols, IPv6 off
[2018/03/24 08:12:37:8878] USER: LWS_CALLBACK_RAW_ADOPT
[2018/03/24 08:12:41:5656] USER: LWS_CALLBACK_RAW_RX (6)
[2018/03/24 08:12:41:5656] NOTICE: 
[2018/03/24 08:12:41:5656] NOTICE: 0000: 68 65 6C 6C 6F 0A                                  hello.          
[2018/03/24 08:12:41:5656] NOTICE: 
 $ nc -u 127.0.0.1 7681
hello
hello