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

TCP/IP协议栈

开发平台:

Visual C++

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "nr4.h"
  8. /* Convert a net/rom transport header to host format structure.
  9.  * Return -1 if error, 0 if OK.
  10.  */
  11. int
  12. ntohnr4(hdr,bpp)
  13. register struct nr4hdr *hdr;
  14. struct mbuf **bpp;
  15. {
  16. uint8 tbuf[NR4MINHDR];
  17. int i;
  18. if(pullup(bpp, tbuf, NR4MINHDR) < NR4MINHDR)
  19. return -1;
  20. hdr->opcode = tbuf[4];
  21. switch(tbuf[4] & NR4OPCODE){
  22. case NR4OPPID: /* protocol ID extension */
  23. hdr->u.pid.family = tbuf[0];
  24. hdr->u.pid.proto = tbuf[1];
  25. break;
  26. case NR4OPCONRQ: /* connect request */
  27. hdr->u.conreq.myindex = tbuf[0];
  28. hdr->u.conreq.myid = tbuf[1];
  29. if((i = PULLCHAR(bpp)) == -1)
  30. return -1;
  31. hdr->u.conreq.window = i;
  32. if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  33. return -1;
  34. if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  35. return -1;
  36. break;
  37. case NR4OPCONAK: /* connect acknowledge */
  38. hdr->yourindex = tbuf[0];
  39. hdr->yourid = tbuf[1];
  40. hdr->u.conack.myindex = tbuf[2];
  41. hdr->u.conack.myid = tbuf[3];
  42. if((i = PULLCHAR(bpp)) == -1)
  43. return -1;
  44. hdr->u.conack.window = i;
  45. break;
  46. case NR4OPDISRQ: /* disconnect request */
  47. hdr->yourindex = tbuf[0];
  48. hdr->yourid = tbuf[1];
  49. break;
  50. case NR4OPDISAK: /* disconnect acknowledge */
  51. hdr->yourindex = tbuf[0];
  52. hdr->yourid = tbuf[1];
  53. break;
  54. case NR4OPINFO: /* information frame */
  55. hdr->yourindex = tbuf[0];
  56. hdr->yourid = tbuf[1];
  57. hdr->u.info.txseq = tbuf[2];
  58. hdr->u.info.rxseq = tbuf[3];
  59. break;
  60. case NR4OPACK: /* information acknowledge */
  61. hdr->yourindex = tbuf[0];
  62. hdr->yourid = tbuf[1];
  63. hdr->u.ack.rxseq = tbuf[3];
  64. break;
  65. default: /* what kind of frame is this? */
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. /* Convert host-format level 4 header to network format */
  71. struct mbuf *
  72. htonnr4(hdr)
  73. register struct nr4hdr *hdr;
  74. {
  75. static uint16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  76. struct mbuf *rbuf;
  77. register uint8 *cp;
  78. unsigned char opcode;
  79. opcode = hdr->opcode & NR4OPCODE;
  80. if(opcode >= NR4NUMOPS)
  81. return NULL;
  82. if(hdr == (struct nr4hdr *)NULL)
  83. return NULL;
  84. if((rbuf = alloc_mbuf(hlen[opcode])) == NULL)
  85. return NULL;
  86. rbuf->cnt = hlen[opcode];
  87. cp = rbuf->data;
  88. cp[4] = hdr->opcode;
  89. switch(opcode){
  90. case NR4OPPID:
  91. *cp++ = hdr->u.pid.family;
  92. *cp = hdr->u.pid.proto;
  93. break;
  94. case NR4OPCONRQ:
  95. *cp++ = hdr->u.conreq.myindex;
  96. *cp++ = hdr->u.conreq.myid;
  97. cp += 3; /* skip to sixth byte */
  98. *cp++ = hdr->u.conreq.window;
  99. memcpy(cp,hdr->u.conreq.user,AXALEN);
  100. cp += AXALEN;
  101. memcpy(cp,hdr->u.conreq.node,AXALEN);
  102. cp += AXALEN;
  103. break;
  104. case NR4OPCONAK:
  105. *cp++ = hdr->yourindex;
  106. *cp++ = hdr->yourid;
  107. *cp++ = hdr->u.conack.myindex;
  108. *cp++ = hdr->u.conack.myid;
  109. cp++; /* already loaded pid */
  110. *cp = hdr->u.conack.window;
  111. break;
  112. case NR4OPDISRQ:
  113. *cp++ = hdr->yourindex;
  114. *cp = hdr->yourid;
  115. break;
  116. case NR4OPDISAK:
  117. *cp++ = hdr->yourindex;
  118. *cp = hdr->yourid;
  119. break;
  120. case NR4OPINFO:
  121. *cp++ = hdr->yourindex;
  122. *cp++ = hdr->yourid;
  123. *cp++ = hdr->u.info.txseq;
  124. *cp = hdr->u.info.rxseq;
  125. break;
  126. case NR4OPACK:
  127. *cp++ = hdr->yourindex;
  128. *cp++ = hdr->yourid;
  129. *++cp = hdr->u.ack.rxseq; /* skip third byte (tricky yuck) */
  130. break;
  131. }
  132. return rbuf;
  133. }