slight bug in waitconnection

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4992 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Guillermo Rodriguez Garcia 1999-12-16 18:19:04 +00:00
parent 982b2cfc58
commit 85806dc2d8
2 changed files with 75 additions and 27 deletions

View File

@ -262,7 +262,8 @@ GAddress *GSocket_GetLocal(GSocket *socket)
{ {
GAddress *address; GAddress *address;
struct sockaddr addr; struct sockaddr addr;
SOCKLEN_T size; SOCKLEN_T size = sizeof(addr);
GSocketError err;
assert(socket != NULL); assert(socket != NULL);
@ -289,10 +290,11 @@ GAddress *GSocket_GetLocal(GSocket *socket)
socket->m_error = GSOCK_MEMERR; socket->m_error = GSOCK_MEMERR;
return NULL; return NULL;
} }
if (_GAddress_translate_from(address, &addr, size) != GSOCK_NOERROR) err = _GAddress_translate_from(address, &addr, size);
if (err != GSOCK_NOERROR)
{ {
socket->m_error = GSOCK_MEMERR;
GAddress_destroy(address); GAddress_destroy(address);
socket->m_error = err;
return NULL; return NULL;
} }
@ -380,6 +382,9 @@ GSocketError GSocket_SetServer(GSocket *sck)
GSocket *GSocket_WaitConnection(GSocket *sck) GSocket *GSocket_WaitConnection(GSocket *sck)
{ {
GSocket *connection; GSocket *connection;
struct sockaddr from;
SOCKLEN_T fromlen = sizeof(from);
GSocketError err;
u_long arg = 1; u_long arg = 1;
assert(sck != NULL); assert(sck != NULL);
@ -409,7 +414,7 @@ GSocket *GSocket_WaitConnection(GSocket *sck)
return NULL; return NULL;
} }
connection->m_fd = accept(sck->m_fd, NULL, NULL); connection->m_fd = accept(sck->m_fd, &from, &fromlen);
if (connection->m_fd == INVALID_SOCKET) if (connection->m_fd == INVALID_SOCKET)
{ {
@ -427,6 +432,23 @@ GSocket *GSocket_WaitConnection(GSocket *sck)
connection->m_stream = TRUE; connection->m_stream = TRUE;
connection->m_oriented = TRUE; connection->m_oriented = TRUE;
/* Setup the peer address field */
connection->m_peer = GAddress_new();
if (!connection->m_peer)
{
GSocket_destroy(connection);
sck->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);
sck->m_error = err;
return NULL;
}
ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg); ioctlsocket(connection->m_fd, FIONBIO, (u_long FAR *) &arg);
_GSocket_Enable_Events(connection); _GSocket_Enable_Events(connection);
@ -956,6 +978,7 @@ int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
struct sockaddr from; struct sockaddr from;
SOCKLEN_T fromlen = sizeof(from); SOCKLEN_T fromlen = sizeof(from);
int ret; int ret;
GSocketError err;
ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen); ret = recvfrom(socket->m_fd, buffer, size, 0, &from, &fromlen);
@ -972,10 +995,12 @@ int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
return -1; return -1;
} }
} }
if (_GAddress_translate_from(socket->m_peer, &from, fromlen) != GSOCK_NOERROR) err = _GAddress_translate_from(socket->m_peer, &from, fromlen);
if (err != GSOCK_NOERROR)
{ {
socket->m_error = GSOCK_MEMERR;
GAddress_destroy(socket->m_peer); GAddress_destroy(socket->m_peer);
socket->m_peer = NULL;
socket->m_error = err;
return -1; return -1;
} }
@ -991,6 +1016,7 @@ int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
{ {
struct sockaddr *addr; struct sockaddr *addr;
int len, ret; int len, ret;
GSocketError err;
if (!socket->m_peer) if (!socket->m_peer)
{ {
@ -998,9 +1024,10 @@ int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size)
return -1; return -1;
} }
if (!_GAddress_translate_to(socket->m_peer, &addr, &len)) err = _GAddress_translate_to(socket->m_peer, &addr, &len);
if (err != GSOCK_NOERROR)
{ {
socket->m_error = GSOCK_MEMERR; socket->m_error = err;
return -1; return -1;
} }

View File

@ -15,7 +15,6 @@
#include <netdb.h> #include <netdb.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#ifdef __VMS__ #ifdef __VMS__
#define SOCK_LEN_TYP (unsigned int*)
#include <socket.h> #include <socket.h>
struct sockaddr_un { struct sockaddr_un {
u_char sun_len; /* sockaddr len including null */ u_char sun_len; /* sockaddr len including null */
@ -25,7 +24,6 @@ struct sockaddr_un {
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#define SOCK_LEN_TYP (int*)
#endif #endif
#include <sys/time.h> #include <sys/time.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -54,15 +52,19 @@ struct sockaddr_un {
#ifndef SOCKLEN_T #ifndef SOCKLEN_T
#ifdef __GLIBC__ #ifdef VMS
# if __GLIBC__ == 2 # define SOCKLEN_T unsigned int
# define SOCKLEN_T socklen_t
# endif
#else #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 #endif // SOCKLEN_T
#define MASK_SIGNAL() \ #define MASK_SIGNAL() \
{ \ { \
@ -210,7 +212,7 @@ GAddress *GSocket_GetLocal(GSocket *socket)
{ {
GAddress *address; GAddress *address;
struct sockaddr addr; struct sockaddr addr;
SOCKLEN_T size; SOCKLEN_T size = sizeof(addr);
GSocketError err; GSocketError err;
assert(socket != NULL); assert(socket != NULL);
@ -225,7 +227,7 @@ GAddress *GSocket_GetLocal(GSocket *socket)
size = sizeof(addr); size = sizeof(addr);
if (getsockname(socket->m_fd, &addr, SOCK_LEN_TYP &size) < 0) { if (getsockname(socket->m_fd, &addr, (SOCKLEN_T *) &size) < 0) {
socket->m_error = GSOCK_IOERR; socket->m_error = GSOCK_IOERR;
return NULL; return NULL;
} }
@ -235,9 +237,10 @@ GAddress *GSocket_GetLocal(GSocket *socket)
socket->m_error = GSOCK_MEMERR; socket->m_error = GSOCK_MEMERR;
return NULL; return NULL;
} }
socket->m_error = _GAddress_translate_from(address, &addr, size); err = _GAddress_translate_from(address, &addr, size); /*xxx*/
if (socket->m_error != GSOCK_NOERROR) { if (err != GSOCK_NOERROR) {
GAddress_destroy(address); GAddress_destroy(address);
socket->m_error = err;
return NULL; return NULL;
} }
@ -319,6 +322,8 @@ GSocketError GSocket_SetServer(GSocket *sck)
*/ */
GSocket *GSocket_WaitConnection(GSocket *socket) GSocket *GSocket_WaitConnection(GSocket *socket)
{ {
struct sockaddr from;
SOCKLEN_T fromlen = sizeof(from);
GSocket *connection; GSocket *connection;
int arg = 1; int arg = 1;
@ -338,7 +343,7 @@ GSocket *GSocket_WaitConnection(GSocket *socket)
connection = GSocket_new(); connection = GSocket_new();
if (!connection) if (!connection)
{ {
connection->m_error = GSOCK_MEMERR; socket->m_error = GSOCK_MEMERR;
return NULL; return NULL;
} }
@ -350,7 +355,7 @@ GSocket *GSocket_WaitConnection(GSocket *socket)
return NULL; 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) if (connection->m_fd == -1)
{ {
@ -368,6 +373,23 @@ GSocket *GSocket_WaitConnection(GSocket *socket)
connection->m_stream = TRUE; connection->m_stream = TRUE;
connection->m_oriented = 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); ioctl(connection->m_fd, FIONBIO, &arg);
_GSocket_Enable_Events(connection); _GSocket_Enable_Events(connection);
@ -799,15 +821,14 @@ int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size)
int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size) int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size)
{ {
struct sockaddr from; struct sockaddr from;
SOCKLEN_T fromlen; SOCKLEN_T fromlen = sizeof(from);
int ret; int ret;
GSocketError err; GSocketError err;
fromlen = sizeof(from); fromlen = sizeof(from);
MASK_SIGNAL(); MASK_SIGNAL();
ret = recvfrom(socket->m_fd, buffer, size, 0, &from, ret = recvfrom(socket->m_fd, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen);
SOCK_LEN_TYP &fromlen);
UNMASK_SIGNAL(); UNMASK_SIGNAL();
if (ret == -1) if (ret == -1)
@ -838,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 _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size)
{ {
int ret; int ret;
GSocketError err;
MASK_SIGNAL(); MASK_SIGNAL();
ret = send(socket->m_fd, buffer, size, 0); ret = send(socket->m_fd, buffer, size, 0);
@ -910,7 +930,7 @@ void _GSocket_Detected_Write(GSocket *socket)
len = sizeof(error); len = sizeof(error);
getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error, getsockopt(socket->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error,
SOCK_LEN_TYP &len); (SOCKLEN_T *) &len);
if (error) if (error)
{ {
@ -1302,3 +1322,4 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
#endif #endif
/* wxUSE_SOCKETS */ /* wxUSE_SOCKETS */