PERFORCE change 164315 for review
Gabor Pali
pgj at FreeBSD.org
Sun Jun 14 01:31:37 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=164315
Change 164315 by pgj at petymeg-current on 2009/06/14 01:30:40
Add numeric address representation to addr_type, use it in netstat
Affected files ...
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#29 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#18 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#16 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#18 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/inet.c#17 edit
Differences ...
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#29 (text+ko) ====
@@ -54,7 +54,8 @@
static void extract_inet_data(struct tcpcb *, struct inpcb *,
struct xsocket *, struct socket_type *);
-static char ntop_buf[INET6_ADDRSTRLEN];
+static char ntop_buf[INET_ADDRSTRLEN];
+static char ntop_buf6[INET6_ADDRSTRLEN];
static struct addr_type *extract_inet_address(int type, const char *proto,
struct in_addr *in, u_short port, int anonport);
@@ -604,6 +605,7 @@
_netstat_at_allocate(NETSTAT_ADDRTYPE_LOCAL, address, NULL);
stp->st_address[stp->st_addrcnt]->at_port = 0;
strcpy(stp->st_address[stp->st_addrcnt]->at_portname, "*");
+ stp->st_address[stp->st_addrcnt]->at_numeric[0] = '\0';
stp->st_addrcnt += 1;
}
stp->st_tcpstate[0] = '\0';
@@ -706,7 +708,7 @@
u_short port, int anonport)
{
struct addr_type *result;
- char address[256];
+ char address[256], numeric[32];
struct hostent *hp;
struct netent *np;
struct servent *sp = NULL;
@@ -732,17 +734,20 @@
}
}
if (in->s_addr == INADDR_ANY)
+ strcpy(numeric, "*");
+ else
+ sprintf(numeric, "%s", inet_ntop(AF_INET, (void *)in, ntop_buf,
+ sizeof(ntop_buf)));
+ if (in->s_addr == INADDR_ANY)
strcpy(address, "*");
- else if (cp) {
+ else if (cp)
strlcpy(address, cp, sizeof(address));
- } else {
- in->s_addr = ntohl(in->s_addr);
-#define C(x) ((u_int)((x) & 0xff))
- sprintf(address, "%u.%u.%u.%u", C(in->s_addr >> 24),
- C(in->s_addr >> 16), C(in->s_addr >> 8),
- C(in->s_addr));
- }
+ else
+ strlcpy(address, numeric, sizeof(address));
result = _netstat_at_allocate(type, address, NULL);
+ if (result == NULL)
+ return (result);
+ strlcpy(result->at_numeric, numeric, sizeof(result->at_numeric));
result->at_port = ntohs(port);
sp = getservbyport((int)port, proto);
if ((sp != NULL || port == 0) && !anonport)
@@ -751,7 +756,6 @@
sprintf(result->at_portname, "%d", result->at_port);
return (result);
-#undef C
}
struct addr_type *
@@ -759,7 +763,7 @@
u_short port)
{
struct addr_type *result;
- char address[256], domain[MAXHOSTNAMELEN];
+ char address[256], numeric[32], domain[MAXHOSTNAMELEN];
struct hostent *hp;
struct servent *sp = NULL;
char *cp;
@@ -780,15 +784,21 @@
}
}
if (IN6_IS_ADDR_UNSPECIFIED(in))
+ strcpy(numeric, "*");
+ else
+ sprintf(numeric, "%s", inet_ntop(AF_INET6, (void *)in,
+ ntop_buf6, sizeof(ntop_buf6)));
+ if (IN6_IS_ADDR_UNSPECIFIED(in))
strcpy(address, "*");
else if (cp)
strcpy(address, cp);
else
- sprintf(address, "%s",
- inet_ntop(AF_INET6, (void *)in, ntop_buf,
- sizeof(ntop_buf)));
+ strlcpy(address, numeric, sizeof(address));
result = _netstat_at_allocate(type, address, NULL);
+ if (result == NULL)
+ return (result);
+ strlcpy(result->at_numeric, numeric, sizeof(result->at_numeric));
result->at_port = port;
sp = getservbyport((int)port, proto);
if (sp != NULL || port == 0)
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#18 (text+ko) ====
@@ -92,6 +92,7 @@
int index);
const char *netstat_at_get_name(const struct addr_type *atp);
+const char *netstat_at_get_numeric(const struct addr_type *atp);
void netstat_at_get_address(const struct addr_type *atp,
void *addr);
u_short netstat_at_get_port(const struct addr_type *atp);
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#16 (text+ko) ====
@@ -22,6 +22,8 @@
/* XXX: this should be an enum? */
int at_type;
char at_name[1024];
+ /* numeric representation */
+ char at_numeric[32];
void *at_address;
u_short at_port;
char at_portname[32];
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#18 (text+ko) ====
@@ -504,6 +504,12 @@
return (atp->at_name);
}
+const char *
+netstat_at_get_numeric(const struct addr_type *atp)
+{
+ return (atp->at_numeric);
+}
+
void
netstat_at_get_address(const struct addr_type *atp, void *addr)
{
==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/inet.c#17 (text+ko) ====
@@ -929,8 +929,8 @@
int width;
char line[80], *cp;
- /* XXX: Respect numeric for getting name. */
sprintf(line, "%.*s.", Wflag ? 39 : (Aflag && !numeric) ? 12 : 16,
+ numeric_addr ? netstat_at_get_numeric(atp) :
netstat_at_get_name(atp));
cp = index(line, '\0');
if (numeric && (netstat_at_get_port(atp) > 0))
More information about the p4-projects
mailing list