/*-
 * Copyright (c) 2001-2005 Christian S.J. Peron
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#ifdef linux
#define __USE_BSD
#endif

#ifndef lint
static const char rcsid[] = \
    "@(#) $Header: /usr/cvs/ipex/tcp_print.c,v 1.14 2005/04/11 18:52:37 modulus Exp $";
#endif

#include "ipex_includes.h"

union con_addr {
	struct in_addr  ip;
	unsigned char   p[4];
};

union endian_ {
	struct in_addr  ip;
	unsigned long   p;
};

int
generic_print( u_char *b,  struct pcap_pkthdr *h,
     u_char *p)
{
	struct ip *ip = (struct ip *)(p + opts.loffset);
	u_32_t *sa, *da;
	struct ehdr *mhdr = NULL;

	sa = (u_32_t *)&ip->ip_src;
	da = (u_32_t *)&ip->ip_dst;
	if (opts.Lflag)
		mhdr = process_link_headers(b, h, p);
	printf("%s %s%s ",
	    time_stamp(&h->ts, opts.thiszone),
	    (opts.Lflag ? mhdr->smac : "" ),
	    hostname(4, sa));
	printf("> %s%s %s [id=%u tos=0x%02x ttl=%u]\n",
	    (opts.Lflag ? mhdr->smac : "" ),
	    hostname(4, da),
	    getproto(ip->ip_p), ntohs(ip->ip_id),
	    ip->ip_tos, ip->ip_ttl);
        return (0);
}

int
print_tcp(unsigned char *data, struct pcap_pkthdr * h,
    unsigned char *p)
{
	char *d;
	int len;
	struct ip *ip = (struct ip *)(p + opts.loffset);
	unsigned ip_hl = (ip->ip_hl * 0x4);
	unsigned ip_off = ntohs(ip->ip_off);
	struct ehdr *mhdr = NULL;
	unsigned frag = ip_off & (IP_MF | IP_OFFMASK);
	unsigned frag_off = frag ? (ip_off & IP_OFFMASK) * 8 : 0;
	struct tcphdr *tcp = ((struct tcphdr *)(((char *)ip) + ip_hl));
	unsigned tcp_off = (frag ? 0 : (tcp->th_off * 4));
	u_32_t *sa, *da;
	
	d = ((char *)tcp) + tcp_off;
	len = ntohs(ip->ip_len) - ip_hl - tcp_off;
	sa = (u_32_t *)&ip->ip_src;
	da = (u_32_t *)&ip->ip_dst;
	if (opts.Lflag)
		mhdr = process_link_headers(data, h, p);
	printf("%s %s%s:%s > ",
	    time_stamp(&h->ts, opts.thiszone),
	    (opts.Lflag ? mhdr->smac : ""),
	    hostname(4, sa),
	    portname(opts.pflag, "tcp", tcp->th_sport));
	printf( "%s%s:%s tcp%s",
	    (opts.Lflag ? mhdr->dmac : ""),
	    hostname(4, da),
	    portname(opts.pflag, "tcp", tcp->th_dport), "");
	printf(" %s%s%s%s%s%s S:%u A:%u W:%u",
	    (tcp->th_flags & TH_ACK) ? "A" : "",
	    (tcp->th_flags & TH_SYN) ? "S" : "",
	    (tcp->th_flags & TH_RST) ? "R" : "",
	    (tcp->th_flags & TH_FIN) ? "F" : "",
	    (tcp->th_flags & TH_URG) ? "U" : "",
	    (tcp->th_flags & TH_PUSH) ? "P" : "",
	    ntohl(tcp->th_seq),
	    ntohl(tcp->th_ack), ntohs(tcp->th_win));
	if (frag)
		printf(" %d:%d@%d%s\n", ntohs(ip->ip_id),
		    len, frag_off, frag_off ? "" : "+");
	else
		puts(" (DF)");
	return (0);		/* not reached */
}

char *
print_addr_fmt(struct in_addr addr, unsigned int flags)
{
	union con_addr u;
	union endian_ q;
	static char buf[1000];

	u.ip = addr;
	q.ip = addr;
	snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
	    u.p[0], u.p[1], u.p[2], u.p[3]);
	return (buf);
}


syntax highlighted by Code2HTML, v. 0.9.1