2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
52 #include "throughput.h"
58 #include "descriptor.h"
64 #define UDP_CONNECTED 1
65 #define UDP_UNCONNECTED 2
66 #define UDP_MAYBEUNCONNECTED 3
69 struct device dev; /* What struct physical knows about */
70 struct sockaddr_in sock; /* peer address */
71 unsigned connected : 2; /* Have we connect()d ? */
74 #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL)
79 return sizeof(struct udpdevice);
83 udp_Sendto(struct physical *p, const void *v, size_t n)
85 struct udpdevice *dev = device2udp(p->handler);
88 switch (dev->connected) {
90 ret = write(p->fd, v, n);
95 ret = sendto(p->fd, v, n, 0, (struct sockaddr *)&dev->sock,
99 if (dev->connected == UDP_MAYBEUNCONNECTED) {
100 if (ret == -1 && errno == EISCONN) {
101 dev->connected = UDP_CONNECTED;
102 ret = write(p->fd, v, n);
104 dev->connected = UDP_UNCONNECTED;
111 udp_Recvfrom(struct physical *p, void *v, size_t n)
113 struct udpdevice *dev = device2udp(p->handler);
116 if (dev->connected == UDP_CONNECTED)
117 return read(p->fd, v, n);
119 sz = sizeof dev->sock;
120 ret = recvfrom(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, &sz);
122 if (*p->name.full == '\0') {
123 snprintf(p->name.full, sizeof p->name.full, "%s:%d/udp",
124 inet_ntoa(dev->sock.sin_addr), ntohs(dev->sock.sin_port));
125 p->name.base = p->name.full;
132 udp_Free(struct physical *p)
134 struct udpdevice *dev = device2udp(p->handler);
140 udp_device2iov(struct device *d, struct iovec *iov, int *niov,
141 int maxiov, int *auxfd, int *nauxfd)
143 int sz = physical_MaxDeviceSize();
145 iov[*niov].iov_base = realloc(d, sz);
146 if (iov[*niov].iov_base == NULL) {
147 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
148 AbortProgram(EX_OSERR);
150 iov[*niov].iov_len = sz;
154 static const struct device baseudpdevice = {
158 { CD_NOTREQUIRED, 0 },
174 udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
175 int maxiov, int *auxfd, int *nauxfd)
177 if (type == UDP_DEVICE) {
178 struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base;
180 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
182 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
184 AbortProgram(EX_OSERR);
187 /* Refresh function pointers etc */
188 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
190 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
197 static struct udpdevice *
198 udp_CreateDevice(struct physical *p, char *host, char *port)
200 struct udpdevice *dev;
203 if ((dev = malloc(sizeof *dev)) == NULL) {
204 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
205 p->link.name, strerror(errno));
209 dev->sock.sin_family = AF_INET;
210 dev->sock.sin_addr.s_addr = inet_addr(host);
211 dev->sock.sin_addr = GetIpAddr(host);
212 if (dev->sock.sin_addr.s_addr == INADDR_NONE) {
213 log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host);
217 dev->sock.sin_port = htons(atoi(port));
218 if (dev->sock.sin_port == 0) {
219 sp = getservbyname(port, "udp");
221 dev->sock.sin_port = sp->s_port;
223 log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port);
229 log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name,
232 p->fd = socket(PF_INET, SOCK_DGRAM, 0);
234 log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name,
236 if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) {
237 dev->connected = UDP_CONNECTED;
240 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
242 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
252 udp_Create(struct physical *p)
254 char *cp, *host, *port, *svc;
255 struct udpdevice *dev;
259 if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
263 svc = strchr(port, '/');
264 if (svc && strcasecmp(svc, "/udp")) {
269 p->fd--; /* We own the device but maybe can't use it - change fd */
274 dev = udp_CreateDevice(p, host, port);
281 /* See if we're a connected udp socket */
284 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
288 if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
289 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
295 if (sz == sizeof type && type == SOCK_DGRAM) {
296 struct sockaddr_in sock;
297 struct sockaddr *sockp = (struct sockaddr *)&sock;
299 if ((dev = malloc(sizeof *dev)) == NULL) {
300 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
301 p->link.name, strerror(errno));
305 if (getpeername(p->fd, sockp, &sz) == 0) {
306 log_Printf(LogPHASE, "%s: Link is a connected udp socket\n",
308 dev->connected = UDP_CONNECTED;
310 log_Printf(LogPHASE, "%s: Link is a disconnected udp socket\n",
313 dev->connected = UDP_MAYBEUNCONNECTED;
315 if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) {
316 log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n",
318 p->link.lcp.cfg.openmode = OPEN_PASSIVE;
326 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
327 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
328 if (p->cfg.cd.necessity != CD_DEFAULT)
329 log_Printf(LogWARN, "Carrier settings ignored\n");