udp: configure the socket with the TTL option.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2015-08-12 18:45:42 +02:00
parent e7dbc068f6
commit d4995c6d71
1 changed files with 11 additions and 4 deletions

15
udp.c
View File

@ -30,6 +30,7 @@
#include <unistd.h> #include <unistd.h>
#include "address.h" #include "address.h"
#include "config.h"
#include "contain.h" #include "contain.h"
#include "print.h" #include "print.h"
#include "sk.h" #include "sk.h"
@ -92,7 +93,8 @@ static int udp_close(struct transport *t, struct fdarray *fda)
return 0; return 0;
} }
static int open_socket(const char *name, struct in_addr mc_addr[2], short port) static int open_socket(const char *name, struct in_addr mc_addr[2], short port,
int ttl)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
int fd, index, on = 1; int fd, index, on = 1;
@ -123,6 +125,10 @@ static int open_socket(const char *name, struct in_addr mc_addr[2], short port)
pr_err("setsockopt SO_BINDTODEVICE failed: %m"); pr_err("setsockopt SO_BINDTODEVICE failed: %m");
goto no_option; goto no_option;
} }
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl))) {
pr_err("setsockopt IP_MULTICAST_TTL failed: %m");
goto no_option;
}
addr.sin_addr = mc_addr[0]; addr.sin_addr = mc_addr[0];
if (mcast_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) { if (mcast_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) {
pr_err("mcast_join failed"); pr_err("mcast_join failed");
@ -151,8 +157,9 @@ static int udp_open(struct transport *t, const char *name, struct fdarray *fda,
enum timestamp_type ts_type) enum timestamp_type ts_type)
{ {
struct udp *udp = container_of(t, struct udp, t); struct udp *udp = container_of(t, struct udp, t);
int efd, gfd; int efd, gfd, ttl;
ttl = config_get_int(t->cfg, name, "udp_ttl");
udp->mac.len = 0; udp->mac.len = 0;
sk_interface_macaddr(name, &udp->mac); sk_interface_macaddr(name, &udp->mac);
@ -165,11 +172,11 @@ static int udp_open(struct transport *t, const char *name, struct fdarray *fda,
if (!inet_aton(PTP_PDELAY_MCAST_IPADDR, &mcast_addr[MC_PDELAY])) if (!inet_aton(PTP_PDELAY_MCAST_IPADDR, &mcast_addr[MC_PDELAY]))
return -1; return -1;
efd = open_socket(name, mcast_addr, EVENT_PORT); efd = open_socket(name, mcast_addr, EVENT_PORT, ttl);
if (efd < 0) if (efd < 0)
goto no_event; goto no_event;
gfd = open_socket(name, mcast_addr, GENERAL_PORT); gfd = open_socket(name, mcast_addr, GENERAL_PORT, ttl);
if (gfd < 0) if (gfd < 0)
goto no_general; goto no_general;