1

I have a C++ code that generates an IP Packet Header. The code use a struct representing each field in the packet:

struct cip {
   uint8_t        ip_hl:4, /* both fields are 4 bytes */
                  ip_v:4;
   uint8_t        ip_tos;
   uint16_t       ip_len;
   uint16_t       ip_id;
   uint16_t       ip_off;
   uint8_t        ip_ttl;
   uint8_t        ip_p;
   uint16_t       ip_sum;
   struct in_addr ip_src;
   struct in_addr ip_dst;
   char       head[100];
};

The user is prompt an input message to enter the values for each variable in the struct:

Enter the filename to save the packet: packet

Enter IP version(0-15): 4

Enter Header Length(5-15): 5

Enter type of service(0-255): 55

Enter packet total size(bytes, 20, 200): 25

The packet is created and saved in a file:

FILE* f = fopen(file, "w");
int success = fwrite(&packet, sizeof(char), ((unsigned int)packet.ip_hl)*4,f);
if(success <= 0) {
    printf("Error writing packet header");
}
success = fwrite(&data, sizeof(char),ntohs(packet.ip_len)-(4*packet.ip_hl),f);
if(success < 0) {
    printf("Error writing packet data");
}
fflush(f);
fclose(f);
printf("\nPacket Written.\n");

I didn't create this code, someone gave me the code so I can create other program in Python that will validate the packet created by the program above. The validation includes verifying the checksum generated for the packet, the version of the Ip Packet, protocol, length of header and so on.

So I will like to know if someone can help me figuring out how can I read the file and parse the frame. I tried to read the line in the file as a string, but the problem I'm having is that the file looks like this after the creation: (it is unreadable)

O È ,@ šÀ¨À¨ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxDATA_______________________DATA____________ ô·

I don't understand why: (I'm guessing that this is because the variables bigger than 1 byte are converted to big endian by the function "htons":

printf("\nEnter ip ID number(0-65535):\n");
scanf("%d", &input);
packet.ip_id = htons(input);

I tried to search for another option as dealing this with socket.makefile(), but this will help me the socket in my program as a file, but what I need to do is parse the frame gave to me in this file.

Any ideas?

Thanks.

P.S.: Also can someone give me a link where I can find how to convert integer from big endian to small endian and vicerversa in Python. Thanks!

4

1 回答 1

1

您应该像往常一样读取文件(为 Windows 指定“二进制”模式):

with open("test.txt", 'br') as f:
    for line in f.readlines():
        # process lines

要解包二进制数据,您应该使用structpackage,它还可以处理大端和小端等。您的结构示例:

print struct.unpack('BBHHHBBH100s', line)

由于您没有指定其结构的内容,因此我省略ip_src并解包。ip_dst要读取的最小可能值是一个字节,因此要将第一个字段分成两部分,您可以使用:

(ip_hl, ip_v) = (value >> 4, value & 15)

当然,8 位组件的顺序取决于您的结构字节序。

于 2013-05-06T06:25:41.960 回答