xref: /freebsd/usr.sbin/ppp/tcp.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
1 /*-
2  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/uio.h>
40 #include <termios.h>
41 #include <unistd.h>
42 
43 #include "layer.h"
44 #include "defs.h"
45 #include "mbuf.h"
46 #include "log.h"
47 #include "timer.h"
48 #include "lqr.h"
49 #include "hdlc.h"
50 #include "throughput.h"
51 #include "fsm.h"
52 #include "lcp.h"
53 #include "ccp.h"
54 #include "link.h"
55 #include "async.h"
56 #include "descriptor.h"
57 #include "physical.h"
58 #include "tcp.h"
59 
60 static int
61 tcp_OpenConnection(const char *name, char *host, char *port)
62 {
63   struct sockaddr_in dest;
64   int sock;
65   struct servent *sp;
66 
67   dest.sin_family = AF_INET;
68   dest.sin_addr.s_addr = inet_addr(host);
69   dest.sin_addr = GetIpAddr(host);
70   if (dest.sin_addr.s_addr == INADDR_NONE) {
71     log_Printf(LogWARN, "%s: %s: unknown host\n", name, host);
72     return -1;
73   }
74   dest.sin_port = htons(atoi(port));
75   if (dest.sin_port == 0) {
76     sp = getservbyname(port, "tcp");
77     if (sp)
78       dest.sin_port = sp->s_port;
79     else {
80       log_Printf(LogWARN, "%s: %s: unknown service\n", name, port);
81       return -1;
82     }
83   }
84   log_Printf(LogPHASE, "%s: Connecting to %s:%s/tcp\n", name, host, port);
85 
86   sock = socket(PF_INET, SOCK_STREAM, 0);
87   if (sock < 0)
88     return sock;
89 
90   if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) {
91     log_Printf(LogWARN, "%s: connect: %s\n", name, strerror(errno));
92     close(sock);
93     return -1;
94   }
95 
96   return sock;
97 }
98 
99 static struct device tcpdevice = {
100   TCP_DEVICE,
101   "tcp",
102   NULL,
103   NULL,
104   NULL,
105   NULL,
106   NULL,
107   NULL,
108   NULL,
109   NULL,
110   NULL,
111   NULL,
112   NULL,
113   NULL
114 };
115 
116 struct device *
117 tcp_iov2device(int type, struct physical *p, struct iovec *iov,
118                int *niov, int maxiov, int *auxfd, int *nauxfd)
119 {
120   if (type == TCP_DEVICE) {
121     free(iov[(*niov)++].iov_base);
122     physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
123     return &tcpdevice;
124   }
125 
126   return NULL;
127 }
128 
129 struct device *
130 tcp_Create(struct physical *p)
131 {
132   char *cp, *host, *port, *svc;
133 
134   if (p->fd < 0) {
135     if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
136       *cp = '\0';
137       host = p->name.full;
138       port = cp + 1;
139       svc = strchr(port, '/');
140       if (svc && strcasecmp(svc, "/tcp")) {
141         *cp = ':';
142         return 0;
143       }
144       if (svc) {
145         p->fd--;     /* We own the device but maybe can't use it - change fd */
146         *svc = '\0';
147       }
148       if (*host && *port) {
149         p->fd = tcp_OpenConnection(p->link.name, host, port);
150         *cp = ':';
151         if (svc)
152           *svc = '/';
153         if (p->fd >= 0)
154           log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name,
155                      p->name.full);
156       } else {
157         if (svc)
158           *svc = '/';
159         *cp = ':';
160       }
161     }
162   }
163 
164   if (p->fd >= 0) {
165     /* See if we're a tcp socket */
166     int type, sz, err;
167 
168     sz = sizeof type;
169     if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 &&
170         sz == sizeof type && type == SOCK_STREAM) {
171       struct sockaddr_in sock;
172       struct sockaddr *sockp = (struct sockaddr *)&sock;
173 
174       if (*p->name.full == '\0') {
175         sz = sizeof sock;
176         if (getpeername(p->fd, sockp, &sz) != 0 ||
177             sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) {
178           log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n",
179                      p->link.name);
180           return NULL;
181         }
182 
183         log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name);
184 
185         snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp",
186                  inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
187         p->name.base = p->name.full;
188       }
189       physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
190       return &tcpdevice;
191     }
192   }
193 
194   return NULL;
195 }
196