No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DNSHeader-class.R 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. R6Class(
  2. classname = "DNSHeader",
  3. public = list(
  4. q = NULL,
  5. a = NULL,
  6. id = NULL,
  7. auth = NULL,
  8. bitmap = 0L,
  9. qr = NULL,
  10. opcode = NULL,
  11. aa = NULL,
  12. tc = NULL,
  13. rd = NULL,
  14. ra = NULL,
  15. z = NULL,
  16. ad = NULL,
  17. ar = NULL,
  18. cd = NULL,
  19. rcode = NULL,
  20. set_rd = function(val) {
  21. bits(self$bitmap, 8) <- as.integer(val)
  22. },
  23. initialize = function(id = NULL, bitmap = NULL, q = 0L,
  24. a = 0L, auth = 0L, ar = 0L, ...) {
  25. self$id <- id %||% sample(0:65535, 1)
  26. self$bitmap <- bitmap %||% 0L
  27. if (is.null(bitmap)) self$set_rd(1L)
  28. self$q <- q
  29. self$a <- a
  30. self$auth <- auth
  31. self$ar <- ar
  32. valid <- c("qr", "opcode", "aa", "tc", "rd", "ra", "z", "ad", "cd", "rcode")
  33. args <- list(...)
  34. args <- setNames(args, tolower(colnames(args)))
  35. args <- Filter(Negate(is.null), args[valid])
  36. for (n in names(args)) self[[n]] <- args[[n]]
  37. },
  38. parse = function(buf, buf_pos = 1L) {
  39. self$id <- readBin(buf[buf_pos:(buf_pos+1)], "int", size = 2, endian = "big")
  40. self$bitmap <- readBin(buf[(buf_pos+2):(buf_pos+3)], "int", size = 2, endian = "big")
  41. self$q <- readBin(buf[(buf_pos+4):(buf_pos+5)], "int", size = 2, endian = "big")
  42. self$a <- readBin(buf[(buf_pos+6):(buf_pos+7)], "int", size = 2, endian = "big")
  43. self$auth <- readBin(buf[(buf_pos+8):(buf_pos+9)], "int", size = 2, endian = "big")
  44. self$ar <- readBin(buf[(buf_pos+10):(buf_pos+11)], "int", size = 2, endian = "big")
  45. attr(self, "buflen") <- 12L
  46. self
  47. },
  48. pack = function() {
  49. c(
  50. writeBin(as.integer(self$id), raw(), size = 2, endian = "big"),
  51. writeBin(as.integer(self$bitmap), raw(), size = 2, endian = "big"),
  52. writeBin(as.integer(self$q), raw(), size = 2, endian = "big"),
  53. writeBin(as.integer(self$a), raw(), size = 2, endian = "big"),
  54. writeBin(as.integer(self$auth), raw(), size = 2, endian = "big"),
  55. writeBin(as.integer(self$ar), raw(), size = 2, endian = "big")
  56. )
  57. }
  58. )
  59. ) -> DNSHeader