title: How to read packet saved in a file with Python?
body: <p>I have a C++ code that generates an IP Packet Header. The code use a struct representing each field in the packet:</p>
  
  <pre><code>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];
  };
  </code></pre>
  
  <p>The user is prompt an input message to enter the values for each variable  in the struct:</p>
  
  <blockquote>
    <p>Enter the filename to save the packet: packet</p>
    
    <p>Enter IP version(0-15): 4</p>
    
    <p>Enter Header Length(5-15): 5</p>
    
    <p>Enter type of service(0-255): 55 </p>
    
    <p>Enter packet total size(bytes, 20, 200): 25</p>
  </blockquote>
  
  <p>The packet is created and saved in a file:</p>
  
  <pre><code>FILE* f = fopen(file, "w");
  int success = fwrite(&amp;packet, sizeof(char), ((unsigned int)packet.ip_hl)*4,f);
  if(success &lt;= 0) {
      printf("Error writing packet header");
  }
  success = fwrite(&amp;data, sizeof(char),ntohs(packet.ip_len)-(4*packet.ip_hl),f);
  if(success &lt; 0) {
      printf("Error writing packet data");
  }
  fflush(f);
  fclose(f);
  printf("\nPacket Written.\n");
  </code></pre>
  
  <p>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.</p>
  
  <p>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)</p>
  
  <blockquote>
    <p>O  È ,@ šÀ¨À¨
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxDATA_<strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em></strong><em>DATA</em><strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__</strong> ô·
  </blockquote>
  
  <p>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": </p>
  
  <pre><code>printf("\nEnter ip ID number(0-65535):\n");
  scanf("%d", &amp;input);
  packet.ip_id = htons(input);
  </code></pre>
  
  <p>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.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks.</p>
  
  <p>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!</p>