Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
typedef struct _IP_HEADER_
{
   BYTE  ver_ihl;        // Version (4 bits) and Internet Header Length (4 bits)
   BYTE  type;           // Type of Service (8 bits)
   WORD  length;         // Total size of packet (header + data)(16 bits)
   WORD  packet_id;      // (16 bits)
   WORD  flags_foff;     // Flags (3 bits) and Fragment Offset (13 bits)
   BYTE  time_to_live;   // (8 bits)
   BYTE  protocol;       // (8 bits)
   WORD  hdr_chksum;     // Header check sum (16 bits)
   DWORD source_ip;      // Source Address (32 bits)
   DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;

typedef struct _TCP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   DWORD seq_number;        // Sequence Number (32 bits)
   DWORD ack_number;        // Acknowledgment Number (32 bits)
   WORD  info_ctrl;         // Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits)
   WORD  window;            // (16 bits)
   WORD  checksum;          // (16 bits)
   WORD  urgent_pointer;    // (16 bits)
} TCPHEADER;

typedef struct _ICMP_HEADER_
{
   BYTE type;               // (8 bits)  
   BYTE code;               // (8 bits)  
   WORD checksum;           // (16 bits)  
} ICMPHEADER;
Posted

Thus[^].
 
Share this answer
 
Comments
Member 7766180 24-Apr-11 13:54pm    
Like this? But what about the preceeding words, like Byte, Word and DWord?

struct udpheader {
unsigned short int uh_sport;
unsigned short int uh_dport;
unsigned short int uh_len;
unsigned short int uh_check;
}; /* total udp header length: 8 bytes (=64 bits) */
Richard MacCutchan 24-Apr-11 14:45pm    
I guess so. What do you mean "preceding words"?
Member 7766180 24-Apr-11 16:44pm    
The same way that its in the tcp and ip code that started this article. Why are they different.
Albert Holguin 24-Apr-11 22:55pm    
see my solution
Richard MacCutchan 25-Apr-11 3:50am    
What article? I am not a mind reader.
Here's a good graphical representation of what you're asking about:
http://nmap.org/book/tcpip-ref.html[^]

If I understood your follow up question to Richard correctly, the "preceeding words" are type definitions:
C++
typedef unsigned char  BYTE;  // 1byte
typedef unsigned short  WORD; // 2bytes
typedef unsigned long  DWORD; // 4bytes
 
Share this answer
 
Comments
Member 7766180 26-Apr-11 11:49am    
Thank you Albert very informative as usual! However; the scope of this is still beyond me at this point in time. Here is what I have so far....what do I need to add for the missing lines. I'm trying to compare to the sample I posted but there doesn't seem to be a common theme. And I've tried to compare it to other UDP headers written but they don't match the format of the example code.

typedef struct_UDP_HEADER_
(
) UDPHEADER;
Albert Holguin 26-Apr-11 12:52pm    
not sure I understand your follow-up question, care to elaborate?
Member 7766180 26-Apr-11 13:55pm    
I'm sorry, this is rough! Learning on the job "So to speak". I'm trying to mimic the code that I posted at the begining of this thread. I have IP, TCP and ICMP. I just want to add UDP in the same vain as the rest. Thanks
DS
Albert Holguin 26-Apr-11 13:58pm    
best way to learn... in a hurry... :) ...I think you had the right idea above when you defined the structure, you can use "unsigned short int" or use the equivalent "WORD" type for the individual items in the structure
Albert Holguin 26-Apr-11 14:04pm    
If you're having some issue understanding the typedef struct convention, see this:
http://cboard.cprogramming.com/brief-history-cprogramming-com/9524-typedef-struct-vs-struct.html

And this:
http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900