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
26 * $FreeBSD: src/usr.sbin/ppp/udp.c,v 1.10.2.4 2002/09/01 02:12:32 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/udp.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
53 #include "throughput.h"
59 #include "descriptor.h"
65 #define UDP_CONNECTED 1
66 #define UDP_UNCONNECTED 2
67 #define UDP_MAYBEUNCONNECTED 3
70 struct device dev
; /* What struct physical knows about */
71 struct sockaddr_in sock
; /* peer address */
72 unsigned connected
: 2; /* Have we connect()d ? */
75 #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL)
80 return sizeof(struct udpdevice
);
84 udp_Sendto(struct physical
*p
, const void *v
, size_t n
)
86 struct udpdevice
*dev
= device2udp(p
->handler
);
89 switch (dev
->connected
) {
91 ret
= write(p
->fd
, v
, n
);
96 ret
= sendto(p
->fd
, v
, n
, 0, (struct sockaddr
*)&dev
->sock
,
100 if (dev
->connected
== UDP_MAYBEUNCONNECTED
) {
101 if (ret
== -1 && errno
== EISCONN
) {
102 dev
->connected
= UDP_CONNECTED
;
103 ret
= write(p
->fd
, v
, n
);
105 dev
->connected
= UDP_UNCONNECTED
;
112 udp_Recvfrom(struct physical
*p
, void *v
, size_t n
)
114 struct udpdevice
*dev
= device2udp(p
->handler
);
117 if (dev
->connected
== UDP_CONNECTED
)
118 return read(p
->fd
, v
, n
);
120 sz
= sizeof dev
->sock
;
121 ret
= recvfrom(p
->fd
, v
, n
, 0, (struct sockaddr
*)&dev
->sock
, &sz
);
123 if (*p
->name
.full
== '\0') {
124 snprintf(p
->name
.full
, sizeof p
->name
.full
, "%s:%d/udp",
125 inet_ntoa(dev
->sock
.sin_addr
), ntohs(dev
->sock
.sin_port
));
126 p
->name
.base
= p
->name
.full
;
133 udp_Free(struct physical
*p
)
135 struct udpdevice
*dev
= device2udp(p
->handler
);
141 udp_device2iov(struct device
*d
, struct iovec
*iov
, int *niov
,
142 int maxiov
, int *auxfd
, int *nauxfd
)
144 int sz
= physical_MaxDeviceSize();
146 iov
[*niov
].iov_base
= realloc(d
, sz
);
147 if (iov
[*niov
].iov_base
== NULL
) {
148 log_Printf(LogALERT
, "Failed to allocate memory: %d\n", sz
);
149 AbortProgram(EX_OSERR
);
151 iov
[*niov
].iov_len
= sz
;
155 static const struct device baseudpdevice
= {
159 { CD_NOTREQUIRED
, 0 },
177 udp_iov2device(int type
, struct physical
*p
, struct iovec
*iov
, int *niov
,
178 int maxiov
, int *auxfd
, int *nauxfd
)
180 if (type
== UDP_DEVICE
) {
181 struct udpdevice
*dev
= (struct udpdevice
*)iov
[(*niov
)++].iov_base
;
183 dev
= realloc(dev
, sizeof *dev
); /* Reduce to the correct size */
185 log_Printf(LogALERT
, "Failed to allocate memory: %d\n",
187 AbortProgram(EX_OSERR
);
190 /* Refresh function pointers etc */
191 memcpy(&dev
->dev
, &baseudpdevice
, sizeof dev
->dev
);
193 physical_SetupStack(p
, dev
->dev
.name
, PHYSICAL_FORCE_SYNC
);
200 static struct udpdevice
*
201 udp_CreateDevice(struct physical
*p
, char *host
, char *port
)
203 struct udpdevice
*dev
;
206 if ((dev
= malloc(sizeof *dev
)) == NULL
) {
207 log_Printf(LogWARN
, "%s: Cannot allocate a udp device: %s\n",
208 p
->link
.name
, strerror(errno
));
212 dev
->sock
.sin_family
= AF_INET
;
213 dev
->sock
.sin_addr
= GetIpAddr(host
);
214 if (dev
->sock
.sin_addr
.s_addr
== INADDR_NONE
) {
215 log_Printf(LogWARN
, "%s: %s: unknown host\n", p
->link
.name
, host
);
219 dev
->sock
.sin_port
= htons(atoi(port
));
220 if (dev
->sock
.sin_port
== 0) {
221 sp
= getservbyname(port
, "udp");
223 dev
->sock
.sin_port
= sp
->s_port
;
225 log_Printf(LogWARN
, "%s: %s: unknown service\n", p
->link
.name
, port
);
231 log_Printf(LogPHASE
, "%s: Connecting to %s:%s/udp\n", p
->link
.name
,
234 p
->fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
236 log_Printf(LogDEBUG
, "%s: Opened udp socket %s\n", p
->link
.name
,
238 if (connect(p
->fd
, (struct sockaddr
*)&dev
->sock
, sizeof dev
->sock
) == 0) {
239 dev
->connected
= UDP_CONNECTED
;
242 log_Printf(LogWARN
, "%s: connect: %s\n", p
->name
.full
, strerror(errno
));
244 log_Printf(LogWARN
, "%s: socket: %s\n", p
->name
.full
, strerror(errno
));
254 udp_Create(struct physical
*p
)
256 char *cp
, *host
, *port
, *svc
;
257 struct udpdevice
*dev
;
261 if ((cp
= strchr(p
->name
.full
, ':')) != NULL
&& !strchr(cp
+ 1, ':')) {
265 svc
= strchr(port
, '/');
266 if (svc
&& strcasecmp(svc
, "/udp")) {
271 p
->fd
--; /* We own the device but maybe can't use it - change fd */
276 dev
= udp_CreateDevice(p
, host
, port
);
283 /* See if we're a connected udp socket */
286 if (fstat(p
->fd
, &st
) != -1 && (st
.st_mode
& S_IFSOCK
)) {
290 if (getsockopt(p
->fd
, SOL_SOCKET
, SO_TYPE
, &type
, &sz
) == -1) {
291 log_Printf(LogPHASE
, "%s: Link is a closed socket !\n", p
->link
.name
);
297 if (sz
== sizeof type
&& type
== SOCK_DGRAM
) {
298 struct sockaddr_in sock
;
299 struct sockaddr
*sockp
= (struct sockaddr
*)&sock
;
301 if ((dev
= malloc(sizeof *dev
)) == NULL
) {
302 log_Printf(LogWARN
, "%s: Cannot allocate a udp device: %s\n",
303 p
->link
.name
, strerror(errno
));
307 if (getpeername(p
->fd
, sockp
, &sz
) == 0) {
308 log_Printf(LogPHASE
, "%s: Link is a connected udp socket\n",
310 dev
->connected
= UDP_CONNECTED
;
312 log_Printf(LogPHASE
, "%s: Link is a disconnected udp socket\n",
315 dev
->connected
= UDP_MAYBEUNCONNECTED
;
317 if (p
->link
.lcp
.cfg
.openmode
!= OPEN_PASSIVE
) {
318 log_Printf(LogPHASE
, "%s: Changing openmode to PASSIVE\n",
320 p
->link
.lcp
.cfg
.openmode
= OPEN_PASSIVE
;
328 memcpy(&dev
->dev
, &baseudpdevice
, sizeof dev
->dev
);
329 physical_SetupStack(p
, dev
->dev
.name
, PHYSICAL_FORCE_SYNC
);
330 if (p
->cfg
.cd
.necessity
!= CD_DEFAULT
)
331 log_Printf(LogWARN
, "Carrier settings ignored\n");