--- parser3/src/classes/inet.C 2015/08/20 22:16:47 1.9 +++ parser3/src/classes/inet.C 2021/11/05 21:42:07 1.17 @@ -1,7 +1,7 @@ /** @file 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 (http://paf.design.ru) */ @@ -10,10 +10,11 @@ #include "pa_vtable.h" #ifdef _MSC_VER +#include "winsock2.h" #include "ws2tcpip.h" #endif -volatile const char * IDENT_INET_C="$Id: inet.C,v 1.9 2015/08/20 22:16:47 moko Exp $"; +volatile const char * IDENT_INET_C="$Id: inet.C,v 1.17 2021/11/05 21:42:07 moko Exp $"; class MInet: public Methoded { public: @@ -22,7 +23,7 @@ public: // global variables -DECLARE_CLASS_VAR(inet, new MInet, 0); +DECLARE_CLASS_VAR(inet, new MInet); static void _ntoa(Request& r, MethodParams& params){ @@ -30,7 +31,7 @@ static void _ntoa(Request& r, MethodPara 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]; 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){ @@ -73,7 +74,7 @@ static void _aton(Request& r, MethodPara throw Exception(PARSER_RUNTIME, 0, "Invalid IP address '%s' specified.", ip_cstr); } else { result=(result << 8)+(ulong)byte_value; - r.write_no_lang(*new VDouble(result)); + r.write(*new VDouble(result)); } } @@ -96,12 +97,12 @@ static void _ip2name(Request& r, MethodP hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; hints.ai_flags=AI_NUMERICHOST; // to disable DNS lookup - + if(params.count() == 2) if(HashStringValue* options=params.as_hash(1)){ int valid_options=0; 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++; } if(valid_options!=options->count()) @@ -120,11 +121,12 @@ static void _ip2name(Request& r, MethodP freeaddrinfo(info); 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){ throw Exception(PARSER_RUNTIME, 0, "Can't resolve IP address '%s': %s", ip_cstr, gai_strerror(error)); } } + static void _name2ip(Request& r, MethodParams& params){ const String sname=params.as_string(0, PARAMETER_MUST_BE_STRING); if(sname.is_empty()) @@ -143,11 +145,11 @@ static void _name2ip(Request& r, MethodP if(HashStringValue* options=params.as_hash(1)){ int valid_options=0; 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++; } 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); static const String sip("ip"), sversion("version"); *columns+=&sip; *columns+=&sversion; @@ -177,17 +179,26 @@ static void _name2ip(Request& r, MethodP *row+=cur->ai_family == AF_INET ? &sv4 : cur->ai_family == AF_INET6 ? &sv6 : &sunknown; *table+=row; } else { - r.write_no_lang(*saddr); + r.write(*saddr); break; } } if(table) - r.write_no_lang(*new VTable(table)); + r.write(*new VTable(table)); freeaddrinfo(info); } +static void _hostname(Request& r, MethodParams& params){ + char buf[MAX_STRING]; + + if(gethostname(buf, MAX_STRING)) + throw Exception(PARSER_RUNTIME, 0, "Cant't get hostname"); + + r.write(*new String(pa_strdup(buf), String::L_TAINTED)); +} + // constructor MInet::MInet(): Methoded("inet") { add_native_method("ntoa", Method::CT_STATIC, _ntoa, 1, 1); @@ -196,4 +207,6 @@ MInet::MInet(): Methoded("inet") { add_native_method("ip2name", Method::CT_STATIC, _ip2name, 1, 2); // ^inet:name2ip[name; $.ipv[4|6|any] $.table(true) ] add_native_method("name2ip", Method::CT_STATIC, _name2ip, 1, 2); + // ^inet:hostname[] + add_native_method("hostname", Method::CT_STATIC, _hostname, 0, 0); }