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 }; 114 115 struct device * 116 tcp_iov2device(int type, struct physical *p, struct iovec *iov, 117 int *niov, int maxiov) 118 { 119 if (type == TCP_DEVICE) { 120 free(iov[(*niov)++].iov_base); 121 physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); 122 return &tcpdevice; 123 } 124 125 return NULL; 126 } 127 128 struct device * 129 tcp_Create(struct physical *p) 130 { 131 char *cp, *host, *port, *svc; 132 133 if (p->fd < 0) { 134 if ((cp = strchr(p->name.full, ':')) != NULL) { 135 *cp = '\0'; 136 host = p->name.full; 137 port = cp + 1; 138 svc = strchr(port, '/'); 139 if (svc && strcasecmp(svc, "/tcp")) { 140 *cp = ':'; 141 return 0; 142 } 143 if (svc) 144 *svc = '\0'; 145 if (*host && *port) { 146 p->fd = tcp_OpenConnection(p->link.name, host, port); 147 *cp = ':'; 148 if (svc) 149 *svc = '/'; 150 if (p->fd >= 0) 151 log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name, 152 p->name.full); 153 } else { 154 if (svc) 155 *svc = '/'; 156 *cp = ':'; 157 } 158 } 159 } 160 161 if (p->fd >= 0) { 162 /* See if we're a tcp socket */ 163 int type, sz, err; 164 165 sz = sizeof type; 166 if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 && 167 sz == sizeof type && type == SOCK_STREAM) { 168 struct sockaddr_in sock; 169 struct sockaddr *sockp = (struct sockaddr *)&sock; 170 171 if (*p->name.full == '\0') { 172 sz = sizeof sock; 173 if (getpeername(p->fd, sockp, &sz) != 0 || 174 sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) { 175 log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n", 176 p->link.name); 177 return NULL; 178 } 179 180 log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name); 181 182 snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp", 183 inet_ntoa(sock.sin_addr), ntohs(sock.sin_port)); 184 p->name.base = p->name.full; 185 } 186 physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); 187 return &tcpdevice; 188 } 189 } 190 191 return NULL; 192 } 193