void fill_mapping_data_for_ipv6_addr(struct polymorphic_addr *real_ipv6_pa, struct mapping_data *data)
{
	unsigned int my_index = mapping_index++; // this is considered atomic (in a multi-threaded program)
	struct in_addr ipv4_addr;
	int length;
	static __thread struct ipv4_pool_info pool;
	static __thread int pool_initialized = 0;

	if (pool_initialized == 0)
	{
		original_inet_aton(stringified(IPV4_POOL_START), &ipv4_addr);
		pool.start = inet_lnaof(ipv4_addr);
		original_inet_aton(stringified(IPV4_POOL_END), &ipv4_addr);
		pool.end = inet_lnaof(ipv4_addr);
		pool_initialized = 1;
	}
	
	// detect overflows...
	if (my_index > pool.end - pool.start)
	{
		printf("IPV6 CARE: IPV4 POOL OVERFLOW! YOU SHOULD DESACTIVATE IPV6 CARE.\n");
		printf("IPV6 CARE: THIS PROCESS MAY SHOW UNEXPECTED BEHAVIORS IN NAME RESOLUTIONS FROM NOW ON!\n");
		// maybe we are running a critical application, so we should not stop.
		// let's get a correct value, even if it's already used...
		my_index %= pool.end - pool.start + 1;
	}

	// pa[real_ipv6_addr]
	memcpy(&data->pa[real_ipv6_addr], real_ipv6_pa, sizeof(*real_ipv6_pa));

	// pa[mapped_ipv4_addr]
	ipv4_addr = inet_makeaddr(INADDR_ANY, pool.start + my_index);
	copy_ipv4_addr_to_pa(&ipv4_addr, &data->pa[mapped_ipv4_addr]);

	// ip_text_forms[mapped_ipv4]
	original_inet_ntop(AF_INET, &ipv4_addr, data->ip_text_forms[mapped_ipv4], INET_ADDRSTRLEN);

	// ip_text_forms[full_ipv6]
	original_inet_ntop(AF_INET6, &real_ipv6_pa->addr.ipv6_addr, data->ip_text_forms[full_ipv6], INET6_ADDRSTRLEN);

	// ip_text_forms[abbreviated_ipv6]
	if (strlen(data->ip_text_forms[full_ipv6]) > INET_ADDRSTRLEN -1)
	{
		data->ip_text_forms[abbreviated_ipv6][0] = '.';
		data->ip_text_forms[abbreviated_ipv6][1] = '.';
		length = strlen(data->ip_text_forms[full_ipv6]);
		strcpy(&data->ip_text_forms[abbreviated_ipv6][2], &data->ip_text_forms[full_ipv6][length - INET_ADDRSTRLEN +3]);
	}
	else
	{
		strcpy(data->ip_text_forms[abbreviated_ipv6], data->ip_text_forms[full_ipv6]);
	}
}
static int
printaddr(int af, struct sockaddr_storage *ss)
{
	char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
	struct sockaddr_un *sun;
	void *addr = NULL; /* Keep compiler happy. */
	int off, port = 0;

	switch (af) {
	case AF_INET:
		addr = &((struct sockaddr_in *)ss)->sin_addr;
		if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
			addrstr[0] = '*';
		port = ntohs(((struct sockaddr_in *)ss)->sin_port);
		break;
	case AF_INET6:
		addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
		if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
			addrstr[0] = '*';
		port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
		break;
	case AF_UNIX:
		sun = (struct sockaddr_un *)ss;
		off = (int)((char *)&sun->sun_path - (char *)sun);
		return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
	}
	if (addrstr[0] == '\0')
		inet_ntop(af, addr, addrstr, sizeof addrstr);
	if (port == 0)
		return xprintf("%s:*", addrstr);
	else
		return xprintf("%s:%d", addrstr, port);
}
Beispiel #3
0
static int
printaddr(struct sockaddr_storage *ss)
{
	struct sockaddr_un *sun;
	char addrstr[NI_MAXHOST] = { '\0', '\0' };
	int error, off, port = 0;

	switch (ss->ss_family) {
	case AF_INET:
		if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
			addrstr[0] = '*';
		port = ntohs(sstosin(ss)->sin_port);
		break;
	case AF_INET6:
		if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
			addrstr[0] = '*';
		port = ntohs(sstosin6(ss)->sin6_port);
		break;
	case AF_UNIX:
		sun = sstosun(ss);
		off = (int)((char *)&sun->sun_path - (char *)sun);
		return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
	}
	if (addrstr[0] == '\0') {
		error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
		    sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
		if (error)
			errx(1, "getnameinfo()");
	}
	if (port == 0)
		return xprintf("%s:*", addrstr);
	else
		return xprintf("%s:%d", addrstr, port);
}
Beispiel #4
0
int main() {
    struct in_addr addr;
    addr.s_addr = 0x1f23ab02;

    printf("network address is: %x\n", addr.s_addr);
    printf("local network address is: %x\n", inet_lnaof(addr));
    printf("network number is: %x\n", inet_netof(addr));

    return 0;
}
Beispiel #5
0
int
main(int argc,char **argv) {
    int x;
    struct sockaddr_in adr_inet;/* AF_INET */
    const char *addr[] = {
        "44.135.86.12",
        "127.0.0.1",
        "172.16.23.95",
        "192.168.9.1"
    };
    unsigned long net, hst;

    for ( x=0; x<4; ++x ) {
        /*
         * Create a socket address:
         */
        memset(&adr_inet,0,sizeof adr_inet);
        adr_inet.sin_family = AF_INET;
        adr_inet.sin_port = htons(9000);
        if ( !inet_aton(addr[x],
                  &adr_inet.sin_addr) )
            puts("bad address.");

        /*
         * Split address into Host & Net ID
         */
        hst = inet_lnaof(adr_inet.sin_addr);
        net = inet_netof(adr_inet.sin_addr);

        printf("%14s : net=0x%08lX host=0x%08lX\n",
            inet_ntoa(adr_inet.sin_addr),net,hst);

        /*
         * Zero the address to prove later that
         * we can reconstruct this value :
         */
        memset(&adr_inet,0,sizeof adr_inet);
        adr_inet.sin_family = AF_INET;
        adr_inet.sin_port = htons(9000);

        adr_inet.sin_addr =
            inet_makeaddr(net,hst);

        /*
         * Now display the reconstructed
         * address :
         */
        printf("%14s : %s\n\n",
            "inet_makeaddr",
            inet_ntoa(adr_inet.sin_addr));
    }

    return 0;
}
Beispiel #6
0
/*
 * Construct an Internet address representation.
 * If the nflag has been supplied, give
 * numeric value, otherwise try for symbolic name.
 */
char *
inetname(struct in_addr *inp)
{
	char *cp;
	static char line[50];
	struct hostent *hp;
	struct netent *np;
	static char domain[MAXHOSTNAMELEN];
	static int first = 1;

	if (first && !nflag) {
		first = 0;
		if (gethostname(domain, sizeof(domain)) == 0 &&
		    (cp = strchr(domain, '.')))
			(void) strlcpy(domain, cp + 1, sizeof domain);
		else
			domain[0] = '\0';
	}
	cp = NULL;
	if (!nflag && inp->s_addr != INADDR_ANY) {
		int net = inet_netof(*inp);
		int lna = inet_lnaof(*inp);

		if (lna == INADDR_ANY) {
			np = getnetbyaddr(net, AF_INET);
			if (np)
				cp = np->n_name;
		}
		if (cp == NULL) {
			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
			if (hp) {
				if ((cp = strchr(hp->h_name, '.')) &&
				    !strcmp(cp + 1, domain))
					*cp = '\0';
				cp = hp->h_name;
			}
		}
	}
	if (inp->s_addr == INADDR_ANY)
		snprintf(line, sizeof line, "*");
	else if (cp)
		snprintf(line, sizeof line, "%s", cp);
	else {
		inp->s_addr = ntohl(inp->s_addr);
#define C(x)	((x) & 0xff)
		snprintf(line, sizeof line, "%u.%u.%u.%u",
		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
		    C(inp->s_addr >> 8), C(inp->s_addr));
	}
	return (line);
}
Beispiel #7
0
int main()
{	char buffer[32];
	int ret = 0;
	int host = 0;
	int network = 0;
	unsigned int address = 0;
	struct in_addr in;
	in.s_addr = 0;

	/*输入一个以.分隔的字符串形式的ip地址*/
	printf("please input your ip address:");
		fgets(buffer, 31, stdin);
	buffer[31] = '\0';
	
	/*示例使用inet_aton()函数*/
	if((ret = inet_aton(buffer, &in) == 0))
		printf("inet_aton:\t invalid address\n");
	else
		printf("inet_aton:\t0x%x\n", in.s_addr);

	/*示例使用inet_addr()函数*/
	if((address = inet_addr(buffer)) == INADDR_NONE)
		printf("inet_addr:\t invalid address\n");
	else
		printf("inet_addr:\t 0x%x\n", address);

	/*示例使用inet_network()函数*/
	if((address = inet_network(buffer)) == -1)
		printf("inet_work:\t invalid address\n");
	else
		printf("inet_work:\t 0x%x\n", address);

	/*示例使用inet_ntoa()函数*/
	if(inet_ntoa(in) == NULL)
		printf("inet_ntoa:\t invalid address\n");
	else
		printf("inet_ntoa:\t%s\n", inet_ntoa(in));

	/*示例使用inet_lnaof()与inet_netof()函数*/
	host = inet_lnaof(in);
	network = inet_netof(in);
	printf("inet_lnaof:\t 0x%x\n", host);
	printf("inet_netof:\t 0x%x\n", network);
	
	in = inet_makeaddr(network, host);
	printf("inet_makeaddr: 0x%x\n", in.s_addr);

	return 0;
	
}
Beispiel #8
0
/*
 * Construct an Internet address representation.
 * If the nflag has been supplied, give 
 * numeric value, otherwise try for symbolic name.
 */
static char *
inetname(struct in_addr in)
{
	register char *cp;
	static char line[50];
	struct hostent *hp;
	struct netent *np;
	static char domain[MAXHOSTNAMELEN + 1];
	static int first = 1;

	if (first && !nflag) {
		first = 0;
		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
		    (cp = (char *) strchr(domain, '.')))
			(void) strcpy(domain, cp + 1);
		else
			domain[0] = 0;
	}
	cp = 0;
	if (!nflag && in.s_addr != INADDR_ANY) {
		u_long net = inet_netof(in);
		u_long lna = inet_lnaof(in);

		if (lna == INADDR_ANY) {
			np = getnetbyaddr(net, AF_INET);
			if (np)
				cp = np->n_name;
		}
		if (cp == 0) {
			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
			if (hp) {
				if ((cp = (char *) strchr(hp->h_name, '.')) &&
				    !strcmp(cp + 1, domain))
					*cp = 0;
				cp = (char *)hp->h_name;
			}
		}
	}
	if (in.s_addr == INADDR_ANY)
		strcpy(line, "*");
	else if (cp)
		strcpy(line, cp);
	else {
		in.s_addr = ntohl(in.s_addr);
#define C(x)	(unsigned)((x) & 0xff)
		sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
			C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
	}
	return (line);
}
Beispiel #9
0
static int conn_read (void)
{
  struct inpcbtable table;
  struct inpcb *head;
  struct inpcb *next;
  struct inpcb inpcb;
  struct tcpcb tcpcb;
  int status;

  conn_reset_port_entry ();

  /* Read the pcbtable from the kernel */
  status = kread (inpcbtable_off, &table, sizeof (table));
  if (status != 0)
    return (-1);

  /* Get the `head' pcb */
  head = (struct inpcb *) &(inpcbtable_ptr->inpt_queue);
  /* Get the first pcb */
  next = (struct inpcb *)CIRCLEQ_FIRST (&table.inpt_queue);

  while (next != head)
  {
    /* Read the pcb pointed to by `next' into `inpcb' */
    kread ((u_long) next, &inpcb, sizeof (inpcb));

    /* Advance `next' */
    next = (struct inpcb *)CIRCLEQ_NEXT (&inpcb, inp_queue);

    /* Ignore sockets, that are not connected. */
#ifdef __NetBSD__
    if (inpcb.inp_af == AF_INET6)
      continue; /* XXX see netbsd/src/usr.bin/netstat/inet6.c */
#else
    if (!(inpcb.inp_flags & INP_IPV6)
	&& (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY))
      continue;
    if ((inpcb.inp_flags & INP_IPV6)
	&& IN6_IS_ADDR_UNSPECIFIED (&inpcb.inp_laddr6))
      continue;
#endif

    kread ((u_long) inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
    conn_handle_ports (ntohs(inpcb.inp_lport), ntohs(inpcb.inp_fport), tcpcb.t_state);
  } /* while (next != head) */

  conn_submit_all ();

  return (0);
}
unsigned char part_evenAndOddIp(int dlt, const u_char *bytes) {
  unsigned char result;
  switch (dlt) {
  case DLT_EN10MB:
    if (ntohs( ((struct ether_header *)bytes)->ether_type ) == ETHERTYPE_IP) {
      bytes += ETHER_ADDR_LEN;
      result = (inet_lnaof(((struct ip *)bytes)->ip_src) +
		inet_netof(((struct ip *)bytes)->ip_src)) % 2;
    } else {
      result = 0;
    }
    break;
  default: result = 0;
  }
  return result;
}
Beispiel #11
0
	int
main (int argc, char * argv [])
{
	int i;
	struct in_addr adresse;
	unsigned long int reseau;
	unsigned long int locale;
	
	for (i = 1; i < argc; i ++) {
		fprintf (stdout, "inet_netof (%s) = ", argv [i]);

		if (inet_aton (argv [i], & adresse) == 0) {
			fprintf (stdout, "invalide \n");
			continue;
		}
		reseau = inet_netof (adresse);
		locale = inet_lnaof (adresse);
		fprintf (stdout, "%08lX + %08lX\n", reseau, locale);
	}
	return (0);
}
Beispiel #12
0
/*
 * Construct an Internet address representation.
 * If numeric_addr has been supplied, give
 * numeric value, otherwise try for symbolic name.
 */
char *
inetname(struct in_addr *inp)
{
	char *cp;
	static char line[MAXHOSTNAMELEN];
	struct hostent *hp;
	struct netent *np;

	cp = 0;
	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
		int net = inet_netof(*inp);
		int lna = inet_lnaof(*inp);

		if (lna == INADDR_ANY) {
			np = getnetbyaddr(net, AF_INET);
			if (np)
				cp = np->n_name;
		}
		if (cp == NULL) {
			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
			if (hp) {
				cp = hp->h_name;
				trimdomain(cp, strlen(cp));
			}
		}
	}
	if (inp->s_addr == INADDR_ANY)
		strcpy(line, "*");
	else if (cp) {
		strlcpy(line, cp, sizeof(line));
	} else {
		inp->s_addr = ntohl(inp->s_addr);
#define	C(x)	((u_int)((x) & 0xff))
		snprintf(line, sizeof(line), "%u.%u.%u.%u",
		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
		    C(inp->s_addr >> 8), C(inp->s_addr));
	}
	return (line);
}
Beispiel #13
0
int
read_ns(void)
{
	struct inpcbtable pcbtable;
	struct inpcb *head, *prev, *next;
	struct inpcb inpcb;
	struct socket sockb;
	struct tcpcb tcpcb;
	void *off;
	int istcp;

	if (kd == NULL) {
		return (0);
	}

	num_ns = 0;

	if (namelist[X_TCBTABLE].n_value == 0)
		return 0;

	if (protos & TCP) {
		off = NPTR(X_TCBTABLE);
		istcp = 1;
	} else if (protos & UDP) {
		off = NPTR(X_UDBTABLE);
		istcp = 0;
	} else {
		error("No protocols to display");
		return 0;
	}

again:
	KREAD(off, &pcbtable, sizeof (struct inpcbtable));

	prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
	next = CIRCLEQ_FIRST(&pcbtable.inpt_queue);

	while (next != head) {
		KREAD(next, &inpcb, sizeof (inpcb));
		if (CIRCLEQ_PREV(&inpcb, inp_queue) != prev) {
			error("Kernel state in transition");
			return 0;
		}
		prev = next;
		next = CIRCLEQ_NEXT(&inpcb, inp_queue);

		if (!aflag) {
			if (!(inpcb.inp_flags & INP_IPV6) &&
			    inet_lnaof(inpcb.inp_faddr) == INADDR_ANY)
				continue;
			if ((inpcb.inp_flags & INP_IPV6) &&
			    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6))
				continue;
		}
		KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
		if (istcp) {
			KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
			if (!aflag && tcpcb.t_state <= TCPS_LISTEN)
				continue;
			enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
		} else
			enter(&inpcb, &sockb, 0, "udp");
	}
	if (istcp && (protos & UDP)) {
		istcp = 0;
		off = NPTR(X_UDBTABLE);
		goto again;
	}

	num_disp = num_ns;
	return 0;
}
Beispiel #14
0
static void
protopr0(u_long off, char *name, int af)
{
	struct inpcbtable table;
	struct inpcb *head, *next, *prev;
	struct inpcb inpcb;
	int istcp, israw;
#ifdef DCCP
	int isdccp;
#endif
	int first = 1;
	char *name0;
	char namebuf[20];

	name0 = name;
	if (off == 0)
		return;
	istcp = strcmp(name, "tcp") == 0;
	israw = strncmp(name, "ip", 2) == 0;
#ifdef DCCP
	isdccp = strcmp(name, "dccp") == 0;
#endif
	kread(off, (char *)&table, sizeof table);
	prev = head =
	    (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
	next = table.inpt_queue.cqh_first;

	while (next != head) {
		kread((u_long)next, (char *)&inpcb, sizeof inpcb);
		if (inpcb.inp_queue.cqe_prev != prev) {
			printf("???\n");
			break;
		}
		prev = next;
		next = inpcb.inp_queue.cqe_next;

		switch (af) {
		case AF_INET:
			if ((inpcb.inp_flags & INP_IPV6) != 0)
				continue;
			break;
		case AF_INET6:
			if ((inpcb.inp_flags & INP_IPV6) == 0)
				continue;
			break;
		default:
			break;
		}

		if (!aflag &&
		    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
			continue;
		kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
		if (istcp) {
			kread((u_long)inpcb.inp_ppcb,
			    (char *)&tcpcb, sizeof (tcpcb));
		}
#ifdef DCCP
		if (isdccp) {
			kread((u_long)inpcb.inp_ppcb,
			    (char *)&dccpcb, sizeof (dccpcb));
		}
#endif
		if (first) {
			printf("Active Internet connections");
			if (aflag)
				printf(" (including servers)");
			putchar('\n');
			if (Aflag)
				printf("%-*.*s %-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n",
				    PLEN, PLEN, "PCB", "Proto", "Recv-Q",
				    "Send-Q", "Local Address",
				    "Foreign Address", "(state)");
			else
				printf("%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
				    "Proto", "Recv-Q", "Send-Q",
				    "Local Address", "Foreign Address",
				    "(state)");
			first = 0;
		}
		if (Aflag) {
#ifdef DCCP
			if (istcp || isdccp)
#else
			if (istcp)
#endif
				printf("%*p ", PLEN, inpcb.inp_ppcb);
			else
				printf("%*p ", PLEN, prev);
		}
#ifdef INET6
		if (inpcb.inp_flags & INP_IPV6 && !israw) {
			strlcpy(namebuf, name0, sizeof namebuf);
			strlcat(namebuf, "6", sizeof namebuf);
			name = namebuf;
		} else
			name = name0;
#endif
		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
		    sockb.so_snd.sb_cc);
#ifdef INET6
		if (inpcb.inp_flags & INP_IPV6) {
			inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport,
			    name, 1);
			inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport,
			    name, 0);
		} else
#endif
		{
			inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
			    name, 1);
			inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
			    name, 0);
		}
		if (istcp) {
			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
				printf(" %d", tcpcb.t_state);
			else
				printf(" %s", tcpstates[tcpcb.t_state]);
		} else if (israw) {
			struct protoent *pe = NULL;
			u_int8_t proto;
#ifdef INET6
			if (inpcb.inp_flags & INP_IPV6)
				proto = inpcb.inp_ipv6.ip6_nxt;
			else
#endif
				proto = inpcb.inp_ip.ip_p;
			if (!nflag)
				pe = getprotobynumber(proto);
			if (pe)
				printf(" %s", pe->p_name);
			else
				printf(" %u", proto);
		}
#ifdef DCCP
		if (isdccp) {
			if (dccpcb.state >= DCCP_NSTATES)
				printf(" %d", dccpcb.state);
			else
				printf(" %s", dccpstates[dccpcb.state]);
		}
#endif
		putchar('\n');
	}
}
Beispiel #15
0
/**
 * On Linux: Parse connection table /proc/net/tcp and /proc/net/udp to get
 * connections in state "SYN sent" from session user.
 *
 * On FreeBSD: Use sysctl with "net.inet.tcp.pcblist" to get the connection
 * table. Add connections to the our table using tcptable_add().
 */
int tcptable_read(nuauth_session_t * session, conntable_t * ct)
{
#ifdef LINUX
	static FILE *fd_tcp = NULL;
	static FILE *fd_udp = NULL;

#ifdef IPV6
	static FILE *fd_tcp6 = NULL;
#endif

#if DEBUG
	assert(ct != NULL);
	assert(TCP_SYN_SENT == 2);
#endif
	if (!parse_tcptable_file
	    (session, ct, "/proc/net/tcp", &fd_tcp, IPPROTO_TCP, 0))
		return 0;

#ifdef IPV6
	parse_tcptable_file(session, ct, "/proc/net/tcp6", &fd_tcp6,
			    IPPROTO_TCP, 1);
#endif

	if (!parse_tcptable_file
	    (session, ct, "/proc/net/udp", &fd_udp, IPPROTO_UDP, 0))
		return 0;
	return 1;
#elif defined(FREEBSD)
	conn_t c;
	int istcp;
	char *buf;
	const char *mibvar;
	struct tcpcb *tp = NULL;
	struct inpcb *inp;
	struct xinpgen *xig, *oxig;
	struct xsocket *so;
	size_t len;
	int proto = IPPROTO_TCP;
#if 0
	istcp = 0;
	switch (proto) {
	case IPPROTO_TCP:
#endif
		istcp = 1;
		mibvar = "net.inet.tcp.pcblist";
#if 0
		break;
	case IPPROTO_UDP:
		mibvar = "net.inet.udp.pcblist";
		break;
	}
#endif
	/* get connection table size, and then allocate a buffer */
	len = 0;
	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
		if (errno != ENOENT)
			printf("sysctl: %s", mibvar);
		return 0;
	}
	buf = malloc(len);
	if (buf == NULL) {
		printf("malloc %lu bytes", (u_long) len);
		return 0;
	}

	/* read connection table */
	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
		printf("sysctl: %s", mibvar);
		free(buf);
		return 0;
	}

	oxig = xig = (struct xinpgen *) buf;
	for (xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
	     xig->xig_len > sizeof(struct xinpgen);
	     xig = (struct xinpgen *) ((char *) xig + xig->xig_len)) {
		if (istcp) {
			tp = &((struct xtcpcb *) xig)->xt_tp;
			inp = &((struct xtcpcb *) xig)->xt_inp;
			so = &((struct xtcpcb *) xig)->xt_socket;
		} else {
			inp = &((struct xinpcb *) xig)->xi_inp;
			so = &((struct xinpcb *) xig)->xi_socket;
		}

		/* Ignore sockets for protocols other than the desired one. */
		if (so->xso_protocol != (int) proto)
			continue;

		/* Ignore PCBs which were freed during copyout. */
		if (inp->inp_gencnt > oxig->xig_gen)
			continue;

		/* only do IPV4 for now */
		if ((inp->inp_vflag & INP_IPV4) == 0)
			continue;

		/* check SYN_SENT and get rid of NULL address */
		if ((istcp && tp->t_state != TCPS_SYN_SENT)
		    || (inet_lnaof(inp->inp_laddr) == INADDR_ANY))
			continue;

		uint32_to_ipv6(inp->inp_laddr.s_addr, &c.ip_src);
		c.port_src = ntohs(inp->inp_lport);

		uint32_to_ipv6(inp->inp_faddr.s_addr, &c.ip_dst);
		c.port_dst = ntohs(inp->inp_fport);
		c.protocol = IPPROTO_TCP;

		tcptable_add(ct, &c);
	}
	free(buf);
	return 1;
#endif
}
Beispiel #16
0
/*
 * Print a summary of connections related to an Internet
 * protocol.  For TCP, also give state of connection.
 * Listening processes (aflag) are suppressed unless the
 * -a (all) flag is specified.
 */
void
protopr(u_long off, const char *name, int af1, int proto)
{
	static int first = 1;
	int istcp;
	char *buf;
	const char *vchar;
	struct xtcpcb *tp;
	struct xinpcb *inp;
	struct xinpgen *xig, *oxig;
	struct xsocket *so;

	istcp = 0;
	switch (proto) {
	case IPPROTO_TCP:
#ifdef INET6
		if (strncmp(name, "sdp", 3) != 0) {
			if (tcp_done != 0)
				return;
			else
				tcp_done = 1;
		} else {
			if (sdp_done != 0)
				return;
			else
				sdp_done = 1;
		}
#endif
		istcp = 1;
		break;
	case IPPROTO_UDP:
#ifdef INET6
		if (udp_done != 0)
			return;
		else
			udp_done = 1;
#endif
		break;
	}

	if (!pcblist_sysctl(proto, name, &buf))
		return;

	oxig = xig = (struct xinpgen *)buf;
	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
	    xig->xig_len > sizeof(struct xinpgen);
	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
		if (istcp) {
			tp = (struct xtcpcb *)xig;
			inp = &tp->xt_inp;
		} else {
			inp = (struct xinpcb *)xig;
		}
		so = &inp->xi_socket;

		/* Ignore sockets for protocols other than the desired one. */
		if (so->xso_protocol != proto)
			continue;

		/* Ignore PCBs which were freed during copyout. */
		if (inp->inp_gencnt > oxig->xig_gen)
			continue;

		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
#ifdef INET6
		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
#endif /* INET6 */
		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
#ifdef INET6
					  && (inp->inp_vflag & INP_IPV6) == 0
#endif /* INET6 */
			))
		    )
			continue;
		if (!aflag &&
		    (
		     (istcp && tp->t_state == TCPS_LISTEN)
		     || (af1 == AF_INET &&
		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
#ifdef INET6
		     || (af1 == AF_INET6 &&
			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
#endif /* INET6 */
		     || (af1 == AF_UNSPEC &&
			 (((inp->inp_vflag & INP_IPV4) != 0 &&
			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
#ifdef INET6
			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
#endif
			  ))
		     ))
			continue;

		if (first) {
			if (!Lflag) {
				xo_emit("Active Internet connections");
				if (aflag)
					xo_emit(" (including servers)");
			} else
				xo_emit(
	"Current listen queue sizes (qlen/incqlen/maxqlen)");
			xo_emit("\n");
			if (Aflag)
				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
				    "Tcpcb");
			if (Lflag)
				xo_emit((Aflag && !Wflag) ?
				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
				    ((!Wflag || af1 == AF_INET) ?
				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
				    "Proto", "Listen", "Local Address");
			else if (Tflag)
				xo_emit((Aflag && !Wflag) ?
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
				    ((!Wflag || af1 == AF_INET) ?
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
				    "Proto", "Rexmit", "OOORcv", "0-win",
				    "Local Address", "Foreign Address");
			else {
				xo_emit((Aflag && !Wflag) ?
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
				    ((!Wflag || af1 == AF_INET) ?
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
    "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
				    "Proto", "Recv-Q", "Send-Q",
				    "Local Address", "Foreign Address");
				if (!xflag && !Rflag)
					xo_emit(" (state)");
			}
			if (xflag) {
				xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
				    "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
				    "rexmt", "persist", "keep", "2msl",
				    "delack", "rcvtime");
			} else if (Rflag) {
				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
				    "flowid", "ftype");
			}
			xo_emit("\n");
			first = 0;
		}
		if (Lflag && so->so_qlimit == 0)
			continue;
		xo_open_instance("socket");
		if (Aflag) {
			if (istcp)
				xo_emit("{q:address/%*lx} ",
				    2 * (int)sizeof(void *),
				    (u_long)inp->inp_ppcb);
			else
				xo_emit("{q:address/%*lx} ",
				    2 * (int)sizeof(void *),
				    (u_long)so->so_pcb);
		}
#ifdef INET6
		if ((inp->inp_vflag & INP_IPV6) != 0)
			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
			    "46" : "6";
		else
#endif
		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
		    "4" : "";
		if (istcp && (tp->t_flags & TF_TOE) != 0)
			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
		else
			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
		if (Lflag) {
			char buf1[33];

			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
			    so->so_incqlen, so->so_qlimit);
			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
		} else if (Tflag) {
			if (istcp)
				xo_emit("{:sent-retransmit-packets/%6u} "
				    "{:received-out-of-order-packets/%6u} "
				    "{:sent-zero-window/%6u} ",
				    tp->t_sndrexmitpack, tp->t_rcvoopack,
				    tp->t_sndzerowin);
			else
				xo_emit("{P:/%21s}", "");
		} else {
			xo_emit("{:receive-bytes-waiting/%6u} "
			    "{:send-bytes-waiting/%6u} ",
			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
		}
		if (numeric_port) {
			if (inp->inp_vflag & INP_IPV4) {
				inetprint("local", &inp->inp_laddr,
				    (int)inp->inp_lport, name, 1, af1);
				if (!Lflag)
					inetprint("remote", &inp->inp_faddr,
					    (int)inp->inp_fport, name, 1, af1);
			}
#ifdef INET6
			else if (inp->inp_vflag & INP_IPV6) {
				inet6print("local", &inp->in6p_laddr,
				    (int)inp->inp_lport, name, 1);
				if (!Lflag)
					inet6print("remote", &inp->in6p_faddr,
					    (int)inp->inp_fport, name, 1);
			} /* else nothing printed now */
#endif /* INET6 */
		} else if (inp->inp_flags & INP_ANONPORT) {
			if (inp->inp_vflag & INP_IPV4) {
				inetprint("local", &inp->inp_laddr,
				    (int)inp->inp_lport, name, 1, af1);
				if (!Lflag)
					inetprint("remote", &inp->inp_faddr,
					    (int)inp->inp_fport, name, 0, af1);
			}
#ifdef INET6
			else if (inp->inp_vflag & INP_IPV6) {
				inet6print("local", &inp->in6p_laddr,
				    (int)inp->inp_lport, name, 1);
				if (!Lflag)
					inet6print("remote", &inp->in6p_faddr,
					    (int)inp->inp_fport, name, 0);
			} /* else nothing printed now */
#endif /* INET6 */
		} else {
			if (inp->inp_vflag & INP_IPV4) {
				inetprint("local", &inp->inp_laddr,
				    (int)inp->inp_lport, name, 0, af1);
				if (!Lflag)
					inetprint("remote", &inp->inp_faddr,
					    (int)inp->inp_fport, name,
					    inp->inp_lport != inp->inp_fport,
					    af1);
			}
#ifdef INET6
			else if (inp->inp_vflag & INP_IPV6) {
				inet6print("local", &inp->in6p_laddr,
				    (int)inp->inp_lport, name, 0);
				if (!Lflag)
					inet6print("remote", &inp->in6p_faddr,
					    (int)inp->inp_fport, name,
					    inp->inp_lport != inp->inp_fport);
			} /* else nothing printed now */
#endif /* INET6 */
		}
		if (xflag) {
			xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
			    "{:receive-clusters/%6u} {:send-clusters/%6u} "
			    "{:receive-high-water/%6u} {:send-high-water/%6u} "
			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
			    "{:receive-mbuf-bytes-max/%6u} "
			    "{:send-mbuf-bytes-max/%6u}",
			    so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
			    so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
			if (istcp)
				xo_emit(" {:retransmit-timer/%4d.%02d} "
				    "{:persist-timer/%4d.%02d} "
				    "{:keepalive-timer/%4d.%02d} "
				    "{:msl2-timer/%4d.%02d} "
				    "{:delay-ack-timer/%4d.%02d} "
				    "{:inactivity-timer/%4d.%02d}",
				    tp->tt_rexmt / 1000,
				    (tp->tt_rexmt % 1000) / 10,
				    tp->tt_persist / 1000,
				    (tp->tt_persist % 1000) / 10,
				    tp->tt_keep / 1000,
				    (tp->tt_keep % 1000) / 10,
				    tp->tt_2msl / 1000,
				    (tp->tt_2msl % 1000) / 10,
				    tp->tt_delack / 1000,
				    (tp->tt_delack % 1000) / 10,
				    tp->t_rcvtime / 1000,
				    (tp->t_rcvtime % 1000) / 10);
		}
		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
				xo_emit("{:tcp-state/%d}", tp->t_state);
			else {
				xo_emit("{:tcp-state/%s}",
				    tcpstates[tp->t_state]);
#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
				/* Show T/TCP `hidden state' */
				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
					xo_emit("{:need-syn-or-fin/*}");
#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
			}
		}
		if (Rflag) {
			/* XXX: is this right Alfred */
			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
			    inp->inp_flowid,
			    inp->inp_flowtype);
		}
		xo_emit("\n");
		xo_close_instance("socket");
	}
	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
		if (oxig->xig_count > xig->xig_count) {
			xo_emit("Some {d:lost/%s} sockets may have been "
			    "deleted.\n", name);
		} else if (oxig->xig_count < xig->xig_count) {
			xo_emit("Some {d:created/%s} sockets may have been "
			    "created.\n", name);
		} else {
			xo_emit("Some {d:changed/%s} sockets may have been "
			    "created or deleted.\n", name);
		}
	}
	free(buf);
}
Beispiel #17
0
/*
 * Print a summary of connections related to an Internet
 * protocol.  For TCP, also give state of connection.
 * Listening processes (aflag) are suppressed unless the
 * -a (all) flag is specified.
 */
void
protopr(u_long off, char *name, int af, u_int tableid, u_long pcbaddr)
{
	struct inpcbtable table;
	struct inpcb *prev, *next;
	struct inpcb inpcb, prevpcb;
	int istcp, israw, isany;
	int addrlen = 22;
	int first = 1;
	char *name0;
	char namebuf[20];

	name0 = name;
	if (off == 0)
		return;
	istcp = strcmp(name, "tcp") == 0;
	israw = strncmp(name, "ip", 2) == 0;
	kread(off, &table, sizeof table);
	prev = NULL;
	next = TAILQ_FIRST(&table.inpt_queue);

	while (next != NULL) {
		kread((u_long)next, &inpcb, sizeof inpcb);
		if (prev != NULL) {
			kread((u_long)prev, &prevpcb, sizeof prevpcb);
			if (TAILQ_NEXT(&prevpcb, inp_queue) != next) {
				printf("PCB list changed\n");
				break;
			}
		}
		prev = next;
		next = TAILQ_NEXT(&inpcb, inp_queue);

		switch (af) {
		case AF_INET:
			if ((inpcb.inp_flags & INP_IPV6) != 0)
				continue;
			isany = inet_lnaof(inpcb.inp_faddr) == INADDR_ANY;
			break;
		case AF_INET6:
			if ((inpcb.inp_flags & INP_IPV6) == 0)
				continue;
			isany = IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6);
			break;
		default:
			isany = 0;
			break;
		}

		if (Pflag) {
			if (istcp && pcbaddr == (u_long)inpcb.inp_ppcb) {
				if (vflag)
					socket_dump((u_long)inpcb.inp_socket);
				else
					tcpcb_dump(pcbaddr);
			} else if (pcbaddr == (u_long)prev) {
				if (vflag)
					socket_dump((u_long)inpcb.inp_socket);
				else
					inpcb_dump(pcbaddr, 0, af);
			}
			continue;
		}

		if (inpcb.inp_rtableid != tableid)
			continue;

		kread((u_long)inpcb.inp_socket, &sockb, sizeof (sockb));
		if (istcp) {
			kread((u_long)inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
			if (!aflag && tcpcb.t_state <= TCPS_LISTEN)
				continue;
		} else if (!aflag && isany)
			continue;
		if (first) {
			printf("Active Internet connections");
			if (aflag)
				printf(" (including servers)");
			putchar('\n');
			if (Aflag) {
				addrlen = 18;
				printf("%-*.*s ", PLEN, PLEN, "PCB");
			}
			printf("%-7.7s %-6.6s %-6.6s ",
			    "Proto", "Recv-Q", "Send-Q");
			if (Bflag && istcp)
				printf("%-6.6s %-6.6s %-6.6s ",
				    "Recv-W", "Send-W", "Cgst-W");
			printf(" %-*.*s %-*.*s %s\n",
			    addrlen, addrlen, "Local Address",
			    addrlen, addrlen, "Foreign Address", "(state)");
			first = 0;
		}
		if (Aflag) {
			if (istcp)
				printf("%*p ", PLEN, hideroot ? 0 : inpcb.inp_ppcb);
			else
				printf("%*p ", PLEN, hideroot ? 0 : prev);
		}
		if (inpcb.inp_flags & INP_IPV6 && !israw) {
			strlcpy(namebuf, name0, sizeof namebuf);
			strlcat(namebuf, "6", sizeof namebuf);
			name = namebuf;
		} else
			name = name0;
		printf("%-7.7s %6lu %6lu ",
		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
		if (Bflag && istcp)
			printf("%6lu %6lu %6lu ", tcpcb.rcv_wnd, tcpcb.snd_wnd,
			    (tcpcb.t_state == TCPS_ESTABLISHED) ?
			    tcpcb.snd_cwnd : 0);

		if (inpcb.inp_flags & INP_IPV6) {
			inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport,
			    name);
			inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport,
			    name);
		} else {
			inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
			    name, 1);
			inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
			    name, 0);
		}
		if (istcp) {
			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
				printf(" %d", tcpcb.t_state);
			else
				printf(" %s", tcpstates[tcpcb.t_state]);
		} else if (israw) {
			u_int8_t proto;

			if (inpcb.inp_flags & INP_IPV6)
				proto = inpcb.inp_ipv6.ip6_nxt;
			else
				proto = inpcb.inp_ip.ip_p;
			printf(" %u", proto);
		}
		putchar('\n');
	}
}
Beispiel #18
0
/*
 * Construct an Internet address representation.
 * If the nflag has been supplied, give
 * numeric value, otherwise try for symbolic name.
 */
char *
inetname(struct in_addr *inp)
{
	char *cp;
	static char line[50];
	struct hostent *hp;
	struct netent *np;
	static char domain[MAXHOSTNAMELEN];
	static int first = 1;
#if defined (WIN32) || defined (cygwin)
        char host_temp[] = "localhost";
#endif

	if (first && !nflag) {
		first = 0;
		if (gethostname(domain, sizeof(domain)) == 0 &&
		    (cp = strchr(domain, '.')))
			(void) strlcpy(domain, cp + 1, sizeof domain);
		else
			domain[0] = '\0';
	}
	cp = NULL;
	if (!nflag && inp->s_addr != INADDR_ANY) {
		int net = inet_netof(*inp);
		int lna = inet_lnaof(*inp);

		if (lna == INADDR_ANY) {
			np = getnetbyaddr(net, AF_INET);
			if (np)
				cp = np->n_name;
		}
		if (cp == NULL) {
			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
			if (hp) {
				if ((cp = strchr(hp->h_name, '.')) &&
				    !strcmp(cp + 1, domain))
					*cp = '\0';
#if defined (WIN32) || defined (cygwin)
                                        /* Windows insists on returning the computer name for 127.0.0.1
                                         * even if the hosts file lists something else such as 'localhost'.
                                         * If we are trying to look up 127.0.0.1, just return 'localhost'   */
                                        if (!strcmp(inet_ntoa(*inp),"127.0.0.1"))
                                             cp = host_temp;
                                        else
#endif                                                                          
				cp = hp->h_name;
			}
		}
	}
	if (inp->s_addr == INADDR_ANY)
		snprintf(line, sizeof line, "*");
	else if (cp)
		snprintf(line, sizeof line, "%s", cp);
	else {
		inp->s_addr = ntohl(inp->s_addr);
#define C(x)	((x) & 0xff)
		snprintf(line, sizeof line, "%u.%u.%u.%u",
		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
		    C(inp->s_addr >> 8), C(inp->s_addr));
	}
	return (line);
}
Beispiel #19
0
static void
outputpcb(int proto, const char *name, struct inpcb *inp, struct xsocket *so, struct tcpcb *tp)
{
	const char *vchar;
	static struct clockinfo clockinfo;

	if (clockinfo.hz == 0) {
		size_t size = sizeof(clockinfo);
		sysctlbyname("kern.clockrate", &clockinfo, &size, NULL, 0);
		if (clockinfo.hz == 0)
			clockinfo.hz = 100;
	}

	/* Ignore sockets for protocols other than the desired one. */
	if (so->xso_protocol != (int)proto)
		return;

	if ((af == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
#ifdef INET6
	    || (af == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
#endif /* INET6 */
	    || (af == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
#ifdef INET6
		&& (inp->inp_vflag & INP_IPV6) == 0
#endif /* INET6 */
		))
	    ) {
		return;
	}
	if (!aflag && ( 
		(proto == IPPROTO_TCP && tp->t_state == TCPS_LISTEN) ||
		(af == AF_INET && inet_lnaof(inp->inp_laddr) == INADDR_ANY)
#ifdef INET6
	    || (af == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
#endif /* INET6 */
	    || (af == AF_UNSPEC && (((inp->inp_vflag & INP_IPV4) != 0 &&
		inet_lnaof(inp->inp_laddr) == INADDR_ANY)
#ifdef INET6
	    || ((inp->inp_vflag & INP_IPV6) != 0 &&
		IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
#endif
		  ))
	     )) {
		return;
	}

	if (ppr_first) {
		if (!Lflag) {
			printf("Active Internet connections");
			if (aflag)
				printf(" (including servers)");
		} else {
			printf("Current listen queue sizes "
				"(qlen/incqlen/maxqlen)");
		}
		putchar('\n');
		if (Aflag)
			printf("%-8.8s ", "Socket");
		if (Pflag)
			printf("%8.8s %8.8s %8.8s ", "TxWin", "Unacked", "RTT/ms");
		if (Lflag) {
			printf("%-5.5s %-14.14s %-22.22s\n",
				"Proto", "Listen", "Local Address");
		} else {
			printf((Aflag && !Wflag) ?
			    "%-5.5s %-6.6s %-6.6s %-17.17s %-17.17s %s\n" :
			    "%-5.5s %-6.6s %-6.6s %-21.21s %-21.21s %s\n",
			    "Proto", "Recv-Q", "Send-Q",
			    "Local Address", "Foreign Address",
			    "(state)");
		}
		ppr_first = 0;
	}
	if (Lflag && so->so_qlimit == 0)
		return;
	if (Aflag) {
		if (tp)
			printf("%8lx ", (u_long)inp->inp_ppcb);
		else
			printf("%8lx ", (u_long)so->so_pcb);
	}
	if (Pflag) {
		if (tp) {
			int window = MIN(tp->snd_cwnd, tp->snd_bwnd);
			if (window == 1073725440)
				printf("%8s ", "max");
			else
				printf("%8d ", (int)MIN(tp->snd_cwnd, tp->snd_bwnd));
			printf("%8d ", (int)(tp->snd_max - tp->snd_una));
			if (tp->t_srtt == 0)
			    printf("%8s ", "-");
			else
			    printf("%8.3f ", (double)tp->t_srtt * 1000.0 / TCP_RTT_SCALE / clockinfo.hz);
		} else {
			printf("%8s %8s %8s ", "-", "-", "-");
		}
	}
#ifdef INET6
	if ((inp->inp_vflag & INP_IPV6) != 0)
		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? "46" : "6 ";
	else
#endif
		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? "4 " : "  ";

	printf("%-3.3s%-2.2s ", name, vchar);
	if (Lflag) {
		char buf[15];

		snprintf(buf, sizeof(buf), "%d/%d/%d", so->so_qlen,
			 so->so_incqlen, so->so_qlimit);
		printf("%-13.13s ", buf);
	} else if (Bflag) {
		printf("%6ld %6ld ",
		       so->so_rcv.sb_hiwat,
		       so->so_snd.sb_hiwat);
	} else {
		printf("%6ld %6ld ",
		       so->so_rcv.sb_cc,
		       so->so_snd.sb_cc);
	}
	if (numeric_port) {
		if (inp->inp_vflag & INP_IPV4) {
			inetprint(&inp->inp_laddr, (int)inp->inp_lport,
				  name, 1);
			if (!Lflag)
				inetprint(&inp->inp_faddr,
					  (int)inp->inp_fport, name, 1);
		}
#ifdef INET6
		else if (inp->inp_vflag & INP_IPV6) {
			inet6print(&inp->in6p_laddr,
				   (int)inp->inp_lport, name, 1);
			if (!Lflag)
				inet6print(&inp->in6p_faddr,
					   (int)inp->inp_fport, name, 1);
		} /* else nothing printed now */
#endif /* INET6 */
	} else if (inp->inp_flags & INP_ANONPORT) {
		if (inp->inp_vflag & INP_IPV4) {
			inetprint(&inp->inp_laddr, (int)inp->inp_lport,
				  name, 1);
			if (!Lflag)
				inetprint(&inp->inp_faddr,
					  (int)inp->inp_fport, name, 0);
		}
#ifdef INET6
		else if (inp->inp_vflag & INP_IPV6) {
			inet6print(&inp->in6p_laddr,
				   (int)inp->inp_lport, name, 1);
			if (!Lflag)
				inet6print(&inp->in6p_faddr,
					   (int)inp->inp_fport, name, 0);
		} /* else nothing printed now */
#endif /* INET6 */
	} else {
		if (inp->inp_vflag & INP_IPV4) {
			inetprint(&inp->inp_laddr, (int)inp->inp_lport,
				  name, 0);
			if (!Lflag)
				inetprint(&inp->inp_faddr,
					  (int)inp->inp_fport, name,
					  inp->inp_lport !=
						inp->inp_fport);
		}
#ifdef INET6
		else if (inp->inp_vflag & INP_IPV6) {
			inet6print(&inp->in6p_laddr,
				   (int)inp->inp_lport, name, 0);
			if (!Lflag)
				inet6print(&inp->in6p_faddr,
					   (int)inp->inp_fport, name,
					   inp->inp_lport !=
						inp->inp_fport);
		} /* else nothing printed now */
#endif /* INET6 */
	}
	if (tp && !Lflag) {
		if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
			printf("%d", tp->t_state);
	      else {
			printf("%s", tcpstates[tp->t_state]);
#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
		      /* Show T/TCP `hidden state' */
		      if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
			      putchar('*');
#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
	      }
	}
	putchar('\n');
}
Beispiel #20
0
void rip_input(struct sockaddr *sa, int size)
{
	struct sockaddr_in *from = (struct sockaddr_in *)sa;
	register struct rip *msg = (struct rip *)packet;
	register struct netinfo *n;
	const char *name;
	int lna, net, subnet;
	struct hostent *hp;
	struct netent *np;

	if (msg->rip_cmd != RIPCMD_RESPONSE)
		return;
	printf("%d bytes from ", size);
	if (nflag)
		printf("%s:\n", inet_ntoa(from->sin_addr));
	else {
		hp = gethostbyaddr((char *)&from->sin_addr,
		    sizeof (struct in_addr), AF_INET);
		name = hp == 0 ? "???" : hp->h_name;
		printf("%s(%s):\n", name, inet_ntoa(from->sin_addr));
	}
	size -= sizeof (int);
	n = msg->rip_nets;
	while (size > 0) {
	    if (size < (int)sizeof (struct netinfo))
		    break;
	    if (msg->rip_vers > 0) {
		    n->rip_dst.sa_family =
			    ntohs(n->rip_dst.sa_family);
		    n->rip_metric = ntohl(n->rip_metric);
	    }
	    switch (n->rip_dst.sa_family) {

	    case AF_INET:
		{ register struct sockaddr_in *sin;

		sin = (struct sockaddr_in *)&n->rip_dst;
		net = inet_netof(sin->sin_addr);
		subnet = inet_subnetof(sin->sin_addr);
		lna = inet_lnaof(sin->sin_addr);
		name = "???";
		if (!nflag) {
			if (sin->sin_addr.s_addr == 0)
				name = "default";
			else if (lna == INADDR_ANY) {
				np = getnetbyaddr(net, AF_INET);
				if (np)
					name = np->n_name;
				else if (net == 0)
					name = "default";
			} else if ((lna & 0xff) == 0 &&
			    (np = getnetbyaddr(subnet, AF_INET))) {
				struct in_addr subnaddr;

				subnaddr = inet_makeaddr(subnet, INADDR_ANY);
				if (memcmp(&sin->sin_addr, &subnaddr,
				    sizeof(subnaddr)) == 0)
					name = np->n_name;
				else
					goto host;
			} else {
	host:
				hp = gethostbyaddr((char *)&sin->sin_addr,
				    sizeof (struct in_addr), AF_INET);
				if (hp)
					name = hp->h_name;
			}
			printf("\t%-17s metric %2d name %s\n",
				inet_ntoa(sin->sin_addr), n->rip_metric, name);
		} else
			printf("\t%-17s metric %2d\n",
				inet_ntoa(sin->sin_addr), n->rip_metric);
		break;
		}

	    default:
		{ u_short *p = (u_short *)n->rip_dst.sa_data;

		printf("\t(af %d) %x %x %x %x %x %x %x, metric %d\n",
		    p[0], p[1], p[2], p[3], p[4], p[5], p[6],
		    n->rip_dst.sa_family,
		    n->rip_metric);
		break;
		}
			
	    }
	    size -= sizeof (struct netinfo), n++;
	}
}
Beispiel #21
-1
/**
@SYMTestCaseID          SYSLIB-STDLIB-CT-1072
@SYMTestCaseDesc	    Tests for ARPA net functions
@SYMTestPriority 	    High
@SYMTestActions  	    Tests for all basic network functions 
@SYMTestExpectedResults Test must not fail
@SYMREQ                 REQ0000
*/
void testArpa()
	{
	char* cp;
	char* cp2;
	struct in_addr iaddr;
	unsigned long ul1, ul2;
	int err;
	int i;

	test_Next("ARPA/INET.H functions");

	iaddr.s_addr=11;
	cp="16.33.50.67";
	err=inet_aton(cp, &iaddr);
	test(err==1);
	test(iaddr.s_addr==htonl(0x10213243));
	test(iaddr.s_addr==inet_addr(cp));
	cp2=inet_ntoa(iaddr);
	test(strcmp(cp2,cp)==0);

	iaddr.s_addr=11;
	err=inet_aton("16.rubbish.67", &iaddr);
	test(err==0);

	for (i=0;i<N_ADDRESSES;i++)
		{
		iaddr=inet_makeaddr(addresses[i][0], addresses[i][1]);
		test(iaddr.s_addr==ntohl(addresses[i][2]));
		ul1=inet_netof(iaddr);
		ul2=inet_lnaof(iaddr);
		test(ul1==addresses[i][0]);
		test(ul2==addresses[i][1]);
		}
	}