Example #1
0
static void
_print_udp(int family, u_char *p, int length)
{
	struct udp_hdr *udp;
	char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];

	if (family == AF_INET6) {
			struct ip6_hdr *ip6 = (struct ip6_hdr *)p;
			udp = (struct udp_hdr *)(p + IP6_HDR_LEN);

			ip6_ntop(&ip6->ip6_src, src, sizeof(src));
			ip6_ntop(&ip6->ip6_dst, dst, sizeof(dst));
	} else {
			struct ip_hdr *ip;

			ip = (struct ip_hdr *)p;
			udp = (struct udp_hdr *)(p + (ip->ip_hl * 4));

			ip_ntop(&ip->ip_src, src, sizeof(src));
			ip_ntop(&ip->ip_dst, dst, sizeof(dst));
	}

	/* XXX - truncation? */
	printf("%s.%d > %s.%d:", src, ntohs(udp->uh_sport),
		dst, ntohs(udp->uh_dport));
	printf(" udp %d", ntohs(udp->uh_ulen) - UDP_HDR_LEN);
}
Example #2
0
void output_link (FILE *fp, struct node *n, char *cloudname)
{
    int nl1, i ;
    iptext_t ipaddr ;
    l3tol1_t tab [MAXIFPERIP] ;

    if (! MK_ISSELECTED (n))
	return ;

    nl1 = get_l3tol1 (fp, n, tab, NTAB(tab)) ;
    for (i = 0 ; i < nl1 ; i++)
    {
	l3tol1_t *p ;

	p = &tab [i] ;
	fprintf (fp, "link %s", n->eq->name) ;
	if (tab [i].r != NULL)
	    fprintf (fp, ":%s", p->r->u.router.name) ;

	fprintf (fp, " %s.%d", p->l1->u.l1.ifname, p->l2->u.l2.vlan) ;

	/* XXX : should we really print preflen ? */
	ip_ntop (&n->u.l3.addr, ipaddr, 1) ;
	fprintf (fp, " %s", ipaddr) ;

	if (p->l1->u.l1.link != NULL)
	    fprintf (fp, " %s", p->l1->u.l1.link) ;
	else
	    fprintf (fp, " (nolink)") ;

	fprintf (fp, " %s", cloudname) ;

	fprintf (fp, "\n") ;
    }
}
Example #3
0
char *
ip6_ntop(const ip6_addr_t *ip6, char *dst, size_t len)
{
	struct { int base, len; } best, cur;
	char *p = dst;
	int i;
	
	if (len < 46)
		return (NULL);
	
	best.base = cur.base = -1;
	/*
	 * Algorithm borrowed from Vixie's inet_pton6()
	 */
	for (i = 0; i < IP6_ADDR_LEN; i += 2) {
		if (*((uint16_t *)&ip6->data[i]) == 0) {
			if (cur.base == -1) {
				cur.base = i;
				cur.len = 0;
			} else
				cur.len += 2;
		} else {
			if (cur.base != -1) {
				if (best.base == -1 || cur.len > best.len)
					best = cur;
				cur.base = -1;
			}
		}
	}
	if (cur.base != -1 && (best.base == -1 || cur.len > best.len))
		best = cur;
	if (best.base != -1 && best.len < 2)
		best.base = -1;
	if (best.base == 0)
		*p++ = ':';

	for (i = 0; i < IP6_ADDR_LEN; i += 2) {
		if (i == best.base) {
			*p++ = ':';
			i += best.len;
		} else if (i == 12 && best.base == 0 &&
		    (best.len == 10 || (best.len == 8 &&
			*((uint16_t *)&ip6->data[10]) == 0xffff))) {
			if (ip_ntop((ip_addr_t *)&ip6->data[12], p,
			    len - (p - dst)) == NULL)
				return (NULL);
			return (dst);
		} else p += sprintf(p, "%x:",
		    ntohs(*((uint16_t *)&ip6->data[i])));
	}
	if (best.base + 2 + best.len == IP6_ADDR_LEN) {
		*p = '\0';
	} else
		p[-1] = '\0';

	return (dst);
}
Example #4
0
void format_write_ip_destination_hex(FILE *fd, const connection *cxt, const char *prefix)
{
    char ip_s[IP_ADDRMAX];

    if ( ! ip_ntop(&cxt->d_ip, ip_s, IP_ADDRMAX, IP_NUMERIC_HEX) )
        perror("Something died in ip_ntop for dest");

    fprintf(fd, "%s%s", prefix, ip_s);
}
Example #5
0
void format_write_ip_source(FILE *fd, const connection *cxt, const char *prefix)
{
    char ip_s[IP_ADDRMAX];

    if ( ! ip_ntop(&cxt->s_ip, ip_s, IP_ADDRMAX, 0) )
        perror("Something died in ip_ntop for src");

    fprintf(fd, "%s%s", prefix, ip_s);
}
Example #6
0
void output_direct (FILE *fp, struct node *n [2])
{
    int nl1 ;
    iptext_t ipaddr ;
    l3tol1_t tab [MAXIFPERIP] ;
    l3tol1_t *p ;
    int peer ;

    if (! (MK_ISSELECTED (n [0]) && MK_ISSELECTED (n [1])))
	return ;

    fprintf (fp, "direct ") ;

    for (peer = 0 ; peer < 2 ; peer++)
    {
	nl1 = get_l3tol1 (fp, n [peer], tab, NTAB(tab)) ;
	if (nl1 != 1)
	{
	    fprintf (stderr, "Direct link detected with 2 L1 on the same L3 node\n") ;
	    exit (1) ;
	}

	p = &tab [0] ;
	fprintf (fp, " %s", n [peer]->eq->name) ;
	if (tab [0].r != NULL)
	    fprintf (fp, ":%s", p->r->u.router.name) ;

	fprintf (fp, " %s.%d", p->l1->u.l1.ifname, p->l2->u.l2.vlan) ;

	/* XXX : should we really print preflen ? */
	ip_ntop (&n [peer]->u.l3.addr, ipaddr, 1) ;
	fprintf (fp, " %s", ipaddr) ;

	if (peer == 0)
	{
	    if (p->l1->u.l1.link != NULL)
		fprintf (fp, " %s", p->l1->u.l1.link) ;
	    else
		fprintf (fp, " (nolink)") ;
	}
    }

    fprintf (fp, "\n") ;
}
Example #7
0
char *
addr_ntop(const struct addr *src, char *dst, size_t size)
{
	if (src->addr_type == ADDR_TYPE_IP && size >= 20) {
		if (ip_ntop(&src->addr_ip, dst, size) != NULL) {
			if (src->addr_bits != IP_ADDR_BITS)
				sprintf(dst + strlen(dst), "/%d",
				    src->addr_bits);
			return (dst);
		}
	} else if (src->addr_type == ADDR_TYPE_IP6 && size >= 42) {
		if (ip6_ntop(&src->addr_ip6, dst, size) != NULL) {
			if (src->addr_bits != IP6_ADDR_BITS)
				sprintf(dst + strlen(dst), "/%d",
				    src->addr_bits);
			return (dst);
		}
	} else if (src->addr_type == ADDR_TYPE_ETH && size >= 18) {
		if (src->addr_bits == ETH_ADDR_BITS)
			return (eth_ntop(&src->addr_eth, dst, size));
	}
	errno = EINVAL;
	return (NULL);
}
Example #8
0
void output_cloud (FILE *fp, struct node *n, char *cloudname, size_t size)
{
    static int cloudno = 0 ;
    struct node *l1node, *l2node ;
    vlanset_t vs ;
    ip_t tabnet [MAXNET] ;
    int ntab ;
    iptext_t addr ;
    int i ;

    cloudno++ ;
    snprintf (cloudname, size, CLOUDFORMAT, cloudno) ;

    fprintf (fp, "cloud %s", cloudname) ;

    /*
     * Reference to the broadcast domain
     * - get the first marked L1 interface
     * - name of equipement of this L1
     * - get the first marked L2 from this L1
     */

    find_interface (&l1node, &l2node) ;
    if (l1node == NULL || l2node == NULL)
    {
	/*
	 * Degenerated case such as an L3 node not connected to the
	 * rest of the world...
	 */
	if (l2node == NULL)
	    l2node = get_neighbour (n, NT_L2) ;

	if (l2node != NULL)
	    fprintf (fp, " {%s %s %d}", l2node->eq->name, "-", l2node->u.l2.vlan) ;
	else
	    fprintf (fp, " {%s %s %d}", "-", "-", 0) ;
    }
    else
	fprintf (fp, " {%s %s %d}",
			    l1node->eq->name, l1node->u.l1.ifname, l2node->u.l2.vlan) ;

    /*
     * Get all vlan used
     */

    traversed_vlans (vs) ;
    fprintf (fp, " {") ;
    print_vlanlist (fp, vs, 1) ;
    fprintf (fp, "}") ;

    /*
     * Get all network CIDR from L3 nodes
     */

    ntab = find_networks (tabnet, NTAB (tabnet)) ;
    fprintf (fp, " {") ;
    for (i = 0 ; i < ntab ; i++)
    {
	if (i != 0)
	    fprintf (fp, " ") ;
	ip_ntop (&tabnet [i], addr, 1) ;
	fprintf (fp, "%s", addr) ;
    }
    fprintf (fp, "}") ;

    fprintf (fp, "\n") ;
}
Example #9
0
void
_print_tcp(int family, unsigned char *p, int length)
{
	struct tcp_hdr *tcp;
	u_short sport, dport, win, urp;
	u_long seq, ack;
	int len, tcp_hl;
	register char ch;

	char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];

	if (family == AF_INET6) {
			struct ip6_hdr *ip6 = (struct ip6_hdr *)p;
			tcp = (struct tcp_hdr *)(p + IP6_HDR_LEN);
			len = length;

			ip6_ntop(&ip6->ip6_src, src, sizeof(src));
			ip6_ntop(&ip6->ip6_dst, dst, sizeof(dst));
	} else {
			struct ip_hdr *ip;

			ip = (struct ip_hdr *)p;
			tcp = (struct tcp_hdr *)(p + (ip->ip_hl * 4));
			len = length - (ip->ip_hl * 4);

			ip_ntop(&ip->ip_src, src, sizeof(src));
			ip_ntop(&ip->ip_dst, dst, sizeof(dst));
	}

	if (len < TCP_HDR_LEN) {
		printf("truncated-tcp %d", len);
		return;
	}
	sport = ntohs(tcp->th_sport);
	dport = ntohs(tcp->th_dport);
	seq = ntohl(tcp->th_seq);
	ack = ntohl(tcp->th_ack);
	win = ntohs(tcp->th_win);
	urp = ntohs(tcp->th_urp);
	tcp_hl = tcp->th_off * 4;

	printf("%s.%d > %s.%d: ", src, sport, dst, dport);

	if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_PUSH)) {
		if (tcp->th_flags & TH_SYN)	putchar('S');
		if (tcp->th_flags & TH_FIN)	putchar('F');
		if (tcp->th_flags & TH_RST)	putchar('R');
		if (tcp->th_flags & TH_PUSH)	putchar('P');
	} else
		putchar('.');
	
	if (tcp_hl > len) {
		printf(" [bad hdr length]");
		return;
	}
	len -= tcp_hl;
	
	if (len > 0 || tcp->th_flags & (TH_SYN | TH_FIN | TH_RST))
		printf(" %lu:%lu(%d)", seq, seq + len, len);
	
	if (tcp->th_flags & TH_ACK)
		printf(" ack %lu", ack);
	printf(" win %d", win);
	if (tcp->th_flags & TH_URG)
		printf(" urg %d", urp);
	
	/* Handle options. */
	if ((tcp_hl -= TCP_HDR_LEN) > 0) {
		register const u_char *cp;
		register int i, opt, len, datalen;
		
		cp = (const u_char *)tcp + TCP_HDR_LEN;
		putchar(' ');
		ch = '<';
		
		while (tcp_hl > 0) {
			putchar(ch);
			opt = *cp++;
			if (TCP_OPT_TYPEONLY(opt)) {
				len = 1;
			} else {
				len = *cp++;    /* total including type, len */
				if (len < 2 || len > tcp_hl)
					goto bad;
				--tcp_hl;         /* account for length byte */
			}
			--tcp_hl;           /* account for type byte */
			datalen = 0;
			
/* Bail if "l" bytes of data are not left or were not captured  */
#define LENCHECK(l) { if ((l) > tcp_hl) goto bad; }
			
			switch (opt) {
			case TCP_OPT_MSS:
				printf("mss");
				datalen = 2;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_16BITS(cp));
				break;
			case TCP_OPT_EOL:
				printf("eol");
				break;
			case TCP_OPT_NOP:
				printf("nop");
				break;
			case TCP_OPT_WSCALE:
				printf("wscale");
				datalen = 1;
				LENCHECK(datalen);
				printf(" %u", *cp);
				break;
			case TCP_OPT_SACKOK:
				printf("sackOK");
				if (len != 2)
					printf("[len %d]", len);
				break;
			case TCP_OPT_SACK:
				datalen = len - 2;
				if ((datalen % 8) != 0 ||
				    !(tcp->th_flags & TH_ACK)) {
					printf("malformed sack ");
					printf("[len %d] ", datalen);
					break;
				}
				printf("sack %d ", datalen / 8);
				break;
			case TCP_OPT_ECHO:
				printf("echo");
				datalen = 4;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp));
				break;
			case TCP_OPT_ECHOREPLY:
				printf("echoreply");
				datalen = 4;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp));
				break;
			case TCP_OPT_TIMESTAMP:
				printf("timestamp");
				datalen = 8;
				LENCHECK(4);
				printf(" %u", EXTRACT_32BITS(cp));
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp + 4));
				break;
			case TCP_OPT_CC:
				printf("cc");
				datalen = 4;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp));
				break;
			case TCP_OPT_CCNEW:
				printf("ccnew");
				datalen = 4;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp));
				break;
			case TCP_OPT_CCECHO:
				printf("ccecho");
				datalen = 4;
				LENCHECK(datalen);
				printf(" %u", EXTRACT_32BITS(cp));
				break;
			default:
				printf("opt-%d:", opt);
				datalen = len - 2;
				for (i = 0; i < datalen; ++i) {
					LENCHECK(i);
					printf("%02x", cp[i]);
				}
				break;
			}
			/* Account for data printed */
			cp += datalen;
			tcp_hl -= datalen;
			
			/* Check specification against observed length */
			++datalen;                /* option octet */
			if (!TCP_OPT_TYPEONLY(opt))
				++datalen;              /* size octet */
			if (datalen != len)
				printf("[len %d]", len);
			ch = ',';
			if (opt == TCP_OPT_EOL)
				break;
		}
		putchar('>');
	}
	return;
 bad:
	fputs("[bad opt]", stdout);
	if (ch != '\0')
		putchar('>');
	return;
}
Example #10
0
char *
ip6_ntop(const ip6_addr_t *ip6, char *dst, size_t len)
{
	uint16_t data[IP6_ADDR_LEN / 2];
	struct { int base, len; } best, cur;
	char *p = dst;
	int i;

	cur.len = best.len = 0;
	
	if (len < 46)
		return (NULL);
	
	/* Copy into 16-bit array. */
	for (i = 0; i < IP6_ADDR_LEN / 2; i++) {
		data[i] = ip6->data[2 * i] << 8;
		data[i] |= ip6->data[2 * i + 1];
	}
	
	best.base = cur.base = -1;
	/*
	 * Algorithm borrowed from Vixie's inet_pton6()
	 */
	for (i = 0; i < IP6_ADDR_LEN; i += 2) {
		if (data[i / 2] == 0) {
			if (cur.base == -1) {
				cur.base = i;
				cur.len = 0;
			} else
				cur.len += 2;
		} else {
			if (cur.base != -1) {
				if (best.base == -1 || cur.len > best.len)
					best = cur;
				cur.base = -1;
			}
		}
	}
	if (cur.base != -1 && (best.base == -1 || cur.len > best.len))
		best = cur;
	if (best.base != -1 && best.len < 2)
		best.base = -1;
	if (best.base == 0)
		*p++ = ':';

	for (i = 0; i < IP6_ADDR_LEN; i += 2) {
		if (i == best.base) {
			*p++ = ':';
			i += best.len;
		} else if (i == 12 && best.base == 0 &&
		    (best.len == 10 || (best.len == 8 &&
			data[5] == 0xffff))) {
			if (ip_ntop((ip_addr_t *)&data[6], p,
			    len - (p - dst)) == NULL)
				return (NULL);
			return (dst);
		} else p += sprintf(p, "%x:", data[i / 2]);
	}
	if (best.base + 2 + best.len == IP6_ADDR_LEN) {
		*p = '\0';
	} else
		p[-1] = '\0';

	return (dst);
}