/*
 * author: Darren Bounds <dbounds@intrusense.com>
 * copyright: Copyright (C) 2002 by Darren Bounds
 * license: This software is under GPL version 2 of license
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * packit official page at http://packit.sourceforge.net
 */

#include "../include/packit.h"
#include "../include/capture.h"
#include "../include/error.h"

void
print_tcp_hdr(unsigned char *packet)
{
    char flags[7];

    struct libnet_tcp_hdr *tcphdr;
    struct servent *port_src, *port_dst;

#ifdef __DEBUG
    fprintf(stderr, "DEBUG: print_tcp_hdr()\n");
#endif

    bzero(&port_src, sizeof(port_src));
    bzero(&port_dst, sizeof(port_dst));

    memset(flags, 0, sizeof(flags));

    tcphdr = (struct libnet_tcp_hdr *)(packet + PACKIT_IPV4_H + header_len);

    if(tcphdr->th_flags & TH_URG)
        strcat(flags, "U");

    if(tcphdr->th_flags & TH_ACK)
        strcat(flags, "A");

    if(tcphdr->th_flags & TH_PUSH)
        strcat(flags, "P");

    if(tcphdr->th_flags & TH_RST)
        strcat(flags, "R");

    if(tcphdr->th_flags & TH_SYN)
        strcat(flags, "S");

    if(tcphdr->th_flags & TH_FIN)
        strcat(flags, "F");

    if(strlen(flags) == 0)
        strcpy(flags, "None");

    fprintf(stdout, "TCP header:  Src Port: %u  Dst Port: %u  Flag(s): %s\n",
        htons(tcphdr->th_sport),
        htons(tcphdr->th_dport),
        flags);

    fprintf(stdout, "\t     Window: %d  ", htons(tcphdr->th_win));

    if(tcphdr->th_seq > 0)
#ifdef __LINUX
        fprintf(stdout, "Seqn: %u  ", ntohl(tcphdr->th_seq));
#else
        fprintf(stdout, "Seqn: %lu  ", ntohl(tcphdr->th_seq));
#endif

    if(tcphdr->th_ack > 0)
#ifdef __LINUX
        fprintf(stdout, "Ackn: %u  ", ntohl(tcphdr->th_ack));
#else
        fprintf(stdout, "Ackn: %lu  ", ntohl(tcphdr->th_ack));
#endif

    if(tcphdr->th_urp)
        fprintf(stdout, "Urg: %d  ", ntohs(tcphdr->th_urp));

    fprintf(stdout, "\n");

    return;
}
