📄 stafsocket.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/ #include "STAF.h"#include <fcntl.h>#include <arpa/nameser.h>#include <resolv.h>#ifdef STAF_OS_NAME_FREEBSD#include <netdb.h>#endif#include "STAFSocket.h"#include "STAF_iostream.h"#ifndef NI_MAXHOST#define NI_MAXHOST 1025#endifunsigned int STAFSocketIsValidSocket(STAFSocket_t theSocket){ return (theSocket < 0) ? 1 : 0;}STAFRC_t STAFSocketInit(STAFString_t *errorBuffer){ return kSTAFOk;}STAFRC_t STAFSocketClose(STAFSocket_t theSocket){ return (close(theSocket) == 0) ? kSTAFOk : kSTAFCommunicationError;}int STAFSocketGetLastError(){ return errno;}STAFRC_t STAFSocketGetMyHostInfo(STAFString_t *hostname, STAFString_t *ipaddr, STAFString_t *errorBuffer){ if (hostname == 0) return kSTAFInvalidParm; if (ipaddr == 0) return kSTAFInvalidParm; #ifdef STAF_USE_IPV6 char buffer1[NI_MAXHOST] = { 0 }; if (gethostname(buffer1, sizeof(buffer1)) == SOCKET_ERROR) { STAFString error = STAFString("Error getting hostname: gethostname()" "RC=") + errno; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } STAFString hostname1; hostname1 = STAFString(buffer1); //if (hostname1.count(kUTF8_PERIOD) > 2) //{ // *hostname = hostname1.adoptImpl(); // return kSTAFOk; //} struct addrinfo hints = { 0 }; hints.ai_flags = 0; // default hints.ai_family = PF_UNSPEC; // Any family is accepted hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; // Any protocol is accepted struct addrinfo *result, *ressave; int rc = getaddrinfo(buffer1, "6500", &hints, &result); if (rc != 0) { STAFString error = STAFString("Error getting address info: ") + buffer1; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } ressave = result; while (result) { if (result->ai_family == PF_INET6) break; if (result->ai_family == PF_INET) break; result = result->ai_next; } if (!result) { freeaddrinfo(ressave); STAFString error = STAFString("Error getting address info: no valid family"); if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } // Get the hostname char buffer2[NI_MAXHOST] = { 0 }; rc = getnameinfo(result->ai_addr, result->ai_addrlen, buffer2, sizeof(buffer2), NULL, 0, NI_NAMEREQD); if (rc != 0) { freeaddrinfo(ressave); STAFString error = STAFString("Error getting hostname: " "getnameinfo() RC=") + rc; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } STAFString hostname2; hostname2 = STAFString(buffer2); // Get the IP address STAFString_t ipAddress = 0; STAFString_t errorBuffer2 = 0; rc = STAFIPv6SocketGetPrintableAddressFromInAddr( result->ai_addr, result->ai_addrlen, &ipAddress, &errorBuffer2); freeaddrinfo(ressave); if (rc != kSTAFOk) { STAFString error( "Error getting printable IP address, " "STAFIPv6SocketGetPrintableAddressFromInAddr(), RC: " + STAFString(rc) + ", Info: " + STAFString(errorBuffer2, STAFString::kShallow)); if (errorBuffer) *errorBuffer = error.adoptImpl(); errorBuffer2 = 0; *ipaddr = STAFString("0.0.0.0", STAFString::kShallow).adoptImpl(); return kSTAFCommunicationError; } *ipaddr = ipAddress; #else // IPv4 char buffer[256] = { 0 }; if (gethostname(buffer, sizeof(buffer)) == SOCKET_ERROR) { STAFString error = STAFString("Error getting hostname: gethostname()" "RC=") + errno; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } struct hostent *host = gethostbyname(buffer); if (host == 0) { STAFString error = STAFString("Error getting hostent structure: " "gethostbyname() RC=") + errno; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } // Get the IP address STAFString_t ipAddress = 0; STAFString_t errorBuffer2 = 0; int rc = STAFSocketGetPrintableAddressFromInAddr( (in_addr *)host->h_addr, &ipAddress, &errorBuffer2); if (rc != kSTAFOk) { STAFString error( "Error getting printable IP address, " "STAFSocketGetPrintableAddressAFromInAddr(), RC: " + STAFString(rc) + ", Info: " + STAFString(errorBuffer2, STAFString::kShallow)); if (errorBuffer) *errorBuffer = error.adoptImpl(); errorBuffer2 = 0; *ipaddr = STAFString("0.0.0.0", STAFString::kShallow).adoptImpl(); return kSTAFCommunicationError; } *ipaddr = ipAddress; // Get the hostname STAFString hostname1(host->h_name); if (hostname1.count(kUTF8_PERIOD) > 2) { *hostname = hostname1.adoptImpl(); return kSTAFOk; } int addr = *(int *)host->h_addr; host = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); if (host == 0) { STAFString error = STAFString("Error getting hostent structure: " "gethostbyaddr() RC=") + errno; if (errorBuffer) *errorBuffer = error.adoptImpl(); return kSTAFCommunicationError; } STAFString hostname2(host->h_name);#endif if (hostname2.count(kUTF8_PERIOD) > 2) { *hostname = hostname2.adoptImpl(); return kSTAFOk; } if (hostname1.length() > hostname2.length()) *hostname = hostname1.adoptImpl(); else *hostname = hostname2.adoptImpl(); return kSTAFOk;}STAFRC_t STAFSocketGetInAddrByName(STAFStringConst_t name, in_addr *addr, STAFString_t *errorBuffer){ if (name == 0) return kSTAFInvalidParm; if (addr == 0) return kSTAFInvalidParm; STAFString theName = name;#if defined(STAF_GETHOSTBYNAME_R_6PARM) struct hostent hostent_data = { 0 }; struct hostent *hostname = &hostent_data; char hostdata_buff[2048] = { 0 }; struct hostent *hostname2 = 0; int host_error = 0; int rc = gethostbyname_r(theName.toCurrentCodePage()->buffer(), hostname, hostdata_buff, sizeof(hostdata_buff), &hostname2, &host_error); if ((rc != 0) || (hostname == NULL) || (hostname2 == NULL)) { STAFString error = ""; if (rc != 0) { error = STAFString( "Error getting hostent structure for host name: ") + theName.toCurrentCodePage()->buffer() + ", gethostbyname_r() RC=" + rc; } else if (host_error == HOST_NOT_FOUND) { error = STAFString("Unknown host name: ") +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -