inet6.c (304519) | inet6.c (311670) |
---|---|
1/* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */ 2/*- 3 * Copyright (c) 1983, 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 21 unchanged lines hidden (view full) --- 30 31#if 0 32#ifndef lint 33static char sccsid[] = "@(#)inet6.c 8.4 (Berkeley) 4/20/94"; 34#endif /* not lint */ 35#endif 36 37#include <sys/cdefs.h> | 1/* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */ 2/*- 3 * Copyright (c) 1983, 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 21 unchanged lines hidden (view full) --- 30 31#if 0 32#ifndef lint 33static char sccsid[] = "@(#)inet6.c 8.4 (Berkeley) 4/20/94"; 34#endif /* not lint */ 35#endif 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: stable/11/usr.bin/netstat/inet6.c 304519 2016-08-20 07:44:41Z tuexen $"); | 38__FBSDID("$FreeBSD: stable/11/usr.bin/netstat/inet6.c 311670 2017-01-08 07:25:22Z delphij $"); |
39 40#ifdef INET6 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/socketvar.h> 44#include <sys/ioctl.h> 45#include <sys/mbuf.h> 46#include <sys/protosw.h> --- 18 unchanged lines hidden (view full) --- 65#include <stdio.h> 66#include <stdbool.h> 67#include <errno.h> 68#include <string.h> 69#include <unistd.h> 70#include <libxo/xo.h> 71#include "netstat.h" 72 | 39 40#ifdef INET6 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/socketvar.h> 44#include <sys/ioctl.h> 45#include <sys/mbuf.h> 46#include <sys/protosw.h> --- 18 unchanged lines hidden (view full) --- 65#include <stdio.h> 66#include <stdbool.h> 67#include <errno.h> 68#include <string.h> 69#include <unistd.h> 70#include <libxo/xo.h> 71#include "netstat.h" 72 |
73char *inet6name(struct in6_addr *); 74 | |
75static char ntop_buf[INET6_ADDRSTRLEN]; 76 77static const char *ip6nh[] = { 78 "hop by hop", 79 "ICMP", 80 "IGMP", 81 "#3", 82 "IP", --- 1182 unchanged lines hidden (view full) --- 1265 1266void 1267inet6print(const char *container, struct in6_addr *in6, int port, 1268 const char *proto, int numeric) 1269{ 1270 struct servent *sp = 0; 1271 char line[80], *cp; 1272 int width; | 73static char ntop_buf[INET6_ADDRSTRLEN]; 74 75static const char *ip6nh[] = { 76 "hop by hop", 77 "ICMP", 78 "IGMP", 79 "#3", 80 "IP", --- 1182 unchanged lines hidden (view full) --- 1263 1264void 1265inet6print(const char *container, struct in6_addr *in6, int port, 1266 const char *proto, int numeric) 1267{ 1268 struct servent *sp = 0; 1269 char line[80], *cp; 1270 int width; |
1271 size_t alen, plen; |
|
1273 1274 if (container) 1275 xo_open_container(container); 1276 | 1272 1273 if (container) 1274 xo_open_container(container); 1275 |
1277 sprintf(line, "%.*s.", Wflag ? 39 : (Aflag && !numeric) ? 12 : 16, | 1276 snprintf(line, sizeof(line), "%.*s.", 1277 Wflag ? 39 : (Aflag && !numeric) ? 12 : 16, |
1278 inet6name(in6)); | 1278 inet6name(in6)); |
1279 cp = strchr(line, '\0'); | 1279 alen = strlen(line); 1280 cp = line + alen; |
1280 if (!numeric && port) 1281 GETSERVBYPORT6(port, proto, sp); 1282 if (sp || port == 0) | 1281 if (!numeric && port) 1282 GETSERVBYPORT6(port, proto, sp); 1283 if (sp || port == 0) |
1283 sprintf(cp, "%.15s", sp ? sp->s_name : "*"); | 1284 snprintf(cp, sizeof(line) - alen, 1285 "%.15s", sp ? sp->s_name : "*"); |
1284 else | 1286 else |
1285 sprintf(cp, "%d", ntohs((u_short)port)); | 1287 snprintf(cp, sizeof(line) - alen, 1288 "%d", ntohs((u_short)port)); |
1286 width = Wflag ? 45 : Aflag ? 18 : 22; 1287 1288 xo_emit("{d:target/%-*.*s} ", width, width, line); 1289 | 1289 width = Wflag ? 45 : Aflag ? 18 : 22; 1290 1291 xo_emit("{d:target/%-*.*s} ", width, width, line); 1292 |
1290 int alen = cp - line - 1, plen = strlen(cp) - 1; | 1293 plen = strlen(cp) - 1; 1294 alen--; |
1291 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen, 1292 plen, cp); 1293 1294 if (container) 1295 xo_close_container(container); 1296} 1297 1298/* 1299 * Construct an Internet address representation. 1300 * If the numeric_addr has been supplied, give 1301 * numeric value, otherwise try for symbolic name. 1302 */ 1303 1304char * 1305inet6name(struct in6_addr *in6p) 1306{ 1307 struct sockaddr_in6 sin6; 1308 char hbuf[NI_MAXHOST], *cp; | 1295 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen, 1296 plen, cp); 1297 1298 if (container) 1299 xo_close_container(container); 1300} 1301 1302/* 1303 * Construct an Internet address representation. 1304 * If the numeric_addr has been supplied, give 1305 * numeric value, otherwise try for symbolic name. 1306 */ 1307 1308char * 1309inet6name(struct in6_addr *in6p) 1310{ 1311 struct sockaddr_in6 sin6; 1312 char hbuf[NI_MAXHOST], *cp; |
1309 static char line[50]; | 1313 static char line[NI_MAXHOST]; |
1310 static char domain[MAXHOSTNAMELEN]; 1311 static int first = 1; 1312 int flags, error; 1313 1314 if (IN6_IS_ADDR_UNSPECIFIED(in6p)) { 1315 strcpy(line, "*"); 1316 return (line); 1317 } 1318 if (first && !numeric_addr) { 1319 first = 0; | 1314 static char domain[MAXHOSTNAMELEN]; 1315 static int first = 1; 1316 int flags, error; 1317 1318 if (IN6_IS_ADDR_UNSPECIFIED(in6p)) { 1319 strcpy(line, "*"); 1320 return (line); 1321 } 1322 if (first && !numeric_addr) { 1323 first = 0; |
1320 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && | 1324 if (gethostname(domain, sizeof(domain)) == 0 && |
1321 (cp = strchr(domain, '.'))) | 1325 (cp = strchr(domain, '.'))) |
1322 (void) strcpy(domain, cp + 1); | 1326 strlcpy(domain, cp + 1, sizeof(domain)); |
1323 else 1324 domain[0] = 0; 1325 } 1326 memset(&sin6, 0, sizeof(sin6)); 1327 memcpy(&sin6.sin6_addr, in6p, sizeof(*in6p)); 1328 sin6.sin6_family = AF_INET6; 1329 /* XXX: in6p.s6_addr[2] can contain scopeid. */ 1330 in6_fillscopeid(&sin6); 1331 flags = (numeric_addr) ? NI_NUMERICHOST : 0; 1332 error = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), hbuf, 1333 sizeof(hbuf), NULL, 0, flags); 1334 if (error == 0) { 1335 if ((flags & NI_NUMERICHOST) == 0 && 1336 (cp = strchr(hbuf, '.')) && 1337 !strcmp(cp + 1, domain)) 1338 *cp = 0; | 1327 else 1328 domain[0] = 0; 1329 } 1330 memset(&sin6, 0, sizeof(sin6)); 1331 memcpy(&sin6.sin6_addr, in6p, sizeof(*in6p)); 1332 sin6.sin6_family = AF_INET6; 1333 /* XXX: in6p.s6_addr[2] can contain scopeid. */ 1334 in6_fillscopeid(&sin6); 1335 flags = (numeric_addr) ? NI_NUMERICHOST : 0; 1336 error = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), hbuf, 1337 sizeof(hbuf), NULL, 0, flags); 1338 if (error == 0) { 1339 if ((flags & NI_NUMERICHOST) == 0 && 1340 (cp = strchr(hbuf, '.')) && 1341 !strcmp(cp + 1, domain)) 1342 *cp = 0; |
1339 strcpy(line, hbuf); | 1343 strlcpy(line, hbuf, sizeof(line)); |
1340 } else { 1341 /* XXX: this should not happen. */ | 1344 } else { 1345 /* XXX: this should not happen. */ |
1342 sprintf(line, "%s", | 1346 snprintf(line, sizeof(line), "%s", |
1343 inet_ntop(AF_INET6, (void *)&sin6.sin6_addr, ntop_buf, 1344 sizeof(ntop_buf))); 1345 } 1346 return (line); 1347} 1348#endif /*INET6*/ | 1347 inet_ntop(AF_INET6, (void *)&sin6.sin6_addr, ntop_buf, 1348 sizeof(ntop_buf))); 1349 } 1350 return (line); 1351} 1352#endif /*INET6*/ |