ws2_32: Rewrite getsockopt to make it more readable.

This commit is contained in:
Kai Blin 2006-12-04 09:46:10 +01:00 committed by Alexandre Julliard
parent 84fb1e1f3f
commit 2a4df56548
2 changed files with 310 additions and 115 deletions

View File

@ -1814,22 +1814,107 @@ INT WINAPI WS_getsockopt(SOCKET s, INT level,
TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n", TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
s, level, optname, optval, *optlen); s, level, optname, optval, *optlen);
/* SO_OPENTYPE does not require a valid socket handle. */
if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE) switch(level)
{ {
if (!optlen || *optlen < sizeof(int) || !optval) case WS_SOL_SOCKET:
{
switch(optname)
{
/* Handle common cases. The special cases are below, sorted
* alphabetically */
case WS_SO_ACCEPTCONN:
case WS_SO_BROADCAST:
case WS_SO_DEBUG:
case WS_SO_ERROR:
case WS_SO_KEEPALIVE:
case WS_SO_OOBINLINE:
case WS_SO_RCVBUF:
case WS_SO_SNDBUF:
case WS_SO_TYPE:
if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
return SOCKET_ERROR;
convert_sockopt(&level, &optname);
if (getsockopt(fd,(int) level, optname, optval,
(unsigned int *)optlen) != 0 )
{
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR;
}
release_sock_fd( s, fd );
return ret;
case WS_SO_DONTLINGER:
{
struct linger lingval;
unsigned int len = sizeof(struct linger);
if (!optlen || *optlen < sizeof(BOOL)|| !optval)
{ {
SetLastError(WSAEFAULT); SetLastError(WSAEFAULT);
return SOCKET_ERROR; return SOCKET_ERROR;
} }
*(int *)optval = get_per_thread_data()->opentype; if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
*optlen = sizeof(int); return SOCKET_ERROR;
TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
return 0; if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
{
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR;
}
else
{
*(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
*optlen = sizeof(BOOL);
} }
if (level == WS_SOL_SOCKET && optname == WS_SO_MAX_MSG_SIZE) release_sock_fd( s, fd );
return ret;
}
/* As mentioned in setsockopt, Windows ignores this, so we
* always return true here */
case WS_SO_DONTROUTE:
if (!optlen || *optlen < sizeof(BOOL) || !optval)
{ {
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
*(BOOL *)optval = TRUE;
*optlen = sizeof(BOOL);
return 0;
case WS_SO_LINGER:
{
struct linger lingval;
unsigned int len = sizeof(struct linger);
/* struct linger and LINGER have different sizes */
if (!optlen || *optlen < sizeof(LINGER) || !optval)
{
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
return SOCKET_ERROR;
if (getsockopt(fd, SOL_SOCKET, SO_LINGER, &lingval, &len) != 0 )
{
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR;
}
else
{
((LINGER *)optval)->l_onoff = lingval.l_onoff;
((LINGER *)optval)->l_linger = lingval.l_linger;
*optlen = sizeof(struct linger);
}
release_sock_fd( s, fd );
return ret;
}
case WS_SO_MAX_MSG_SIZE:
if (!optlen || *optlen < sizeof(int) || !optval) if (!optlen || *optlen < sizeof(int) || !optval)
{ {
SetLastError(WSAEFAULT); SetLastError(WSAEFAULT);
@ -1839,10 +1924,74 @@ INT WINAPI WS_getsockopt(SOCKET s, INT level,
*(int *)optval = 65507; *(int *)optval = 65507;
*optlen = sizeof(int); *optlen = sizeof(int);
return 0; return 0;
/* SO_OPENTYPE does not require a valid socket handle. */
case WS_SO_OPENTYPE:
if (!optlen || *optlen < sizeof(int) || !optval)
{
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
*(int *)optval = get_per_thread_data()->opentype;
*optlen = sizeof(int);
TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
return 0;
#ifdef SO_RCVTIMEO
case WS_SO_RCVTIMEO:
#endif
#ifdef SO_SNDTIMEO
case WS_SO_SNDTIMEO:
#endif
#if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
{
struct timeval tv;
unsigned int len = sizeof(struct timeval);
if (!optlen || *optlen < sizeof(int)|| !optval)
{
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
return SOCKET_ERROR;
convert_sockopt(&level, &optname);
if (getsockopt(fd,(int) level, optname, &tv, &len) != 0 )
{
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR;
}
else
{
*(int *)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
*optlen = sizeof(int);
} }
release_sock_fd( s, fd );
return ret;
}
#endif
/* As mentioned in setsockopt, the windows style SO_REUSEADDR is
* not possible in Unix, so always return false here. */
case WS_SO_REUSEADDR:
if (!optlen || *optlen < sizeof(int) || !optval)
{
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
*(int *)optval = 0;
*optlen = sizeof(int);
return 0;
default:
TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname);
SetLastError(WSAENOPROTOOPT);
return SOCKET_ERROR;
} /* end switch(optname) */
}/* end case WS_SOL_SOCKET */
#ifdef HAVE_IPX #ifdef HAVE_IPX
if(level == NSPROTO_IPX) case NSPROTO_IPX:
{ {
struct WS_sockaddr_ipx addr; struct WS_sockaddr_ipx addr;
IPX_ADDRESS_DATA *data; IPX_ADDRESS_DATA *data;
@ -1850,31 +1999,34 @@ INT WINAPI WS_getsockopt(SOCKET s, INT level,
switch(optname) switch(optname)
{ {
case IPX_PTYPE: case IPX_PTYPE:
fd = get_sock_fd( s, 0, NULL ); if ((fd = get_sock_fd( s, 0, NULL )) == -1) return SOCKET_ERROR;
#ifdef SOL_IPX #ifdef SOL_IPX
if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1) if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, (unsigned int*)optlen) == -1)
{ {
return SOCKET_ERROR; ret = SOCKET_ERROR;
} }
#else #else
{ {
struct ipx val; struct ipx val;
socklen_t len=sizeof(struct ipx); socklen_t len=sizeof(struct ipx);
if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 ) if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
return SOCKET_ERROR; ret = SOCKET_ERROR;
else
*optval = (int)val.ipx_pt; *optval = (int)val.ipx_pt;
} }
#endif #endif
TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd); TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
release_sock_fd( s, fd ); release_sock_fd( s, fd );
return ret;
return 0;
case IPX_ADDRESS: case IPX_ADDRESS:
/* /*
* On a Win2000 system with one network card there are usually three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps. * On a Win2000 system with one network card there are usually
* Using this call you can then retrieve info about this all. In case of Linux it is a bit different. Usually you have * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
* only "one" device active and further it is not possible to query things like the linkspeed. * Using this call you can then retrieve info about this all.
* In case of Linux it is a bit different. Usually you have
* only "one" device active and further it is not possible to
* query things like the linkspeed.
*/ */
FIXME("IPX_ADDRESS\n"); FIXME("IPX_ADDRESS\n");
namelen = sizeof(struct WS_sockaddr_ipx); namelen = sizeof(struct WS_sockaddr_ipx);
@ -1888,65 +2040,75 @@ INT WINAPI WS_getsockopt(SOCKET s, INT level,
data->wan = FALSE; /* We are not on a wan for now .. */ data->wan = FALSE; /* We are not on a wan for now .. */
data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */ data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */ data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit; note 1MB = 1000kB in this case */ data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit;
* note 1MB = 1000kB in this case */
return 0; return 0;
case IPX_MAX_ADAPTER_NUM: case IPX_MAX_ADAPTER_NUM:
FIXME("IPX_MAX_ADAPTER_NUM\n"); FIXME("IPX_MAX_ADAPTER_NUM\n");
*(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */ *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
return 0; return 0;
default: default:
FIXME("IPX optname:%x\n", optname); FIXME("IPX optname:%x\n", optname);
return SOCKET_ERROR; return SOCKET_ERROR;
} }/* end switch(optname) */
} } /* end case NSPROTO_IPX */
#endif #endif
/* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
case WS_IPPROTO_TCP:
switch(optname)
{
case WS_TCP_NODELAY:
if ( (fd = get_sock_fd( s, 0, NULL )) == -1) if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
return SOCKET_ERROR; return SOCKET_ERROR;
convert_sockopt(&level, &optname);
if (!convert_sockopt(&level, &optname)) { if (getsockopt(fd,(int) level, optname, optval,
SetLastError(WSAENOPROTOOPT); /* Unknown option */ (unsigned int *)optlen) != 0 )
ret = SOCKET_ERROR; {
} else {
struct timeval tv;
struct linger lingval;
unsigned int len, *plen = (unsigned int*)optlen;
char *pval = optval;
if(level == SOL_SOCKET && is_timeout_option(optname)) {
len = sizeof(tv);
plen = &len;
pval = (char *) &tv;
} else if( level == SOL_SOCKET && optname == SO_LINGER) {
len = sizeof(lingval);
plen = &len;
pval = (char *) &lingval;
}
if (getsockopt(fd, (int) level, optname, pval, plen) != 0 ) {
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno()); SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR; ret = SOCKET_ERROR;
} else if(level == SOL_SOCKET && is_timeout_option(optname)) {
if( *optlen >= sizeof(INT) ) {
*optlen = sizeof(INT);
*(INT*)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
} else {
SetLastError(WSAEFAULT);
ret = SOCKET_ERROR;
}
} else if( level == SOL_SOCKET && optname == SO_LINGER) {
if( *optlen >= sizeof( LINGER) ) {
(( LINGER *) optval)->l_onoff = lingval.l_onoff;
(( LINGER *) optval)->l_linger = lingval.l_linger;
} else {
SetLastError(WSAEFAULT);
ret = SOCKET_ERROR;
}
}
} }
release_sock_fd( s, fd ); release_sock_fd( s, fd );
return ret; return ret;
} }
FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname);
return SOCKET_ERROR;
case WS_IPPROTO_IP:
switch(optname)
{
case WS_IP_ADD_MEMBERSHIP:
case WS_IP_DROP_MEMBERSHIP:
#ifdef IP_HDRINCL
case WS_IP_HDRINCL:
#endif
case WS_IP_MULTICAST_IF:
case WS_IP_MULTICAST_LOOP:
case WS_IP_MULTICAST_TTL:
case WS_IP_OPTIONS:
case WS_IP_TOS:
case WS_IP_TTL:
if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
return SOCKET_ERROR;
convert_sockopt(&level, &optname);
if (getsockopt(fd,(int) level, optname, optval,
(unsigned int *)optlen) != 0 )
{
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
ret = SOCKET_ERROR;
}
release_sock_fd( s, fd );
return ret;
}
FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
return SOCKET_ERROR;
default:
FIXME("Unknown level: 0x%08x\n", level);
return SOCKET_ERROR;
} /* end switch(level) */
}
/*********************************************************************** /***********************************************************************
* htonl (WINSOCK.8) * htonl (WINSOCK.8)

View File

@ -1417,6 +1417,8 @@ static void test_extendedSocketOptions()
struct sockaddr_in sa; struct sockaddr_in sa;
int sa_len = sizeof(struct sockaddr_in); int sa_len = sizeof(struct sockaddr_in);
int optval, optlen = sizeof(int), ret; int optval, optlen = sizeof(int), ret;
BOOL bool_opt_val;
LINGER linger_val;
if(WSAStartup(MAKEWORD(2,0), &wsa)){ if(WSAStartup(MAKEWORD(2,0), &wsa)){
trace("Winsock failed: 0x%08x. Aborting test\n", WSAGetLastError()); trace("Winsock failed: 0x%08x. Aborting test\n", WSAGetLastError());
@ -1442,11 +1444,42 @@ static void test_extendedSocketOptions()
return; return;
} }
ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, (int *)&optlen); ret = getsockopt(sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret); ok(ret == 0, "getsockopt failed to query SO_MAX_MSG_SIZE, return value is 0x%08x\n", ret);
ok(optval == 65507, "SO_MAX_MSG_SIZE reported %d, expected 65507\n", optval); ok(optval == 65507, "SO_MAX_MSG_SIZE reported %d, expected 65507\n", optval);
optlen = sizeof(LINGER);
ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
todo_wine{
ok(ret == SOCKET_ERROR, "getsockopt should fail for UDP sockets but return value is 0x%08x\n", ret);
}
closesocket(sock);
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) < 0){
trace("Creating the socket failed: 0x%08x\n", WSAGetLastError());
WSACleanup();
return;
}
if(bind(sock, (struct sockaddr *) &sa, sa_len) < 0){
trace("Failed to bind socket: 0x%08x\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
ret = getsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger_val, &optlen);
ok(ret == 0, "getsockopt failed to query SO_LINGER, return value is 0x%08x\n", ret);
optlen = sizeof(BOOL);
ret = getsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)&bool_opt_val, &optlen);
ok(ret == 0, "getsockopt failed to query SO_DONTLINGER, return value is 0x%08x\n", ret);
ok((linger_val.l_onoff && !bool_opt_val) || (!linger_val.l_onoff && bool_opt_val),
"Return value of SO_DONTLINGER is %d, but SO_LINGER returned l_onoff == %d.\n",
bool_opt_val, linger_val.l_onoff);
closesocket(sock); closesocket(sock);
WSACleanup(); WSACleanup();
} }