Guest User

Untitled

a guest
Aug 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. /**
  2.  * Helper to build an IP header given the source host address (sha), target
  3.  * host address (tha), and the desired TTL. The header is created where
  4.  * iphdr points to.
  5.  */
  6. void
  7. buildIpHeader(struct iphdr * iphdr, struct in_addr * sha, struct in_addr * tha,
  8.         u_int8_t ttl) {
  9.     /* ==================================================================== *
  10.      * YOUR TASK                                                            *
  11.      * Build an IP Header                                                   *
  12.      * ==================================================================== */
  13.      
  14.      iphdr->version = 4;
  15.      iphdr->ihl = 5;
  16.      iphdr->tos = 0;
  17.      
  18.      iphdr->tot_len = htons(28);
  19.      iphdr->id = htons(rand());
  20.      iphdr->frag_off = htons(0);
  21.      
  22.      iphdr->ttl = ttl;
  23.      iphdr->protocol = IPPROTO_ICMP;
  24.      
  25.      iphdr->saddr = sha->s_addr;
  26.      iphdr->daddr = tha->s_addr;
  27.        
  28.      iphdr->check = in_cksum( (u_int16_t *)iphdr, sizeof(struct iphdr));
  29.    
  30.     /* TASK END.
  31.      * ==================================================================== */
  32. }
  33.  
  34.  
  35. /**
  36.  * Helper to create an ICMP Echo Request (type 8, code 0) whith given Id
  37.  * and sequence number. The header is created where icmphdr points to.
  38.  */
  39. void
  40. buildIcmpEchoRequest(struct icmphdr * icmphdr, u_int16_t id, u_int16_t seq) {
  41.     /* ==================================================================== *
  42.      * YOUR TASK                                                            *
  43.      * Build an ICMP Echo Request Header.                                   *
  44.      * ==================================================================== */
  45.      
  46.      icmphdr->type = 8;
  47.      icmphdr->code = 0;
  48.      icmphdr->un.echo.id = htons(id);
  49.      icmphdr->un.echo.sequence = htons(seq);
  50.      
  51.      icmphdr->checksum = in_cksum((u_int16_t *)icmphdr, sizeof(struct icmphdr));
  52.    
  53.     /* TASK END.
  54.      * ==================================================================== */
  55. }
Add Comment
Please, Sign In to add comment