UDPInterface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "wire/Error.h"
  22. #define ArrayList_TYPE struct Sockaddr
  23. #define ArrayList_NAME Sockaddr
  24. #include "util/ArrayList.h"
  25. struct UDPInterface_pvt
  26. {
  27. struct UDPInterface pub;
  28. struct Log* log;
  29. struct Allocator* allocator;
  30. struct Allocator* bcastAddrAlloc;
  31. struct ArrayList_Sockaddr* bcastAddrs;
  32. struct Allocator* bcastIfaceAlloc;
  33. struct StringList* bcastIfaces;
  34. struct UDPAddrIface* commIf;
  35. struct UDPAddrIface* bcastIf;
  36. struct GlobalConfig* globalConf;
  37. struct Iface commSock;
  38. struct Iface bcastSock;
  39. uint16_t beaconPort_be;
  40. uint16_t commPort_be;
  41. Identity
  42. };
  43. static struct Sockaddr* mkBcastAddr(
  44. uint16_t beaconPort_be,
  45. uv_interface_address_t* iface,
  46. struct Allocator* alloc)
  47. {
  48. struct sockaddr_in bcast4 = {
  49. .sin_family = AF_INET,
  50. .sin_port = beaconPort_be,
  51. .sin_addr = {
  52. .s_addr =
  53. (
  54. iface->address.address4.sin_addr.s_addr &
  55. iface->netmask.netmask4.sin_addr.s_addr
  56. ) | ~iface->netmask.netmask4.sin_addr.s_addr
  57. }
  58. };
  59. return Sockaddr_fromNative(&bcast4, sizeof(struct sockaddr_in), alloc);
  60. }
  61. static int updateBcastAddrs(struct UDPInterface_pvt* ctx)
  62. {
  63. bool all = false;
  64. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  65. String* iface = StringList_get(ctx->bcastIfaces, i);
  66. if (String_equals(iface, String_CONST("all"))) { all = true; }
  67. }
  68. uv_interface_address_t* interfaces;
  69. int count;
  70. int res = uv_interface_addresses(&interfaces, &count);
  71. if (res) {
  72. Log_info(ctx->log, "uv_interface_addresses failed [%s]", uv_strerror(-res));
  73. return -1;
  74. }
  75. if (ctx->bcastAddrAlloc) { Allocator_free(ctx->bcastAddrAlloc); }
  76. struct Allocator* alloc = ctx->bcastAddrAlloc = Allocator_child(ctx->allocator);
  77. ctx->bcastAddrs = ArrayList_Sockaddr_new(alloc);
  78. String* tunDev = GlobalConfig_getTunName(ctx->globalConf);
  79. for (int i = 0; i < count; i++) {
  80. if (interfaces[i].is_internal) { continue; }
  81. if (interfaces[i].address.address4.sin_family != AF_INET) { continue; }
  82. if (tunDev && !CString_strncmp(interfaces[i].name, tunDev->bytes, tunDev->len)) {
  83. continue;
  84. }
  85. struct Sockaddr* addr = mkBcastAddr(ctx->beaconPort_be, &interfaces[i], alloc);
  86. if (!all) {
  87. String* addrStr = String_new(Sockaddr_print(addr, alloc), alloc);
  88. bool found = false;
  89. for (int j = 0; ctx->bcastIfaces && j < ctx->bcastIfaces->length; j++) {
  90. String* iface = StringList_get(ctx->bcastIfaces, j);
  91. if (String_equals(iface, addrStr)) { found = true; }
  92. if (String_equals(iface, String_CONST(interfaces[i].name))) { found = true; }
  93. }
  94. if (!found) { continue; }
  95. }
  96. ArrayList_Sockaddr_add(ctx->bcastAddrs, addr);
  97. }
  98. uv_free_interface_addresses(interfaces, count);
  99. return 0;
  100. }
  101. static Iface_DEFUN sendPacket(struct Message* m, struct Iface* iface)
  102. {
  103. struct UDPInterface_pvt* ctx =
  104. Identity_containerOf(iface, struct UDPInterface_pvt, pub.generic.iface);
  105. Assert_true(Message_getLength(m) > Sockaddr_OVERHEAD);
  106. struct Sockaddr* sa = (struct Sockaddr*) m->msgbytes;
  107. Assert_true(Message_getLength(m) > sa->addrLen);
  108. // Regular traffic
  109. if (!(sa->flags & Sockaddr_flags_BCAST)) { return Iface_next(&ctx->commSock, m); }
  110. if (updateBcastAddrs(ctx)) {
  111. return Error(m, "updateBcastAddrs check logs");
  112. }
  113. // bcast
  114. struct UDPInterface_BroadcastHeader hdr = {
  115. .fffffffc_be = Endian_hostToBigEndian32(0xfffffffc),
  116. .version = UDPInterface_CURRENT_VERSION,
  117. .zero = 0,
  118. .commPort_be = ctx->commPort_be
  119. };
  120. Er_assert(Message_eshift(m, -sa->addrLen));
  121. Er_assert(Message_epush(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  122. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  123. struct Allocator* tmpAlloc = Allocator_child(ctx->allocator);
  124. struct Message* mm = Message_clone(m, tmpAlloc);
  125. struct Sockaddr* addr = ArrayList_Sockaddr_get(ctx->bcastAddrs, i);
  126. Er_assert(Message_epush(mm, addr, addr->addrLen));
  127. Iface_send(&ctx->bcastSock, mm);
  128. Allocator_free(tmpAlloc);
  129. }
  130. return NULL;
  131. }
  132. static Iface_DEFUN fromCommSock(struct Message* m, struct Iface* iface)
  133. {
  134. struct UDPInterface_pvt* ctx =
  135. Identity_containerOf(iface, struct UDPInterface_pvt, commSock);
  136. return Iface_next(&ctx->pub.generic.iface, m);
  137. }
  138. static Iface_DEFUN fromBcastSock(struct Message* m, struct Iface* iface)
  139. {
  140. struct UDPInterface_pvt* ctx =
  141. Identity_containerOf(iface, struct UDPInterface_pvt, bcastSock);
  142. if (Message_getLength(m) < UDPInterface_BroadcastHeader_SIZE + Sockaddr_OVERHEAD) {
  143. Log_debug(ctx->log, "DROP runt bcast");
  144. return Error(m, "RUNT bcast");
  145. }
  146. struct Sockaddr_storage ss;
  147. Er_assert(Message_epop(m, &ss, Sockaddr_OVERHEAD));
  148. if (Message_getLength(m) < UDPInterface_BroadcastHeader_SIZE + ss.addr.addrLen - Sockaddr_OVERHEAD) {
  149. Log_debug(ctx->log, "DROP runt bcast");
  150. return Error(m, "RUNT bcast");
  151. }
  152. Er_assert(Message_epop(m, &ss.nativeAddr, ss.addr.addrLen - Sockaddr_OVERHEAD));
  153. struct UDPInterface_BroadcastHeader hdr;
  154. Er_assert(Message_epop(m, &hdr, UDPInterface_BroadcastHeader_SIZE));
  155. if (hdr.fffffffc_be != Endian_hostToBigEndian32(0xfffffffc)) {
  156. Log_debug(ctx->log, "DROP bcast bad magic, expected 0xfffffffc got [%08x]",
  157. Endian_bigEndianToHost32(hdr.fffffffc_be));
  158. return Error(m, "INVALID bcast, bad magic");
  159. }
  160. if (hdr.version != UDPInterface_CURRENT_VERSION) {
  161. Log_debug(ctx->log, "DROP bcast bad version [%u]", hdr.version);
  162. return Error(m, "INVALID bcast, bad version");
  163. }
  164. if (hdr.zero) {
  165. Log_debug(ctx->log, "DROP bcast malformed (zero not zero)");
  166. return Error(m, "INVALID bcast, hdr.zero isn't 0");
  167. }
  168. uint16_t commPort = Endian_bigEndianToHost16(hdr.commPort_be);
  169. // Fake that it came from the communication port
  170. Sockaddr_setPort(&ss.addr, commPort);
  171. ss.addr.flags |= Sockaddr_flags_BCAST;
  172. Er_assert(Message_epush(m, &ss.addr, ss.addr.addrLen));
  173. return Iface_next(&ctx->pub.generic.iface, m);
  174. }
  175. Er_DEFUN(struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  176. struct Sockaddr* bindAddr,
  177. uint16_t beaconPort,
  178. struct Allocator* alloc,
  179. struct Log* logger,
  180. struct GlobalConfig* globalConf))
  181. {
  182. if (beaconPort && Sockaddr_getFamily(bindAddr) != Sockaddr_AF_INET) {
  183. Er_raise(alloc, "UDP broadcast only supported by ipv4.");
  184. }
  185. if (beaconPort && Sockaddr_getPort(bindAddr) == beaconPort) {
  186. Er_raise(alloc, "UDP broadcast port must be different from communication port.");
  187. }
  188. struct UDPAddrIface* uai = Er(UDPAddrIface_new(eventBase, bindAddr, alloc, logger));
  189. uint16_t commPort = Sockaddr_getPort(uai->generic.addr);
  190. struct UDPInterface_pvt* context = Allocator_calloc(alloc, sizeof(struct UDPInterface_pvt), 1);
  191. Identity_set(context);
  192. context->log = logger;
  193. context->allocator = alloc;
  194. context->beaconPort_be = Endian_hostToBigEndian16(beaconPort);
  195. context->commPort_be = Endian_hostToBigEndian16(commPort);
  196. context->pub.generic.addr = uai->generic.addr;
  197. context->pub.generic.alloc = alloc;
  198. context->pub.generic.iface.send = sendPacket;
  199. context->commSock.send = fromCommSock;
  200. context->bcastSock.send = fromBcastSock;
  201. context->commIf = uai;
  202. context->globalConf = globalConf;
  203. Iface_plumb(&uai->generic.iface, &context->commSock);
  204. if (beaconPort) {
  205. struct Sockaddr* bcastAddr = Sockaddr_clone(bindAddr, alloc);
  206. Sockaddr_setPort(bcastAddr, beaconPort);
  207. struct UDPAddrIface* bcast =
  208. Er(UDPAddrIface_new(eventBase, bcastAddr, alloc, logger));
  209. UDPAddrIface_setBroadcast(bcast, 1);
  210. Iface_plumb(&bcast->generic.iface, &context->bcastSock);
  211. context->bcastIf = bcast;
  212. }
  213. Er_ret(&context->pub);
  214. }
  215. Er_DEFUN(List* UDPInterface_listDevices(struct Allocator* alloc))
  216. {
  217. List* out = List_new(alloc);
  218. uv_interface_address_t* interfaces;
  219. int count;
  220. int res = uv_interface_addresses(&interfaces, &count);
  221. if (res) { Er_raise(alloc, "uv_interface_addresses failed [%s]", uv_strerror(-res)); }
  222. for (int i = 0; i < count; i++) {
  223. if (interfaces[i].is_internal) { continue; }
  224. if (interfaces[i].address.address4.sin_family != AF_INET) { continue; }
  225. List_addString(out, String_new(interfaces[i].name, alloc), alloc);
  226. }
  227. uv_free_interface_addresses(interfaces, count);
  228. Er_ret(out);
  229. }
  230. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices)
  231. {
  232. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  233. if (ctx->bcastIfaceAlloc) { Allocator_free(ctx->bcastIfaceAlloc); }
  234. struct Allocator* alloc = ctx->bcastIfaceAlloc = Allocator_child(ctx->allocator);
  235. struct StringList* bcastIfaces = ctx->bcastIfaces = StringList_new(alloc);
  236. int len = List_size(devices);
  237. for (uint32_t i = 0; i < (unsigned) len; i++) {
  238. String* dev = List_getString(devices, i);
  239. StringList_add(bcastIfaces, String_clone(dev, alloc));
  240. }
  241. }
  242. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc)
  243. {
  244. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  245. List* out = List_new(alloc);
  246. for (int i = 0; ctx->bcastIfaces && i < ctx->bcastIfaces->length; i++) {
  247. List_addString(out, StringList_get(ctx->bcastIfaces, i), alloc);
  248. }
  249. return out;
  250. }
  251. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc)
  252. {
  253. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  254. List* out = List_new(alloc);
  255. if (updateBcastAddrs(ctx)) {
  256. // TODO(cjd): There should be some way to return the fact that there was an error
  257. return out;
  258. }
  259. for (int i = 0; i < ctx->bcastAddrs->length; i++) {
  260. char* addr = Sockaddr_print(ArrayList_Sockaddr_get(ctx->bcastAddrs, i), alloc);
  261. List_addStringC(out, addr, alloc);
  262. }
  263. return out;
  264. }
  265. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp)
  266. {
  267. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  268. int res = UDPAddrIface_setDSCP(ctx->commIf, dscp);
  269. if (res) { return res; }
  270. if (ctx->bcastIf) { return UDPAddrIface_setDSCP(ctx->bcastIf, dscp); }
  271. return 0;
  272. }
  273. int UDPInterface_getFd(struct UDPInterface* udpif)
  274. {
  275. struct UDPInterface_pvt* ctx = Identity_check((struct UDPInterface_pvt*) udpif);
  276. return UDPAddrIface_getFd(ctx->commIf);
  277. }