udpread.c.bad 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "udpcksum.h"
  2. struct udpiphdr *udp_check (char *, int);
  3. /*
  4. * Read from the network until a UDP datagram is read that matches
  5. * the arguments.
  6. */
  7. /* include udp_read */
  8. struct udpiphdr *
  9. udp_read (void)
  10. {
  11. int len;
  12. char *ptr;
  13. struct ether_header *eptr;
  14. for (;;)
  15. {
  16. ptr = next_pcap (&len);
  17. switch (datalink)
  18. {
  19. case DLT_NULL: /* loopback header = 4 bytes */
  20. return (udp_check (ptr + 4, len - 4));
  21. case DLT_EN10MB:
  22. eptr = (struct ether_header *) ptr;
  23. if (ntohs (eptr->ether_type) != ETHERTYPE_IP)
  24. err_quit ("Ethernet type %x not IP", ntohs (eptr->ether_type));
  25. return (udp_check (ptr + 14, len - 14));
  26. case DLT_SLIP: /* SLIP header = 24 bytes */
  27. return (udp_check (ptr + 24, len - 24));
  28. case DLT_PPP: /* PPP header = 24 bytes */
  29. return (udp_check (ptr + 24, len - 24));
  30. default:
  31. err_quit ("unsupported data link (%d)", datalink);
  32. }
  33. }
  34. }
  35. /* end udp_read */
  36. /*
  37. * Check the received packet.
  38. * If UDP and OK, return pointer to packet.
  39. * If ICMP error, return NULL.
  40. * We assume the filter picks out desired UDP datagrams.
  41. */
  42. /* include udp_check */
  43. struct udpiphdr *
  44. udp_check (char *ptr, int len)
  45. {
  46. int hlen;
  47. struct ip *ip;
  48. struct udpiphdr *ui;
  49. if (len < sizeof (struct ip) + sizeof (struct udphdr))
  50. err_quit ("len = %d", len);
  51. /* minimal verification of IP header */
  52. ip = (struct ip *) ptr;
  53. if (ip->ip_v != IPVERSION)
  54. err_quit ("ip_v = %d", ip->ip_v);
  55. hlen = ip->ip_hl << 2;
  56. if (hlen < sizeof (struct ip))
  57. err_quit ("ip_hl = %d", ip->ip_hl);
  58. if (len < hlen + sizeof (struct udphdr))
  59. err_quit ("len = %d, hlen = %d", len, hlen);
  60. if ((ip->ip_sum = in_cksum ((u_short *) ip, hlen)) != 0)
  61. err_quit ("ip checksum error");
  62. if (ip->ip_p == IPPROTO_UDP)
  63. {
  64. ui = (struct udpiphdr *) ip;
  65. return (ui);
  66. }
  67. else
  68. err_quit ("not a UDP packet");
  69. }
  70. /* end udp_check */