UDPInterface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 int 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 -1;
  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. return 0;
  99. }
  100. static Iface_DEFUN sendPacket(struct Message* m, struct Iface* iface)
  101. {
  102. struct UDPInterface_pvt* ctx =
  103. Identity_containerOf(iface, struct UDPInterface_pvt, pub.generic.iface);
  104. Assert_true(m->length > Sockaddr_OVERHEAD);
  105. struct Sockaddr* sa = (struct Sockaddr*) m->bytes;
  106. Assert_true(m->length > sa->addrLen);
  107. // Regular traffic
  108. if (!(sa->flags & Sockaddr_flags_BCAST)) { return Iface_next(&ctx->commSock, m); }
  109. if (updateBcastAddrs(ctx)) {
  110. return NULL;
  111. }
  112. // bcast
  113. struct UDPInterface_BroadcastHeader hdr = {
  114. .fffffffc_be = Endian_hostToBigEndian32(0xfffffffc),
  115. .version = UDPInterface_CURRENT_VERSION,
  116. .zero = 0,
  117. .commPort_be = ctx->commPort_be
  118. };
  119. Er_assert(Message_eshift(m, -sa->addrLen));
  120. Er_assert(Message_epush(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  121. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  122. struct Allocator* tmpAlloc = Allocator_child(ctx->allocator);
  123. struct Message* mm = Message_clone(m, tmpAlloc);
  124. struct Sockaddr* addr = ArrayList_Sockaddr_get(ctx->bcastAddrs, i);
  125. Er_assert(Message_epush(mm, addr, addr->addrLen));
  126. Iface_send(&ctx->bcastSock, mm);
  127. Allocator_free(tmpAlloc);
  128. }
  129. return NULL;
  130. }
  131. static Iface_DEFUN fromCommSock(struct Message* m, struct Iface* iface)
  132. {
  133. struct UDPInterface_pvt* ctx =
  134. Identity_containerOf(iface, struct UDPInterface_pvt, commSock);
  135. return Iface_next(&ctx->pub.generic.iface, m);
  136. }
  137. static Iface_DEFUN fromBcastSock(struct Message* m, struct Iface* iface)
  138. {
  139. struct UDPInterface_pvt* ctx =
  140. Identity_containerOf(iface, struct UDPInterface_pvt, bcastSock);
  141. if (m->length < UDPInterface_BroadcastHeader_SIZE + Sockaddr_OVERHEAD) {
  142. Log_debug(ctx->log, "DROP runt bcast");
  143. return NULL;
  144. }
  145. struct Sockaddr_storage ss;
  146. Er_assert(Message_epop(m, &ss, Sockaddr_OVERHEAD));
  147. if (m->length < UDPInterface_BroadcastHeader_SIZE + ss.addr.addrLen - Sockaddr_OVERHEAD) {
  148. Log_debug(ctx->log, "DROP runt bcast");
  149. return NULL;
  150. }
  151. Er_assert(Message_epop(m, &ss.nativeAddr, ss.addr.addrLen - Sockaddr_OVERHEAD));
  152. struct UDPInterface_BroadcastHeader hdr;
  153. Er_assert(Message_epop(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  154. if (hdr.fffffffc_be != Endian_hostToBigEndian32(0xfffffffc)) {
  155. Log_debug(ctx->log, "DROP bcast bad magic, expected 0xfffffffc got [%08x]",
  156. Endian_bigEndianToHost32(hdr.fffffffc_be));
  157. return NULL;
  158. }
  159. if (hdr.version != UDPInterface_CURRENT_VERSION) {
  160. Log_debug(ctx->log, "DROP bcast bad version [%u]", hdr.version);
  161. return NULL;
  162. }
  163. if (hdr.zero) {
  164. Log_debug(ctx->log, "DROP bcast malformed (zero not zero)");
  165. return NULL;
  166. }
  167. uint16_t commPort = Endian_bigEndianToHost16(hdr.commPort_be);
  168. // Fake that it came from the communication port
  169. Sockaddr_setPort(&ss.addr, commPort);
  170. ss.addr.flags |= Sockaddr_flags_BCAST;
  171. Er_assert(Message_epush(m, &ss.addr, ss.addr.addrLen));
  172. return Iface_next(&ctx->pub.generic.iface, m);
  173. }
  174. Er_DEFUN(struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  175. struct Sockaddr* bindAddr,
  176. uint16_t beaconPort,
  177. struct Allocator* alloc,
  178. struct Log* logger,
  179. struct GlobalConfig* globalConf))
  180. {
  181. if (beaconPort && Sockaddr_getFamily(bindAddr) != Sockaddr_AF_INET) {
  182. Er_raise(alloc, "UDP broadcast only supported by ipv4.");
  183. }
  184. if (beaconPort && Sockaddr_getPort(bindAddr) == beaconPort) {
  185. Er_raise(alloc, "UDP broadcast port must be different from communication port.");
  186. }
  187. struct UDPAddrIface* uai = Er(UDPAddrIface_new(eventBase, bindAddr, alloc, logger));
  188. uint16_t commPort = Sockaddr_getPort(uai->generic.addr);
  189. struct UDPInterface_pvt* context = Allocator_calloc(alloc, sizeof(struct UDPInterface_pvt), 1);
  190. Identity_set(context);
  191. context->log = logger;
  192. context->allocator = alloc;
  193. context->beaconPort_be = Endian_hostToBigEndian16(beaconPort);
  194. context->commPort_be = Endian_hostToBigEndian16(commPort);
  195. context->pub.generic.addr = uai->generic.addr;
  196. context->pub.generic.alloc = alloc;
  197. context->pub.generic.iface.send = sendPacket;
  198. context->commSock.send = fromCommSock;
  199. context->bcastSock.send = fromBcastSock;
  200. context->commIf = uai;
  201. context->globalConf = globalConf;
  202. Iface_plumb(&uai->generic.iface, &context->commSock);
  203. if (beaconPort) {
  204. struct Sockaddr* bcastAddr = Sockaddr_clone(bindAddr, alloc);
  205. Sockaddr_setPort(bcastAddr, beaconPort);
  206. struct UDPAddrIface* bcast =
  207. Er(UDPAddrIface_new(eventBase, bcastAddr, alloc, logger));
  208. UDPAddrIface_setBroadcast(bcast, 1);
  209. Iface_plumb(&bcast->generic.iface, &context->bcastSock);
  210. context->bcastIf = bcast;
  211. }
  212. Er_ret(&context->pub);
  213. }
  214. Er_DEFUN(List* UDPInterface_listDevices(struct Allocator* alloc))
  215. {
  216. List* out = List_new(alloc);
  217. uv_interface_address_t* interfaces;
  218. int count;
  219. int res = uv_interface_addresses(&interfaces, &count);
  220. if (res) { Er_raise(alloc, "uv_interface_addresses failed [%s]", uv_strerror(-res)); }
  221. for (int i = 0; i < count; i++) {
  222. if (interfaces[i].is_internal) { continue; }
  223. if (interfaces[i].address.address4.sin_family != AF_INET) { continue; }
  224. List_addString(out, String_new(interfaces[i].name, alloc), alloc);
  225. }
  226. uv_free_interface_addresses(interfaces, count);
  227. Er_ret(out);
  228. }
  229. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices)
  230. {
  231. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  232. if (ctx->bcastIfaceAlloc) { Allocator_free(ctx->bcastIfaceAlloc); }
  233. struct Allocator* alloc = ctx->bcastIfaceAlloc = Allocator_child(ctx->allocator);
  234. struct StringList* bcastIfaces = ctx->bcastIfaces = StringList_new(alloc);
  235. int len = List_size(devices);
  236. for (uint32_t i = 0; i < (unsigned) len; i++) {
  237. String* dev = List_getString(devices, i);
  238. StringList_add(bcastIfaces, String_clone(dev, alloc));
  239. }
  240. }
  241. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc)
  242. {
  243. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  244. List* out = List_new(alloc);
  245. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  246. List_addString(out, StringList_get(ctx->bcastIfaces, i), alloc);
  247. }
  248. return out;
  249. }
  250. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc)
  251. {
  252. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  253. List* out = List_new(alloc);
  254. if (updateBcastAddrs(ctx)) {
  255. // TODO(cjd): There should be some way to return the fact that there was an error
  256. return out;
  257. }
  258. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  259. char* addr = Sockaddr_print(ArrayList_Sockaddr_get(ctx->bcastAddrs, i), alloc);
  260. List_addStringC(out, addr, alloc);
  261. }
  262. return out;
  263. }
  264. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp)
  265. {
  266. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  267. int res = UDPAddrIface_setDSCP(ctx->commIf, dscp);
  268. if (res) { return res; }
  269. if (ctx->bcastIf) { return UDPAddrIface_setDSCP(ctx->bcastIf, dscp); }
  270. return 0;
  271. }
  272. int UDPInterface_getFd(struct UDPInterface* udpif)
  273. {
  274. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  275. return UDPAddrIface_getFd(ctx->commIf);
  276. }