aoemd.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 res;
  8. uchar cmd;
  9. uchar ea[6];
  10. } Hdr;
  11. enum {
  12. Ocmd,
  13. Oea,
  14. Hsize = 8,
  15. };
  16. static Field p_fields[] = {
  17. {"cmd", Fnum, Ocmd, "command", },
  18. {"ea", Fnum, Oea, "ethernet addr", },
  19. nil
  20. };
  21. static void
  22. p_compile(Filter *f)
  23. {
  24. if(f->op == '='){
  25. compile_cmp(aoemd.name, f, p_fields);
  26. return;
  27. }
  28. sysfatal("unknown aoemd field: %s", f->s);
  29. }
  30. static int
  31. p_filter(Filter *f, Msg *m)
  32. {
  33. uchar buf[6];
  34. int i;
  35. Hdr *h;
  36. if(m->pe - m->ps < Hsize)
  37. return 0;
  38. h = (Hdr*)m->ps;
  39. m->ps += Hsize;
  40. switch(f->subop){
  41. case Ocmd:
  42. return h->cmd == f->ulv;
  43. case Oea:
  44. for(i = 0; i < 6; i++)
  45. buf[i] = f->ulv >> ((5 - i)*8);
  46. return memcmp(buf, h->ea, 6) == 0;
  47. }
  48. return 0;
  49. }
  50. static char *ctab[] = {
  51. " ",
  52. " +",
  53. " -",
  54. };
  55. static int
  56. p_seprint(Msg *m)
  57. {
  58. char *s;
  59. Hdr *h;
  60. if(m->pe - m->ps < Hsize)
  61. return 0;
  62. h = (Hdr*)m->ps;
  63. m->ps += Hsize;
  64. /* no next protocol */
  65. m->pr = nil;
  66. s = "unk";
  67. if(h->cmd < nelem(ctab))
  68. s = ctab[h->cmd];
  69. m->p = seprint(m->p, m->e, "cmd=%d%s ea=%E\n", h->cmd, s, h->ea);
  70. return 0;
  71. }
  72. Proto aoemd = {
  73. "aoemd",
  74. p_compile,
  75. p_filter,
  76. p_seprint,
  77. nil,
  78. nil,
  79. p_fields,
  80. defaultframer,
  81. };