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/stat.h> 40 #include <sys/uio.h> 41 #include <termios.h> 42 #include <unistd.h> 43 44 #include "layer.h" 45 #include "defs.h" 46 #include "mbuf.h" 47 #include "log.h" 48 #include "timer.h" 49 #include "lqr.h" 50 #include "hdlc.h" 51 #include "throughput.h" 52 #include "fsm.h" 53 #include "lcp.h" 54 #include "ccp.h" 55 #include "link.h" 56 #include "async.h" 57 #include "descriptor.h" 58 #include "physical.h" 59 #include "tcp.h" 60 61 static int 62 tcp_OpenConnection(const char *name, char *host, char *port) 63 { 64 struct sockaddr_in dest; 65 int sock; 66 struct servent *sp; 67 68 dest.sin_family = AF_INET; 69 dest.sin_addr.s_addr = inet_addr(host); 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); 73 return -2; 74 } 75 dest.sin_port = htons(atoi(port)); 76 if (dest.sin_port == 0) { 77 sp = getservbyname(port, "tcp"); 78 if (sp) 79 dest.sin_port = sp->s_port; 80 else { 81 log_Printf(LogWARN, "%s: %s: unknown service\n", name, port); 82 return -2; 83 } 84 } 85 log_Printf(LogPHASE, "%s: Connecting to %s:%s/tcp\n", name, host, port); 86 87 sock = socket(PF_INET, SOCK_STREAM, 0); 88 if (sock < 0) 89 return -2; 90 91 if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) { 92 log_Printf(LogWARN, "%s: connect: %s\n", name, strerror(errno)); 93 close(sock); 94 return -2; 95 } 96 97 return sock; 98 } 99 100 static struct device tcpdevice = { 101 TCP_DEVICE, 102 "tcp", 103 0, 104 { CD_NOTREQUIRED, 0 }, 105 NULL, 106 NULL, 107 NULL, 108 NULL, 109 NULL, 110 NULL, 111 NULL, 112 NULL, 113 NULL, 114 NULL, 115 NULL, 116 NULL 117 }; 118 119 struct device * 120 tcp_iov2device(int type, struct physical *p, struct iovec *iov, 121 int *niov, int maxiov, int *auxfd, int *nauxfd) 122 { 123 if (type == TCP_DEVICE) { 124 free(iov[(*niov)++].iov_base); 125 physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); 126 return &tcpdevice; 127 } 128 129 return NULL; 130 } 131 132 struct device * 133 tcp_Create(struct physical *p) 134 { 135 char *cp, *host, *port, *svc; 136 137 if (p->fd < 0) { 138 if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) { 139 *cp = '\0'; 140 host = p->name.full; 141 port = cp + 1; 142 svc = strchr(port, '/'); 143 if (svc && strcasecmp(svc, "/tcp")) { 144 *cp = ':'; 145 return 0; 146 } 147 if (svc) { 148 p->fd--; /* We own the device but maybe can't use it - change fd */ 149 *svc = '\0'; 150 } 151 if (*host && *port) { 152 p->fd = tcp_OpenConnection(p->link.name, host, port); 153 *cp = ':'; 154 if (svc) 155 *svc = '/'; 156 if (p->fd >= 0) 157 log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name, 158 p->name.full); 159 } else { 160 if (svc) 161 *svc = '/'; 162 *cp = ':'; 163 } 164 } 165 } 166 167 if (p->fd >= 0) { 168 /* See if we're a tcp socket */ 169 struct stat st; 170 171 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) { 172 int type, sz; 173 174 sz = sizeof type; 175 if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) { 176 log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name); 177 close(p->fd); 178 p->fd = -1; 179 return NULL; 180 } 181 182 if (sz == sizeof type && type == SOCK_STREAM) { 183 struct sockaddr_in sock; 184 struct sockaddr *sockp = (struct sockaddr *)&sock; 185 186 if (*p->name.full == '\0') { 187 sz = sizeof sock; 188 if (getpeername(p->fd, sockp, &sz) != 0 || 189 sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) { 190 log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n", 191 p->link.name); 192 return NULL; 193 } 194 195 log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name); 196 197 snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp", 198 inet_ntoa(sock.sin_addr), ntohs(sock.sin_port)); 199 p->name.base = p->name.full; 200 } 201 physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); 202 if (p->cfg.cd.necessity != CD_DEFAULT) 203 log_Printf(LogWARN, "Carrier settings ignored\n"); 204 return &tcpdevice; 205 } 206 } 207 } 208 209 return NULL; 210 } 211