WinPcapを使ってネットプログラミングを学んでいます。スニペットは次のとおりです。
int ip_hlen = (ih->ver_ihl & 0xf) * 4; /* get ip header length */
tcp_header *th = (tcp_header *) ((u_char*)ih + ip_len);
int tcp_hlen = (ntohs(th->th_len_resv_code) & 0xf000) >> 12)*4; /* get tcp header length */
問題は、なぜがnotntohs
を取得するときにのみ使用される のかということです。
確かに、パラメーターが少し説明しているように、取るだけです。http://msdn.microsoft.com/en-us/library/windows/desktop/ms740075%28v=vs.85%29.aspxtcp_hlen
ip_hlen
ntohs
u_short
ntohs を使用していないときにいつ使用するかについては、まだ戸惑っています。
以下は、IP および TCP パケット定義の構造体です。
/* ipv4 header */
typedef struct ip_header {
u_char ver_ihl; /* version and ip header length */
u_char tos; /* type of service */
u_short tlen; /* total length */
u_short identification; /* identification */
u_short flags_fo; // flags and fragment offset
u_char ttl; /* time to live */
u_char proto; /* protocol */
u_short crc; /* header checksum */
ip_address saddr; /* source address */
ip_address daddr; /* destination address */
u_int op_pad; /* option and padding */
}ip_header;
/* tcp header */
typedef struct tcp_header {
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
u_int th_seq; /* sequence number */
u_int th_ack; /* acknowledgement number */
u_short th_len_resv_code; /* datagram length and reserved code */
u_short th_window; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
}tcp_header;