mongo.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/
  2. #define MONGO_OP_COMPRESSED 2012
  3. #define MONGO_OP_MSG 2013
  4. struct mongo_header {
  5. __s32 length;
  6. __s32 request_id;
  7. __s32 response_to;
  8. __s32 op_code;
  9. };
  10. static __always_inline
  11. int is_mongo_query(char *buf, __u64 buf_size) {
  12. struct mongo_header h = {};
  13. if (buf_size < sizeof(h)) {
  14. return 0;
  15. }
  16. bpf_read(buf, h);
  17. if (h.response_to == 0 && (h.op_code == MONGO_OP_MSG || h.op_code == MONGO_OP_COMPRESSED)) {
  18. return 1;
  19. }
  20. return 0;
  21. }
  22. static __always_inline
  23. int is_mongo_response(char *buf, __u64 buf_size, __u8 partial) {
  24. if (partial == 0 && buf_size == 4) { //partial read
  25. return 2;
  26. }
  27. struct mongo_header h = {};
  28. if (partial) {
  29. bpf_read(buf+4, h.response_to);
  30. bpf_read(buf+8, h.op_code);
  31. } else {
  32. bpf_read(buf, h);
  33. }
  34. if (h.response_to == 0) {
  35. return 0;
  36. }
  37. if (h.op_code == MONGO_OP_MSG || h.op_code == MONGO_OP_COMPRESSED) {
  38. return 1;
  39. }
  40. return 0;
  41. }