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/tcp.c,v 1.10.2.4 2002/09/01 02:12:32 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/tcp.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>
52 #include "throughput.h"
58 #include "descriptor.h"
63 tcp_OpenConnection(const char *name
, char *host
, char *port
)
65 struct sockaddr_in dest
;
69 dest
.sin_family
= AF_INET
;
70 dest
.sin_addr
= GetIpAddr(host
);
71 if (dest
.sin_addr
.s_addr
== INADDR_NONE
) {
72 log_Printf(LogWARN
, "%s: %s: unknown host\n", name
, host
);
75 dest
.sin_port
= htons(atoi(port
));
76 if (dest
.sin_port
== 0) {
77 sp
= getservbyname(port
, "tcp");
79 dest
.sin_port
= sp
->s_port
;
81 log_Printf(LogWARN
, "%s: %s: unknown service\n", name
, port
);
85 log_Printf(LogPHASE
, "%s: Connecting to %s:%s/tcp\n", name
, host
, port
);
87 sock
= socket(PF_INET
, SOCK_STREAM
, 0);
91 if (connect(sock
, (struct sockaddr
*)&dest
, sizeof dest
) < 0) {
92 log_Printf(LogWARN
, "%s: connect: %s\n", name
, strerror(errno
));
100 static struct device tcpdevice
= {
104 { CD_NOTREQUIRED
, 0 },
122 tcp_iov2device(int type
, struct physical
*p
, struct iovec
*iov
,
123 int *niov
, int maxiov __unused
, int *auxfd __unused
, int *nauxfd __unused
)
125 if (type
== TCP_DEVICE
) {
126 free(iov
[(*niov
)++].iov_base
);
127 physical_SetupStack(p
, tcpdevice
.name
, PHYSICAL_FORCE_ASYNC
);
135 tcp_Create(struct physical
*p
)
137 char *cp
, *host
, *port
, *svc
;
140 if ((cp
= strchr(p
->name
.full
, ':')) != NULL
&& !strchr(cp
+ 1, ':')) {
144 svc
= strchr(port
, '/');
145 if (svc
&& strcasecmp(svc
, "/tcp")) {
150 p
->fd
--; /* We own the device but maybe can't use it - change fd */
153 if (*host
&& *port
) {
154 p
->fd
= tcp_OpenConnection(p
->link
.name
, host
, port
);
159 log_Printf(LogDEBUG
, "%s: Opened tcp socket %s\n", p
->link
.name
,
170 /* See if we're a tcp socket */
173 if (fstat(p
->fd
, &st
) != -1 && (st
.st_mode
& S_IFSOCK
)) {
177 if (getsockopt(p
->fd
, SOL_SOCKET
, SO_TYPE
, &type
, &sz
) == -1) {
178 log_Printf(LogPHASE
, "%s: Link is a closed socket !\n", p
->link
.name
);
184 if (sz
== sizeof type
&& type
== SOCK_STREAM
) {
185 struct sockaddr_in sock
;
186 struct sockaddr
*sockp
= (struct sockaddr
*)&sock
;
188 if (*p
->name
.full
== '\0') {
190 if (getpeername(p
->fd
, sockp
, &sz
) != 0 ||
191 sz
!= sizeof(struct sockaddr_in
) || sock
.sin_family
!= AF_INET
) {
192 log_Printf(LogDEBUG
, "%s: Link is SOCK_STREAM, but not inet\n",
197 log_Printf(LogPHASE
, "%s: Link is a tcp socket\n", p
->link
.name
);
199 snprintf(p
->name
.full
, sizeof p
->name
.full
, "%s:%d/tcp",
200 inet_ntoa(sock
.sin_addr
), ntohs(sock
.sin_port
));
201 p
->name
.base
= p
->name
.full
;
203 physical_SetupStack(p
, tcpdevice
.name
, PHYSICAL_FORCE_ASYNC
);
204 if (p
->cfg
.cd
.necessity
!= CD_DEFAULT
)
205 log_Printf(LogWARN
, "Carrier settings ignored\n");