2011-11-06 15:16:19 +08:00
|
|
|
/**
|
|
|
|
* @file udp.c
|
|
|
|
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-12-18 15:21:47 +08:00
|
|
|
#include "print.h"
|
2012-03-17 21:53:10 +08:00
|
|
|
#include "sk.h"
|
2012-03-13 15:28:37 +08:00
|
|
|
#include "transport_private.h"
|
2011-11-06 15:16:19 +08:00
|
|
|
#include "udp.h"
|
|
|
|
|
|
|
|
#define EVENT_PORT 319
|
|
|
|
#define GENERAL_PORT 320
|
2012-04-05 23:18:49 +08:00
|
|
|
#define PTP_PRIMARY_MCAST_IPADDR "224.0.1.129"
|
|
|
|
#define PTP_PDELAY_MCAST_IPADDR "224.0.0.107"
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2011-11-21 01:31:33 +08:00
|
|
|
static int mcast_bind(int fd, int index)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ip_mreqn req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.imr_ifindex = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &req, sizeof(req));
|
|
|
|
if (err) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("setsockopt IP_MULTICAST_IF failed: %m");
|
2011-11-21 01:31:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-06 15:16:19 +08:00
|
|
|
static int mcast_join(int fd, int index, const struct sockaddr *grp,
|
|
|
|
socklen_t grplen)
|
|
|
|
{
|
2012-01-02 02:44:49 +08:00
|
|
|
int err, off = 0;
|
2011-12-30 17:19:21 +08:00
|
|
|
struct ip_mreqn req;
|
|
|
|
struct sockaddr_in *sa = (struct sockaddr_in *) grp;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
memcpy(&req.imr_multiaddr, &sa->sin_addr, sizeof(struct in_addr));
|
|
|
|
req.imr_ifindex = index;
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req));
|
2011-11-06 15:16:19 +08:00
|
|
|
if (err) {
|
2011-12-30 17:19:21 +08:00
|
|
|
pr_err("setsockopt IP_ADD_MEMBERSHIP failed: %m");
|
2012-01-02 02:44:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &off, sizeof(off));
|
|
|
|
if (err) {
|
|
|
|
pr_err("setsockopt IP_MULTICAST_LOOP failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static int udp_close(struct transport *t, struct fdarray *fda)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
close(fda->fd[0]);
|
|
|
|
close(fda->fd[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
static int open_socket(char *name, struct in_addr mc_addr[2], short port)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int fd, index, on = 1;
|
|
|
|
|
2012-11-26 00:45:37 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2011-11-06 15:16:19 +08:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("socket failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_socket;
|
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
index = sk_interface_index(fd, name);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (index < 0)
|
|
|
|
goto no_option;
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("setsockopt SO_REUSEADDR failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("bind failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
2012-01-07 14:56:57 +08:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
|
|
|
|
pr_err("setsockopt SO_BINDTODEVICE failed: %m");
|
|
|
|
goto no_option;
|
|
|
|
}
|
2012-04-05 23:18:49 +08:00
|
|
|
addr.sin_addr = mc_addr[0];
|
|
|
|
if (mcast_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) {
|
|
|
|
pr_err("mcast_join failed");
|
|
|
|
goto no_option;
|
|
|
|
}
|
|
|
|
addr.sin_addr = mc_addr[1];
|
2011-11-06 15:16:19 +08:00
|
|
|
if (mcast_join(fd, index, (struct sockaddr *) &addr, sizeof(addr))) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("mcast_join failed");
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_option;
|
|
|
|
}
|
2011-11-21 01:31:33 +08:00
|
|
|
if (mcast_bind(fd, index)) {
|
|
|
|
goto no_option;
|
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
return fd;
|
|
|
|
no_option:
|
|
|
|
close(fd);
|
|
|
|
no_socket:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
enum { MC_PRIMARY, MC_PDELAY };
|
|
|
|
|
|
|
|
static struct in_addr mcast_addr[2];
|
2011-11-06 15:16:19 +08:00
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static int udp_open(struct transport *t, char *name, struct fdarray *fda,
|
|
|
|
enum timestamp_type ts_type)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
int efd, gfd;
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
if (!inet_aton(PTP_PRIMARY_MCAST_IPADDR, &mcast_addr[MC_PRIMARY]))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!inet_aton(PTP_PDELAY_MCAST_IPADDR, &mcast_addr[MC_PDELAY]))
|
2011-11-06 15:16:19 +08:00
|
|
|
return -1;
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
efd = open_socket(name, mcast_addr, EVENT_PORT);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (efd < 0)
|
|
|
|
goto no_event;
|
|
|
|
|
2012-04-05 23:18:49 +08:00
|
|
|
gfd = open_socket(name, mcast_addr, GENERAL_PORT);
|
2011-11-06 15:16:19 +08:00
|
|
|
if (gfd < 0)
|
|
|
|
goto no_general;
|
|
|
|
|
2012-10-25 21:19:31 +08:00
|
|
|
if (sk_timestamping_init(efd, name, ts_type, TRANS_UDP_IPV4))
|
2011-11-06 15:16:19 +08:00
|
|
|
goto no_timestamping;
|
|
|
|
|
|
|
|
fda->fd[FD_EVENT] = efd;
|
|
|
|
fda->fd[FD_GENERAL] = gfd;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
no_timestamping:
|
|
|
|
close(gfd);
|
|
|
|
no_general:
|
|
|
|
close(efd);
|
|
|
|
no_event:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static int udp_recv(struct transport *t, int fd, void *buf, int buflen,
|
|
|
|
struct hw_timestamp *hwts)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
2012-03-17 21:53:10 +08:00
|
|
|
return sk_receive(fd, buf, buflen, hwts, 0);
|
2011-11-06 15:16:19 +08:00
|
|
|
}
|
|
|
|
|
2012-04-05 23:15:32 +08:00
|
|
|
static int udp_send(struct transport *t, struct fdarray *fda, int event, int peer,
|
2012-03-13 15:28:37 +08:00
|
|
|
void *buf, int len, struct hw_timestamp *hwts)
|
2011-11-06 15:16:19 +08:00
|
|
|
{
|
|
|
|
ssize_t cnt;
|
|
|
|
int fd = event ? fda->fd[FD_EVENT] : fda->fd[FD_GENERAL];
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
unsigned char junk[1600];
|
|
|
|
|
2012-11-26 00:45:37 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2011-11-06 15:16:19 +08:00
|
|
|
addr.sin_family = AF_INET;
|
2012-04-05 23:18:49 +08:00
|
|
|
addr.sin_addr = peer ? mcast_addr[MC_PDELAY] : mcast_addr[MC_PRIMARY];
|
2011-11-06 15:16:19 +08:00
|
|
|
addr.sin_port = htons(event ? EVENT_PORT : GENERAL_PORT);
|
|
|
|
|
|
|
|
cnt = sendto(fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr));
|
2011-12-13 12:28:54 +08:00
|
|
|
if (cnt < 1) {
|
2011-12-18 15:21:47 +08:00
|
|
|
pr_err("sendto failed: %m");
|
2011-11-06 15:16:19 +08:00
|
|
|
return cnt;
|
2011-12-13 12:28:54 +08:00
|
|
|
}
|
2011-11-06 15:16:19 +08:00
|
|
|
/*
|
|
|
|
* Get the time stamp right away.
|
|
|
|
*/
|
2012-03-17 21:53:10 +08:00
|
|
|
return event ? sk_receive(fd, junk, len, hwts, MSG_ERRQUEUE) : cnt;
|
2011-11-06 15:16:19 +08:00
|
|
|
}
|
|
|
|
|
2012-03-17 21:54:09 +08:00
|
|
|
static void udp_release(struct transport *t)
|
|
|
|
{
|
|
|
|
/* No need for any per-instance deallocation. */
|
|
|
|
}
|
|
|
|
|
2012-03-13 15:28:37 +08:00
|
|
|
static struct transport the_udp_transport = {
|
|
|
|
.close = udp_close,
|
|
|
|
.open = udp_open,
|
|
|
|
.recv = udp_recv,
|
|
|
|
.send = udp_send,
|
2012-03-17 21:54:09 +08:00
|
|
|
.release = udp_release,
|
2012-03-13 15:28:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct transport *udp_transport_create(void)
|
|
|
|
{
|
|
|
|
/* No need for any per-instance allocation. */
|
|
|
|
return &the_udp_transport;
|
|
|
|
}
|