Hello,

In my application writen in C for linux system. I have a post hook filter which tries to print out the outgoing packets source and destination ports for TCP connections only. When I print the value of the ports I get strange values, here's the code for the post_hook:

Code:
static struct nf_hook_ops nfho_post;                 // Struct holding set of hook function options

unsigned int nf_hook_post(unsigned int hooknum,
                          struct sk_buff *skb,
                          const struct net_device *in,
                          const struct net_device *out,
                          int (*okfn) (struct sk_buff *))
{
    struct iphdr *iph;
    struct tcphdr *th;

    if (skb == NULL)
    {
        printk(KERN_INFO "%s: Socket buffer NULL.\n", __FUNCTION__);
        return NF_ACCEPT;
    }

    if(skb_is_nonlinear(skb))
        skb_linearize(skb);

    iph = ip_hdr(skb);
    if(iph == NULL)
    {
        printk("%s: IP header NULL.\n", __FUNCTION__);
        return NF_ACCEPT;
    }

    /* Handles only TCP connection: if transport protocol isn't TCP, we don't care about it. */
    if(iph->protocol != IPPROTO_TCP)
        return NF_ACCEPT;

    th = tcp_hdr(skb);
    if(th == NULL)
    {
        printk("%s: TCP header NULL.\n", __FUNCTION__);
        return NF_ACCEPT;
    }

    printk("1 Source port=%u, Destination port=%u.\n", ntohl((unsigned short int) th->source)
           ,ntohl((unsigned short int) th->dest));

    printk("2 Source port=%u, Destination port=%u.\n", htonl((unsigned short int) th->source)
           ,htonl((unsigned short int) th->dest));

    printk("3 Source port=%u, Destination port=%u.\n", (unsigned short int) th->source
           ,(unsigned short int) th->dest);

    return NF_ACCEPT;
}
I tried to print the values using different methods but until now I did not succed. So please can anyone tell me what is the problem and how to solve it?