X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/11e1c70d8e6aa75e2ecb660fffd61b6cfcb8ffc7..5ea6dbbfff67d1dfad08044ab721ba56425ef6e1:/src/unix/gsocket.c diff --git a/src/unix/gsocket.c b/src/unix/gsocket.c index 587395297c..0ad0608f4e 100644 --- a/src/unix/gsocket.c +++ b/src/unix/gsocket.c @@ -11,18 +11,23 @@ #if wxUSE_SOCKETS #include -#include #include -#ifdef vms +#include +#include +#ifdef __VMS__ #include +struct sockaddr_un { + u_char sun_len; /* sockaddr len including null */ + u_char sun_family; /* AF_UNIX */ + char sun_path[108]; /* path name (gag) */ +}; #else #include -#endif #include +#endif #include #include #include -#include #include #include @@ -47,15 +52,19 @@ #ifndef SOCKLEN_T -#ifdef __GLIBC__ -# if __GLIBC__ == 2 -# define SOCKLEN_T socklen_t -# endif +#ifdef VMS +# define SOCKLEN_T unsigned int #else -# define SOCKLEN_T int +# ifdef __GLIBC__ +# if __GLIBC__ == 2 +# define SOCKLEN_T socklen_t +# endif +# else +# define SOCKLEN_T int +# endif #endif -#endif +#endif // SOCKLEN_T #define MASK_SIGNAL() \ { \ @@ -203,7 +212,7 @@ GAddress *GSocket_GetLocal(GSocket *socket) { GAddress *address; struct sockaddr addr; - SOCKLEN_T size; + SOCKLEN_T size = sizeof(addr); GSocketError err; assert(socket != NULL); @@ -218,7 +227,7 @@ GAddress *GSocket_GetLocal(GSocket *socket) size = sizeof(addr); - if (getsockname(socket->m_fd, &addr, &size) < 0) { + if (getsockname(socket->m_fd, &addr, (SOCKLEN_T *) &size) < 0) { socket->m_error = GSOCK_IOERR; return NULL; } @@ -228,9 +237,10 @@ GAddress *GSocket_GetLocal(GSocket *socket) socket->m_error = GSOCK_MEMERR; return NULL; } - socket->m_error = _GAddress_translate_from(address, &addr, size); - if (socket->m_error != GSOCK_NOERROR) { + err = _GAddress_translate_from(address, &addr, size); /*xxx*/ + if (err != GSOCK_NOERROR) { GAddress_destroy(address); + socket->m_error = err; return NULL; } @@ -312,6 +322,8 @@ GSocketError GSocket_SetServer(GSocket *sck) */ GSocket *GSocket_WaitConnection(GSocket *socket) { + struct sockaddr from; + SOCKLEN_T fromlen = sizeof(from); GSocket *connection; int arg = 1; @@ -331,7 +343,7 @@ GSocket *GSocket_WaitConnection(GSocket *socket) connection = GSocket_new(); if (!connection) { - connection->m_error = GSOCK_MEMERR; + socket->m_error = GSOCK_MEMERR; return NULL; } @@ -343,7 +355,7 @@ GSocket *GSocket_WaitConnection(GSocket *socket) return NULL; } - connection->m_fd = accept(socket->m_fd, NULL, NULL); + connection->m_fd = accept(socket->m_fd, &from, (SOCKLEN_T *) &fromlen); if (connection->m_fd == -1) { @@ -361,6 +373,23 @@ GSocket *GSocket_WaitConnection(GSocket *socket) connection->m_stream = TRUE; connection->m_oriented = TRUE; + /* Setup the peer address field */ /*xxx*/ + connection->m_peer = GAddress_new(); + if (!connection->m_peer) + { + GSocket_destroy(connection); + socket->m_error = GSOCK_MEMERR; + return NULL; + } + err = _GAddress_translate_from(connection->m_peer, &from, fromlen); + if (err != GSOCK_NOERROR) + { + GAddress_destroy(connection->m_peer); + GSocket_destroy(connection); + socket->m_error = err; + return NULL; + } + ioctl(connection->m_fd, FIONBIO, &arg); _GSocket_Enable_Events(connection); @@ -485,12 +514,10 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) close(sck->m_fd); sck->m_fd = -1; /* sck->m_error is set in _GSocket_Output_Timeout */ - fprintf(stderr, "Blocking connect timeouts\n"); return GSOCK_TIMEDOUT; } else { - fprintf(stderr, "Blocking connect OK\n"); return GSOCK_NOERROR; } } @@ -505,7 +532,6 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) { sck->m_error = GSOCK_WOULDBLOCK; sck->m_establishing = TRUE; - fprintf(stderr, "Nonblocking connect in progress\n"); return GSOCK_WOULDBLOCK; } @@ -517,11 +543,9 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) sck->m_fd = -1; sck->m_error = GSOCK_IOERR; - fprintf(stderr, "Connect failed (generic err)\n"); return GSOCK_IOERR; } - fprintf(stderr, "Connect OK\n"); return GSOCK_NOERROR; } @@ -797,14 +821,14 @@ int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size) int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size) { struct sockaddr from; - SOCKLEN_T fromlen; + SOCKLEN_T fromlen = sizeof(from); int ret; GSocketError err; fromlen = sizeof(from); MASK_SIGNAL(); - ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen); + ret = recvfrom(socket->m_fd, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen); UNMASK_SIGNAL(); if (ret == -1) @@ -835,7 +859,6 @@ int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size) int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size) { int ret; - GSocketError err; MASK_SIGNAL(); ret = send(socket->m_fd, buffer, size, 0); @@ -906,7 +929,8 @@ void _GSocket_Detected_Write(GSocket *socket) socket->m_establishing = FALSE; len = sizeof(error); - getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error, &len); + getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error, + (SOCKLEN_T *) &len); if (error) { @@ -1074,18 +1098,22 @@ GSocketError _GAddress_translate_to(GAddress *address, GSocketError _GAddress_Init_INET(GAddress *address) { - address->m_addr = (struct sockaddr *)malloc(sizeof(struct sockaddr_in)); - if (address->m_addr == NULL) { + address->m_len = sizeof(struct sockaddr_in); + address->m_addr = (struct sockaddr *)malloc(address->m_len); + if (address->m_addr == NULL) + { address->m_error = GSOCK_MEMERR; return GSOCK_MEMERR; } - address->m_len = sizeof(struct sockaddr_in); - address->m_family = GSOCK_INET; address->m_realfamily = PF_INET; ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; - ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; + /* + INADDR_BROADCAST is identical to INADDR_NONE which is not defined + on all unices. INADDR_BROADCAST should be fine to indicate an error. + */ + ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_BROADCAST; return GSOCK_NOERROR; } @@ -1103,18 +1131,22 @@ GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) /* If it is a numeric host name, convert it now */ #if defined(HAVE_INET_ATON) - if (inet_aton(hostname, addr) == 0) { + if (inet_aton(hostname, addr) == 0) + { #elif defined(HAVE_INET_ADDR) /* Fix from Guillermo Rodriguez Garcia */ - if ( (addr->s_addr = inet_addr(hostname)) == -1 ) { + if ( (addr->s_addr = inet_addr(hostname)) == -1 ) + { #else /* Use gethostbyname by default */ - if (1) { + if (1) + { #endif struct in_addr *array_addr; /* It is a real name, we solve it */ - if ((he = gethostbyname(hostname)) == NULL) { + if ((he = gethostbyname(hostname)) == NULL) + { address->m_error = GSOCK_NOHOST; return GSOCK_NOHOST; } @@ -1148,14 +1180,16 @@ GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, assert(address != NULL); CHECK_ADDRESS(address, INET, GSOCK_INVADDR); - if (!port) { + if (!port) + { address->m_error = GSOCK_INVPORT; return GSOCK_INVPORT; } se = getservbyname(port, protocol); if (!se) { - if (isdigit(port[0])) { + if (isdigit(port[0])) + { int port_int; port_int = atoi(port); @@ -1199,8 +1233,9 @@ GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t addr = (struct sockaddr_in *)address->m_addr; addr_buf = (char *)&(addr->sin_addr); - he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); - if (he == NULL) { + he = gethostbyaddr(addr_buf, sizeof(addr->sin_addr), AF_INET); + if (he == NULL) + { address->m_error = GSOCK_NOHOST; return GSOCK_NOHOST; } @@ -1241,13 +1276,14 @@ unsigned short GAddress_INET_GetPort(GAddress *address) GSocketError _GAddress_Init_UNIX(GAddress *address) { + address->m_len = sizeof(struct sockaddr_un); address->m_addr = (struct sockaddr *)malloc(address->m_len); - if (address->m_addr == NULL) { + if (address->m_addr == NULL) + { address->m_error = GSOCK_MEMERR; return GSOCK_MEMERR; } - address->m_len = sizeof(struct sockaddr_un); address->m_family = GSOCK_UNIX; address->m_realfamily = PF_UNIX; ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; @@ -1286,3 +1322,4 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) #endif /* wxUSE_SOCKETS */ +