|
|
| version 1.17, 2021/11/05 21:42:07 | version 1.24, 2026/10/18 21:05:35 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b inet parser class. | Parser: @b inet parser class. |
| Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| Line 10 | Line 10 |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #ifdef _MSC_VER | #ifdef _MSC_VER |
| #include "winsock2.h" | #include <ws2tcpip.h> |
| #include "ws2tcpip.h" | |
| #endif | #endif |
| volatile const char * IDENT_INET_C="$Id$"; | volatile const char * IDENT_INET_C="$Id$"; |
| Line 28 DECLARE_CLASS_VAR(inet, new MInet); | Line 27 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/*zero-teminator*/+1/*for faulty snprintfs*/; | static const int ip_cstr_bufsize=3*4+3+1/*zero-teminator*/; |
| 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); | pa_snprintf(ip_cstr, ip_cstr_bufsize, "%u.%u.%u.%u", (l>>24) & 0xFF, (l>>16) & 0xFF, (l>>8) & 0xFF, l & 0xFF); |
| r.write(*new String(ip_cstr)); | r.write(*new String(ip_cstr)); |
| } | } |
| Line 127 static void _ip2name(Request& r, MethodP | Line 126 static void _ip2name(Request& r, MethodP |
| } | } |
| } | } |
| // also used in curl.C, returns first address or fills the table if not null | |
| const char *pa_name2ip(const char *name, int family, Table *table, const char *exception_type){ | |
| struct addrinfo hints, *info; | |
| memset(&hints, 0, sizeof(hints)); | |
| hints.ai_family=family; | |
| hints.ai_socktype=SOCK_STREAM; | |
| int error=getaddrinfo(name, NULL, &hints, &info); | |
| if(error) | |
| throw Exception(exception_type, 0, "Can't resolve domain name '%s': %s", name, gai_strerror(error)); | |
| const char *result=0; | |
| char hbuf[INET6_ADDRSTRLEN]; | |
| for(struct addrinfo *cur=info; cur; cur=cur->ai_next) { | |
| if(error=getnameinfo(cur->ai_addr, cur->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST)) | |
| throw Exception(exception_type, 0, "Can't translate address: %s", gai_strerror(error)); | |
| if(table){ | |
| Table::element_type row(new ArrayString()); | |
| static const String sv4("4"), sv6("6"), sunknown("unknown"); | |
| *row+=new String(pa_strdup(hbuf), String::L_TAINTED); | |
| *row+=cur->ai_family == AF_INET ? &sv4 : cur->ai_family == AF_INET6 ? &sv6 : &sunknown; | |
| *table+=row; | |
| } else { | |
| result=pa_strdup(hbuf); | |
| break; | |
| } | |
| } | |
| freeaddrinfo(info); | |
| return result; | |
| } | |
| static void _name2ip(Request& r, MethodParams& params){ | static void _name2ip(Request& r, MethodParams& params){ |
| const String sname=params.as_string(0, PARAMETER_MUST_BE_STRING); | const String sname=params.as_string(0, PARAMETER_MUST_BE_STRING); |
| if(sname.is_empty()) | if(sname.is_empty()) |
| Line 134 static void _name2ip(Request& r, MethodP | Line 167 static void _name2ip(Request& r, MethodP |
| const char* name_cstr=pa_idna_encode(sname.cstr(), r.charsets.source()); | const char* name_cstr=pa_idna_encode(sname.cstr(), r.charsets.source()); |
| struct addrinfo hints, *info; | int family=AF_INET; |
| memset(&hints, 0, sizeof(hints)); | |
| hints.ai_family=AF_INET; | |
| hints.ai_socktype=SOCK_STREAM; | |
| Table *table=NULL; | Table *table=NULL; |
| if(params.count() == 2) | if(params.count() == 2) |
| 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(*value).as_string()); | family=ipv_format(r.process(*value).as_string()); |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("table")){ | if(Value* value=options->get("table")){ |
| Line 161 static void _name2ip(Request& r, MethodP | Line 190 static void _name2ip(Request& r, MethodP |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| int error=getaddrinfo(name_cstr, NULL, &hints, &info); | if(const char *ip=pa_name2ip(name_cstr, family, table, PARSER_RUNTIME)) |
| r.write(*new String(ip, String::L_TAINTED)); | |
| if(error) | |
| throw Exception(PARSER_RUNTIME, 0, "Can't resolve domain name '%s': %s", name_cstr, gai_strerror(error)); | |
| char hbuf[INET6_ADDRSTRLEN]; | |
| for(struct addrinfo *cur=info; cur; cur=cur->ai_next) { | |
| if(error=getnameinfo(cur->ai_addr, cur->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST)) | |
| throw Exception(PARSER_RUNTIME, 0, "Can't translate address: %s", gai_strerror(error)); | |
| String *saddr=new String(pa_strdup(hbuf), String::L_TAINTED); | |
| if(table){ | |
| Table::element_type row(new ArrayString()); | |
| static const String sv4("4"), sv6("6"), sunknown("unknown"); | |
| *row+=saddr; | |
| *row+=cur->ai_family == AF_INET ? &sv4 : cur->ai_family == AF_INET6 ? &sv6 : &sunknown; | |
| *table+=row; | |
| } else { | |
| r.write(*saddr); | |
| break; | |
| } | |
| } | |
| if(table) | if(table) |
| r.write(*new VTable(table)); | r.write(*new VTable(table)); |
| freeaddrinfo(info); | |
| } | } |
| static void _hostname(Request& r, MethodParams& params){ | static void _hostname(Request& r, MethodParams&){ |
| char buf[MAX_STRING]; | char buf[MAX_STRING]; |
| if(gethostname(buf, MAX_STRING)) | if(gethostname(buf, MAX_STRING)) |