debugpacket.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <venti.h>
  12. #include <mp.h>
  13. #include <libsec.h>
  14. #define MAGIC 0x54798314
  15. #define NOTFREE(p) assert((p)->magic == MAGIC)
  16. struct Packet
  17. {
  18. char *data;
  19. int len;
  20. void (*free)(void*);
  21. void *arg;
  22. int magic;
  23. };
  24. Packet*
  25. packetalloc(void)
  26. {
  27. Packet *p;
  28. p = vtmallocz(sizeof *p);
  29. p->free = vtfree;
  30. p->arg = nil;
  31. p->magic = MAGIC;
  32. return p;
  33. }
  34. void
  35. packetappend(Packet *p, uint8_t *buf, int n)
  36. {
  37. NOTFREE(p);
  38. if(n < 0)
  39. abort();
  40. if(p->free != vtfree)
  41. sysfatal("packetappend");
  42. p->data = vtrealloc(p->data, p->len+n);
  43. p->arg = p->data;
  44. memmove(p->data+p->len, buf, n);
  45. p->len += n;
  46. }
  47. uint
  48. packetasize(Packet *p)
  49. {
  50. NOTFREE(p);
  51. return p->len;
  52. }
  53. int
  54. packetcmp(Packet *p, Packet *q)
  55. {
  56. int i, len;
  57. NOTFREE(p);
  58. NOTFREE(q);
  59. len = p->len;
  60. if(len > q->len)
  61. len = q->len;
  62. if(len && (i=memcmp(p->data, q->data, len)) != 0)
  63. return i;
  64. if(p->len > len)
  65. return 1;
  66. if(q->len > len)
  67. return -1;
  68. return 0;
  69. }
  70. void
  71. packetconcat(Packet *p, Packet *q)
  72. {
  73. NOTFREE(p);
  74. NOTFREE(q);
  75. packetappend(p, q->data, q->len);
  76. if(q->free == vtfree)
  77. memset(q->data, 0xFE, q->len);
  78. q->free(q->arg);
  79. q->data = nil;
  80. q->len = 0;
  81. }
  82. int
  83. packetconsume(Packet *p, uint8_t *buf, int n)
  84. {
  85. NOTFREE(p);
  86. if(n < 0)
  87. abort();
  88. if(p->len < n)
  89. abort();
  90. memmove(buf, p->data, n);
  91. p->len -= n;
  92. memmove(p->data, p->data+n, p->len);
  93. return 0;
  94. }
  95. int
  96. packetcopy(Packet *p, uint8_t *buf, int offset, int n)
  97. {
  98. NOTFREE(p);
  99. if(offset < 0 || n < 0)
  100. abort();
  101. if(offset > p->len)
  102. abort();
  103. if(offset+n > p->len)
  104. n = p->len - offset;
  105. memmove(buf, p->data+offset, n);
  106. return 0;
  107. }
  108. Packet*
  109. packetdup(Packet *p, int offset, int n)
  110. {
  111. Packet *q;
  112. NOTFREE(p);
  113. if(offset < 0 || n < 0)
  114. abort();
  115. if(offset > p->len)
  116. abort();
  117. if(offset+n > p->len)
  118. n = p->len - offset;
  119. q = packetalloc();
  120. packetappend(q, p->data+offset, n);
  121. return q;
  122. }
  123. Packet*
  124. packetforeign(uint8_t *buf, int n, void (*free)(void*), void *a)
  125. {
  126. Packet *p;
  127. if(n < 0)
  128. abort();
  129. p = packetalloc();
  130. p->data = (char*)buf;
  131. p->len = n;
  132. p->free = free;
  133. p->arg = a;
  134. return p;
  135. }
  136. int
  137. packetfragments(Packet *p, IOchunk *io, int nio, int offset)
  138. {
  139. NOTFREE(p);
  140. if(offset < 0)
  141. abort();
  142. if(nio == 0)
  143. return 0;
  144. memset(io, 0, sizeof(io[0])*nio);
  145. if(offset >= p->len)
  146. return 0;
  147. io[0].addr = p->data + offset;
  148. io[0].len = p->len - offset;
  149. return p->len;
  150. }
  151. void
  152. packetfree(Packet *p)
  153. {
  154. NOTFREE(p);
  155. if(p->free == free)
  156. memset(p->data, 0xFE, p->len);
  157. p->free(p->arg);
  158. p->data = nil;
  159. p->len = 0;
  160. memset(p, 0xFB, sizeof *p);
  161. free(p);
  162. }
  163. uint8_t*
  164. packetheader(Packet *p, int n)
  165. {
  166. NOTFREE(p);
  167. if(n < 0)
  168. abort();
  169. if(n > p->len)
  170. abort();
  171. return p->data;
  172. }
  173. uint8_t*
  174. packetpeek(Packet *p, uint8_t *buf, int offset, int n)
  175. {
  176. NOTFREE(p);
  177. if(offset < 0 || n < 0)
  178. abort();
  179. if(offset+n > p->len)
  180. abort();
  181. return p->data+offset;
  182. }
  183. void
  184. packetprefix(Packet *p, uint8_t *buf, int n)
  185. {
  186. NOTFREE(p);
  187. if(n < 0)
  188. abort();
  189. if(p->free != free)
  190. sysfatal("packetappend");
  191. p->data = vtrealloc(p->data, p->len+n);
  192. p->arg = p->data;
  193. memmove(p->data+n, p->data, p->len);
  194. memmove(p->data, buf, n);
  195. p->len += n;
  196. }
  197. void
  198. packetsha1(Packet *p, uint8_t d[20])
  199. {
  200. NOTFREE(p);
  201. sha1((uint8_t*)p->data, p->len, d, nil);
  202. }
  203. uint
  204. packetsize(Packet *p)
  205. {
  206. NOTFREE(p);
  207. return p->len;
  208. }
  209. Packet*
  210. packetsplit(Packet *p, int n)
  211. {
  212. Packet *q;
  213. NOTFREE(p);
  214. q = packetalloc();
  215. q->data = vtmalloc(n);
  216. q->arg = q->data;
  217. q->free = vtfree;
  218. packetconsume(p, q->data, n);
  219. return q;
  220. }
  221. void
  222. packetstats(void)
  223. {
  224. }
  225. uint8_t*
  226. packettrailer(Packet *p, int n)
  227. {
  228. NOTFREE(p);
  229. if(n < 0)
  230. abort();
  231. if(n > p->len)
  232. abort();
  233. return p->data + p->len - n;
  234. }
  235. int
  236. packettrim(Packet *p, int offset, int n)
  237. {
  238. NOTFREE(p);
  239. if(offset < 0 || n < 0)
  240. abort();
  241. if(offset+n > p->len)
  242. abort();
  243. memmove(p->data+offset, p->data+offset+n, p->len-offset-n);
  244. p->len -= n;
  245. return 0;
  246. }