From 4f1333c03a7f065c5c078fe2b7a3fec38896e517 Mon Sep 17 00:00:00 2001 From: Arnaud MASSERANN Date: Wed, 9 Jan 2019 11:00:00 +0100 Subject: [PATCH] Added custom network ports Makes it possible to run multiple devices on the same computer. For debug purposes only. --- artnet/artnet.c | 7 +++++++ artnet/artnet.h | 5 +++++ artnet/network.c | 11 +++++++---- artnet/private.h | 2 ++ artnet/transmit.c | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/artnet/artnet.c b/artnet/artnet.c index fc016ad..6a39731 100644 --- a/artnet/artnet.c +++ b/artnet/artnet.c @@ -116,6 +116,8 @@ artnet_node artnet_new(const char *ip, int verbose) { n->state.report_code = ARTNET_RCPOWEROK; n->state.reply_addr.s_addr = 0; n->state.mode = ARTNET_STANDBY; + n->receive_port = ARTNET_PORT; + n->send_port = ARTNET_PORT; // set all ports to MERGE HTP mode and disable for (i=0; i < ARTNET_MAX_PORTS; i++) { @@ -220,6 +222,11 @@ int artnet_destroy(artnet_node vn) { return ARTNET_EOK; } +void artnet_set_network_ports(artnet_node vn, uint16_t receive_port, uint16_t send_port) { + node n = (node)vn; + n->receive_port = receive_port; + n->send_port = send_port; +} /* * Set the OEM code diff --git a/artnet/artnet.h b/artnet/artnet.h index 2b79134..db7eb15 100644 --- a/artnet/artnet.h +++ b/artnet/artnet.h @@ -225,6 +225,11 @@ EXTERN int artnet_start(artnet_node n); EXTERN int artnet_read(artnet_node n, int timeout); EXTERN int artnet_stop(artnet_node n); EXTERN int artnet_destroy(artnet_node n); +// Changes the network ports used by the node. This function is useful for debugging +// (it makes it possible to run multiple devices on the same computer), but must not +// be used in production, because the network port is fixed by the spec. +// Must be called before artnet_start(). +EXTERN void artnet_set_network_ports(artnet_node n, uint16_t receive_port, uint16_t send_port); int artnet_join(artnet_node vn1, artnet_node vn2); diff --git a/artnet/network.c b/artnet/network.c index 1653f91..fc5f727 100644 --- a/artnet/network.c +++ b/artnet/network.c @@ -508,7 +508,7 @@ int artnet_net_start(node n) { memset(&servAddr, 0x00, sizeof(servAddr)); servAddr.sin_family = AF_INET; - servAddr.sin_port = htons(ARTNET_PORT); + servAddr.sin_port = htons(n->receive_port); servAddr.sin_addr.s_addr = htonl(INADDR_ANY); if (n->state.verbose) @@ -628,8 +628,11 @@ int artnet_net_recv(node n, artnet_packet p, int delay) { return ARTNET_ENET; } - if (cliAddr.sin_addr.s_addr == n->state.ip_addr.s_addr || - ntohl(cliAddr.sin_addr.s_addr) == LOOPBACK_IP) { + // Prevent receiving our own messages, except if using non-standard ports, + // in which case the user is running several nodes on the same machine for debugging. + if ((cliAddr.sin_addr.s_addr == n->state.ip_addr.s_addr || + ntohl(cliAddr.sin_addr.s_addr) == LOOPBACK_IP) && + n->send_port == n->receive_port ) { p->length = 0; return ARTNET_EOK; } @@ -652,7 +655,7 @@ int artnet_net_send(node n, artnet_packet p) { return ARTNET_EACTION; addr.sin_family = AF_INET; - addr.sin_port = htons(ARTNET_PORT); + addr.sin_port = htons(n->send_port); addr.sin_addr = p->to; p->from = n->state.ip_addr; diff --git a/artnet/private.h b/artnet/private.h index cc1be11..4706d26 100644 --- a/artnet/private.h +++ b/artnet/private.h @@ -457,6 +457,8 @@ typedef struct artnet_node_s{ node_list_t node_list; // node list firmware_transfer_t firmware; // firmware details node_peering_t peering; // peer if we've joined a group + uint16_t receive_port; // The network port on which we receive data. + uint16_t send_port; // The network port on which we send data. } artnet_node_t; diff --git a/artnet/transmit.c b/artnet/transmit.c index ce19b11..9f4c277 100644 --- a/artnet/transmit.c +++ b/artnet/transmit.c @@ -365,7 +365,7 @@ int artnet_tx_build_art_poll_reply(node n) { memcpy(&ar->id, ARTNET_STRING, ARTNET_STRING_SIZE); ar->opCode = htols(ARTNET_REPLY); memcpy(&ar->ip, &n->state.ip_addr.s_addr, 4); - ar->port = htols(ARTNET_PORT); + ar->port = htols(n->send_port); ar->verH = 0; ar->ver = 0; ar->subH = 0;