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 __unused
, int *auxfd __unused
, int *nauxfd __unused
)
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 },
176 udp_iov2device(int type
, struct physical
*p
, struct iovec
*iov
, int *niov
,
177 int maxiov __unused
, int *auxfd __unused
, int *nauxfd __unused
)
179 if (type
== UDP_DEVICE
) {
180 struct udpdevice
*dev
= (struct udpdevice
*)iov
[(*niov
)++].iov_base
;
182 dev
= realloc(dev
, sizeof *dev
); /* Reduce to the correct size */
184 log_Printf(LogALERT
, "Failed to allocate memory: %d\n",
186 AbortProgram(EX_OSERR
);
189 /* Refresh function pointers etc */
190 memcpy(&dev
->dev
, &baseudpdevice
, sizeof dev
->dev
);
192 physical_SetupStack(p
, dev
->dev
.name
, PHYSICAL_FORCE_SYNC
);
199 static struct udpdevice
*
200 udp_CreateDevice(struct physical
*p
, char *host
, char *port
)
202 struct udpdevice
*dev
;
205 if ((dev
= malloc(sizeof *dev
)) == NULL
) {
206 log_Printf(LogWARN
, "%s: Cannot allocate a udp device: %s\n",
207 p
->link
.name
, strerror(errno
));
211 dev
->sock
.sin_family
= AF_INET
;
212 dev
->sock
.sin_addr
= GetIpAddr(host
);
213 if (dev
->sock
.sin_addr
.s_addr
== INADDR_NONE
) {
214 log_Printf(LogWARN
, "%s: %s: unknown host\n", p
->link
.name
, host
);
218 dev
->sock
.sin_port
= htons(atoi(port
));
219 if (dev
->sock
.sin_port
== 0) {
220 sp
= getservbyname(port
, "udp");
222 dev
->sock
.sin_port
= sp
->s_port
;
224 log_Printf(LogWARN
, "%s: %s: unknown service\n", p
->link
.name
, port
);
230 log_Printf(LogPHASE
, "%s: Connecting to %s:%s/udp\n", p
->link
.name
,
233 p
->fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
235 log_Printf(LogDEBUG
, "%s: Opened udp socket %s\n", p
->link
.name
,
237 if (connect(p
->fd
, (struct sockaddr
*)&dev
->sock
, sizeof dev
->sock
) == 0) {
238 dev
->connected
= UDP_CONNECTED
;
241 log_Printf(LogWARN
, "%s: connect: %s\n", p
->name
.full
, strerror(errno
));
243 log_Printf(LogWARN
, "%s: socket: %s\n", p
->name
.full
, strerror(errno
));
253 udp_Create(struct physical
*p
)
255 char *cp
, *host
, *port
, *svc
;
256 struct udpdevice
*dev
;
260 if ((cp
= strchr(p
->name
.full
, ':')) != NULL
&& !strchr(cp
+ 1, ':')) {
264 svc
= strchr(port
, '/');
265 if (svc
&& strcasecmp(svc
, "/udp")) {
270 p
->fd
--; /* We own the device but maybe can't use it - change fd */
275 dev
= udp_CreateDevice(p
, host
, port
);
282 /* See if we're a connected udp socket */
285 if (fstat(p
->fd
, &st
) != -1 && (st
.st_mode
& S_IFSOCK
)) {
289 if (getsockopt(p
->fd
, SOL_SOCKET
, SO_TYPE
, &type
, &sz
) == -1) {
290 log_Printf(LogPHASE
, "%s: Link is a closed socket !\n", p
->link
.name
);
296 if (sz
== sizeof type
&& type
== SOCK_DGRAM
) {
297 struct sockaddr_in sock
;
298 struct sockaddr
*sockp
= (struct sockaddr
*)&sock
;
300 if ((dev
= malloc(sizeof *dev
)) == NULL
) {
301 log_Printf(LogWARN
, "%s: Cannot allocate a udp device: %s\n",
302 p
->link
.name
, strerror(errno
));
306 if (getpeername(p
->fd
, sockp
, &sz
) == 0) {
307 log_Printf(LogPHASE
, "%s: Link is a connected udp socket\n",
309 dev
->connected
= UDP_CONNECTED
;
311 log_Printf(LogPHASE
, "%s: Link is a disconnected udp socket\n",
314 dev
->connected
= UDP_MAYBEUNCONNECTED
;
316 if (p
->link
.lcp
.cfg
.openmode
!= OPEN_PASSIVE
) {
317 log_Printf(LogPHASE
, "%s: Changing openmode to PASSIVE\n",
319 p
->link
.lcp
.cfg
.openmode
= OPEN_PASSIVE
;
327 memcpy(&dev
->dev
, &baseudpdevice
, sizeof dev
->dev
);
328 physical_SetupStack(p
, dev
->dev
.name
, PHYSICAL_FORCE_SYNC
);
329 if (p
->cfg
.cd
.necessity
!= CD_DEFAULT
)
330 log_Printf(LogWARN
, "Carrier settings ignored\n");