2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
54 #include "throughput.h"
60 #include "descriptor.h"
66 #define UDP_CONNECTED 1
67 #define UDP_UNCONNECTED 2
68 #define UDP_MAYBEUNCONNECTED 3
71 struct device dev; /* What struct physical knows about */
72 struct sockaddr_in sock; /* peer address */
73 unsigned connected : 2; /* Have we connect()d ? */
76 #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL)
81 return sizeof(struct udpdevice);
85 udp_Sendto(struct physical *p, const void *v, size_t n)
87 struct udpdevice *dev = device2udp(p->handler);
90 switch (dev->connected) {
92 ret = write(p->fd, v, n);
97 ret = sendto(p->fd, v, n, 0, (struct sockaddr *)&dev->sock,
101 if (dev->connected == UDP_MAYBEUNCONNECTED) {
102 if (ret == -1 && errno == EISCONN) {
103 dev->connected = UDP_CONNECTED;
104 ret = write(p->fd, v, n);
106 dev->connected = UDP_UNCONNECTED;
113 udp_Recvfrom(struct physical *p, void *v, size_t n)
115 struct udpdevice *dev = device2udp(p->handler);
118 if (dev->connected == UDP_CONNECTED)
119 return read(p->fd, v, n);
121 sz = sizeof dev->sock;
122 ret = recvfrom(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, &sz);
124 if (*p->name.full == '\0') {
125 snprintf(p->name.full, sizeof p->name.full, "%s:%d/udp",
126 inet_ntoa(dev->sock.sin_addr), ntohs(dev->sock.sin_port));
127 p->name.base = p->name.full;
134 udp_Free(struct physical *p)
136 struct udpdevice *dev = device2udp(p->handler);
142 udp_device2iov(struct device *d, struct iovec *iov, int *niov,
143 int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
145 int sz = physical_MaxDeviceSize();
147 iov[*niov].iov_base = realloc(d, sz);
148 if (iov[*niov].iov_base == NULL) {
149 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
150 AbortProgram(EX_OSERR);
152 iov[*niov].iov_len = sz;
156 static const struct device baseudpdevice = {
160 { CD_NOTREQUIRED, 0 },
178 udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
179 int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
181 if (type == UDP_DEVICE) {
182 struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base;
184 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
186 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
188 AbortProgram(EX_OSERR);
191 /* Refresh function pointers etc */
192 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
194 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
201 static struct udpdevice *
202 udp_CreateDevice(struct physical *p, char *host, char *port)
204 struct udpdevice *dev;
207 if ((dev = malloc(sizeof *dev)) == NULL) {
208 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
209 p->link.name, strerror(errno));
213 dev->sock.sin_family = AF_INET;
214 dev->sock.sin_addr = GetIpAddr(host);
215 if (dev->sock.sin_addr.s_addr == INADDR_NONE) {
216 log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host);
220 dev->sock.sin_port = htons(atoi(port));
221 if (dev->sock.sin_port == 0) {
222 sp = getservbyname(port, "udp");
224 dev->sock.sin_port = sp->s_port;
226 log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port);
232 log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name,
235 p->fd = socket(PF_INET, SOCK_DGRAM, 0);
237 log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name,
239 if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) {
240 dev->connected = UDP_CONNECTED;
243 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
245 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
255 udp_Create(struct physical *p)
257 char *cp, *host, *port, *svc;
258 struct udpdevice *dev;
262 if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
266 svc = strchr(port, '/');
267 if (svc && strcasecmp(svc, "/udp")) {
272 p->fd--; /* We own the device but maybe can't use it - change fd */
277 dev = udp_CreateDevice(p, host, port);
284 /* See if we're a connected udp socket */
287 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
291 if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
292 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
298 if (sz == sizeof type && type == SOCK_DGRAM) {
299 struct sockaddr_in sock;
300 struct sockaddr *sockp = (struct sockaddr *)&sock;
302 if ((dev = malloc(sizeof *dev)) == NULL) {
303 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
304 p->link.name, strerror(errno));
308 if (getpeername(p->fd, sockp, &sz) == 0) {
309 log_Printf(LogPHASE, "%s: Link is a connected udp socket\n",
311 dev->connected = UDP_CONNECTED;
313 log_Printf(LogPHASE, "%s: Link is a disconnected udp socket\n",
316 dev->connected = UDP_MAYBEUNCONNECTED;
318 if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) {
319 log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n",
321 p->link.lcp.cfg.openmode = OPEN_PASSIVE;
329 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
330 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
331 if (p->cfg.cd.necessity != CD_DEFAULT)
332 log_Printf(LogWARN, "Carrier settings ignored\n");