|
|
| version 1.7, 2015/05/01 21:40:09 | version 1.16, 2020/12/15 17:10:28 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b inet parser class. | Parser: @b inet parser class. |
| Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru) |
| */ | */ |
| Line 9 | Line 9 |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #ifdef _MSC_VER | |
| #include "winsock2.h" | |
| #include "ws2tcpip.h" | |
| #endif | |
| volatile const char * IDENT_INET_C="$Id$"; | volatile const char * IDENT_INET_C="$Id$"; |
| class MInet: public Methoded { | class MInet: public Methoded { |
| Line 18 public: | Line 23 public: |
| // global variables | // global variables |
| DECLARE_CLASS_VAR(inet, new MInet, 0); | DECLARE_CLASS_VAR(inet, new MInet); |
| static void _ntoa(Request& r, MethodParams& params){ | static void _ntoa(Request& r, MethodParams& params){ |
| unsigned long l=(unsigned long)trunc(params.as_double(0, "parameter must be expression", r)); | unsigned long l=(unsigned long)trunc(params.as_double(0, "parameter must be expression", r)); |
| static const int ip_cstr_bufsize=3*4+3+1+1; | static const int ip_cstr_bufsize=3*4+3+1/*zero-teminator*/+1/*for faulty snprintfs*/; |
| char* ip_cstr=new(PointerFreeGC) char[ip_cstr_bufsize]; | char* ip_cstr=new(PointerFreeGC) char[ip_cstr_bufsize]; |
| snprintf(ip_cstr, ip_cstr_bufsize, "%u.%u.%u.%u", (l>>24) & 0xFF, (l>>16) & 0xFF, (l>>8) & 0xFF, l & 0xFF); | snprintf(ip_cstr, ip_cstr_bufsize, "%u.%u.%u.%u", (l>>24) & 0xFF, (l>>16) & 0xFF, (l>>8) & 0xFF, l & 0xFF); |
| r.write_no_lang(*new String(ip_cstr)); | r.write(*new String(ip_cstr)); |
| } | } |
| static void _aton(Request& r, MethodParams& params){ | static void _aton(Request& r, MethodParams& params){ |
| Line 69 static void _aton(Request& r, MethodPara | Line 74 static void _aton(Request& r, MethodPara |
| throw Exception(PARSER_RUNTIME, 0, "Invalid IP address '%s' specified.", ip_cstr); | throw Exception(PARSER_RUNTIME, 0, "Invalid IP address '%s' specified.", ip_cstr); |
| } else { | } else { |
| result=(result << 8)+(ulong)byte_value; | result=(result << 8)+(ulong)byte_value; |
| r.write_no_lang(*new VDouble(result)); | r.write(*new VDouble(result)); |
| } | } |
| } | } |
| Line 97 static void _ip2name(Request& r, MethodP | Line 102 static void _ip2name(Request& r, MethodP |
| if(HashStringValue* options=params.as_hash(1)){ | if(HashStringValue* options=params.as_hash(1)){ |
| int valid_options=0; | int valid_options=0; |
| if(Value* value=options->get("ipv")){ | if(Value* value=options->get("ipv")){ |
| hints.ai_family=ipv_format(r.process_to_value(*value).as_string()); | hints.ai_family=ipv_format(r.process(*value).as_string()); |
| valid_options++; | valid_options++; |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| Line 116 static void _ip2name(Request& r, MethodP | Line 121 static void _ip2name(Request& r, MethodP |
| freeaddrinfo(info); | freeaddrinfo(info); |
| if(!error){ | if(!error){ |
| r.write_no_lang(*new String(pa_idna_decode(pa_strdup(hbuf), r.charsets.source()), String::L_TAINTED)); | r.write(*new String(pa_idna_decode(pa_strdup(hbuf), r.charsets.source()), String::L_TAINTED)); |
| } else if(error!=EAI_NONAME){ | } else if(error!=EAI_NONAME){ |
| throw Exception(PARSER_RUNTIME, 0, "Can't resolve IP address '%s': %s", ip_cstr, gai_strerror(error)); | throw Exception(PARSER_RUNTIME, 0, "Can't resolve IP address '%s': %s", ip_cstr, gai_strerror(error)); |
| } | } |
| Line 139 static void _name2ip(Request& r, MethodP | Line 144 static void _name2ip(Request& r, MethodP |
| if(HashStringValue* options=params.as_hash(1)){ | if(HashStringValue* options=params.as_hash(1)){ |
| int valid_options=0; | int valid_options=0; |
| if(Value* value=options->get("ipv")){ | if(Value* value=options->get("ipv")){ |
| hints.ai_family=ipv_format(r.process_to_value(*value).as_string()); | hints.ai_family=ipv_format(r.process(*value).as_string()); |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("table")){ | if(Value* value=options->get("table")){ |
| if(r.process_to_value(*value).as_bool()){ | if(r.process(*value).as_bool()){ |
| Table::columns_type columns(new ArrayString); | Table::columns_type columns(new ArrayString); |
| static const String sip("ip"), sversion("version"); | static const String sip("ip"), sversion("version"); |
| *columns+=&sip; *columns+=&sversion; | *columns+=&sip; *columns+=&sversion; |
| Line 173 static void _name2ip(Request& r, MethodP | Line 178 static void _name2ip(Request& r, MethodP |
| *row+=cur->ai_family == AF_INET ? &sv4 : cur->ai_family == AF_INET6 ? &sv6 : &sunknown; | *row+=cur->ai_family == AF_INET ? &sv4 : cur->ai_family == AF_INET6 ? &sv6 : &sunknown; |
| *table+=row; | *table+=row; |
| } else { | } else { |
| r.write_no_lang(*saddr); | r.write(*saddr); |
| break; | break; |
| } | } |
| } | } |
| if(table) | if(table) |
| r.write_no_lang(*new VTable(table)); | r.write(*new VTable(table)); |
| freeaddrinfo(info); | freeaddrinfo(info); |
| } | } |