Skip to content

Commit

Permalink
Use a static sockaddr_storage for rpc.udp_dest
Browse files Browse the repository at this point in the history
There is no need to allocate and deallocate this structue every time
we update the udp destinateion.

For the client side, where we set the destination just once per lifetime
of the context it might not matter too much but once we add udp server support
we will need to update the sockaddr for every rpc we receive.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Dec 17, 2016
1 parent 853474d commit 7596d0f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/libnfs-private.h
Expand Up @@ -117,7 +117,7 @@ struct rpc_context {

/* special fields for UDP, which can sometimes be BROADCASTed */
int is_udp;
struct sockaddr *udp_dest;
struct sockaddr_storage udp_dest;
int is_broadcast;

/* track the address we connect to so we can auto-reconnect on session failure */
Expand Down
5 changes: 0 additions & 5 deletions lib/init.c
Expand Up @@ -336,11 +336,6 @@ void rpc_destroy_context(struct rpc_context *rpc)
rpc->error_string = NULL;
}

if (rpc->udp_dest != NULL) {
free(rpc->udp_dest);
rpc->udp_dest = NULL;
}

rpc->magic = 0;
free(rpc);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/pdu.c
Expand Up @@ -186,7 +186,9 @@ int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
unsigned int hash;

// XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h
if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) {
if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT,
(struct sockaddr *)&rpc->udp_dest,
sizeof(rpc->udp_dest)) < 0) {
rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
rpc_free_pdu(rpc, pdu);
return -1;
Expand Down
12 changes: 1 addition & 11 deletions lib/socket.c
Expand Up @@ -765,17 +765,7 @@ int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int i
return -1;
}

if (rpc->udp_dest) {
free(rpc->udp_dest);
rpc->udp_dest = NULL;
}
rpc->udp_dest = malloc(ai->ai_addrlen);
if (rpc->udp_dest == NULL) {
rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure");
freeaddrinfo(ai);
return -1;
}
memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
memcpy(&rpc->udp_dest, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai);

rpc->is_broadcast = is_broadcast;
Expand Down

0 comments on commit 7596d0f

Please sign in to comment.