bproto_addr.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. DO NOT EDIT THIS FILE!
  3. This file was automatically generated by the bproto generator.
  4. */
  5. #include <stdint.h>
  6. #include <misc/debug.h>
  7. #include <misc/byteorder.h>
  8. #include <bproto/BProto.h>
  9. #define addr_SIZEtype (sizeof(struct BProto_header_s) + sizeof(struct BProto_uint8_s))
  10. #define addr_SIZEip_port (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (2))
  11. #define addr_SIZEipv4_addr (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (4))
  12. #define addr_SIZEipv6_addr (sizeof(struct BProto_header_s) + sizeof(struct BProto_data_header_s) + (16))
  13. typedef struct {
  14. uint8_t *out;
  15. int used;
  16. int type_count;
  17. int ip_port_count;
  18. int ipv4_addr_count;
  19. int ipv6_addr_count;
  20. } addrWriter;
  21. static void addrWriter_Init (addrWriter *o, uint8_t *out);
  22. static int addrWriter_Finish (addrWriter *o);
  23. static void addrWriter_Addtype (addrWriter *o, uint8_t v);
  24. static uint8_t * addrWriter_Addip_port (addrWriter *o);
  25. static uint8_t * addrWriter_Addipv4_addr (addrWriter *o);
  26. static uint8_t * addrWriter_Addipv6_addr (addrWriter *o);
  27. typedef struct {
  28. uint8_t *buf;
  29. int buf_len;
  30. int type_start;
  31. int type_span;
  32. int type_pos;
  33. int ip_port_start;
  34. int ip_port_span;
  35. int ip_port_pos;
  36. int ipv4_addr_start;
  37. int ipv4_addr_span;
  38. int ipv4_addr_pos;
  39. int ipv6_addr_start;
  40. int ipv6_addr_span;
  41. int ipv6_addr_pos;
  42. } addrParser;
  43. static int addrParser_Init (addrParser *o, uint8_t *buf, int buf_len);
  44. static int addrParser_GotEverything (addrParser *o);
  45. static int addrParser_Gettype (addrParser *o, uint8_t *v);
  46. static void addrParser_Resettype (addrParser *o);
  47. static void addrParser_Forwardtype (addrParser *o);
  48. static int addrParser_Getip_port (addrParser *o, uint8_t **data);
  49. static void addrParser_Resetip_port (addrParser *o);
  50. static void addrParser_Forwardip_port (addrParser *o);
  51. static int addrParser_Getipv4_addr (addrParser *o, uint8_t **data);
  52. static void addrParser_Resetipv4_addr (addrParser *o);
  53. static void addrParser_Forwardipv4_addr (addrParser *o);
  54. static int addrParser_Getipv6_addr (addrParser *o, uint8_t **data);
  55. static void addrParser_Resetipv6_addr (addrParser *o);
  56. static void addrParser_Forwardipv6_addr (addrParser *o);
  57. void addrWriter_Init (addrWriter *o, uint8_t *out)
  58. {
  59. o->out = out;
  60. o->used = 0;
  61. o->type_count = 0;
  62. o->ip_port_count = 0;
  63. o->ipv4_addr_count = 0;
  64. o->ipv6_addr_count = 0;
  65. }
  66. int addrWriter_Finish (addrWriter *o)
  67. {
  68. ASSERT(o->used >= 0)
  69. ASSERT(o->type_count == 1)
  70. ASSERT(o->ip_port_count >= 0 && o->ip_port_count <= 1)
  71. ASSERT(o->ipv4_addr_count >= 0 && o->ipv4_addr_count <= 1)
  72. ASSERT(o->ipv6_addr_count >= 0 && o->ipv6_addr_count <= 1)
  73. return o->used;
  74. }
  75. void addrWriter_Addtype (addrWriter *o, uint8_t v)
  76. {
  77. ASSERT(o->used >= 0)
  78. ASSERT(o->type_count == 0)
  79. ((struct BProto_header_s *)(o->out + o->used))->id = htol16(1);
  80. ((struct BProto_header_s *)(o->out + o->used))->type = htol16(BPROTO_TYPE_UINT8);
  81. o->used += sizeof(struct BProto_header_s);
  82. ((struct BProto_uint8_s *)(o->out + o->used))->v = htol8(v);
  83. o->used += sizeof(struct BProto_uint8_s);
  84. o->type_count++;
  85. }
  86. uint8_t * addrWriter_Addip_port (addrWriter *o)
  87. {
  88. ASSERT(o->used >= 0)
  89. ASSERT(o->ip_port_count == 0)
  90. ((struct BProto_header_s *)(o->out + o->used))->id = htol16(2);
  91. ((struct BProto_header_s *)(o->out + o->used))->type = htol16(BPROTO_TYPE_CONSTDATA);
  92. o->used += sizeof(struct BProto_header_s);
  93. ((struct BProto_data_header_s *)(o->out + o->used))->len = htol32(2);
  94. o->used += sizeof(struct BProto_data_header_s);
  95. uint8_t *dest = (o->out + o->used);
  96. o->used += (2);
  97. o->ip_port_count++;
  98. return dest;
  99. }
  100. uint8_t * addrWriter_Addipv4_addr (addrWriter *o)
  101. {
  102. ASSERT(o->used >= 0)
  103. ASSERT(o->ipv4_addr_count == 0)
  104. ((struct BProto_header_s *)(o->out + o->used))->id = htol16(3);
  105. ((struct BProto_header_s *)(o->out + o->used))->type = htol16(BPROTO_TYPE_CONSTDATA);
  106. o->used += sizeof(struct BProto_header_s);
  107. ((struct BProto_data_header_s *)(o->out + o->used))->len = htol32(4);
  108. o->used += sizeof(struct BProto_data_header_s);
  109. uint8_t *dest = (o->out + o->used);
  110. o->used += (4);
  111. o->ipv4_addr_count++;
  112. return dest;
  113. }
  114. uint8_t * addrWriter_Addipv6_addr (addrWriter *o)
  115. {
  116. ASSERT(o->used >= 0)
  117. ASSERT(o->ipv6_addr_count == 0)
  118. ((struct BProto_header_s *)(o->out + o->used))->id = htol16(4);
  119. ((struct BProto_header_s *)(o->out + o->used))->type = htol16(BPROTO_TYPE_CONSTDATA);
  120. o->used += sizeof(struct BProto_header_s);
  121. ((struct BProto_data_header_s *)(o->out + o->used))->len = htol32(16);
  122. o->used += sizeof(struct BProto_data_header_s);
  123. uint8_t *dest = (o->out + o->used);
  124. o->used += (16);
  125. o->ipv6_addr_count++;
  126. return dest;
  127. }
  128. int addrParser_Init (addrParser *o, uint8_t *buf, int buf_len)
  129. {
  130. ASSERT(buf_len >= 0)
  131. o->buf = buf;
  132. o->buf_len = buf_len;
  133. o->type_start = o->buf_len;
  134. o->type_span = 0;
  135. o->type_pos = 0;
  136. o->ip_port_start = o->buf_len;
  137. o->ip_port_span = 0;
  138. o->ip_port_pos = 0;
  139. o->ipv4_addr_start = o->buf_len;
  140. o->ipv4_addr_span = 0;
  141. o->ipv4_addr_pos = 0;
  142. o->ipv6_addr_start = o->buf_len;
  143. o->ipv6_addr_span = 0;
  144. o->ipv6_addr_pos = 0;
  145. int type_count = 0;
  146. int ip_port_count = 0;
  147. int ipv4_addr_count = 0;
  148. int ipv6_addr_count = 0;
  149. int pos = 0;
  150. int left = o->buf_len;
  151. while (left > 0) {
  152. int entry_pos = pos;
  153. if (!(left >= sizeof(struct BProto_header_s))) {
  154. return 0;
  155. }
  156. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + pos);
  157. pos += sizeof(struct BProto_header_s);
  158. left -= sizeof(struct BProto_header_s);
  159. uint16_t type = ltoh16(header->type);
  160. uint16_t id = ltoh16(header->id);
  161. switch (type) {
  162. case BPROTO_TYPE_UINT8: {
  163. if (!(left >= sizeof(struct BProto_uint8_s))) {
  164. return 0;
  165. }
  166. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + pos);
  167. pos += sizeof(struct BProto_uint8_s);
  168. left -= sizeof(struct BProto_uint8_s);
  169. switch (id) {
  170. case 1:
  171. if (o->type_start == o->buf_len) {
  172. o->type_start = entry_pos;
  173. }
  174. o->type_span = pos - o->type_start;
  175. type_count++;
  176. break;
  177. default:
  178. return 0;
  179. }
  180. } break;
  181. case BPROTO_TYPE_UINT16: {
  182. if (!(left >= sizeof(struct BProto_uint16_s))) {
  183. return 0;
  184. }
  185. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + pos);
  186. pos += sizeof(struct BProto_uint16_s);
  187. left -= sizeof(struct BProto_uint16_s);
  188. switch (id) {
  189. default:
  190. return 0;
  191. }
  192. } break;
  193. case BPROTO_TYPE_UINT32: {
  194. if (!(left >= sizeof(struct BProto_uint32_s))) {
  195. return 0;
  196. }
  197. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + pos);
  198. pos += sizeof(struct BProto_uint32_s);
  199. left -= sizeof(struct BProto_uint32_s);
  200. switch (id) {
  201. default:
  202. return 0;
  203. }
  204. } break;
  205. case BPROTO_TYPE_UINT64: {
  206. if (!(left >= sizeof(struct BProto_uint64_s))) {
  207. return 0;
  208. }
  209. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + pos);
  210. pos += sizeof(struct BProto_uint64_s);
  211. left -= sizeof(struct BProto_uint64_s);
  212. switch (id) {
  213. default:
  214. return 0;
  215. }
  216. } break;
  217. case BPROTO_TYPE_DATA:
  218. case BPROTO_TYPE_CONSTDATA:
  219. {
  220. if (!(left >= sizeof(struct BProto_data_header_s))) {
  221. return 0;
  222. }
  223. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + pos);
  224. pos += sizeof(struct BProto_data_header_s);
  225. left -= sizeof(struct BProto_data_header_s);
  226. uint32_t payload_len = ltoh32(val->len);
  227. if (!(left >= payload_len)) {
  228. return 0;
  229. }
  230. pos += payload_len;
  231. left -= payload_len;
  232. switch (id) {
  233. case 2:
  234. if (!(type == BPROTO_TYPE_CONSTDATA)) {
  235. return 0;
  236. }
  237. if (!(payload_len == (2))) {
  238. return 0;
  239. }
  240. if (o->ip_port_start == o->buf_len) {
  241. o->ip_port_start = entry_pos;
  242. }
  243. o->ip_port_span = pos - o->ip_port_start;
  244. ip_port_count++;
  245. break;
  246. case 3:
  247. if (!(type == BPROTO_TYPE_CONSTDATA)) {
  248. return 0;
  249. }
  250. if (!(payload_len == (4))) {
  251. return 0;
  252. }
  253. if (o->ipv4_addr_start == o->buf_len) {
  254. o->ipv4_addr_start = entry_pos;
  255. }
  256. o->ipv4_addr_span = pos - o->ipv4_addr_start;
  257. ipv4_addr_count++;
  258. break;
  259. case 4:
  260. if (!(type == BPROTO_TYPE_CONSTDATA)) {
  261. return 0;
  262. }
  263. if (!(payload_len == (16))) {
  264. return 0;
  265. }
  266. if (o->ipv6_addr_start == o->buf_len) {
  267. o->ipv6_addr_start = entry_pos;
  268. }
  269. o->ipv6_addr_span = pos - o->ipv6_addr_start;
  270. ipv6_addr_count++;
  271. break;
  272. default:
  273. return 0;
  274. }
  275. } break;
  276. default:
  277. return 0;
  278. }
  279. }
  280. if (!(type_count == 1)) {
  281. return 0;
  282. }
  283. if (!(ip_port_count <= 1)) {
  284. return 0;
  285. }
  286. if (!(ipv4_addr_count <= 1)) {
  287. return 0;
  288. }
  289. if (!(ipv6_addr_count <= 1)) {
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. int addrParser_GotEverything (addrParser *o)
  295. {
  296. return (
  297. o->type_pos == o->type_span
  298. &&
  299. o->ip_port_pos == o->ip_port_span
  300. &&
  301. o->ipv4_addr_pos == o->ipv4_addr_span
  302. &&
  303. o->ipv6_addr_pos == o->ipv6_addr_span
  304. );
  305. }
  306. int addrParser_Gettype (addrParser *o, uint8_t *v)
  307. {
  308. ASSERT(o->type_pos >= 0)
  309. ASSERT(o->type_pos <= o->type_span)
  310. int left = o->type_span - o->type_pos;
  311. while (left > 0) {
  312. ASSERT(left >= sizeof(struct BProto_header_s))
  313. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->type_start + o->type_pos);
  314. o->type_pos += sizeof(struct BProto_header_s);
  315. left -= sizeof(struct BProto_header_s);
  316. uint16_t type = ltoh16(header->type);
  317. uint16_t id = ltoh16(header->id);
  318. switch (type) {
  319. case BPROTO_TYPE_UINT8: {
  320. ASSERT(left >= sizeof(struct BProto_uint8_s))
  321. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + o->type_start + o->type_pos);
  322. o->type_pos += sizeof(struct BProto_uint8_s);
  323. left -= sizeof(struct BProto_uint8_s);
  324. if (id == 1) {
  325. *v = ltoh8(val->v);
  326. return 1;
  327. }
  328. } break;
  329. case BPROTO_TYPE_UINT16: {
  330. ASSERT(left >= sizeof(struct BProto_uint16_s))
  331. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->type_start + o->type_pos);
  332. o->type_pos += sizeof(struct BProto_uint16_s);
  333. left -= sizeof(struct BProto_uint16_s);
  334. } break;
  335. case BPROTO_TYPE_UINT32: {
  336. ASSERT(left >= sizeof(struct BProto_uint32_s))
  337. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + o->type_start + o->type_pos);
  338. o->type_pos += sizeof(struct BProto_uint32_s);
  339. left -= sizeof(struct BProto_uint32_s);
  340. } break;
  341. case BPROTO_TYPE_UINT64: {
  342. ASSERT(left >= sizeof(struct BProto_uint64_s))
  343. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + o->type_start + o->type_pos);
  344. o->type_pos += sizeof(struct BProto_uint64_s);
  345. left -= sizeof(struct BProto_uint64_s);
  346. } break;
  347. case BPROTO_TYPE_DATA:
  348. case BPROTO_TYPE_CONSTDATA:
  349. {
  350. ASSERT(left >= sizeof(struct BProto_data_header_s))
  351. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->type_start + o->type_pos);
  352. o->type_pos += sizeof(struct BProto_data_header_s);
  353. left -= sizeof(struct BProto_data_header_s);
  354. uint32_t payload_len = ltoh32(val->len);
  355. ASSERT(left >= payload_len)
  356. uint8_t *payload = o->buf + o->type_start + o->type_pos;
  357. o->type_pos += payload_len;
  358. left -= payload_len;
  359. } break;
  360. default:
  361. ASSERT(0);
  362. }
  363. }
  364. return 0;
  365. }
  366. void addrParser_Resettype (addrParser *o)
  367. {
  368. o->type_pos = 0;
  369. }
  370. void addrParser_Forwardtype (addrParser *o)
  371. {
  372. o->type_pos = o->type_span;
  373. }
  374. int addrParser_Getip_port (addrParser *o, uint8_t **data)
  375. {
  376. ASSERT(o->ip_port_pos >= 0)
  377. ASSERT(o->ip_port_pos <= o->ip_port_span)
  378. int left = o->ip_port_span - o->ip_port_pos;
  379. while (left > 0) {
  380. ASSERT(left >= sizeof(struct BProto_header_s))
  381. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  382. o->ip_port_pos += sizeof(struct BProto_header_s);
  383. left -= sizeof(struct BProto_header_s);
  384. uint16_t type = ltoh16(header->type);
  385. uint16_t id = ltoh16(header->id);
  386. switch (type) {
  387. case BPROTO_TYPE_UINT8: {
  388. ASSERT(left >= sizeof(struct BProto_uint8_s))
  389. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  390. o->ip_port_pos += sizeof(struct BProto_uint8_s);
  391. left -= sizeof(struct BProto_uint8_s);
  392. } break;
  393. case BPROTO_TYPE_UINT16: {
  394. ASSERT(left >= sizeof(struct BProto_uint16_s))
  395. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  396. o->ip_port_pos += sizeof(struct BProto_uint16_s);
  397. left -= sizeof(struct BProto_uint16_s);
  398. } break;
  399. case BPROTO_TYPE_UINT32: {
  400. ASSERT(left >= sizeof(struct BProto_uint32_s))
  401. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  402. o->ip_port_pos += sizeof(struct BProto_uint32_s);
  403. left -= sizeof(struct BProto_uint32_s);
  404. } break;
  405. case BPROTO_TYPE_UINT64: {
  406. ASSERT(left >= sizeof(struct BProto_uint64_s))
  407. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  408. o->ip_port_pos += sizeof(struct BProto_uint64_s);
  409. left -= sizeof(struct BProto_uint64_s);
  410. } break;
  411. case BPROTO_TYPE_DATA:
  412. case BPROTO_TYPE_CONSTDATA:
  413. {
  414. ASSERT(left >= sizeof(struct BProto_data_header_s))
  415. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->ip_port_start + o->ip_port_pos);
  416. o->ip_port_pos += sizeof(struct BProto_data_header_s);
  417. left -= sizeof(struct BProto_data_header_s);
  418. uint32_t payload_len = ltoh32(val->len);
  419. ASSERT(left >= payload_len)
  420. uint8_t *payload = o->buf + o->ip_port_start + o->ip_port_pos;
  421. o->ip_port_pos += payload_len;
  422. left -= payload_len;
  423. if (type == BPROTO_TYPE_CONSTDATA && id == 2) {
  424. *data = payload;
  425. return 1;
  426. }
  427. } break;
  428. default:
  429. ASSERT(0);
  430. }
  431. }
  432. return 0;
  433. }
  434. void addrParser_Resetip_port (addrParser *o)
  435. {
  436. o->ip_port_pos = 0;
  437. }
  438. void addrParser_Forwardip_port (addrParser *o)
  439. {
  440. o->ip_port_pos = o->ip_port_span;
  441. }
  442. int addrParser_Getipv4_addr (addrParser *o, uint8_t **data)
  443. {
  444. ASSERT(o->ipv4_addr_pos >= 0)
  445. ASSERT(o->ipv4_addr_pos <= o->ipv4_addr_span)
  446. int left = o->ipv4_addr_span - o->ipv4_addr_pos;
  447. while (left > 0) {
  448. ASSERT(left >= sizeof(struct BProto_header_s))
  449. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  450. o->ipv4_addr_pos += sizeof(struct BProto_header_s);
  451. left -= sizeof(struct BProto_header_s);
  452. uint16_t type = ltoh16(header->type);
  453. uint16_t id = ltoh16(header->id);
  454. switch (type) {
  455. case BPROTO_TYPE_UINT8: {
  456. ASSERT(left >= sizeof(struct BProto_uint8_s))
  457. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  458. o->ipv4_addr_pos += sizeof(struct BProto_uint8_s);
  459. left -= sizeof(struct BProto_uint8_s);
  460. } break;
  461. case BPROTO_TYPE_UINT16: {
  462. ASSERT(left >= sizeof(struct BProto_uint16_s))
  463. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  464. o->ipv4_addr_pos += sizeof(struct BProto_uint16_s);
  465. left -= sizeof(struct BProto_uint16_s);
  466. } break;
  467. case BPROTO_TYPE_UINT32: {
  468. ASSERT(left >= sizeof(struct BProto_uint32_s))
  469. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  470. o->ipv4_addr_pos += sizeof(struct BProto_uint32_s);
  471. left -= sizeof(struct BProto_uint32_s);
  472. } break;
  473. case BPROTO_TYPE_UINT64: {
  474. ASSERT(left >= sizeof(struct BProto_uint64_s))
  475. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  476. o->ipv4_addr_pos += sizeof(struct BProto_uint64_s);
  477. left -= sizeof(struct BProto_uint64_s);
  478. } break;
  479. case BPROTO_TYPE_DATA:
  480. case BPROTO_TYPE_CONSTDATA:
  481. {
  482. ASSERT(left >= sizeof(struct BProto_data_header_s))
  483. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->ipv4_addr_start + o->ipv4_addr_pos);
  484. o->ipv4_addr_pos += sizeof(struct BProto_data_header_s);
  485. left -= sizeof(struct BProto_data_header_s);
  486. uint32_t payload_len = ltoh32(val->len);
  487. ASSERT(left >= payload_len)
  488. uint8_t *payload = o->buf + o->ipv4_addr_start + o->ipv4_addr_pos;
  489. o->ipv4_addr_pos += payload_len;
  490. left -= payload_len;
  491. if (type == BPROTO_TYPE_CONSTDATA && id == 3) {
  492. *data = payload;
  493. return 1;
  494. }
  495. } break;
  496. default:
  497. ASSERT(0);
  498. }
  499. }
  500. return 0;
  501. }
  502. void addrParser_Resetipv4_addr (addrParser *o)
  503. {
  504. o->ipv4_addr_pos = 0;
  505. }
  506. void addrParser_Forwardipv4_addr (addrParser *o)
  507. {
  508. o->ipv4_addr_pos = o->ipv4_addr_span;
  509. }
  510. int addrParser_Getipv6_addr (addrParser *o, uint8_t **data)
  511. {
  512. ASSERT(o->ipv6_addr_pos >= 0)
  513. ASSERT(o->ipv6_addr_pos <= o->ipv6_addr_span)
  514. int left = o->ipv6_addr_span - o->ipv6_addr_pos;
  515. while (left > 0) {
  516. ASSERT(left >= sizeof(struct BProto_header_s))
  517. struct BProto_header_s *header = (struct BProto_header_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  518. o->ipv6_addr_pos += sizeof(struct BProto_header_s);
  519. left -= sizeof(struct BProto_header_s);
  520. uint16_t type = ltoh16(header->type);
  521. uint16_t id = ltoh16(header->id);
  522. switch (type) {
  523. case BPROTO_TYPE_UINT8: {
  524. ASSERT(left >= sizeof(struct BProto_uint8_s))
  525. struct BProto_uint8_s *val = (struct BProto_uint8_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  526. o->ipv6_addr_pos += sizeof(struct BProto_uint8_s);
  527. left -= sizeof(struct BProto_uint8_s);
  528. } break;
  529. case BPROTO_TYPE_UINT16: {
  530. ASSERT(left >= sizeof(struct BProto_uint16_s))
  531. struct BProto_uint16_s *val = (struct BProto_uint16_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  532. o->ipv6_addr_pos += sizeof(struct BProto_uint16_s);
  533. left -= sizeof(struct BProto_uint16_s);
  534. } break;
  535. case BPROTO_TYPE_UINT32: {
  536. ASSERT(left >= sizeof(struct BProto_uint32_s))
  537. struct BProto_uint32_s *val = (struct BProto_uint32_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  538. o->ipv6_addr_pos += sizeof(struct BProto_uint32_s);
  539. left -= sizeof(struct BProto_uint32_s);
  540. } break;
  541. case BPROTO_TYPE_UINT64: {
  542. ASSERT(left >= sizeof(struct BProto_uint64_s))
  543. struct BProto_uint64_s *val = (struct BProto_uint64_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  544. o->ipv6_addr_pos += sizeof(struct BProto_uint64_s);
  545. left -= sizeof(struct BProto_uint64_s);
  546. } break;
  547. case BPROTO_TYPE_DATA:
  548. case BPROTO_TYPE_CONSTDATA:
  549. {
  550. ASSERT(left >= sizeof(struct BProto_data_header_s))
  551. struct BProto_data_header_s *val = (struct BProto_data_header_s *)(o->buf + o->ipv6_addr_start + o->ipv6_addr_pos);
  552. o->ipv6_addr_pos += sizeof(struct BProto_data_header_s);
  553. left -= sizeof(struct BProto_data_header_s);
  554. uint32_t payload_len = ltoh32(val->len);
  555. ASSERT(left >= payload_len)
  556. uint8_t *payload = o->buf + o->ipv6_addr_start + o->ipv6_addr_pos;
  557. o->ipv6_addr_pos += payload_len;
  558. left -= payload_len;
  559. if (type == BPROTO_TYPE_CONSTDATA && id == 4) {
  560. *data = payload;
  561. return 1;
  562. }
  563. } break;
  564. default:
  565. ASSERT(0);
  566. }
  567. }
  568. return 0;
  569. }
  570. void addrParser_Resetipv6_addr (addrParser *o)
  571. {
  572. o->ipv6_addr_pos = 0;
  573. }
  574. void addrParser_Forwardipv6_addr (addrParser *o)
  575. {
  576. o->ipv6_addr_pos = o->ipv6_addr_span;
  577. }