VIRT-PHY: Use new OSMO_SOCK_F_NO_MCAST_{LOOP,ALL} flags
libosmocore has recently gained support for flags like OSMO_SOCK_F_NO_MCAST_LOOP and OSMO_SOCK_F_NO_MCAST_ALL that can be passed to the socket initializer functions, further reducing complexity of the code in osmo_mcast_sock.c here. The related change-IDs are I5ab5de45c0b64ceb3636ea98245a23defa24ffd4 and I24a5b1ebc3f84d2d5d4734e54df50efaea26490b in libosmocore.git. Change-Id: I961aa07a381fef2cf9a2fb5357937864364ca04b
This commit is contained in:
parent
48d2a55b40
commit
4f9913f1fd
|
@ -28,24 +28,19 @@ int mcast_server_sock_setup(struct osmo_fd *ofd, const char* tx_mcast_group,
|
|||
uint16_t tx_mcast_port, bool loopback)
|
||||
{
|
||||
int rc;
|
||||
unsigned int flags = OSMO_SOCK_F_CONNECT;
|
||||
|
||||
if (!loopback)
|
||||
flags |= OSMO_SOCK_F_NO_MCAST_LOOP;
|
||||
|
||||
/* setup mcast server socket */
|
||||
rc = osmo_sock_init_ofd(ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
|
||||
tx_mcast_group, tx_mcast_port, OSMO_SOCK_F_CONNECT);
|
||||
tx_mcast_group, tx_mcast_port, flags);
|
||||
if (rc < 0) {
|
||||
perror("Failed to create Multicast Server Socket");
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
rc = setsockopt(ofd->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback, sizeof(loopback));
|
||||
if (rc < 0) {
|
||||
perror("Failed to configure multicast loopback.\n");
|
||||
fd_close(ofd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -56,8 +51,7 @@ int mcast_client_sock_setup(struct osmo_fd *ofd, const char *mcast_group, uint16
|
|||
int (*fd_rx_cb)(struct osmo_fd *ofd, unsigned int what),
|
||||
void *osmo_fd_data)
|
||||
{
|
||||
struct ip_mreq mreq;
|
||||
int rc, loopback = 1, all = 0;
|
||||
int rc;
|
||||
|
||||
ofd->cb = fd_rx_cb;
|
||||
ofd->when = BSC_FD_READ;
|
||||
|
@ -65,42 +59,20 @@ int mcast_client_sock_setup(struct osmo_fd *ofd, const char *mcast_group, uint16
|
|||
|
||||
/* Create mcast client socket */
|
||||
rc = osmo_sock_init_ofd(ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
|
||||
NULL, mcast_port, OSMO_SOCK_F_BIND);
|
||||
NULL, mcast_port, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NO_MCAST_ALL);
|
||||
if (rc < 0) {
|
||||
perror("Could not create mcast client socket");
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* 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(ofd->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
|
||||
&loopback, sizeof(loopback));
|
||||
if (rc < 0) {
|
||||
perror("Failed to enable IP_MULTICAST_LOOP");
|
||||
fd_close(ofd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Configure and join the multicast group */
|
||||
memset(&mreq, 0, sizeof(mreq));
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(mcast_group);
|
||||
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
rc = setsockopt(ofd->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
|
||||
rc = osmo_sock_mcast_subscribe(ofd->fd, mcast_group);
|
||||
if (rc < 0) {
|
||||
perror("Failed to join to mcast goup");
|
||||
fd_close(ofd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* 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(ofd->fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)) < 0) {
|
||||
perror("Failed to modify delivery policy to explicitly joined.\n");
|
||||
fd_close(ofd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue