0

I'm using the pcap C library to read packets. Currently, I use the following to check and see whether a flag in the struct tcphdr (this struct is defined in the netinet/tcp.h library) is set:

struct tcphdr *tcp = ....

if(tcp->th_flags & TH_SYN) {
        //SYN FLAG IS SET?
    }

Will this always work for checking if a particular flag is set in the struct? Or is there a better way? Would greatly appreciate any advice/tips :)

  • For those confused by the `u_int16_t syn:1;` notation in the definition for `tcphdr`, see [here](https://stackoverflow.com/questions/3186008/in-c-what-does-a-colon-mean-inside-a-declaration) – Kyle Aug 20 '19 at 15:38

1 Answers1

2

That looks fine to me. TH_SYN is a single bit, so that expression will be true (nonzero) if that bit is set in th_flags.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22