il.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "dat.h"
  5. #include "protos.h"
  6. typedef struct Hdr Hdr;
  7. struct Hdr
  8. {
  9. uchar sum[2]; /* Checksum including header */
  10. uchar len[2]; /* Packet length */
  11. uchar type; /* Packet type */
  12. uchar spec; /* Special */
  13. uchar sport[2]; /* Src port */
  14. uchar dport[2]; /* Dst port */
  15. uchar id[4]; /* Sequence id */
  16. uchar ack[4]; /* Acked sequence */
  17. };
  18. enum
  19. {
  20. ILLEN= 18,
  21. };
  22. enum
  23. {
  24. Os,
  25. Od,
  26. Osd,
  27. };
  28. static Field p_fields[] =
  29. {
  30. {"s", Fnum, Os, "source port", } ,
  31. {"d", Fnum, Od, "dest port", } ,
  32. {"a", Fnum, Osd, "source/dest port", } ,
  33. {"sd", Fnum, Osd, "source/dest port", } ,
  34. {0}
  35. };
  36. static Mux p_mux[] =
  37. {
  38. {"ninep", 17007, }, /* exportfs */
  39. {"ninep", 17008, }, /* 9fs */
  40. {"ninep", 17005, }, /* ocpu */
  41. {"ninep", 17010, }, /* ncpu */
  42. {"ninep", 17013, }, /* cpu */
  43. {0},
  44. };
  45. static void
  46. p_compile(Filter *f)
  47. {
  48. Mux *m;
  49. if(f->op == '='){
  50. compile_cmp(udp.name, f, p_fields);
  51. return;
  52. }
  53. for(m = p_mux; m->name != nil; m++)
  54. if(strcmp(f->s, m->name) == 0){
  55. f->pr = m->pr;
  56. f->ulv = m->val;
  57. f->subop = Osd;
  58. return;
  59. }
  60. sysfatal("unknown il field or protocol: %s", f->s);
  61. }
  62. static int
  63. p_filter(Filter *f, Msg *m)
  64. {
  65. Hdr *h;
  66. if(m->pe - m->ps < ILLEN)
  67. return 0;
  68. h = (Hdr*)m->ps;
  69. m->ps += ILLEN;
  70. switch(f->subop){
  71. case Os:
  72. return NetS(h->sport) == f->ulv;
  73. case Od:
  74. return NetS(h->dport) == f->ulv;
  75. case Osd:
  76. return NetS(h->sport) == f->ulv || NetS(h->dport) == f->ulv;
  77. }
  78. return 0;
  79. }
  80. char *pktnames[] =
  81. {
  82. "Sync",
  83. "Data",
  84. "Dataquery",
  85. "Ack",
  86. "Query",
  87. "State",
  88. "Close"
  89. };
  90. static char*
  91. pkttype(int t)
  92. {
  93. char b[10];
  94. if(t > 6){
  95. sprint(b, "%d", t);
  96. return b;
  97. }
  98. return pktnames[t];
  99. }
  100. static int
  101. p_seprint(Msg *m)
  102. {
  103. Hdr *h;
  104. int dport, sport;
  105. if(m->pe - m->ps < ILLEN)
  106. return -1;
  107. h = (Hdr*)m->ps;
  108. m->ps += ILLEN;
  109. dport = NetS(h->dport);
  110. sport = NetS(h->sport);
  111. demux(p_mux, sport, dport, m, &dump);
  112. m->p = seprint(m->p, m->e, "s=%d d=%d t=%s id=%lud ack=%lud spec=%d ck=%4.4ux ln=%d",
  113. sport, dport, pkttype(h->type),
  114. (ulong)NetL(h->id), (ulong)NetL(h->ack),
  115. h->spec,
  116. NetS(h->sum), NetS(h->len));
  117. return 0;
  118. }
  119. Proto il =
  120. {
  121. "il",
  122. p_compile,
  123. p_filter,
  124. p_seprint,
  125. p_mux,
  126. "%lud",
  127. p_fields,
  128. defaultframer,
  129. };