From dd94566f7b69cf5b18f5e82ea66059aab76f7891 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 13 Jul 2017 13:54:21 +0200 Subject: VIRT-PHY: mcast_sock: Avoid dynamic allocation of osmo_fd member Change-Id: I11e0137849797eb67047b4ddca0e917c599ed909 --- .../virt_phy/include/virtphy/osmo_mcast_sock.h | 4 +-- src/host/virt_phy/src/shared/osmo_mcast_sock.c | 39 ++++++++++------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h b/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h index abf2c48d..3c5954d8 100644 --- a/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h +++ b/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h @@ -4,11 +4,11 @@ #include struct mcast_server_sock { - struct osmo_fd *osmo_fd; + struct osmo_fd osmo_fd; }; struct mcast_client_sock { - struct osmo_fd *osmo_fd; + struct osmo_fd osmo_fd; struct ip_mreq *mcast_group; }; diff --git a/src/host/virt_phy/src/shared/osmo_mcast_sock.c b/src/host/virt_phy/src/shared/osmo_mcast_sock.c index fdbbb885..a06d7069 100644 --- a/src/host/virt_phy/src/shared/osmo_mcast_sock.c +++ b/src/host/virt_phy/src/shared/osmo_mcast_sock.c @@ -19,11 +19,8 @@ mcast_server_sock_setup(void *ctx, char* tx_mcast_group, int tx_mcast_port, int struct mcast_server_sock *serv_sock = talloc_zero(ctx, struct mcast_server_sock); int rc; - /* TODO: why allocate those dynamically ?!? */ - serv_sock->osmo_fd = talloc_zero(serv_sock, struct osmo_fd); - /* setup mcast server socket */ - rc = osmo_sock_init_ofd(serv_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, + rc = osmo_sock_init_ofd(&serv_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, tx_mcast_group, tx_mcast_port, OSMO_SOCK_F_CONNECT); if (rc < 0) { perror("Failed to create Multicast Server Socket"); @@ -32,7 +29,7 @@ mcast_server_sock_setup(void *ctx, char* tx_mcast_group, int tx_mcast_port, int /* determines whether sent mcast packets should be looped back to the local sockets. * loopback must be enabled if the mcast client is on the same machine */ - if (setsockopt(serv_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_LOOP, + if (setsockopt(serv_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback, sizeof(loopback)) < 0) { perror("Failed to configure multicast loopback.\n"); return NULL; @@ -53,15 +50,14 @@ mcast_client_sock_setup(void *ctx, char* mcast_group, int mcast_port, int rc, loopback = 1, all = 0; /* TODO: why allocate those dynamically ?!? */ - client_sock->osmo_fd = talloc_zero(client_sock, struct osmo_fd); client_sock->mcast_group = talloc_zero(client_sock, struct ip_mreq); - client_sock->osmo_fd->cb = fd_rx_cb; - client_sock->osmo_fd->when = BSC_FD_READ; - client_sock->osmo_fd->data = osmo_fd_data; + client_sock->osmo_fd.cb = fd_rx_cb; + client_sock->osmo_fd.when = BSC_FD_READ; + client_sock->osmo_fd.data = osmo_fd_data; /* Create mcast client socket */ - rc = osmo_sock_init_ofd(client_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, + rc = osmo_sock_init_ofd(&client_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, mcast_port, OSMO_SOCK_F_BIND); if (rc < 0) { perror("Could not create mcast client socket"); @@ -71,7 +67,7 @@ mcast_client_sock_setup(void *ctx, char* mcast_group, int mcast_port, /* Enable loopback of msgs to the host. */ /* Loopback must be enabled for the client, so multiple * processes are able to receive a mcast package. */ - rc = setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_LOOP, + rc = setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback, sizeof(loopback)); if (rc < 0) { perror("Failed to enable IP_MULTICAST_LOOP"); @@ -81,7 +77,7 @@ mcast_client_sock_setup(void *ctx, char* mcast_group, int mcast_port, /* Configure and join the multicast group */ client_sock->mcast_group->imr_multiaddr.s_addr = inet_addr(mcast_group); client_sock->mcast_group->imr_interface.s_addr = htonl(INADDR_ANY); - rc = setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + rc = setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, client_sock->mcast_group, sizeof(*client_sock->mcast_group)); if (rc < 0) { perror("Failed to join to mcast goup"); @@ -90,7 +86,7 @@ mcast_client_sock_setup(void *ctx, char* mcast_group, int mcast_port, /* this option will set the delivery option so that only packets * from sockets we are subscribed to via IP_ADD_MEMBERSHIP are received */ - if (setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)) < 0) { + if (setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)) < 0) { perror("Failed to modify delivery policy to explicitly joined.\n"); return NULL; } @@ -119,13 +115,13 @@ mcast_bidir_sock_setup(void *ctx, char* tx_mcast_group, int tx_mcast_port, int mcast_client_sock_rx(struct mcast_client_sock *client_sock, void* buf, int buf_len) { - return recv(client_sock->osmo_fd->fd, buf, buf_len, 0); + return recv(client_sock->osmo_fd.fd, buf, buf_len, 0); } int mcast_server_sock_tx(struct mcast_server_sock *serv_sock, void* data, int data_len) { - return send(serv_sock->osmo_fd->fd, data, data_len, 0); + return send(serv_sock->osmo_fd.fd, data, data_len, 0); } int mcast_bidir_sock_tx(struct mcast_bidir_sock *bidir_sock, void* data, @@ -141,20 +137,19 @@ int mcast_bidir_sock_rx(struct mcast_bidir_sock *bidir_sock, void* buf, int buf_ void mcast_client_sock_close(struct mcast_client_sock *client_sock) { - setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, + setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, client_sock->mcast_group, sizeof(*client_sock->mcast_group)); - osmo_fd_unregister(client_sock->osmo_fd); - client_sock->osmo_fd->fd = -1; - client_sock->osmo_fd->when = 0; - close(client_sock->osmo_fd->fd); + osmo_fd_unregister(&client_sock->osmo_fd); + client_sock->osmo_fd.fd = -1; + client_sock->osmo_fd.when = 0; + close(client_sock->osmo_fd.fd); talloc_free(client_sock->mcast_group); - talloc_free(client_sock->osmo_fd); talloc_free(client_sock); } void mcast_server_sock_close(struct mcast_server_sock *serv_sock) { - close(serv_sock->osmo_fd->fd); + close(serv_sock->osmo_fd.fd); talloc_free(serv_sock); } -- cgit v1.2.3