1 /* $NetBSD: netcmds.c,v 1.20 2004/11/04 07:18:47 dsl Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93"; 36 #endif 37 __RCSID("$NetBSD: netcmds.c,v 1.20 2004/11/04 07:18:47 dsl Exp $"); 38 #endif /* not lint */ 39 40 /* 41 * Common network command support routines. 42 */ 43 #include <sys/param.h> 44 #include <sys/mbuf.h> 45 #include <sys/protosw.h> 46 47 #include <net/route.h> 48 #include <netinet/in.h> 49 #include <netinet/in_systm.h> 50 #include <netinet/ip.h> 51 #include <netinet/in_pcb.h> 52 #ifdef INET6 53 #include <netinet/ip6.h> 54 #include <netinet6/in6_pcb.h> 55 #endif 56 57 #include <arpa/inet.h> 58 59 #include <ctype.h> 60 #include <netdb.h> 61 #include <stdlib.h> 62 #include <string.h> 63 64 #include "systat.h" 65 #include "extern.h" 66 67 #define streq(a,b) (strcmp(a,b)==0) 68 69 static struct hitem { 70 struct sockaddr_storage addr; 71 int onoff; 72 } *hosts = NULL; 73 74 int nports, nhosts, protos; 75 76 static void changeitems(char *, int); 77 static void selectproto(char *); 78 static void showprotos(void); 79 static int selectport(long, int); 80 static void showports(void); 81 static int addrcmp(struct sockaddr *, struct sockaddr *); 82 static int selecthost(struct sockaddr *, int); 83 static void showhosts(void); 84 85 /* please note: there are also some netstat commands in netstat.c */ 86 87 void 88 netstat_display(char *args) 89 { 90 changeitems(args, 1); 91 } 92 93 void 94 netstat_ignore(char *args) 95 { 96 changeitems(args, 0); 97 } 98 99 void 100 netstat_reset(char *args) 101 { 102 selectproto(0); 103 selecthost(0, 0); 104 selectport(-1, 0); 105 } 106 107 void 108 netstat_show(char *args) 109 { 110 move(CMDLINE, 0); clrtoeol(); 111 if (!args || *args == '\0') { 112 showprotos(); 113 showhosts(); 114 showports(); 115 return; 116 } 117 if (strstr(args, "protos") == args) 118 showprotos(); 119 else if (strstr(args, "hosts") == args) 120 showhosts(); 121 else if (strstr(args, "ports") == args) 122 showports(); 123 else 124 addstr("show what?"); 125 } 126 127 void 128 netstat_tcp(char *args) 129 { 130 selectproto("tcp"); 131 } 132 133 void 134 netstat_udp(char *args) 135 { 136 selectproto("udp"); 137 } 138 139 static void 140 changeitems(char *args, int onoff) 141 { 142 char *cp; 143 struct servent *sp; 144 struct addrinfo hints, *res, *res0; 145 146 cp = strchr(args, '\n'); 147 if (cp) 148 *cp = '\0'; 149 for (;;args = cp) { 150 for (cp = args; *cp && isspace((unsigned char)*cp); cp++) 151 ; 152 args = cp; 153 for (; *cp && !isspace((unsigned char)*cp); cp++) 154 ; 155 if (*cp) 156 *cp++ = '\0'; 157 if (cp - args == 0) 158 break; 159 sp = getservbyname(args, 160 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 161 if (sp) { 162 selectport(sp->s_port, onoff); 163 continue; 164 } 165 166 memset(&hints, 0, sizeof(hints)); 167 hints.ai_family = PF_UNSPEC; 168 hints.ai_socktype = SOCK_DGRAM; 169 if (getaddrinfo(args, "0", &hints, &res0) != 0) { 170 error("%s: unknown host or port", args); 171 continue; 172 } 173 for (res = res0; res; res = res->ai_next) 174 selecthost(res->ai_addr, onoff); 175 freeaddrinfo(res0); 176 } 177 } 178 179 static void 180 selectproto(char *proto) 181 { 182 183 if (proto == 0 || streq(proto, "all")) 184 protos = TCP|UDP; 185 else if (streq(proto, "tcp")) 186 protos = TCP; 187 else if (streq(proto, "udp")) 188 protos = UDP; 189 } 190 191 static void 192 showprotos(void) 193 { 194 195 if ((protos & TCP) == 0) 196 addch('!'); 197 addstr("tcp "); 198 if ((protos & UDP) == 0) 199 addch('!'); 200 addstr("udp "); 201 } 202 203 static struct pitem { 204 long port; 205 int onoff; 206 } *ports = NULL; 207 208 static int 209 selectport(long port, int onoff) 210 { 211 struct pitem *p; 212 213 if (port == -1) { 214 if (ports == NULL) 215 return (0); 216 free(ports); 217 ports = NULL; 218 nports = 0; 219 return (1); 220 } 221 for (p = ports; p < ports+nports; p++) 222 if (p->port == port) { 223 p->onoff = onoff; 224 return (0); 225 } 226 p = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 227 if (p == NULL) { 228 error("malloc failed"); 229 die(0); 230 } 231 ports = p; 232 p = &ports[nports++]; 233 p->port = port; 234 p->onoff = onoff; 235 return (1); 236 } 237 238 int 239 checkport(struct inpcb *inp) 240 { 241 struct pitem *p; 242 243 if (ports) 244 for (p = ports; p < ports+nports; p++) 245 if (p->port == inp->inp_lport || p->port == inp->inp_fport) 246 return (p->onoff); 247 return (1); 248 } 249 250 #ifdef INET6 251 int 252 checkport6(struct in6pcb *in6p) 253 { 254 struct pitem *p; 255 256 if (ports) 257 for (p = ports; p < ports+nports; p++) 258 if (p->port == in6p->in6p_lport || p->port == in6p->in6p_fport) 259 return (p->onoff); 260 return (1); 261 } 262 #endif 263 264 static void 265 showports(void) 266 { 267 struct pitem *p; 268 struct servent *sp; 269 270 for (p = ports; p < ports+nports; p++) { 271 sp = getservbyport(p->port, 272 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp"); 273 if (!p->onoff) 274 addch('!'); 275 if (sp) 276 printw("%s ", sp->s_name); 277 else 278 printw("%ld ", p->port); 279 } 280 } 281 282 static int 283 addrcmp(struct sockaddr *sa1, struct sockaddr *sa2) 284 { 285 if (sa1->sa_family != sa2->sa_family) 286 return 0; 287 if (sa1->sa_len != sa2->sa_len) 288 return 0; 289 switch (sa1->sa_family) { 290 case AF_INET: 291 if (((struct sockaddr_in *)sa1)->sin_addr.s_addr == 292 ((struct sockaddr_in *)sa2)->sin_addr.s_addr) 293 return 1; 294 break; 295 #ifdef INET6 296 case AF_INET6: 297 if (IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)sa1)->sin6_addr, 298 &((struct sockaddr_in6 *)sa2)->sin6_addr)) 299 return 1; 300 break; 301 #endif 302 default: 303 if (memcmp(sa1, sa2, sa1->sa_len) == 0) 304 return 1; 305 break; 306 } 307 return 0; 308 } 309 310 static int 311 selecthost(struct sockaddr *sa, int onoff) 312 { 313 struct hitem *p; 314 315 if (sa == 0) { 316 if (hosts == 0) 317 return (0); 318 free((char *)hosts), hosts = 0; 319 nhosts = 0; 320 return (1); 321 } 322 for (p = hosts; p < hosts+nhosts; p++) 323 if (addrcmp((struct sockaddr *)&p->addr, sa)) { 324 p->onoff = onoff; 325 return (0); 326 } 327 if (sa->sa_len > sizeof(struct sockaddr_storage)) 328 return (-1); /*XXX*/ 329 p = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 330 if (p == NULL) { 331 error("malloc failed"); 332 die(0); 333 } 334 hosts = p; 335 p = &hosts[nhosts++]; 336 memcpy(&p->addr, sa, sa->sa_len); 337 p->onoff = onoff; 338 return (1); 339 } 340 341 int 342 checkhost(struct inpcb *inp) 343 { 344 struct hitem *p; 345 struct sockaddr_in *sin; 346 347 if (hosts) 348 for (p = hosts; p < hosts+nhosts; p++) { 349 if (((struct sockaddr *)&p->addr)->sa_family != AF_INET) 350 continue; 351 sin = (struct sockaddr_in *)&p->addr; 352 if (sin->sin_addr.s_addr == inp->inp_laddr.s_addr || 353 sin->sin_addr.s_addr == inp->inp_faddr.s_addr) 354 return (p->onoff); 355 } 356 return (1); 357 } 358 359 #ifdef INET6 360 int 361 checkhost6(struct in6pcb *in6p) 362 { 363 struct hitem *p; 364 struct sockaddr_in6 *sin6; 365 366 if (hosts) 367 for (p = hosts; p < hosts+nhosts; p++) { 368 if (((struct sockaddr *)&p->addr)->sa_family != AF_INET6) 369 continue; 370 sin6 = (struct sockaddr_in6 *)&p->addr; 371 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_laddr) || 372 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_faddr)) 373 return (p->onoff); 374 } 375 return (1); 376 } 377 #endif 378 379 static void 380 showhosts(void) 381 { 382 struct hitem *p; 383 char hbuf[NI_MAXHOST]; 384 struct sockaddr *sa; 385 int flags; 386 387 #if 0 388 flags = nflag ? NI_NUMERICHOST : 0; 389 #else 390 flags = 0; 391 #endif 392 for (p = hosts; p < hosts+nhosts; p++) { 393 sa = (struct sockaddr *)&p->addr; 394 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, 395 flags) != 0) 396 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 397 if (!p->onoff) 398 addch('!'); 399 printw("%s ", hbuf); 400 } 401 } 402