udp.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 sport[2]; /* Source port */
  10. uchar dport[2]; /* Destination port */
  11. uchar len[2]; /* data length */
  12. uchar cksum[2]; /* Checksum */
  13. };
  14. enum
  15. {
  16. UDPLEN= 8,
  17. };
  18. enum
  19. {
  20. Os,
  21. Od,
  22. Osd,
  23. Osetport,
  24. };
  25. static Field p_fields[] =
  26. {
  27. {"s", Fnum, Os, "source port", } ,
  28. {"d", Fnum, Od, "dest port", } ,
  29. {"a", Fnum, Osd, "source/dest port", } ,
  30. {"sd", Fnum, Osd, "source/dest port", } ,
  31. {0}
  32. };
  33. #define ANYPORT ~0UL
  34. static Mux p_mux[] =
  35. {
  36. {"bootp", 67, },
  37. {"ninep", 6346, }, /* tvs */
  38. {"rtp", ANYPORT, },
  39. {"rtcp", ANYPORT, },
  40. {0},
  41. };
  42. /* default next protocol, can be changed by p_filter, reset by p_compile */
  43. static Proto *defproto = &dump;
  44. static void
  45. p_compile(Filter *f)
  46. {
  47. Mux *m;
  48. if(f->op == '='){
  49. compile_cmp(udp.name, f, p_fields);
  50. return;
  51. }
  52. for(m = p_mux; m->name != nil; m++)
  53. if(strcmp(f->s, m->name) == 0){
  54. f->pr = m->pr;
  55. f->ulv = m->val;
  56. f->subop = Osd;
  57. return;
  58. }
  59. sysfatal("unknown udp field or protocol: %s", f->s);
  60. }
  61. static int
  62. p_filter(Filter *f, Msg *m)
  63. {
  64. Hdr *h;
  65. if(m->pe - m->ps < UDPLEN)
  66. return 0;
  67. h = (Hdr*)m->ps;
  68. m->ps += UDPLEN;
  69. switch(f->subop){
  70. case Os:
  71. return NetS(h->sport) == f->ulv;
  72. case Od:
  73. return NetS(h->dport) == f->ulv;
  74. case Osd:
  75. if(f->ulv == ANYPORT){
  76. defproto = f->pr;
  77. return 1;
  78. }
  79. return NetS(h->sport) == f->ulv || NetS(h->dport) == f->ulv;
  80. }
  81. return 0;
  82. }
  83. static int
  84. p_seprint(Msg *m)
  85. {
  86. Hdr *h;
  87. int dport, sport;
  88. if(m->pe - m->ps < UDPLEN)
  89. return -1;
  90. h = (Hdr*)m->ps;
  91. m->ps += UDPLEN;
  92. /* next protocol */
  93. sport = NetS(h->sport);
  94. dport = NetS(h->dport);
  95. demux(p_mux, sport, dport, m, defproto);
  96. defproto = &dump;
  97. m->p = seprint(m->p, m->e, "s=%d d=%d ck=%4.4ux ln=%4d",
  98. NetS(h->sport), dport,
  99. NetS(h->cksum), NetS(h->len));
  100. return 0;
  101. }
  102. Proto udp =
  103. {
  104. "udp",
  105. p_compile,
  106. p_filter,
  107. p_seprint,
  108. p_mux,
  109. "%lud",
  110. p_fields,
  111. defaultframer,
  112. };