ip_protocols.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package flowclass
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "idio.link/go/parser"
  7. )
  8. /*********************** IPPDelim *************************/
  9. type fcIPPDelim struct {
  10. val *bytes.Buffer
  11. }
  12. func (t fcIPPDelim) Shift(sm *parser.SM, e rune) error {
  13. switch e {
  14. case ' ':
  15. case ':':
  16. t.val.WriteRune(e)
  17. default:
  18. if string(t.val.Bytes()) != ":" {
  19. return fmt.Errorf("ipp delimiter: unexpected input: '%s'", t.val.Bytes())
  20. }
  21. return parser.NewUnrecognized("ipp delimiter", e)
  22. }
  23. return nil
  24. }
  25. func (_ fcIPPDelim) Coalesce(_ []*parser.CSTNode) any {
  26. return nil
  27. }
  28. func (t fcIPPDelim) Value() *bytes.Buffer {
  29. return t.val
  30. }
  31. /************************* IPPs ***************************/
  32. type fcIPPs struct {
  33. }
  34. func (t fcIPPs) Shift(sm *parser.SM, e rune) error {
  35. switch {
  36. case e == ' ':
  37. case e == '{':
  38. sm.Push(fcIPPSet{})
  39. case isDigit(e) || isAlpha(e):
  40. return sm.Push(fcIPPSet{}).Shift(sm, e)
  41. default:
  42. return parser.NewUnrecognized("ipps", e)
  43. }
  44. return nil
  45. }
  46. func (t fcIPPs) Coalesce(nodes []*parser.CSTNode) any {
  47. return nodes[0].Coalesce()
  48. }
  49. func (t fcIPPs) Value() *bytes.Buffer {
  50. return nil
  51. }
  52. /************************ IPPSet **************************/
  53. type fcIPPSet struct {
  54. }
  55. func (t fcIPPSet) Shift(sm *parser.SM, e rune) error {
  56. next := fcSet[fcIPPInst, IPP]{
  57. newToken: func() fcIPPInst {
  58. return fcIPPInst{}
  59. },
  60. }
  61. if len(sm.Top().Nodes) == 0 {
  62. return sm.Push(next).Shift(sm, e)
  63. }
  64. return parser.NewUnrecognized("ipp set", e)
  65. }
  66. func (t fcIPPSet) Coalesce(nodes []*parser.CSTNode) any {
  67. return nodes[0].Coalesce()
  68. }
  69. func (t fcIPPSet) Value() *bytes.Buffer {
  70. return nil
  71. }
  72. /*********************** IPPInst **************************/
  73. type fcIPPInst struct {
  74. }
  75. func (t fcIPPInst) Shift(sm *parser.SM, e rune) error {
  76. length := len(sm.Top().Nodes)
  77. if length == 0 {
  78. return sm.Push(fcIPP{}).Shift(sm, e)
  79. }
  80. switch {
  81. case e == '(':
  82. if length > 1 {
  83. return fmt.Errorf("ipp instance: unexpected state: %+v; event %v", sm, e)
  84. }
  85. // ippinst -> ipp -> octet | ippname
  86. ippVal :=
  87. sm.Top().Nodes[0].Nodes[0].ASTNode.Value().Bytes()
  88. if bytes.Equal(ippVal, []byte("icmp")) ||
  89. bytes.Equal(ippVal, []byte("1")) {
  90. sm.Push(fcICMPParms{})
  91. }
  92. if bytes.Equal(ippVal, []byte("tcp")) ||
  93. bytes.Equal(ippVal, []byte("6")) {
  94. sm.Push(fcTCPParms{})
  95. }
  96. if bytes.Equal(ippVal, []byte("udp")) ||
  97. bytes.Equal(ippVal, []byte("17")) {
  98. sm.Push(fcUDPParms{})
  99. }
  100. default:
  101. return parser.NewUnrecognized("ipp instance", e)
  102. }
  103. return nil
  104. }
  105. func (t fcIPPInst) Coalesce(nodes []*parser.CSTNode) any {
  106. if len(nodes) == 2 {
  107. return nodes[1].Coalesce()
  108. }
  109. return nodes[0].Coalesce()
  110. }
  111. func (t fcIPPInst) Value() *bytes.Buffer {
  112. return nil
  113. }
  114. /************************* IPP ****************************/
  115. type fcIPP struct {
  116. }
  117. func (t fcIPP) Shift(sm *parser.SM, e rune) error {
  118. switch {
  119. case isDigit(e):
  120. return sm.Push(fcIPPDecimal{}).Shift(sm, e)
  121. case isAlpha(e):
  122. buf := new(bytes.Buffer)
  123. buf.WriteRune(e)
  124. sm.Push(fcIPPName{val: buf})
  125. default:
  126. return parser.NewUnrecognized("ipp", e)
  127. }
  128. return nil
  129. }
  130. func (t fcIPP) Coalesce(nodes []*parser.CSTNode) any {
  131. return nodes[0].Coalesce()
  132. }
  133. func (t fcIPP) Value() *bytes.Buffer {
  134. return nil
  135. }
  136. /*********************** IPPName **************************/
  137. var ippNameToProto = map[string]byte{
  138. "any": 0,
  139. "icmp": 1,
  140. "tcp": 6,
  141. "udp": 17,
  142. }
  143. type fcIPPName struct {
  144. val *bytes.Buffer
  145. }
  146. func (t fcIPPName) Shift(sm *parser.SM, e rune) error {
  147. switch {
  148. case isAlpha(e):
  149. t.val.WriteRune(e)
  150. default:
  151. name := string(t.val.Bytes())
  152. if _, ok := ippNameToProto[name]; !ok {
  153. return fmt.Errorf("ipp name: unknown ipp name '%s'", name)
  154. }
  155. return parser.NewUnrecognized("ipp name", e)
  156. }
  157. return nil
  158. }
  159. func (t fcIPPName) Coalesce(_ []*parser.CSTNode) any {
  160. switch string(t.val.Bytes()) {
  161. case "any":
  162. return ipProtocol(0)
  163. case "icmp":
  164. return ippICMP{}
  165. case "tcp":
  166. return ippTCP{}
  167. case "udp":
  168. return ippUDP{}
  169. default:
  170. panic("BUG: unknown ipp name")
  171. }
  172. }
  173. func (t fcIPPName) Value() *bytes.Buffer {
  174. return t.val
  175. }
  176. /********************** IPPDecimal ************************/
  177. type fcIPPDecimal struct {
  178. }
  179. func (t fcIPPDecimal) Shift(sm *parser.SM, e rune) error {
  180. if len(sm.Top().Nodes) == 0 {
  181. return sm.Push(fcOctet{}).Shift(sm, e)
  182. }
  183. return parser.NewUnrecognized("ipp decimal", e)
  184. }
  185. func (t fcIPPDecimal) Coalesce(nodes []*parser.CSTNode) any {
  186. oct := nodes[0].Coalesce().(byte)
  187. switch oct {
  188. case 1:
  189. return ippICMP{}
  190. case 6:
  191. return ippTCP{}
  192. case 17:
  193. return ippUDP{}
  194. default:
  195. return ipProtocol(oct)
  196. }
  197. }
  198. func (t fcIPPDecimal) Value() *bytes.Buffer {
  199. return nil
  200. }
  201. /********************** Parameters ************************/
  202. type fcICMPParms struct {
  203. }
  204. func (t fcICMPParms) Value() *bytes.Buffer {
  205. return nil
  206. }
  207. func (t fcICMPParms) Shift(sm *parser.SM, e rune) error {
  208. next := fcVector[fcOctet, byte]{
  209. dim: 2,
  210. newToken: func() fcOctet {
  211. return fcOctet{val: new(bytes.Buffer)}
  212. },
  213. }
  214. if len(sm.Top().Nodes) == 0 {
  215. return sm.Push(next).Shift(sm, e)
  216. }
  217. return parser.NewUnrecognized("icmp params", e)
  218. }
  219. func (t fcICMPParms) Coalesce(nodes []*parser.CSTNode) any {
  220. return ippICMP{
  221. icmpType: nodes[0].Nodes[0].Coalesce().(byte),
  222. code: nodes[0].Nodes[1].Coalesce().(byte),
  223. }
  224. }
  225. type fcTCPParms struct {
  226. }
  227. func (t fcTCPParms) Shift(sm *parser.SM, e rune) error {
  228. next := fcVector[fcPort, uint16]{
  229. dim: 2,
  230. newToken: func() fcPort {
  231. return fcPort{val: new(bytes.Buffer)}
  232. },
  233. }
  234. if len(sm.Top().Nodes) == 0 {
  235. return sm.Push(next).Shift(sm, e)
  236. }
  237. return parser.NewUnrecognized("tcp params", e)
  238. }
  239. func (t fcTCPParms) Coalesce(nodes []*parser.CSTNode) any {
  240. return ippTCP{
  241. spt: nodes[0].Nodes[0].Coalesce().(uint16),
  242. dpt: nodes[0].Nodes[1].Coalesce().(uint16),
  243. }
  244. }
  245. func (t fcTCPParms) Value() *bytes.Buffer {
  246. return nil
  247. }
  248. type fcUDPParms struct {
  249. }
  250. func (t fcUDPParms) Shift(sm *parser.SM, e rune) error {
  251. next := fcVector[fcPort, uint16]{
  252. dim: 2,
  253. newToken: func() fcPort {
  254. return fcPort{val: new(bytes.Buffer)}
  255. },
  256. }
  257. if len(sm.Top().Nodes) == 0 {
  258. return sm.Push(next).Shift(sm, e)
  259. }
  260. return parser.NewUnrecognized("udp params", e)
  261. }
  262. func (t fcUDPParms) Coalesce(nodes []*parser.CSTNode) any {
  263. return ippUDP{
  264. spt: nodes[0].Nodes[0].Coalesce().(uint16),
  265. dpt: nodes[0].Nodes[1].Coalesce().(uint16),
  266. }
  267. }
  268. func (t fcUDPParms) Value() *bytes.Buffer {
  269. return nil
  270. }
  271. type fcPort struct {
  272. val *bytes.Buffer
  273. }
  274. func (t fcPort) Shift(sm *parser.SM, e rune) error {
  275. return accNatural(e, "65535", t.val, "port")
  276. }
  277. func (t fcPort) Coalesce(_ []*parser.CSTNode) any {
  278. p, _ := strconv.Atoi(string(t.val.Bytes()))
  279. return uint16(p)
  280. }
  281. func (t fcPort) Value() *bytes.Buffer {
  282. return t.val
  283. }