0

I have this code:

bool CBSocketConnect(uint64_t socketID,uint8_t * IP,bool IPv6,uint16_t port){
    // Create sockaddr_in6 information for a IPv6 address
    int res;
    if (IPv6) {
        struct sockaddr_in6 address;
        memset(&address, 0, sizeof(address)); // Clear structure.
        address.sin6_family = AF_INET6;
        memcpy(&address.sin6_addr, IP, 16); // Move IP address into place.
        address.sin6_port = htons(port); // Port number to network order
        res = connect((evutil_socket_t)socketID, (struct sockaddr *)&address, sizeof(address));
    }else{
        struct sockaddr_in address;
        memset(&address, 0, sizeof(address)); // Clear structure.
        address.sin_family = AF_INET;
        memcpy(&address.sin_addr, IP + 12, 4); // Move IP address into place. Last 4 bytes for IPv4.
        address.sin_port = htons(port); // Port number to network order
        res = connect((evutil_socket_t)socketID, (struct sockaddr *)&address, sizeof(address));
    }
    if (NOT res || errno == EINPROGRESS)
        return true;
    return false;
}

With IPv6 set to false, the IP set to ::ffff:127.0.0.1 (IPv4 loopback address) and the port number set to 45562, res is set to -1 and errno is set to ENOENT (2). Why would this be?

The platform I am on is OSX Mountain Lion. I'm using the sockets with libevent version "2.0.19-stable".

Thank you.

Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122
  • The normal sequence for a client that is using TCP to connect to a server is to do a socket() call first to obtain a socket and to then use the connect() call to create a connection to the server using the socket handle that you have obtained from socket(). Do you have a good socket handle? – Richard Chambers Aug 11 '12 at 19:27
  • The socket is created elsewhere and it is created without an issue. – Matthew Mitchell Aug 11 '12 at 20:25
  • Also I check the errno after the connect call. My program is multithreaded but errno is designed to work on multi-threaded programs as a POSIX standard if I remember correctly. – Matthew Mitchell Aug 11 '12 at 20:27
  • I found a problem in another thread that Xcode wasn't bringing to my attention... Might be causing some sort of corruption... – Matthew Mitchell Aug 11 '12 at 22:22
  • Why do you have `socketID` as `uint64_t`? It is `int` for all socket system calls. Also `ENOENT` ("The named socket does not exist") error indicates that you somehow confuse the kernel to think you are dealing with Unix sockets. – Nikolai Fetissov Aug 11 '12 at 22:30
  • I'm using uint64_t because the prototype for the function is declared with weak linkage. uint64_t gives more options for the implementation of the function, including using 64 bit pointers for the sockets. – Matthew Mitchell Aug 11 '12 at 23:37
  • I'm currently checking the other thread that is causing problems. Maybe if I fix that issue, connect() will return OK. – Matthew Mitchell Aug 11 '12 at 23:37
  • The problems I was getting with the thread have been resolved. I'm still getting the same issue with the connect(). – Matthew Mitchell Aug 12 '12 at 00:22
  • 1
    Post the code that calls `socket(2)` and passes in that socket to this function. Are you setting the address family correctly for that socket? – Adam Rosenfield Aug 12 '12 at 04:07

1 Answers1

0

I found the problem: LLDB

LLDB was telling me errno was ENOENT but when I use GDB (As I clearly should) it told me errno was EINPROGRESS! It was OK all along. It was all the debugger's fault.

Lesson: Use GDB and never LLDB.

Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122