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,139 +1814,301 @@ 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)
{
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;
}
if (level == WS_SOL_SOCKET && optname == WS_SO_MAX_MSG_SIZE) switch(level)
{ {
if(!optlen || *optlen < sizeof(int) || !optval) case WS_SOL_SOCKET:
{
switch(optname)
{ {
SetLastError(WSAEFAULT); /* Handle common cases. The special cases are below, sorted
return SOCKET_ERROR; * alphabetically */
} case WS_SO_ACCEPTCONN:
TRACE("getting global SO_MAX_MSG_SIZE = 65507\n"); case WS_SO_BROADCAST:
*(int *)optval = 65507; case WS_SO_DEBUG:
*optlen = sizeof(int); case WS_SO_ERROR:
return 0; 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);
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
{
*(BOOL *)optval = (lingval.l_onoff) ? FALSE : TRUE;
*optlen = sizeof(BOOL);
}
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)
{
SetLastError(WSAEFAULT);
return SOCKET_ERROR;
}
TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
*(int *)optval = 65507;
*optlen = sizeof(int);
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;
int namelen; int namelen;
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 ) ret = SOCKET_ERROR;
return 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:
/*
* On a Win2000 system with one network card there are usually three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
* 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");
namelen = sizeof(struct WS_sockaddr_ipx);
memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
data = (IPX_ADDRESS_DATA*)optval; case IPX_ADDRESS:
memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum)); /*
memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum)); * On a Win2000 system with one network card there are usually
data->adapternum = 0; * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
data->wan = FALSE; /* We are not on a wan for now .. */ * Using this call you can then retrieve info about this all.
data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */ * In case of Linux it is a bit different. Usually you have
data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */ * only "one" device active and further it is not possible to
data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit; note 1MB = 1000kB in this case */ * query things like the linkspeed.
return 0; */
case IPX_MAX_ADAPTER_NUM: FIXME("IPX_ADDRESS\n");
FIXME("IPX_MAX_ADAPTER_NUM\n"); namelen = sizeof(struct WS_sockaddr_ipx);
*(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */ memset(&addr, 0, sizeof(struct WS_sockaddr_ipx));
WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
return 0; data = (IPX_ADDRESS_DATA*)optval;
default: memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
FIXME("IPX optname:%x\n", optname); memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
return SOCKET_ERROR; data->adapternum = 0;
} 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->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 */
return 0;
case IPX_MAX_ADAPTER_NUM:
FIXME("IPX_MAX_ADAPTER_NUM\n");
*(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
return 0;
default:
FIXME("IPX optname:%x\n", optname);
return SOCKET_ERROR;
}/* end switch(optname) */
} /* end case NSPROTO_IPX */
#endif #endif
/* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
if( (fd = get_sock_fd( s, 0, NULL )) == -1) case WS_IPPROTO_TCP:
switch(optname)
{
case WS_TCP_NODELAY:
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_TCP optname 0x%08x\n", optname);
return SOCKET_ERROR; return SOCKET_ERROR;
if (!convert_sockopt(&level, &optname)) { case WS_IPPROTO_IP:
SetLastError(WSAENOPROTOOPT); /* Unknown option */ switch(optname)
ret = SOCKET_ERROR; {
} else { case WS_IP_ADD_MEMBERSHIP:
struct timeval tv; case WS_IP_DROP_MEMBERSHIP:
struct linger lingval; #ifdef IP_HDRINCL
unsigned int len, *plen = (unsigned int*)optlen; case WS_IP_HDRINCL:
char *pval = optval; #endif
if(level == SOL_SOCKET && is_timeout_option(optname)) { case WS_IP_MULTICAST_IF:
len = sizeof(tv); case WS_IP_MULTICAST_LOOP:
plen = &len; case WS_IP_MULTICAST_TTL:
pval = (char *) &tv; case WS_IP_OPTIONS:
} else if( level == SOL_SOCKET && optname == SO_LINGER) { case WS_IP_TOS:
len = sizeof(lingval); case WS_IP_TTL:
plen = &len; if ( (fd = get_sock_fd( s, 0, NULL )) == -1)
pval = (char *) &lingval; return SOCKET_ERROR;
} convert_sockopt(&level, &optname);
if (getsockopt(fd, (int) level, optname, pval, plen) != 0 ) { if (getsockopt(fd,(int) level, optname, optval,
SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno()); (unsigned int *)optlen) != 0 )
ret = SOCKET_ERROR; {
} else if(level == SOL_SOCKET && is_timeout_option(optname)) { SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
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; ret = SOCKET_ERROR;
} }
release_sock_fd( s, fd );
return ret;
} }
} FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname);
release_sock_fd( s, fd ); return SOCKET_ERROR;
return ret;
}
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();
} }