2012-08-24 21:22:40 +08:00
|
|
|
/**
|
|
|
|
* @file uds.c
|
|
|
|
* @note Copyright (C) 2012 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 <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
#include "address.h"
|
2012-08-24 21:22:40 +08:00
|
|
|
#include "contain.h"
|
|
|
|
#include "print.h"
|
|
|
|
#include "transport_private.h"
|
|
|
|
#include "uds.h"
|
|
|
|
|
2013-09-30 02:15:45 +08:00
|
|
|
char uds_path[MAX_IFNAME_SIZE + 1] = "/var/run/ptp4l";
|
|
|
|
|
2012-08-24 21:22:40 +08:00
|
|
|
#define UDS_FILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) /*0660*/
|
|
|
|
|
|
|
|
struct uds {
|
|
|
|
struct transport t;
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address address;
|
2012-08-24 21:22:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int uds_close(struct transport *t, struct fdarray *fda)
|
|
|
|
{
|
2014-07-08 22:14:19 +08:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
socklen_t len = sizeof(sa);
|
|
|
|
|
|
|
|
if (!getsockname(fda->fd[FD_GENERAL], (struct sockaddr *) &sa, &len) &&
|
|
|
|
sa.sun_family == AF_LOCAL) {
|
|
|
|
unlink(sa.sun_path);
|
|
|
|
}
|
|
|
|
|
2012-08-24 21:22:40 +08:00
|
|
|
close(fda->fd[FD_GENERAL]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
static int uds_open(struct transport *t, const char *name, struct fdarray *fda,
|
2012-08-24 21:22:40 +08:00
|
|
|
enum timestamp_type tt)
|
|
|
|
{
|
|
|
|
int fd, err;
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
struct uds *uds = container_of(t, struct uds, t);
|
|
|
|
|
|
|
|
fd = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("uds: failed to create socket: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sun_family = AF_LOCAL;
|
2013-09-30 02:15:45 +08:00
|
|
|
strncpy(sa.sun_path, name, sizeof(sa.sun_path) - 1);
|
2012-08-24 21:22:40 +08:00
|
|
|
|
|
|
|
unlink(name);
|
|
|
|
|
|
|
|
err = bind(fd, (struct sockaddr *) &sa, sizeof(sa));
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("uds: bind failed: %m");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For client use, pre load the server path. */
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sun_family = AF_LOCAL;
|
2013-09-30 02:15:45 +08:00
|
|
|
strncpy(sa.sun_path, uds_path, sizeof(sa.sun_path) - 1);
|
2014-04-22 22:00:59 +08:00
|
|
|
uds->address.sun = sa;
|
|
|
|
uds->address.len = sizeof(sa);
|
2012-08-24 21:22:40 +08:00
|
|
|
|
|
|
|
chmod(name, UDS_FILEMODE);
|
2012-08-28 03:05:34 +08:00
|
|
|
fda->fd[FD_EVENT] = -1;
|
2012-08-24 21:22:40 +08:00
|
|
|
fda->fd[FD_GENERAL] = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uds_recv(struct transport *t, int fd, void *buf, int buflen,
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address *addr, struct hw_timestamp *hwts)
|
2012-08-24 21:22:40 +08:00
|
|
|
{
|
|
|
|
int cnt;
|
|
|
|
struct uds *uds = container_of(t, struct uds, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
|
|
|
|
addr->len = sizeof(addr->sun);
|
|
|
|
cnt = recvfrom(fd, buf, buflen, 0, &addr->sa, &addr->len);
|
2012-08-24 21:22:40 +08:00
|
|
|
if (cnt <= 0) {
|
|
|
|
pr_err("uds: recvfrom failed: %m");
|
2014-04-22 22:00:59 +08:00
|
|
|
return cnt;
|
2012-08-24 21:22:40 +08:00
|
|
|
}
|
2014-04-22 22:00:59 +08:00
|
|
|
uds->address = *addr;
|
2012-08-24 21:22:40 +08:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uds_send(struct transport *t, struct fdarray *fda, int event,
|
2014-04-22 22:00:59 +08:00
|
|
|
int peer, void *buf, int buflen, struct address *addr,
|
|
|
|
struct hw_timestamp *hwts)
|
2012-08-24 21:22:40 +08:00
|
|
|
{
|
|
|
|
int cnt, fd = fda->fd[FD_GENERAL];
|
|
|
|
struct uds *uds = container_of(t, struct uds, t);
|
2014-04-22 22:00:59 +08:00
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
addr = &uds->address;
|
|
|
|
|
|
|
|
cnt = sendto(fd, buf, buflen, 0, &addr->sa, addr->len);
|
2014-05-02 18:37:44 +08:00
|
|
|
if (cnt <= 0 && errno != ECONNREFUSED) {
|
2012-08-24 21:22:40 +08:00
|
|
|
pr_err("uds: sendto failed: %m");
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uds_release(struct transport *t)
|
|
|
|
{
|
|
|
|
struct uds *uds = container_of(t, struct uds, t);
|
|
|
|
free(uds);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct transport *uds_transport_create(void)
|
|
|
|
{
|
|
|
|
struct uds *uds;
|
|
|
|
uds = calloc(1, sizeof(*uds));
|
|
|
|
if (!uds)
|
|
|
|
return NULL;
|
|
|
|
uds->t.close = uds_close;
|
|
|
|
uds->t.open = uds_open;
|
|
|
|
uds->t.recv = uds_recv;
|
|
|
|
uds->t.send = uds_send;
|
|
|
|
uds->t.release = uds_release;
|
|
|
|
return &uds->t;
|
|
|
|
}
|
|
|
|
|