icmphdr.c
上传用户:hepax88
上传日期:2007-01-03
资源大小:1101k
文件大小:2k
源码类别:

TCP/IP协议栈

开发平台:

Visual C++

  1. /* ICMP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "internet.h"
  7. #include "ip.h"
  8. #include "icmp.h"
  9. /* Generate ICMP header in network byte order, link data, compute checksum */
  10. void
  11. htonicmp(
  12. struct icmp *icmp,
  13. struct mbuf **bpp
  14. ){
  15. register uint8 *cp;
  16. uint16 checksum;
  17. pushdown(bpp,NULL,ICMPLEN);
  18. cp = (*bpp)->data;
  19. *cp++ = icmp->type;
  20. *cp++ = icmp->code;
  21. cp = put16(cp,0); /* Clear checksum */
  22. switch(icmp->type){
  23. case ICMP_DEST_UNREACH:
  24. if(icmp->code == ICMP_FRAG_NEEDED){
  25. /* Deering/Mogul max MTU indication */
  26. cp = put16(cp,0);
  27. cp = put16(cp,icmp->args.mtu);
  28. } else
  29. cp = put32(cp,0L);
  30. break;
  31. case ICMP_PARAM_PROB:
  32. *cp++ = icmp->args.pointer;
  33. *cp++ = 0;
  34. cp = put16(cp,0);
  35. break;
  36. case ICMP_REDIRECT:
  37. cp = put32(cp,icmp->args.address);
  38. break;
  39. case ICMP_ECHO:
  40. case ICMP_ECHO_REPLY:
  41. case ICMP_TIMESTAMP:
  42. case ICMP_TIME_REPLY:
  43. case ICMP_INFO_RQST:
  44. case ICMP_INFO_REPLY:
  45. cp = put16(cp,icmp->args.echo.id);
  46. cp = put16(cp,icmp->args.echo.seq);
  47. break;
  48. default:
  49. cp = put32(cp,0L);
  50. break;
  51. }
  52. /* Compute checksum, and stash result */
  53. checksum = cksum(NULL,*bpp,len_p(*bpp));
  54. cp = &(*bpp)->data[2];
  55. cp = put16(cp,checksum);
  56. }
  57. /* Pull off ICMP header */
  58. int
  59. ntohicmp(icmp,bpp)
  60. struct icmp *icmp;
  61. struct mbuf **bpp;
  62. {
  63. uint8 icmpbuf[8];
  64. if(icmp == (struct icmp *)NULL)
  65. return -1;
  66. if(pullup(bpp,icmpbuf,8) != 8)
  67. return -1;
  68. icmp->type = icmpbuf[0];
  69. icmp->code = icmpbuf[1];
  70. switch(icmp->type){
  71. case ICMP_DEST_UNREACH:
  72. /* Retrieve Deering/Mogul MTU value */
  73. if(icmp->code == ICMP_FRAG_NEEDED)
  74. icmp->args.mtu = get16(&icmpbuf[6]);
  75. break;
  76. case ICMP_PARAM_PROB:
  77. icmp->args.pointer = icmpbuf[4];
  78. break;
  79. case ICMP_REDIRECT:
  80. icmp->args.address = get32(&icmpbuf[4]);
  81. break;
  82. case ICMP_ECHO:
  83. case ICMP_ECHO_REPLY:
  84. case ICMP_TIMESTAMP:
  85. case ICMP_TIME_REPLY:
  86. case ICMP_INFO_RQST:
  87. case ICMP_INFO_REPLY:
  88. icmp->args.echo.id = get16(&icmpbuf[4]);
  89. icmp->args.echo.seq = get16(&icmpbuf[6]);
  90. break;
  91. }
  92. return 0;
  93. }