Пример #1
0
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	int hash, n, pos;

	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
	    "USER", "COMMAND", "PID", "FD", "PROTO",
	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
	if (opt_s)
		printf(" %-12s", "STATE");
	printf("\n");
	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next) {
			if ((void *)s->socket != xf->xf_data)
				continue;
			if (!check_ports(s))
				continue;
			s->shown = 1;
			pos = 0;
			if ((pwd = getpwuid(xf->xf_uid)) == NULL)
				pos += xprintf("%lu ", (u_long)xf->xf_uid);
			else
				pos += xprintf("%s ", pwd->pw_name);
			while (pos < 9)
				pos += xprintf(" ");
			pos += xprintf("%.10s", getprocname(xf->xf_pid));
			while (pos < 20)
				pos += xprintf(" ");
			pos += xprintf("%lu ", (u_long)xf->xf_pid);
			while (pos < 26)
				pos += xprintf(" ");
			pos += xprintf("%d ", xf->xf_fd);
			displaysock(s, pos);
		}
	}
	if (opt_j >= 0)
		return;
	for (hash = 0; hash < HASHSIZE; hash++) {
		for (s = sockhash[hash]; s != NULL; s = s->next) {
			if (s->shown)
				continue;
			if (!check_ports(s))
				continue;
			pos = 0;
			pos += xprintf("%-8s %-10s %-5s %-2s ",
			    "?", "?", "?", "?");
			displaysock(s, pos);
		}
	}
}
static bool check_options(
    bool         is_icmp,
    bool         is_tcp,
    bool         is_udp,
    bool         is_ipv4,
    bool         is_ipv6,
    int          dst_port_enabled,
    int          src_port_enabled,
    const char * protocol_name,
    const char * algorithm_name
) {
    return check_ip_version(is_ipv4, is_ipv6)
           && check_protocol(is_icmp, is_tcp, is_udp, protocol_name)
           && check_ports(is_icmp, dst_port_enabled, src_port_enabled)
           && check_algorithm(algorithm_name);
}
Пример #3
0
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	void *p;
	int hash, n, pos;

	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
	    "USER", "COMMAND", "PID", "FD", "PROTO",
	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next)
			if ((void *)s->socket == xf->xf_data)
				break;
		if (s == NULL)
			continue;
		if (!check_ports(s))
			continue;
		pos = 0;
		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
			pos += xprintf("%lu ", (u_long)xf->xf_uid);
		else
			pos += xprintf("%s ", pwd->pw_name);
		while (pos < 9)
			pos += xprintf(" ");
		pos += xprintf("%.10s", getprocname(xf->xf_pid));
		while (pos < 20)
			pos += xprintf(" ");
		pos += xprintf("%lu ", (u_long)xf->xf_pid);
		while (pos < 26)
			pos += xprintf(" ");
		pos += xprintf("%d ", xf->xf_fd);
		while (pos < 29)
			pos += xprintf(" ");
		pos += xprintf("%s", s->protoname);
		if (s->vflag & INP_IPV4)
			pos += xprintf("4 ");
		if (s->vflag & INP_IPV6)
			pos += xprintf("6 ");
		while (pos < 36)
			pos += xprintf(" ");
		switch (s->family) {
		case AF_INET:
		case AF_INET6:
			pos += printaddr(s->family, &s->laddr);
			if (s->family == AF_INET6 && pos >= 58)
				pos += xprintf(" ");
			while (pos < 58)
				pos += xprintf(" ");
			pos += printaddr(s->family, &s->faddr);
			break;
		case AF_UNIX:
			/* server */
			if (s->laddr.ss_len > 0) {
				pos += printaddr(s->family, &s->laddr);
				break;
			}
			/* client */
			p = *(void **)&s->faddr;
			if (p == NULL) {
				pos += xprintf("(not connected)");
				break;
			}
			pos += xprintf("-> ");
			for (hash = 0; hash < HASHSIZE; ++hash) {
				for (s = sockhash[hash]; s != NULL; s = s->next)
					if (s->pcb == p)
						break;
				if (s != NULL)
					break;
			}
			if (s == NULL || s->laddr.ss_len == 0)
				pos += xprintf("??");
			else
				pos += printaddr(s->family, &s->laddr);
			break;
		default:
			abort();
		}
		xprintf("\n");
	}
}