UDPInterface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/events/libuv/UvWrapper.h"
  16. #include "benc/StringList.h"
  17. #include "interface/UDPInterface.h"
  18. #include "wire/Message.h"
  19. #include "util/events/UDPAddrIface.h"
  20. #include "util/GlobalConfig.h"
  21. #define ArrayList_TYPE struct Sockaddr
  22. #define ArrayList_NAME Sockaddr
  23. #include "util/ArrayList.h"
  24. struct UDPInterface_pvt
  25. {
  26. struct UDPInterface pub;
  27. struct Log* log;
  28. struct Allocator* allocator;
  29. struct Allocator* bcastAddrAlloc;
  30. struct ArrayList_Sockaddr* bcastAddrs;
  31. struct Allocator* bcastIfaceAlloc;
  32. struct StringList* bcastIfaces;
  33. struct UDPAddrIface* commIf;
  34. struct UDPAddrIface* bcastIf;
  35. struct GlobalConfig* globalConf;
  36. struct Iface commSock;
  37. struct Iface bcastSock;
  38. uint16_t beaconPort_be;
  39. uint16_t commPort_be;
  40. Identity
  41. };
  42. static struct Sockaddr* mkBcastAddr(
  43. uint16_t beaconPort_be,
  44. uv_interface_address_t* iface,
  45. struct Allocator* alloc)
  46. {
  47. struct sockaddr_in bcast4 = {
  48. .sin_family = AF_INET,
  49. .sin_port = beaconPort_be,
  50. .sin_addr = {
  51. .s_addr =
  52. (
  53. iface->address.address4.sin_addr.s_addr &
  54. iface->netmask.netmask4.sin_addr.s_addr
  55. ) | ~iface->netmask.netmask4.sin_addr.s_addr
  56. }
  57. };
  58. return Sockaddr_fromNative(&bcast4, sizeof(struct sockaddr_in), alloc);
  59. }
  60. static void updateBcastAddrs(struct UDPInterface_pvt* ctx)
  61. {
  62. bool all = false;
  63. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  64. String* iface = StringList_get(ctx->bcastIfaces, i);
  65. if (String_equals(iface, String_CONST("all"))) { all = true; }
  66. }
  67. uv_interface_address_t* interfaces;
  68. int count;
  69. int res = uv_interface_addresses(&interfaces, &count);
  70. if (res) {
  71. Log_info(ctx->log, "uv_interface_addresses failed [%s]", uv_strerror(-res));
  72. return;
  73. }
  74. if (ctx->bcastAddrAlloc) { Allocator_free(ctx->bcastAddrAlloc); }
  75. struct Allocator* alloc = ctx->bcastAddrAlloc = Allocator_child(ctx->allocator);
  76. ctx->bcastAddrs = ArrayList_Sockaddr_new(alloc);
  77. String* tunDev = GlobalConfig_getTunName(ctx->globalConf);
  78. for (int i = 0; i < count; i++) {
  79. if (interfaces[i].is_internal) { continue; }
  80. if (interfaces[i].address.address4.sin_family != AF_INET) { continue; }
  81. if (tunDev && !CString_strncmp(interfaces[i].name, tunDev->bytes, tunDev->len)) {
  82. continue;
  83. }
  84. struct Sockaddr* addr = mkBcastAddr(ctx->beaconPort_be, &interfaces[i], alloc);
  85. if (!all) {
  86. String* addrStr = String_new(Sockaddr_print(addr, alloc), alloc);
  87. bool found = false;
  88. for (int j = 0; ctx->bcastIfaces && j < ctx->bcastIfaces->length; j++) {
  89. String* iface = StringList_get(ctx->bcastIfaces, j);
  90. if (String_equals(iface, addrStr)) { found = true; }
  91. if (String_equals(iface, String_CONST(interfaces[i].name))) { found = true; }
  92. }
  93. if (!found) { continue; }
  94. }
  95. ArrayList_Sockaddr_add(ctx->bcastAddrs, addr);
  96. }
  97. uv_free_interface_addresses(interfaces, count);
  98. }
  99. static Iface_DEFUN sendPacket(struct Message* m, struct Iface* iface)
  100. {
  101. struct UDPInterface_pvt* ctx =
  102. Identity_containerOf(iface, struct UDPInterface_pvt, pub.generic.iface);
  103. Assert_true(m->length > Sockaddr_OVERHEAD);
  104. struct Sockaddr* sa = (struct Sockaddr*) m->bytes;
  105. Assert_true(m->length > sa->addrLen);
  106. // Regular traffic
  107. if (!(sa->flags & Sockaddr_flags_BCAST)) { return Iface_next(&ctx->commSock, m); }
  108. updateBcastAddrs(ctx);
  109. // bcast
  110. struct UDPInterface_BroadcastHeader hdr = {
  111. .fffffffc_be = Endian_hostToBigEndian32(0xfffffffc),
  112. .version = UDPInterface_CURRENT_VERSION,
  113. .zero = 0,
  114. .commPort_be = ctx->commPort_be
  115. };
  116. Message_shift(m, -sa->addrLen, NULL);
  117. Message_push(m, &hdr, UDPInterface_BroadcastHeader_SIZE, NULL);
  118. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  119. struct Allocator* tmpAlloc = Allocator_child(ctx->allocator);
  120. struct Message* mm = Message_clone(m, tmpAlloc);
  121. struct Sockaddr* addr = ArrayList_Sockaddr_get(ctx->bcastAddrs, i);
  122. Message_push(mm, addr, addr->addrLen, NULL);
  123. Iface_send(&ctx->bcastSock, mm);
  124. Allocator_free(tmpAlloc);
  125. }
  126. return NULL;
  127. }
  128. static Iface_DEFUN fromCommSock(struct Message* m, struct Iface* iface)
  129. {
  130. struct UDPInterface_pvt* ctx =
  131. Identity_containerOf(iface, struct UDPInterface_pvt, commSock);
  132. return Iface_next(&ctx->pub.generic.iface, m);
  133. }
  134. static Iface_DEFUN fromBcastSock(struct Message* m, struct Iface* iface)
  135. {
  136. struct UDPInterface_pvt* ctx =
  137. Identity_containerOf(iface, struct UDPInterface_pvt, bcastSock);
  138. if (m->length < UDPInterface_BroadcastHeader_SIZE + UDPInterface_BroadcastHeader_SIZE) {
  139. Log_debug(ctx->log, "DROP runt bcast");
  140. return NULL;
  141. }
  142. struct AddrIface_Header aihdr;
  143. Message_pop(m, &aihdr, AddrIface_Header_SIZE, NULL);
  144. struct UDPInterface_BroadcastHeader hdr;
  145. Message_pop(m, &hdr, UDPInterface_BroadcastHeader_SIZE, NULL);
  146. if (hdr.fffffffc_be != Endian_hostToBigEndian32(0xfffffffc)) {
  147. Log_debug(ctx->log, "DROP bcast bad magic, expected 0xfffffffc got [%08x]",
  148. Endian_bigEndianToHost32(hdr.fffffffc_be));
  149. return NULL;
  150. }
  151. if (hdr.version != UDPInterface_CURRENT_VERSION) {
  152. Log_debug(ctx->log, "DROP bcast bad version [%u]", hdr.version);
  153. return NULL;
  154. }
  155. if (hdr.zero) {
  156. Log_debug(ctx->log, "DROP bcast malformed (zero not zero)");
  157. return NULL;
  158. }
  159. uint16_t commPort = Endian_bigEndianToHost16(hdr.commPort_be);
  160. // Fake that it came from the communication port
  161. Sockaddr_setPort(&aihdr.addr.addr, commPort);
  162. aihdr.addr.addr.flags |= Sockaddr_flags_BCAST;
  163. Message_push(m, &aihdr, AddrIface_Header_SIZE, NULL);
  164. return Iface_next(&ctx->pub.generic.iface, m);
  165. }
  166. struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  167. struct Sockaddr* bindAddr,
  168. uint16_t beaconPort,
  169. struct Allocator* alloc,
  170. struct Except* exHandler,
  171. struct Log* logger,
  172. struct GlobalConfig* globalConf)
  173. {
  174. if (beaconPort && Sockaddr_getFamily(bindAddr) != Sockaddr_AF_INET) {
  175. Except_throw(exHandler, "UDP broadcast only supported by ipv4.");
  176. }
  177. if (beaconPort && Sockaddr_getPort(bindAddr) == beaconPort) {
  178. Except_throw(exHandler, "UDP broadcast port must be different from communication port.");
  179. }
  180. struct UDPAddrIface* uai = UDPAddrIface_new(eventBase, bindAddr, alloc, exHandler, logger);
  181. uint16_t commPort = Sockaddr_getPort(uai->generic.addr);
  182. struct UDPInterface_pvt* context = Allocator_calloc(alloc, sizeof(struct UDPInterface_pvt), 1);
  183. Identity_set(context);
  184. context->log = logger;
  185. context->allocator = alloc;
  186. context->beaconPort_be = Endian_hostToBigEndian16(beaconPort);
  187. context->commPort_be = Endian_hostToBigEndian16(commPort);
  188. context->pub.generic.addr = uai->generic.addr;
  189. context->pub.generic.alloc = alloc;
  190. context->pub.generic.iface.send = sendPacket;
  191. context->commSock.send = fromCommSock;
  192. context->bcastSock.send = fromBcastSock;
  193. context->commIf = uai;
  194. context->globalConf = globalConf;
  195. Iface_plumb(&uai->generic.iface, &context->commSock);
  196. if (beaconPort) {
  197. struct Sockaddr* bcastAddr = Sockaddr_clone(bindAddr, alloc);
  198. Sockaddr_setPort(bcastAddr, beaconPort);
  199. struct UDPAddrIface* bcast =
  200. UDPAddrIface_new(eventBase, bcastAddr, alloc, exHandler, logger);
  201. UDPAddrIface_setBroadcast(bcast, 1);
  202. Iface_plumb(&bcast->generic.iface, &context->bcastSock);
  203. context->bcastIf = bcast;
  204. }
  205. return &context->pub;
  206. }
  207. List* UDPInterface_listDevices(struct Allocator* alloc, struct Except* eh)
  208. {
  209. List* out = List_new(alloc);
  210. uv_interface_address_t* interfaces;
  211. int count;
  212. int res = uv_interface_addresses(&interfaces, &count);
  213. if (res) { Except_throw(eh, "uv_interface_addresses failed [%s]", uv_strerror(-res)); }
  214. for (int i = 0; i < count; i++) {
  215. if (interfaces[i].is_internal) { continue; }
  216. if (interfaces[i].address.address4.sin_family != AF_INET) { continue; }
  217. List_addString(out, String_new(interfaces[i].name, alloc), alloc);
  218. }
  219. uv_free_interface_addresses(interfaces, count);
  220. return out;
  221. }
  222. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices)
  223. {
  224. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  225. if (ctx->bcastIfaceAlloc) { Allocator_free(ctx->bcastIfaceAlloc); }
  226. struct Allocator* alloc = ctx->bcastIfaceAlloc = Allocator_child(ctx->allocator);
  227. struct StringList* bcastIfaces = ctx->bcastIfaces = StringList_new(alloc);
  228. int len = List_size(devices);
  229. for (uint32_t i = 0; i < (unsigned) len; i++) {
  230. String* dev = List_getString(devices, i);
  231. StringList_add(bcastIfaces, String_clone(dev, alloc));
  232. }
  233. }
  234. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc)
  235. {
  236. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  237. List* out = List_new(alloc);
  238. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  239. List_addString(out, StringList_get(ctx->bcastIfaces, i), alloc);
  240. }
  241. return out;
  242. }
  243. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc)
  244. {
  245. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  246. updateBcastAddrs(ctx);
  247. List* out = List_new(alloc);
  248. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  249. char* addr = Sockaddr_print(ArrayList_Sockaddr_get(ctx->bcastAddrs, i), alloc);
  250. List_addStringC(out, addr, alloc);
  251. }
  252. return out;
  253. }
  254. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp)
  255. {
  256. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  257. int res = UDPAddrIface_setDSCP(ctx->commIf, dscp);
  258. if (res) { return res; }
  259. if (ctx->bcastIf) { return UDPAddrIface_setDSCP(ctx->bcastIf, dscp); }
  260. return 0;
  261. }
  262. bool UDPInterface_timestampPackets(struct UDPInterface* udpif, bool enable)
  263. {
  264. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  265. return UDPAddrIface_timestampPackets(ctx->commIf, enable);
  266. }