Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,444 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,296 people online right now. Registration is fast and FREE... Join Now!




An ip packet structure

 
Reply to this topicStart new topic

An ip packet structure

aviator
1 Dec, 2007 - 07:24 AM
Post #1

New D.I.C Head
*

Joined: 31 Oct, 2007
Posts: 30


My Contributions
I try to solve this but again some errors came out!!

Can anyone help me?

CODE


#include<stdio.h>
#include<netinet/in.h>

#define MACLENGTH 6

int main(void)

{

typedef struct {
unsigned int ihl:4;
unsigned int ver:4;
unsigned char tos;
unsigned int length;
unsigned int identification;
unsigned int flag_offset;
unsigned char ttl;
unsigned char protocol;
unsigned int checksum;
unsigned long int ucSource[MACLENGTH];
unsigned long int ucDestination[MACLENGTH];
}packet;

unsigned char ucSingle;
int iLoop,iVal;
char *ptr;

packet frame;

printf("Creating destination MAC address\n");
for(iLoop=0;iLoop<MACLENGTH;iLoop++)
{
printf("\nOctet%d=",iLoop);
scanf("%d", &iVal);
frame.ucDestination[iLoop] = (unsigned char)iVal;
}
printf("Enter the the type of length field value \n");
scanf("%d",&frame.length);

{
packet.ver = 4;
packet.ihl = 5;
packet.tos = 0;
packet.length = htons(0x4321);
packet.identification = htons(0x9876);
packet.flag_offset = 0;
packet.ttl = 64;
packet.protocol = 6;
packet.checksum = htons(0x1111);
packet.source = htonl(0xc0a8fa03);
packet.destination = htonl(0xc0a8faa8);
}
ptr = (char*) &frame;
printf("frame complete\n");
for(iLoop=0;iLoop<14;iLoop++)
{
ucSingle = (unsigned char)ptr[iLoop];
printf("[%d]",(int)ucSingle);
}
ptr = (char*)&packet;
printf("packet complete\n");
for(iLoop=0;iLoop<20;iLoop++)
{
ucSingle = (unsigned char)ptr[iLoop];
printf("[%d]",(int)ucSingle);
}
return 0;
}



-------------------------------------------------------

What are the errors?
-------------------------------------------------------

Sorry that i didn't post the errors!!!


cc: Error: program3.c, line 41: Invalid declarator. (declarator)
packet.ver = 4;
------^
cc: Error: program3.c, line 42: Invalid declarator. (declarator)
packet.ihl = 5;
------^
cc: Error: program3.c, line 43: Invalid declarator. (declarator)
packet.tos = 0;
------^
cc: Error: program3.c, line 44: Invalid declarator. (declarator)
packet.length = htons(0x4321);
------^
cc: Error: program3.c, line 45: Invalid declarator. (declarator)
packet.identification = htons(0x9876);
------^
cc: Error: program3.c, line 46: Invalid declarator. (declarator)
packet.flag_offset = 0;
------^
cc: Error: program3.c, line 47: Invalid declarator. (declarator)
packet.ttl = 64;
------^
cc: Error: program3.c, line 48: Invalid declarator. (declarator)
packet.protocol = 6;
------^
cc: Error: program3.c, line 49: Invalid declarator. (declarator)
packet.checksum = htons(0x1111);
------^
cc: Error: program3.c, line 50: Invalid declarator. (declarator)
packet.source = htonl(0xc0a8fa03);
------^
cc: Error: program3.c, line 51: Invalid declarator. (declarator)
packet.destination = htonl(0xc0a8faa8);
------^
cc: Error: program3.c, line 60: In this statement, "packet" is declared as a typedef, and so cannot occur as an expression. (typeexpr)
ptr = (char*)&packet;
--------------^


CODE


packet frame;



The variable that you have declared is of type packet and the name is frame. Not the opposite.
Simply change the variable name where is indicated from packet.* to frame.*
Hope this help you

-----------------------------------------------------------------------------------------

Hi..Thanks for the reply but I'm afraid that didn't solve the errors....

Can you or someone else give me specific information about that?

Thankss
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: An Ip Packet Structure
2 Dec, 2007 - 11:22 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
The problem with the code is stated above so I will assume that you need help understanding the problem.

First off lets talk about style:
There are a number of stylistic choices that programmers have made over the years that have become rather standard. The reason is that they have proven to be "best practices". By adopting some of these style habits we can correct and avoid many of the problems in your code.

Normally the typedef would not be done inside of the main() function. The reason is that if you define it inside of main() then it can only be used inside of main() and this limits your ability to create modular programs. But if the typedef is placed before the main() function (or in an include file) then it can be used throughout the file.

Additionally, we normally give typedef aliases names that begin with a capital (as apposed to variables which we always begin with a lower case letter). This makes the beginning of your program look like this:

CODE
#include<stdio.h>
#include<netinet/in.h>

#define MACLENGTH 6

typedef struct {
    unsigned short int ihl:4;
    unsigned short int ver:4;
    unsigned char tos;
    unsigned short int length;
    unsigned short int identification;
    unsigned short int flag_offset;
    unsigned char ttl;
    unsigned char protocol;
    unsigned int checksum;
    unsigned long int ucSource;
    unsigned long int ucDestination;
} Packet;


int main(void) {
    unsigned char ucSingle;
    int iLoop, iVal;
    char *ptr;
    Packet frame;
    
    //TODO: ADD YOUR CODE HERE
    
    return 0;
}


Note that 'packet' has become 'Packet'. Also note that I have used indentation between brackets '{ }' so that the blocks of code stand out on their own. This will help us keep track of opening and closing of brackets, but it will also help our eyes catch blocks of code.

Now you seem to use two variables of type 'Packet' in your program, one is named 'frame', the other named 'packet'. So I defined a second variable.

The next thing I noted is that you have an independant block... This is a little odd (there were popular once for controlling scope but I never noted them much in C). You don't use the block for any particular purpose, so I got rid of it, in the process I discovered a problem with your IP Packet structure...

First of all you may wish to look at the definition of the structure. Note that the last two arrays are not part of the structure, they should be 32 bit integers.

Unfortunately C has these wonderful data types, but they don't DEFINE the size of the types. We more or less assume that 1 byte is 8 bits, and a short int is 16, a long int 32... but this is NOT always the case. If you look in ip.h you will see that they define the packet structure thus:
QUOTE
CODE
struct ip
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ip_hl:4;        /* header length */
    unsigned int ip_v:4;        /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
    unsigned int ip_v:4;        /* version */
    unsigned int ip_hl:4;        /* header length */
#endif
    u_int8_t ip_tos;            /* type of service */
    u_short ip_len;            /* total length */
    u_short ip_id;            /* identification */
    u_short ip_off;            /* fragment offset field */
#define    IP_RF 0x8000            /* reserved fragment flag */
#define    IP_DF 0x4000            /* dont fragment flag */
#define    IP_MF 0x2000            /* more fragments flag */
#define    IP_OFFMASK 0x1fff        /* mask for fragmenting bits */
    u_int8_t ip_ttl;            /* time to live */
    u_int8_t ip_p;            /* protocol */
    u_short ip_sum;            /* checksum */
    struct in_addr ip_src, ip_dst;    /* source and dest address */
  };
This file is part of the GNU C Library.
Come to think of it, why arn't you using the ip packet defined in ip.h?

anyway here is what I came up with after fixing all the syntax errors I could find (note that I needed to use the windows lib rather than the unix one):

CODE
#include<stdio.h>
//#include<netinet/in.h>
#include <winsock2.h>

#define MACLENGTH 6

typedef struct {
    unsigned char ihl:4;
    unsigned char ver:4;
    unsigned char tos;
    unsigned short int length;
    unsigned short int identification;
    unsigned short int flag_offset;
    unsigned char ttl;
    unsigned char protocol;
    unsigned short int checksum;
    unsigned long int ucSource;
    unsigned long int ucDestination;
} Packet;


int main(void) {
    unsigned char ucSingle;
    int iLoop,iVal;
    char *ptr;
    Packet frame;
    Packet packet;

    printf("The size: %d\n", sizeof(Packet));
    //TODO: ADD YOUR CODE HERE

    printf("Creating destination MAC address\n");
    ptr = (char *)frame.ucDestination;
    for(iLoop=0;iLoop<MACLENGTH;iLoop++) {
        printf("\nOctet%d=",iLoop);
        scanf("%d", &iVal);
        ptr[iLoop] = (unsigned char)iVal;
    }
    
    printf("Enter the the type of length field value \n");
    scanf("%d",&frame.length);
    
    packet.ver = 4;
    packet.ihl = 5;
    packet.tos = 0;
    packet.length = htons(0x4321);
    packet.identification = htons(0x9876);
    packet.flag_offset = 0;
    packet.ttl = 64;
    packet.protocol = 6;
    packet.checksum = htons(0x1111);
    packet.ucSource = htonl(0xc0a8fa03);
    packet.ucDestination = htonl(0xc0a8faa8);
    
    ptr = (char*) &frame;
    printf("frame complete\n");
    for(iLoop=0;iLoop<14;iLoop++) {
        ucSingle = (unsigned char)ptr[iLoop];
        printf("[%d]",(int)ucSingle);
    }
    
    ptr = (char*)&packet;
    printf("packet complete\n");
    
    for(iLoop=0;iLoop<20;iLoop++) {
        ucSingle = (unsigned char)ptr[iLoop];
        printf("[%d]",(int)ucSingle);
    }

    return 0;
}



User is offlineProfile CardPM
+Quote Post

aviator
RE: An Ip Packet Structure
2 Dec, 2007 - 05:17 PM
Post #3

New D.I.C Head
*

Joined: 31 Oct, 2007
Posts: 30


My Contributions
Thanks very much NickDMax for this all usefull information you posted!!

I look again to it but I have to modified the program this way!!

CODE

#include<stdio.h>
#include<netinet/in.h>

#define MACLENGTH 6

typedef struct {
unsigned char ihl:4;
unsigned char ver:4;
unsigned char tos;
unsigned short int length;
unsigned short int identification;
unsigned short int flag_offset;
unsigned char ttl;
unsigned char protocol;
unsigned short int checksum;
unsigned long int ucSource;
unsigned long int ucDestination;
}Packet;

int main(void){
unsigned char ucSingle;
int iLoop,iVal;
char *ptr;
Packet frame;
Packet packet;

printf("Creating destination MAC address\n");
for(iLoop=0;iLoop<MACLENGTH;iLoop++)
frame.ucDestination[iLoop] = (unsigned char)iLoop;

packet.ver = 4;
packet.ihl = 5;
packet.tos = 0;
packet.length = htons(0x4321);
packet.identification = htons(0x9876);
packet.flag_offset = 0;
packet.ttl = 64;
packet.protocol = 6;
packet.checksum = htons(0x1111);
packet.ucSource= htonl(0xc0a8fa03);
packet.ucDestination = htonl(0xc0a8faa8);

ptr =(char*) &frame;
printf("frame complete\n");
for(iLoop=0;iLoop<14;iLoop++)
{
ucSingle = (unsigned char)ptr[iLoop];
printf("%021X",(unsigned char)ucSingle);
}
ptr =(char*) &packet;
printf("packet complete\n");
for(iLoop=0;iLoop<20;iLoop++)
{
ucSingle = (unsigned char)ptr[iLoop];
printf("%021X",(unsigned char)ucSingle);
}
return 0;
}


Can you look it again for me pls?

I compile it and it cames with one error only this time..!

cc: Error: program3.c, line 29: In this statement, "frame.ucDestination" has an unsigned long type, but occurs in a context that requires a pointer. (needpointer)
frame.ucDestination[iLoop] = (unsigned char)iLoop;
^

Thanks!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: An Ip Packet Structure
2 Dec, 2007 - 06:03 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I think what you were trying to do was something like this:
CODE
    printf("Creating destination MAC address\n");
    ptr = (char *)frame.ucDestination;
    for(iLoop=0;iLoop<MACLENGTH;iLoop++) {
        printf("\nOctet%d=",iLoop);
        scanf("%d", &iVal);
        ptr[iLoop] = (unsigned char)iVal;
    }


note that Packet::ucDestination is not an array... but we want to cheat a little and put data into it byte-by-byte... so we are going to use a char pointer to point to its address. Note normally this is NOT the best way to do this. One thing that comes to mind is Big Endian vs Little Endian, because of how the computers will store data in memory (some computers put the low byte at a lower address then the high byte, and some computers the other way so the low byte has a higher address than the high byte).

So I would say that you need to keep that in mind.
User is offlineProfile CardPM
+Quote Post

aviator
RE: An Ip Packet Structure
3 Dec, 2007 - 07:44 AM
Post #5

New D.I.C Head
*

Joined: 31 Oct, 2007
Posts: 30


My Contributions
Hi again!

When I compile it it prints only

Octet0=

and then Segmentation fault!

I don't know why!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: An Ip Packet Structure
4 Dec, 2007 - 10:35 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
You new topic was deleted. Please do not post duplicate topics... esp. when you revert back to the original post.

If you are getting a segmentation fault then you are misusing pointers. Think carefully about what the pointers are pointing to. If you are not good at thinking carefully about pointers try using a debugger and watching the variables involved.

I will try to find time to look at it again. Though I have to tell you, seeing you post the same old code over again really made me not want to help you. You don't seem to be learning anything. It is not my job to fix your code, I don't mind helping you learn, but I get payed pretty darn good as a programmer. -- the errors we fixed in this code were very similar to your last post about Ethernet frame structure. You made the same mistakes in your IP packet program... you are not learning.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 02:17PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month