cec.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "dat.h"
  5. #include "protos.h"
  6. typedef struct{
  7. uchar type;
  8. uchar conn;
  9. uchar seq;
  10. uchar len;
  11. }Hdr;
  12. enum{
  13. Hsize = 4,
  14. };
  15. enum{
  16. Otype,
  17. Oconn,
  18. Oseq,
  19. Olen,
  20. };
  21. static Field p_fields[] =
  22. {
  23. {"type", Fnum, Otype, "type", },
  24. {"conn", Fnum, Oconn, "conn", },
  25. {"seq", Fnum, Oseq, "seq", },
  26. {"len", Fnum, Olen, "len", },
  27. {0}
  28. };
  29. static void
  30. p_compile(Filter *f)
  31. {
  32. if(f->op == '='){
  33. compile_cmp(aoe.name, f, p_fields);
  34. return;
  35. }
  36. sysfatal("unknown aoe field: %s", f->s);
  37. }
  38. static int
  39. p_filter(Filter *f, Msg *m)
  40. {
  41. Hdr *h;
  42. if(m->pe - m->ps < Hsize)
  43. return 0;
  44. h = (Hdr*)m->ps;
  45. m->ps += Hsize;
  46. switch(f->subop){
  47. case Otype:
  48. return h->type == f->ulv;
  49. case Oconn:
  50. return h->conn = f->ulv;
  51. case Oseq:
  52. return h->seq = f->ulv;
  53. case Olen:
  54. return h->len = f->ulv;
  55. }
  56. return 0;
  57. }
  58. static char* ttab[] = {
  59. "Tinita",
  60. "Tinitb",
  61. "Tinitc",
  62. "Tdata",
  63. "Tack",
  64. "Tdiscover",
  65. "Toffer",
  66. "Treset",
  67. };
  68. static int
  69. p_seprint(Msg *m)
  70. {
  71. char *s, *p, buf[4];
  72. Hdr *h;
  73. if(m->pe - m->ps < Hsize)
  74. return 0;
  75. h = (Hdr*)m->ps;
  76. m->ps += Hsize;
  77. m->pr = nil;
  78. if(h->type < nelem(ttab))
  79. s = ttab[h->type];
  80. else{
  81. snprint(buf, sizeof buf, "%d", h->type);
  82. s = buf;
  83. }
  84. p = (char*)m->ps;
  85. m->p = seprint(m->p, m->e, "type=%s conn=%d seq=%d len=%d %.*s",
  86. s, h->conn, h->seq, h->len,
  87. (int)utfnlen(p, h->len), p);
  88. return 0;
  89. }
  90. Proto cec =
  91. {
  92. "cec",
  93. p_compile,
  94. p_filter,
  95. p_seprint,
  96. nil,
  97. nil,
  98. p_fields,
  99. defaultframer,
  100. };