⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafsocket.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                theName.toCurrentCodePage()->buffer() +                ", gethostbyname_r()";        }        else        {            error = STAFString(                "Error getting hostent structure for host name: ") +                theName.toCurrentCodePage()->buffer() +                ", gethostbyname_r() RC=" + host_error;        }        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_GETHOSTBYNAME_R_5PARM)    struct hostent hostent_data = { 0 };    struct hostent *hostname = &hostent_data;    char hostdata_buff[2048] = { 0 };    int host_error = 0;    gethostbyname_r(theName.toCurrentCodePage()->buffer(), hostname,                    hostdata_buff, sizeof(hostdata_buff), &host_error);    if (host_error != 0)    {        STAFString error = "";        if (host_error == HOST_NOT_FOUND)        {            error = STAFString("Unknown host name: ") +                theName.toCurrentCodePage()->buffer() +                ", gethostbyname_r()";        }        else        {            error = STAFString(                "Error getting hostent structure for host name: ") +                theName.toCurrentCodePage()->buffer() +                ", gethostbyname_r() RC=" + host_error;        }                if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_GETHOSTBYNAME_R_3PARM)    struct hostent_data hostname_hostent_data = { 0 };    struct hostent hostname_data = { 0 };    struct hostent *hostname = &hostname_data;    int rc = gethostbyname_r(theName.toCurrentCodePage()->buffer(), hostname,                             &hostname_hostent_data);        if (rc != 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyname_r() RC=") + rc;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_NO_GETHOSTBYNAME_R)    struct hostent *hostname =                    gethostbyname(theName.toCurrentCodePage()->buffer());    if (hostname == 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyname() RC=") + h_errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#else#error Undefined format for thread-safe gethostbyname()#endif    addr->s_addr = *reinterpret_cast<unsigned int *>(hostname->h_addr);    return kSTAFOk;}STAFRC_t STAFIPv6SocketGetNameByInAddr(sockaddr *addr, int addrlen,                                   STAFString_t *name,                                   STAFString_t *errorBuffer){#ifdef STAF_USE_IPV6    if (addr == 0) return kSTAFInvalidParm;    if (name == 0) return kSTAFInvalidParm;    char theHost[1025] = "";    int rc = getnameinfo(addr, addrlen, theHost, sizeof(theHost),                          NULL, 0, NI_NAMEREQD);    if (rc != 0)    {        STAFString error = STAFString("Error getting hostname: "                                      "getnameinfo() RC=") + rc;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }    *name = STAFString(theHost).adoptImpl();#endif    return kSTAFOk;}STAFRC_t STAFSocketGetNameByInAddr(in_addr *addr, STAFString_t *name,                                   STAFString_t *errorBuffer){    if (addr == 0) return kSTAFInvalidParm;    if (name == 0) return kSTAFInvalidParm;#if defined(STAF_GETHOSTBYADDR_R_8PARM)    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 = gethostbyaddr_r(reinterpret_cast<char *>(&addr->s_addr),    int rc = gethostbyaddr_r(&addr->s_addr,                             sizeof(addr->s_addr), AF_INET,                             hostname, hostdata_buff, sizeof(hostdata_buff),                             &hostname2, &host_error);    if (host_error != 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyaddr_r() RC=") + host_error;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_GETHOSTBYADDR_R_7PARM)    struct hostent hostent_data = { 0 };    struct hostent *hostname = &hostent_data;    char hostdata_buff[2048] = { 0 };    int host_error = 0;    struct hostent *hostname2 =         gethostbyaddr_r(reinterpret_cast<char *>(&addr->s_addr),                        sizeof(addr->s_addr), AF_INET, hostname,                         hostdata_buff, sizeof(hostdata_buff), &host_error);    if (host_error != 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyaddr_r() RC=") + host_error;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_GETHOSTBYADDR_R_5PARM)    struct hostent_data hostname_hostent_data = { 0 };    struct hostent hostname_data = { 0 };    struct hostent *hostname = &hostname_data;    int rc = gethostbyaddr_r(reinterpret_cast<char *>(&addr->s_addr),                             sizeof(addr->s_addr), AF_INET,                             hostname, &hostname_hostent_data);    if (rc != 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyaddr_r() RC=") + rc;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#elif defined(STAF_NO_GETHOSTBYADDR_R)    struct hostent *hostname =         gethostbyaddr(reinterpret_cast<char *>(&addr->s_addr),                       sizeof(addr->s_addr),                      AF_INET);                          if (hostname == 0)    {        STAFString error = STAFString("Error getting hostent structure: "                                      "gethostbyaddr() RC=") + h_errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }#else#error Undefined format for thread-safe gethostbyaddr()#endif    *name = STAFString(hostname->h_name).adoptImpl();    return kSTAFOk;}STAFRC_t STAFIPv6SocketGetPrintableAddressFromInAddr(struct sockaddr *addr,                                                      int addrlen,                                                     STAFString_t *ipaddr,                                                     STAFString_t *errorBuffer){#ifdef STAF_USE_IPV6    if (addr == 0)   return kSTAFInvalidParm;    if (ipaddr == 0) return kSTAFInvalidParm;    STAFRC_t rc = STAFSocketInit(errorBuffer);    if (rc != kSTAFOk) return rc;    char theAddr[256] = "";    rc = getnameinfo(addr, addrlen, theAddr, sizeof(theAddr),                          NULL, 0, NI_NUMERICHOST);    if (rc != 0)    {        STAFString error = STAFString("Error getting printable IP address: "                                      "getnameinfo() RC=") + errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }    *ipaddr = STAFString(theAddr).adoptImpl();#endif    return kSTAFOk;}STAFRC_t STAFSocketGetPrintableAddressFromInAddr(in_addr *addr,                                                 STAFString_t *ipaddr,                                                 STAFString_t *errorBuffer){    if (addr == 0)   return kSTAFInvalidParm;    if (ipaddr == 0) return kSTAFInvalidParm;    STAFRC_t rc = STAFSocketInit(errorBuffer);    if (rc != kSTAFOk) return rc;    char addrBuffer[32] = { 0 };    const char *theAddr = inet_ntop(AF_INET, addr, addrBuffer,                                    sizeof(addrBuffer));    if (theAddr == 0)    {        STAFString error = STAFString("Error getting printable IP address: "                                      "inet_ntop() RC=") + errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }    *ipaddr = STAFString(theAddr).adoptImpl();    return kSTAFOk;}STAFRC_t STAFSocketSetBlockingMode(STAFSocket_t theSocket,                                   STAFSocketBlockingMode_t blockingMode,                                   STAFString_t *errorBuffer){    int fd_flags = fcntl(theSocket, F_GETFL, 0);    if (fd_flags == -1)    {        STAFString error = STAFString("Error getting file descriptor flags: "                                      "fcntl() RC=") + errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }    if (blockingMode == kSTAFSocketBlocking)        fd_flags &= ~O_NONBLOCK;    else        fd_flags |= O_NONBLOCK;    if (fcntl(theSocket, F_SETFL, fd_flags) == -1)    {        STAFString error = STAFString("Error setting socket flags: "                                      "fcntl() RC=") + errno;        if (errorBuffer) *errorBuffer = error.adoptImpl();        return kSTAFCommunicationError;    }    return kSTAFOk;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -