//////////////////////////////////////////////////////////////// Function name : ClientSocket::ConnectToServer Return type : bool Argument : char* pNetAddress // Can be NULL -- in which case connect to "this machine" Argument : int port Description : Connect to a server ////////////////////////////////////////////////////////////////
| 88 | // |
| 89 | ///////////////////////////////////////////////////////////////////// |
| 90 | bool ClientSocket::ConnectToServer(char const* pNetAddress, int port) |
| 91 | { |
| 92 | CTDEBUG_ENTER_METHOD("ClientSocket::ConnectToServer"); |
| 93 | |
| 94 | SOCKET sock = 0; |
| 95 | |
| 96 | size_t res = 1; // if any of this fails, fall back on creating an internet socket |
| 97 | |
| 98 | #ifdef ENABLE_LOCAL_SOCKETS |
| 99 | |
| 100 | sockaddr_un local_address; |
| 101 | |
| 102 | if (!pNetAddress) |
| 103 | { |
| 104 | memset(&local_address, 0, sizeof(local_address)); |
| 105 | local_address.sun_family = AF_UNIX; |
| 106 | sprintf(local_address.sun_path, "%s%d", sock::GetLocalSocketDir().c_str(), port); |
| 107 | |
| 108 | // set the name of the datasender |
| 109 | this->name = "file "; |
| 110 | this->name.append(local_address.sun_path); |
| 111 | |
| 112 | #ifdef __clang__ |
| 113 | #pragma clang diagnostic push |
| 114 | #pragma clang diagnostic ignored "-Wshorten-64-to-32" |
| 115 | #endif |
| 116 | int len = SUN_LEN(&local_address); |
| 117 | #ifdef __clang__ |
| 118 | #pragma clang diagnostic pop |
| 119 | #endif |
| 120 | |
| 121 | // Create the socket |
| 122 | sock = socket(AF_UNIX, SOCK_STREAM, 0) ; |
| 123 | |
| 124 | res = 1; |
| 125 | if (sock == INVALID_SOCKET) |
| 126 | { |
| 127 | sml::PrintDebug("Error: Error creating client local connection socket") ; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | if (chmod(local_address.sun_path, S_IRWXU) < 0) |
| 132 | { |
| 133 | sml::PrintDebug("Error: Error setting permissions for client local connection socket") ; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | // Try to connect to the server |
| 138 | res = connect(sock, (sockaddr*)&local_address, len) ; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | if (res != 0) |
| 144 | #endif |
| 145 | { |
| 146 | in_addr* pAddress = NULL; |
| 147 |
no test coverage detected