Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to clean-up some code and I'm wondering if In could move the following into a header file, name it as such proto.h, and place the file at the start of my code with the rest of the headers. Do I have to do anything else to do this? Will it work?

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;

typedef struct _UDP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   WORD  length;            // Total size of packet (header + data)(16 bits)
   WORD  checksum;          // (16 bits)
} UDPHEADER;
Posted

I think there is no problem doing that.It will work.Its better moving declarations to header file.Also Add #pragma once at the begining of header file if you are using visual studio compiler, otherwise use macros in such a way that the header file is included only once like this,

#ifndef __PROTO_HEADER__  //__PROTO_HEADER__ Must be unique for your project
#define __PROTO_HEADER__
//Declarations
#endif /* __PROTO_HEADER__ */
 
Share this answer
 
Comments
Member 7766180 23-Jul-11 10:39am    
Did this.
#pragma once
#define BYTE
#define WORD
#define DWORD

// *** Prototypes
void translate_ip(DWORD _ip, char *_cip);
void decode_tcp(char *_packet);
void decode_icmp(char *_packet);
void decode_udp(char *_packet);
void get_this_machine_ip(char *_retIP);

// *** Defines and Typedefs
#define LS_HI_PART(x) ((x>>4) & 0x0F)
#define LS_LO_PART(x) ((x) & 0x0F)
#define LS_MAX_PACKET_SIZE 65535
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;

typedef struct _UDP_HEADER_
{
WORD source_port; // (16 bits)
WORD destination_port; // (16 bits)
WORD length; // Total size of packet (header + data)(16 bits)
WORD checksum; // (16 bits)
} UDPHEADER;

All of the words to the rigt of BYTE, WORD etc. Says No Storage Class or Type specifier.
Albert Holguin 23-Jul-11 13:35pm    
why are you defining things that are already defined in other headers? if anything is defined somewhere else, you probably shouldn't be redefining the original definitions (unless you REALLY know what you're doing)
Member 7766180 23-Jul-11 20:16pm    
Do I "REALLY" know what I'm doing? You know that I don't! But I'll get there! :)))
Albert Holguin 25-Jul-11 10:09am    
lol, well good luck in figuring everything out.. :)
Richard MacCutchan 23-Jul-11 12:37pm    
These items are defined in the windows system header files. Add #include <windows.h> to your header to ensure they are found by the compiler.
That's because you've not defined what they mean. After #define(ing) each of them you've left the rest of the line blank.

Depending on the system you're compiling on i.e 32/64 bits each of these may need to be #define(d) differently.

Perhaps something like:

#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned int

This is because whenever you #define something, then use it later it gets substituted by whatever you've #define(d) it to mean.



Imagine the following:

#define FALSE 0
#define TRUE 1


If I then write: "int myStatement = TRUE;"
This is equivalent to writing: "int myStatement = 1;"
 
Share this answer
 
If you move some code to a header file, say Proto.h, an #include "Proto.h" statement has exactly the same effect as inserting this text, at the place you include it.

You can put about anything you like in a header file. Just imagine that the #include statement is expanded there. Preprocessors are usually able to show you the resulting source code after inclusion and macro substitution, via a suitable option switch.

Obviously, the most common usage is to store declarations so that they can conveniently be repeated in any source that needs them.

Solution 1 is true, as is Solution 2. You shouldn't add extra declarations that weren't there in your original piece of code, like those #define statements, because these are defined elsewhere.

As the order of the declaration matters, so does the order of the file inclusions. It is up to you to find the appropriate place for your declarations. If you move them up, think of the effect.
 
Share this answer
 

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