summaryrefslogtreecommitdiffstats
path: root/src/host/virt_phy/src/shared/osmo_mcast_sock.c
blob: 3a3c77a998da198b56d18f4ebdaddf47d75d2451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/select.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <talloc.h>
#include <unistd.h>
#include <virtphy/osmo_mcast_sock.h>

/* convenience wrapper */
static void fd_close(struct osmo_fd *ofd)
{
	/* multicast memberships of socket are implicitly dropped when
	 * socket is closed */
	osmo_fd_unregister(ofd);
	close(ofd->fd);
	ofd->fd = -1;
	ofd->when = 0;
}

/* server socket is what we use for transmission. It is not subscribed
 * to a multicast group or locally bound, but it is just a normal UDP
 * socket that's connected to the remote mcast group + port */
int mcast_server_sock_setup(struct osmo_fd *ofd, const char* tx_mcast_group, int tx_mcast_port,
			    int loopback)
{
	int rc;

	/* 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);
	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;
}

/* the client socket is what we use for reception.  It is a UDP socket
 * that's bound to the GSMTAP UDP port and subscribed to the respective
 * multicast group */
int mcast_client_sock_setup(struct osmo_fd *ofd, const char *mcast_group, int mcast_port,
			    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;

	ofd->cb = fd_rx_cb;
	ofd->when = BSC_FD_READ;
	ofd->data = osmo_fd_data;

	/* Create mcast client socket */
	rc = osmo_sock_init_ofd(ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
				NULL, mcast_port, OSMO_SOCK_F_BIND);
	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));
	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;
}

struct mcast_bidir_sock *
mcast_bidir_sock_setup(void *ctx, const char *tx_mcast_group, int tx_mcast_port,
			const char *rx_mcast_group, int rx_mcast_port, int loopback,
			int (*fd_rx_cb)(struct osmo_fd *ofd, unsigned int what),
			void *osmo_fd_data)
{
	struct mcast_bidir_sock *bidir_sock = talloc(ctx, struct mcast_bidir_sock);
	int rc;

	if (!bidir_sock)
		return NULL;

	rc = mcast_client_sock_setup(&bidir_sock->rx_ofd, rx_mcast_group, rx_mcast_port,
				fd_rx_cb, osmo_fd_data);
	if (rc < 0) {
		talloc_free(bidir_sock);
		return NULL;
	}
	rc = mcast_server_sock_setup(&bidir_sock->tx_ofd, tx_mcast_group, tx_mcast_port, loopback);
	if (rc < 0) {
		fd_close(&bidir_sock->rx_ofd);
		talloc_free(bidir_sock);
		return NULL;
	}
	return bidir_sock;

}

int mcast_bidir_sock_tx(struct mcast_bidir_sock *bidir_sock, void* data,
                        int data_len)
{
	return send(bidir_sock->tx_ofd.fd, data, data_len, 0);
}

int mcast_bidir_sock_rx(struct mcast_bidir_sock *bidir_sock, void* buf, int buf_len)
{
	return recv(bidir_sock->rx_ofd.fd, buf, buf_len, 0);
}

void mcast_bidir_sock_close(struct mcast_bidir_sock *bidir_sock)
{
	fd_close(&bidir_sock->tx_ofd);
	fd_close(&bidir_sock->rx_ofd);
	talloc_free(bidir_sock);
}