ip_parser.rl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "ip_parser.h"
  2. /** Ragel machine **/
  3. %%{
  4. machine ip_parser;
  5. action is_ipv4 {
  6. ip_type = ip_type_ipv4;
  7. }
  8. action is_ipv6 {
  9. ip_type = ip_type_ipv6;
  10. }
  11. action is_ipv6_reference {
  12. ip_type = ip_type_ipv6_reference;
  13. }
  14. DIGIT = "0".."9";
  15. HEXDIG = DIGIT | "A"i | "B"i | "C"i | "D"i | "E"i | "F"i;
  16. dec_octet = DIGIT | ( 0x31..0x39 DIGIT ) | ( "1" DIGIT{2} ) |
  17. ( "2" 0x30..0x34 DIGIT ) | ( "25" 0x30..0x35 );
  18. IPv4address = dec_octet "." dec_octet "." dec_octet "." dec_octet;
  19. h16 = HEXDIG{1,4};
  20. ls32 = ( h16 ":" h16 ) | IPv4address;
  21. IPv6address = ( ( h16 ":" ){6} ls32 ) |
  22. ( "::" ( h16 ":" ){5} ls32 ) |
  23. ( h16? "::" ( h16 ":" ){4} ls32 ) |
  24. ( ( ( h16 ":" )? h16 )? "::" ( h16 ":" ){3} ls32 ) |
  25. ( ( ( h16 ":" ){,2} h16 )? "::" ( h16 ":" ){2} ls32 ) |
  26. ( ( ( h16 ":" ){,3} h16 )? "::" h16 ":" ls32 ) |
  27. ( ( ( h16 ":" ){,4} h16 )? "::" ls32 ) |
  28. ( ( ( h16 ":" ){,5} h16 )? "::" h16 ) |
  29. ( ( ( h16 ":" ){,6} h16 )? "::" );
  30. IPv6reference = "[" IPv6address "]";
  31. main := IPv4address @is_ipv4 |
  32. IPv6address @is_ipv6 |
  33. IPv6reference @is_ipv6_reference;
  34. }%%
  35. /** Data **/
  36. %% write data;
  37. /** exec **/
  38. enum enum_ip_type ip_parser_execute(const char *str, size_t len)
  39. {
  40. int cs = 0;
  41. const char *p, *pe;
  42. enum enum_ip_type ip_type = ip_type_error;
  43. p = str;
  44. pe = str+len;
  45. %% write init;
  46. %% write exec;
  47. if(len != p-str)
  48. return ip_type_error;
  49. else
  50. return ip_type;
  51. }