forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uds.c
139 lines (120 loc) · 3.43 KB
/
uds.c
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
/**
* @file uds.c
* @note Copyright (C) 2012 Richard Cochran <[email protected]>
*
* 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>
#include "address.h"
#include "contain.h"
#include "print.h"
#include "transport_private.h"
#include "uds.h"
char uds_path[MAX_IFNAME_SIZE + 1] = "/var/run/ptp4l";
#define UDS_FILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) /*0660*/
struct uds {
struct transport t;
struct address address;
};
static int uds_close(struct transport *t, struct fdarray *fda)
{
close(fda->fd[FD_GENERAL]);
return 0;
}
static int uds_open(struct transport *t, const char *name, struct fdarray *fda,
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;
strncpy(sa.sun_path, name, sizeof(sa.sun_path) - 1);
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;
strncpy(sa.sun_path, uds_path, sizeof(sa.sun_path) - 1);
uds->address.sun = sa;
uds->address.len = sizeof(sa);
chmod(name, UDS_FILEMODE);
fda->fd[FD_EVENT] = -1;
fda->fd[FD_GENERAL] = fd;
return 0;
}
static int uds_recv(struct transport *t, int fd, void *buf, int buflen,
struct address *addr, struct hw_timestamp *hwts)
{
int cnt;
struct uds *uds = container_of(t, struct uds, t);
addr->len = sizeof(addr->sun);
cnt = recvfrom(fd, buf, buflen, 0, &addr->sa, &addr->len);
if (cnt <= 0) {
pr_err("uds: recvfrom failed: %m");
return cnt;
}
uds->address = *addr;
return cnt;
}
static int uds_send(struct transport *t, struct fdarray *fda, int event,
int peer, void *buf, int buflen, struct address *addr,
struct hw_timestamp *hwts)
{
int cnt, fd = fda->fd[FD_GENERAL];
struct uds *uds = container_of(t, struct uds, t);
if (!addr)
addr = &uds->address;
cnt = sendto(fd, buf, buflen, 0, &addr->sa, addr->len);
if (cnt <= 0 && errno != ECONNREFUSED) {
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;
}