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