742 cs130_tom 1.1 {
743 if (ptb->se_len >= size ) return ptb->se_buffer;
744 HeapFree( GetProcessHeap(), 0, ptb->se_buffer );
745 }
746 ptb->se_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->se_len = size) );
747 if (!ptb->se_buffer) SetLastError(WSAENOBUFS);
748 return ptb->se_buffer;
749 }
750
751 static struct WS_protoent *check_buffer_pe(int size)
752 {
753 struct per_thread_data * ptb = get_per_thread_data();
754 if (ptb->pe_buffer)
755 {
756 if (ptb->pe_len >= size ) return ptb->pe_buffer;
757 HeapFree( GetProcessHeap(), 0, ptb->pe_buffer );
758 }
759 ptb->pe_buffer = HeapAlloc( GetProcessHeap(), 0, (ptb->pe_len = size) );
760 if (!ptb->pe_buffer) SetLastError(WSAENOBUFS);
761 return ptb->pe_buffer;
762 }
763 cs130_tom 1.1
764 /* ----------------------------------- i/o APIs */
765
766 #ifdef HAVE_IPX
767 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX)
768 #else
769 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET)
770 #endif
771
772
773 /**********************************************************************/
774
775 /* Returns the converted address if successful, NULL if it was too small to
776 * start with. Note that the returned pointer may be the original pointer
777 * if no conversion is necessary.
778 */
779 static const struct sockaddr* ws_sockaddr_ws2u(const struct WS_sockaddr* wsaddr, int wsaddrlen, int *uaddrlen)
780 {
781 switch (wsaddr->sa_family)
782 {
783 #ifdef HAVE_IPX
784 cs130_tom 1.1 case WS_AF_IPX:
785 {
786 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
787 struct sockaddr_ipx* uipx;
788
789 if (wsaddrlen<sizeof(struct WS_sockaddr_ipx))
790 return NULL;
791
792 *uaddrlen=sizeof(struct sockaddr_ipx);
793 uipx=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *uaddrlen);
794 uipx->sipx_family=AF_IPX;
795 uipx->sipx_port=wsipx->sa_socket;
796 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
797 * in one go
798 */
799 memcpy(&uipx->sipx_network,wsipx->sa_netnum,sizeof(uipx->sipx_network)+sizeof(uipx->sipx_node));
800 #ifdef IPX_FRAME_NONE
801 uipx->sipx_type=IPX_FRAME_NONE;
802 #endif
803 return (const struct sockaddr*)uipx;
804 }
805 cs130_tom 1.1 #endif
806
807 default:
808 if (wsaddrlen<sizeof(struct WS_sockaddr))
809 return NULL;
810
811 /* No conversion needed, just return the original address */
812 *uaddrlen=wsaddrlen;
813 return (const struct sockaddr*)wsaddr;
814 }
815 }
816
817 /* Allocates a Unix sockaddr structure to receive the data */
818 inline struct sockaddr* ws_sockaddr_alloc(const struct WS_sockaddr* wsaddr, int* wsaddrlen, int* uaddrlen)
819 {
820 if (wsaddr==NULL)
821 {
822 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
823
824 /* This is not strictly the right thing to do. Hope it works however */
825 *uaddrlen=0;
826 cs130_tom 1.1
827 return NULL;
828 }
829
830 if (*wsaddrlen==0)
831 *uaddrlen=0;
832 else
833 *uaddrlen=max(sizeof(struct sockaddr),*wsaddrlen);
834
835 return HeapAlloc(GetProcessHeap(), 0, *uaddrlen);
836 }
837
838 /* Returns 0 if successful, -1 if the buffer is too small */
839 static int ws_sockaddr_u2ws(const struct sockaddr* uaddr, int uaddrlen, struct WS_sockaddr* wsaddr, int* wsaddrlen)
840 {
841 int res;
842
843 switch(uaddr->sa_family)
844 {
845 #ifdef HAVE_IPX
846 case AF_IPX:
847 cs130_tom 1.1 {
848 struct sockaddr_ipx* uipx=(struct sockaddr_ipx*)uaddr;
849 struct WS_sockaddr_ipx* wsipx=(struct WS_sockaddr_ipx*)wsaddr;
850
851 res=-1;
852 switch (*wsaddrlen) /* how much can we copy? */
853 {
854 default:
855 res=0; /* enough */
856 *wsaddrlen=uaddrlen;
857 wsipx->sa_socket=uipx->sipx_port;
858 /* fall through */
859 case 13:
860 case 12:
861 memcpy(wsipx->sa_nodenum,uipx->sipx_node,sizeof(wsipx->sa_nodenum));
862 /* fall through */
863 case 11:
864 case 10:
865 case 9:
866 case 8:
867 case 7:
868 cs130_tom 1.1 case 6:
869 memcpy(wsipx->sa_netnum,&uipx->sipx_network,sizeof(wsipx->sa_netnum));
870 /* fall through */
871 case 5:
872 case 4:
873 case 3:
874 case 2:
875 wsipx->sa_family=WS_AF_IPX;
876 /* fall through */
877 case 1:
878 case 0:
879 /* way too small */
880 break;
881 }
882 }
883 break;
884 #endif
885
886 default:
887 /* No conversion needed */
888 memcpy(wsaddr,uaddr,*wsaddrlen);
889 cs130_tom 1.1 if (*wsaddrlen<uaddrlen) {
890 res=-1;
891 } else {
892 *wsaddrlen=uaddrlen;
893 res=0;
894 }
895 }
896 return res;
897 }
898
899 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
900 * ws_sockaddr_alloc
901 */
902 inline void ws_sockaddr_free(const struct sockaddr* uaddr, const struct WS_sockaddr* wsaddr)
903 {
904 if (uaddr!=(const struct sockaddr*)wsaddr)
905 HeapFree(GetProcessHeap(), 0, (void *)uaddr);
906 }
907
908 /**************************************************************************
909 * Functions for handling overlapped I/O
910 cs130_tom 1.1 **************************************************************************/
911
912 static void CALLBACK ws2_async_terminate(ws2_async* as, IO_STATUS_BLOCK* iosb)
913 {
914 TRACE( "as: %p uovl %p ovl %p\n", as, as->user_overlapped, iosb );
915
916 wine_server_release_fd( as->hSocket, as->fd );
917 if ( as->event != INVALID_HANDLE_VALUE )
918 NtSetEvent( as->event, NULL );
919
920 if (as->completion_func)
921 as->completion_func( NtStatusToWSAError (iosb->u.Status),
922 iosb->Information, as->user_overlapped, as->flags );
923 if ( !as->user_overlapped )
924 {
925 #if 0
926 /* FIXME: I don't think this is really used */
927 if ( as->overlapped->hEvent != INVALID_HANDLE_VALUE )
928 WSACloseEvent( as->overlapped->hEvent );
929 #endif
930 HeapFree( GetProcessHeap(), 0, iosb );
931 cs130_tom 1.1 }
932
933 HeapFree( GetProcessHeap(), 0, as->iovec );
934 HeapFree( GetProcessHeap(), 0, as );
935 }
936
937 /***********************************************************************
938 * WS2_make_async (INTERNAL)
939 */
940
941 static void WINAPI WS2_async_recv(void*, IO_STATUS_BLOCK*, ULONG);
942 static void WINAPI WS2_async_send(void*, IO_STATUS_BLOCK*, ULONG);
943 static void WINAPI WS2_async_shutdown( void*, IO_STATUS_BLOCK*, ULONG);
944
945 inline static struct ws2_async*
946 WS2_make_async(SOCKET s, int fd, enum ws2_mode mode, struct iovec *iovec, DWORD dwBufferCount,
947 LPDWORD lpFlags, struct WS_sockaddr *addr,
948 LPINT addrlen, LPWSAOVERLAPPED lpOverlapped,
949 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
950 IO_STATUS_BLOCK **piosb)
951 {
952 cs130_tom 1.1 struct ws2_async *wsa = HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async ) );
953
954 TRACE( "wsa %p\n", wsa );
955
956 if (!wsa)
957 return NULL;
958
959 wsa->hSocket = (HANDLE) s;
960 wsa->mode = mode;
961 switch (mode)
962 {
963 case ws2m_read:
964 case ws2m_sd_read:
965 wsa->flags = *lpFlags;
966 wsa->addrlen.ptr = addrlen;
967 break;
968 case ws2m_write:
969 case ws2m_sd_write:
970 wsa->flags = 0;
971 wsa->addrlen.val = *addrlen;
972 break;
973 cs130_tom 1.1 default:
974 ERR("Invalid async mode: %d\n", mode);
975 }
976 wsa->user_overlapped = lpOverlapped;
977 wsa->completion_func = lpCompletionRoutine;
978 wsa->iovec = iovec;
979 wsa->n_iovecs = dwBufferCount;
980 wsa->addr = addr;
981 wsa->fd = fd;
982 wsa->event = INVALID_HANDLE_VALUE;
983
984 if ( lpOverlapped )
985 {
986 *piosb = (IO_STATUS_BLOCK*)lpOverlapped;
987 if (!lpCompletionRoutine)
988 {
989 wsa->event = lpOverlapped->hEvent;
990 NtResetEvent(wsa->event, NULL);
991 }
992 }
993 else if (!(*piosb = HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK))))
994 cs130_tom 1.1 goto error;
995
996 (*piosb)->Information = 0;
997 (*piosb)->u.Status = STATUS_PENDING;
998 TRACE( "wsa %p, h %p, ev %p, fd %d, iosb %p, uov %p, cfunc %p\n",
999 wsa, wsa->hSocket, wsa->event, wsa->fd,
1000 *piosb, wsa->user_overlapped, wsa->completion_func );
1001
1002 return wsa;
1003
1004 error:
1005 TRACE("Error\n");
1006 HeapFree( GetProcessHeap(), 0, wsa );
1007 return NULL;
1008 }
1009
1010 static ULONG ws2_queue_async(struct ws2_async* wsa, IO_STATUS_BLOCK* iosb)
1011 {
1012 PIO_APC_ROUTINE apc;
1013 int type;
1014 NTSTATUS status;
1015 cs130_tom 1.1
1016 switch (wsa->mode)
1017 {
1018 case ws2m_read: apc = WS2_async_recv; type = ASYNC_TYPE_READ; break;
1019 case ws2m_write: apc = WS2_async_send; type = ASYNC_TYPE_WRITE; break;
1020 case ws2m_sd_read: apc = WS2_async_shutdown; type = ASYNC_TYPE_READ; break;
1021 case ws2m_sd_write: apc = WS2_async_shutdown; type = ASYNC_TYPE_WRITE; break;
1022 default: FIXME("Unknown internal mode (%d)\n", wsa->mode); return STATUS_INVALID_PARAMETER;
1023 }
1024
1025 SERVER_START_REQ( register_async )
1026 {
1027 req->handle = wsa->hSocket;
1028 req->io_apc = apc;
1029 req->io_sb = iosb;
1030 req->io_user = wsa;
1031 req->type = type;
1032 req->count = iosb->Information;
1033 status = wine_server_call( req );
1034 }
1035 SERVER_END_REQ;
1036 cs130_tom 1.1
1037 if ( status ) iosb->u.Status = status;
1038 if ( iosb->u.Status != STATUS_PENDING )
1039 {
1040 (apc)( wsa, iosb, iosb->u.Status );
1041 return iosb->u.Status;
1042 }
1043 NtCurrentTeb()->num_async_io++;
1044 return STATUS_SUCCESS;
1045 }
1046
1047 /***********************************************************************
1048 * WS2_recv (INTERNAL)
1049 *
1050 * Workhorse for both synchronous and asynchronous recv() operations.
1051 */
1052 static int WS2_recv( int fd, struct iovec* iov, int count,
1053 struct WS_sockaddr *lpFrom, LPINT lpFromlen,
1054 LPDWORD lpFlags )
1055 {
1056 struct msghdr hdr;
1057 cs130_tom 1.1 int n;
1058 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %lx\n",
1059 fd, iov, count, debugstr_sockaddr(lpFrom), lpFromlen, *lpFlags);
1060
1061 hdr.msg_name = NULL;
1062
1063 if ( lpFrom )
1064 {
1065 hdr.msg_namelen = *lpFromlen;
1066 hdr.msg_name = ws_sockaddr_alloc( lpFrom, lpFromlen, &hdr.msg_namelen );
1067 if ( !hdr.msg_name )
1068 {
1069 WSASetLastError( WSAEFAULT );
1070 n = -1;
1071 goto out;
1072 }
1073 }
1074 else
1075 hdr.msg_namelen = 0;
1076
1077 hdr.msg_iov = iov;
1078 cs130_tom 1.1 hdr.msg_iovlen = count;
1079 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1080 hdr.msg_accrights = NULL;
1081 hdr.msg_accrightslen = 0;
1082 #else
1083 hdr.msg_control = NULL;
1084 hdr.msg_controllen = 0;
1085 hdr.msg_flags = 0;
1086 #endif
1087
1088 if ( (n = recvmsg(fd, &hdr, *lpFlags)) == -1 )
1089 {
1090 TRACE( "recvmsg error %d\n", errno);
1091 goto out;
1092 }
1093
1094 if ( lpFrom &&
1095 ws_sockaddr_u2ws( hdr.msg_name, hdr.msg_namelen,
1096 lpFrom, lpFromlen ) != 0 )
1097 {
1098 /* The from buffer was too small, but we read the data
1099 cs130_tom 1.1 * anyway. Is that really bad?
1100 */
1101 WSASetLastError( WSAEFAULT );
1102 WARN( "Address buffer too small\n" );
1103 }
1104
1105 out:
1106
1107 ws_sockaddr_free( hdr.msg_name, lpFrom );
1108 TRACE("-> %d\n", n);
1109 return n;
1110 }
1111
1112 /***********************************************************************
1113 * WS2_async_recv (INTERNAL)
1114 *
1115 * Handler for overlapped recv() operations.
1116 */
1117 static void WINAPI WS2_async_recv( void* ovp, IO_STATUS_BLOCK* iosb, ULONG status)
1118 {
1119 ws2_async* wsa = (ws2_async*) ovp;
1120 cs130_tom 1.1 int result, err;
1121
1122 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1123
1124 switch (status)
1125 {
1126 case STATUS_ALERTED:
1127 result = WS2_recv( wsa->fd, wsa->iovec, wsa->n_iovecs,
1128 wsa->addr, wsa->addrlen.ptr, &wsa->flags );
1129 if (result >= 0)
1130 {
1131 iosb->u.Status = STATUS_SUCCESS;
1132 iosb->Information = result;
1133 TRACE( "received %d bytes\n", result );
1134 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1135 }
1136 else
1137 {
1138 err = wsaErrno();
1139 if ( err == WSAEINTR || err == WSAEWOULDBLOCK ) /* errno: EINTR / EAGAIN */
1140 {
1141 cs130_tom 1.1 iosb->u.Status = STATUS_PENDING;
1142 _enable_event( wsa->hSocket, FD_READ, 0, 0 );
1143 TRACE( "still pending\n" );
1144 }
1145 else
1146 {
1147 iosb->u.Status = err; /* FIXME: is this correct ???? */
1148 TRACE( "Error: %x\n", err );
1149 }
1150 }
1151 if (iosb->u.Status == STATUS_PENDING)
1152 ws2_queue_async(wsa, iosb);
1153 else
1154 ws2_async_terminate(wsa, iosb);
1155 break;
1156 default:
1157 FIXME( "status: %ld\n", status );
1158 iosb->u.Status = status;
1159 ws2_async_terminate(wsa, iosb);
1160 return;
1161 }
1162 cs130_tom 1.1 }
1163
1164 /***********************************************************************
1165 * WS2_send (INTERNAL)
1166 *
1167 * Workhorse for both synchronous and asynchronous send() operations.
1168 */
1169 static int WS2_send( int fd, struct iovec* iov, int count,
1170 const struct WS_sockaddr *to, INT tolen, DWORD dwFlags )
1171 {
1172 struct msghdr hdr;
1173 int n = -1;
1174 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %lx\n",
1175 fd, iov, count, debugstr_sockaddr(to), tolen, dwFlags);
1176
1177 hdr.msg_name = NULL;
1178
1179 if ( to )
1180 {
1181 hdr.msg_name = (struct sockaddr*) ws_sockaddr_ws2u( to, tolen, &hdr.msg_namelen );
1182 if ( !hdr.msg_name )
1183 cs130_tom 1.1 {
1184 WSASetLastError( WSAEFAULT );
1185 goto out;
1186 }
1187
1188 #ifdef HAVE_IPX
1189 if(to->sa_family == WS_AF_IPX)
1190 {
1191 #ifdef SOL_IPX
1192 struct sockaddr_ipx* uipx = (struct sockaddr_ipx*)hdr.msg_name;
1193 int val=0;
1194 int len=sizeof(int);
1195
1196 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1197 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1198 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1199 * ipx type in the sockaddr_opx structure with the stored value.
1200 */
1201 if(getsockopt(fd, SOL_IPX, IPX_TYPE, &val, &len) != -1)
1202 {
1203 TRACE("ptype: %d (fd:%d)\n", val, fd);
1204 cs130_tom 1.1 uipx->sipx_type = val;
1205 }
1206 #endif
1207 }
1208 #endif
1209
1210 }
1211 else
1212 hdr.msg_namelen = 0;
1213
1214 hdr.msg_iov = iov;
1215 hdr.msg_iovlen = count;
1216 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1217 hdr.msg_accrights = NULL;
1218 hdr.msg_accrightslen = 0;
1219 #else
1220 hdr.msg_control = NULL;
1221 hdr.msg_controllen = 0;
1222 hdr.msg_flags = 0;
1223 #endif
1224
1225 cs130_tom 1.1 n = sendmsg(fd, &hdr, dwFlags);
1226
1227 out:
1228 ws_sockaddr_free( hdr.msg_name, to );
1229 return n;
1230 }
1231
1232 /***********************************************************************
1233 * WS2_async_send (INTERNAL)
1234 *
1235 * Handler for overlapped send() operations.
1236 */
1237 static void WINAPI WS2_async_send(void* as, IO_STATUS_BLOCK* iosb, ULONG status)
1238 {
1239 ws2_async* wsa = (ws2_async*) as;
1240 int result;
1241
1242 TRACE( "(%p %p %lx)\n", wsa, iosb, status );
1243
1244 switch (status)
1245 {
1246 cs130_tom 1.1 case STATUS_ALERTED:
1247 if (iosb->u.Status != STATUS_PENDING) FIXME("wrong %08lx\n", iosb->u.Status);
1248 /* check to see if the data is ready (non-blocking) */
1249 result = WS2_send( wsa->fd, wsa->iovec, wsa->n_iovecs,
1250 wsa->addr, wsa->addrlen.val, wsa->flags );
1251
1252 if (result >= 0)
1253 {
1254 iosb->u.Status = STATUS_SUCCESS;
1255 iosb->Information = result;
1256 TRACE( "sent %d bytes\n", result );
1257 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1258 }
1259 else
1260 {
1261 int err = wsaErrno();
1262 if ( err == WSAEINTR )
1263 {
1264 iosb->u.Status = STATUS_PENDING;
1265 _enable_event( wsa->hSocket, FD_WRITE, 0, 0 );
1266 TRACE( "still pending\n" );
1267 cs130_tom 1.1 }
1268 else
1269 {
1270 /* We set the status to a winsock error code and check for that
1271 later in NtStatusToWSAError () */
1272 iosb->u.Status = err;
1273 TRACE( "Error: %x\n", err );
1274 }
1275 }
1276 if (iosb->u.Status == STATUS_PENDING)
1277 ws2_queue_async(wsa, iosb);
1278 else
1279 ws2_async_terminate(wsa, iosb);
1280 break;
1281 default:
1282 FIXME( "status: %ld\n", status );
1283 iosb->u.Status = status;
1284 ws2_async_terminate(wsa, iosb);
1285 return;
1286 }
1287
1288 cs130_tom 1.1 }
1289
1290 /***********************************************************************
1291 * WS2_async_shutdown (INTERNAL)
1292 *
1293 * Handler for shutdown() operations on overlapped sockets.
1294 */
1295 static void WINAPI WS2_async_shutdown( void* as, PIO_STATUS_BLOCK iosb, ULONG status )
1296 {
1297 ws2_async* wsa = (ws2_async*) as;
1298 int err = 1;
1299
1300 TRACE( "async %p %d\n", wsa, wsa->mode );
1301 switch (status)
1302 {
1303 case STATUS_ALERTED:
1304 switch ( wsa->mode )
1305 {
1306 case ws2m_sd_read: err = shutdown( wsa->fd, 0 ); break;
1307 case ws2m_sd_write: err = shutdown( wsa->fd, 1 ); break;
1308 default: ERR("invalid mode: %d\n", wsa->mode );
1309 cs130_tom 1.1 }
1310 iosb->u.Status = err ? wsaErrno() : STATUS_SUCCESS;
1311 if (iosb->u.Status == STATUS_PENDING)
1312 ws2_queue_async(wsa, iosb);
1313 else
1314 ws2_async_terminate(wsa, iosb);
1315 break;
1316 default:
1317 iosb->u.Status = status;
1318 ws2_async_terminate(wsa, iosb);
1319 break;
1320 }
1321
1322 }
1323
1324 /***********************************************************************
1325 * WS2_register_async_shutdown (INTERNAL)
1326 *
1327 * Helper function for WS_shutdown() on overlapped sockets.
1328 */
1329 static int WS2_register_async_shutdown( SOCKET s, int fd, enum ws2_mode mode )
1330 cs130_tom 1.1 {
1331 struct ws2_async *wsa;
1332 int ret, err = WSAEFAULT;
1333 DWORD dwflags = 0;
1334 int len = 0;
1335 LPWSAOVERLAPPED ovl = HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED ));
1336 IO_STATUS_BLOCK *iosb;
1337
1338 TRACE("s %d fd %d mode %d\n", s, fd, mode);
1339 if (!ovl)
1340 goto out;
1341
1342 ovl->hEvent = WSACreateEvent();
1343 if ( ovl->hEvent == WSA_INVALID_EVENT )
1344 goto out_free;
1345
1346 wsa = WS2_make_async( s, fd, mode, NULL, 0,
1347 &dwflags, NULL, &len, ovl, NULL, &iosb );
1348 if ( !wsa )
1349 goto out_close;
1350
1351 cs130_tom 1.1 /* Hack: this will cause ws2_async_cleanup() to free the overlapped structure */
1352 wsa->user_overlapped = NULL;
1353 if ( (ret = ws2_queue_async( wsa, iosb )) )
1354 {
1355 err = NtStatusToWSAError( ret );
1356 goto out;
1357 }
1358 /* Try immediate completion */
1359 while ( WaitForSingleObjectEx( ovl->hEvent, 0, TRUE ) == STATUS_USER_APC );
1360 return 0;
1361
1362 out_close:
1363 WSACloseEvent( ovl->hEvent );
1364 out_free:
1365 HeapFree( GetProcessHeap(), 0, iosb );
1366 HeapFree( GetProcessHeap(), 0, ovl );
1367 out:
1368 return err;
1369 }
1370
1371 /***********************************************************************
1372 cs130_tom 1.1 * accept (WS2_32.1)
1373 */
1374 SOCKET WINAPI WS_accept(SOCKET s, struct WS_sockaddr *addr,
1375 int *addrlen32)
1376 {
1377 SOCKET as;
1378
1379 TRACE("socket %04x\n", s );
1380 if (_is_blocking(s))
1381 {
1382 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1383 if (fd == -1) return INVALID_SOCKET;
1384 /* block here */
1385 do_block(fd, POLLIN, -1);
1386 _sync_sock_state(s); /* let wineserver notice connection */
1387 release_sock_fd( s, fd );
1388 /* retrieve any error codes from it */
1389 SetLastError(_get_sock_error(s, FD_ACCEPT_BIT));
1390 /* FIXME: care about the error? */
1391 }
1392 SERVER_START_REQ( accept_socket )
1393 cs130_tom 1.1 {
1394 req->lhandle = SOCKET2HANDLE(s);
1395 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
1396 req->inherit = TRUE;
1397 set_error( wine_server_call( req ) );
1398 as = HANDLE2SOCKET( reply->handle );
1399 }
1400 SERVER_END_REQ;
1401 if (as)
1402 {
1403 if (addr) WS_getpeername(as, addr, addrlen32);
1404 return as;
1405 }
1406 return INVALID_SOCKET;
1407 }
1408
1409 /***********************************************************************
1410 * bind (WS2_32.2)
1411 */
1412 int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen)
1413 {
1414 cs130_tom 1.1 int fd = get_sock_fd( s, 0, NULL );
1415 int res = SOCKET_ERROR;
1416
1417 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1418
1419 if (fd != -1)
1420 {
1421 if (!name || !SUPPORTED_PF(name->sa_family))
1422 {
1423 SetLastError(WSAEAFNOSUPPORT);
1424 }
1425 else
1426 {
1427 const struct sockaddr* uaddr;
1428 int uaddrlen;
1429
1430 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1431 if (uaddr == NULL)
1432 {
1433 SetLastError(WSAEFAULT);
1434 }
1435 cs130_tom 1.1 else
1436 {
1437 if (bind(fd, uaddr, uaddrlen) < 0)
1438 {
1439 int loc_errno = errno;
1440 WARN("\tfailure - errno = %i\n", errno);
1441 errno = loc_errno;
1442 switch (errno)
1443 {
1444 case EBADF:
1445 SetLastError(WSAENOTSOCK);
1446 break;
1447 case EADDRNOTAVAIL:
1448 SetLastError(WSAEINVAL);
1449 break;
1450 default:
1451 SetLastError(wsaErrno());
1452 break;
1453 }
1454 }
1455 else
1456 cs130_tom 1.1 {
1457 res=0; /* success */
1458 }
1459 ws_sockaddr_free(uaddr,name);
1460 }
1461 }
1462 release_sock_fd( s, fd );
1463 }
1464 return res;
1465 }
1466
1467 /***********************************************************************
1468 * closesocket (WS2_32.3)
1469 */
1470 int WINAPI WS_closesocket(SOCKET s)
1471 {
1472 TRACE("socket %04x\n", s);
1473 if (CloseHandle(SOCKET2HANDLE(s))) return 0;
1474 return SOCKET_ERROR;
1475 }
1476
1477 cs130_tom 1.1 /***********************************************************************
1478 * connect (WS2_32.4)
1479 */
1480 int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
1481 {
1482 int fd = get_sock_fd( s, GENERIC_READ, NULL );
1483
1484 TRACE("socket %04x, ptr %p %s, length %d\n", s, name, debugstr_sockaddr(name), namelen);
1485
1486 if (fd != -1)
1487 {
1488 const struct sockaddr* uaddr;
1489 int uaddrlen;
1490
1491 uaddr=ws_sockaddr_ws2u(name,namelen,&uaddrlen);
1492 if (uaddr == NULL)
1493 {
1494 SetLastError(WSAEFAULT);
1495 }
1496 else
1497 {
1498 cs130_tom 1.1 int rc;
1499
1500 rc=connect(fd, uaddr, uaddrlen);
1501 ws_sockaddr_free(uaddr,name);
1502 if (rc == 0)
1503 goto connect_success;
1504 }
1505
1506 if (errno == EINPROGRESS)
1507 {
1508 /* tell wineserver that a connection is in progress */
1509 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1510 FD_CONNECT|FD_READ|FD_WRITE,
1511 FD_WINE_CONNECTED|FD_WINE_LISTENING);
1512 if (_is_blocking(s))
1513 {
1514 int result;
1515 /* block here */
1516 do_block(fd, POLLIN | POLLOUT, -1);
1517 _sync_sock_state(s); /* let wineserver notice connection */
1518 /* retrieve any error codes from it */
1519 cs130_tom 1.1 result = _get_sock_error(s, FD_CONNECT_BIT);
1520 if (result)
1521 SetLastError(result);
1522 else
1523 {
1524 goto connect_success;
1525 }
1526 }
1527 else
1528 {
1529 SetLastError(WSAEWOULDBLOCK);
1530 }
1531 }
1532 else
1533 {
1534 SetLastError(wsaErrno());
1535 }
1536 release_sock_fd( s, fd );
1537 }
1538 return SOCKET_ERROR;
1539
1540 cs130_tom 1.1 connect_success:
1541 release_sock_fd( s, fd );
1542 _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
1543 FD_WINE_CONNECTED|FD_READ|FD_WRITE,
1544 FD_CONNECT|FD_WINE_LISTENING);
1545 return 0;
1546 }
1547
1548 /***********************************************************************
1549 * WSAConnect (WS2_32.30)
1550 */
1551 int WINAPI WSAConnect( SOCKET s, const struct WS_sockaddr* name, int namelen,
1552 LPWSABUF lpCallerData, LPWSABUF lpCalleeData,
1553 LPQOS lpSQOS, LPQOS lpGQOS )
1554 {
1555 if ( lpCallerData || lpCalleeData || lpSQOS || lpGQOS )
1556 FIXME("unsupported parameters!\n");
1557 return WS_connect( s, name, namelen );
1558 }
1559
1560
1561 cs130_tom 1.1 /***********************************************************************
1562 * getpeername (WS2_32.5)
1563 */
1564 int WINAPI WS_getpeername(SOCKET s, struct WS_sockaddr *name, int *namelen)
1565 {
1566 int fd;
1567 int res;
1568
1569 TRACE("socket: %04x, ptr %p, len %08x\n", s, name, *namelen);
1570
1571 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1572 if( (name == NULL) || (namelen == NULL) )
1573 {
1574 SetLastError( WSAEFAULT );
1575 return SOCKET_ERROR;
1576 }
1577
1578 fd = get_sock_fd( s, 0, NULL );
1579 res = SOCKET_ERROR;
1580
1581 if (fd != -1)
1582 cs130_tom 1.1 {
1583 struct sockaddr* uaddr;
1584 int uaddrlen;
1585
1586 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1587 if (getpeername(fd, uaddr, &uaddrlen) != 0)
1588 {
1589 SetLastError(wsaErrno());
1590 }
1591 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1592 {
1593 /* The buffer was too small */
1594 SetLastError(WSAEFAULT);
1595 }
1596 else
1597 {
1598 res=0;
1599 }
1600 ws_sockaddr_free(uaddr,name);
1601 release_sock_fd( s, fd );
1602 }
1603 cs130_tom 1.1 return res;
1604 }
1605
1606 /***********************************************************************
1607 * getsockname (WS2_32.6)
1608 */
1609 int WINAPI WS_getsockname(SOCKET s, struct WS_sockaddr *name, int *namelen)
1610 {
1611 int fd;
1612 int res;
1613
1614 TRACE("socket: %04x, ptr %p, len %8x\n", s, name, *namelen);
1615
1616 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1617 if( (name == NULL) || (namelen == NULL) )
1618 {
1619 SetLastError( WSAEFAULT );
1620 return SOCKET_ERROR;
1621 }
1622
1623 fd = get_sock_fd( s, 0, NULL );
1624 cs130_tom 1.1 res = SOCKET_ERROR;
1625
1626 if (fd != -1)
1627 {
1628 struct sockaddr* uaddr;
1629 int uaddrlen;
1630
1631 uaddr=ws_sockaddr_alloc(name,namelen,&uaddrlen);
1632 if (getsockname(fd, uaddr, &uaddrlen) != 0)
1633 {
1634 SetLastError(wsaErrno());
1635 }
1636 else if (ws_sockaddr_u2ws(uaddr,uaddrlen,name,namelen) != 0)
1637 {
1638 /* The buffer was too small */
1639 SetLastError(WSAEFAULT);
1640 }
1641 else
1642 {
1643 res=0;
1644 }
1645 cs130_tom 1.1 ws_sockaddr_free(uaddr,name);
1646 release_sock_fd( s, fd );
1647 }
1648 return res;
1649 }
1650
1651 /***********************************************************************
1652 * getsockopt (WS2_32.7)
1653 */
1654 INT WINAPI WS_getsockopt(SOCKET s, INT level,
1655 INT optname, char *optval, INT *optlen)
1656 {
1657 int fd;
1658 INT ret = 0;
1659
1660 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %8x, len %d\n", s, level,
1661 (int) optname, (int) optval, (int) *optlen);
1662 /* SO_OPENTYPE does not require a valid socket handle. */
1663 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
1664 {
1665 if (!optlen || *optlen < sizeof(int) || !optval)
1666 cs130_tom 1.1 {
1667 SetLastError(WSAEFAULT);
1668 return SOCKET_ERROR;
1669 }
1670 *(int *)optval = get_per_thread_data()->opentype;
1671 *optlen = sizeof(int);
1672 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval) );
1673 return 0;
1674 }
1675
1676 #ifdef HAVE_IPX
1677 if(level == NSPROTO_IPX)
1678 {
1679 struct WS_sockaddr_ipx addr;
1680 IPX_ADDRESS_DATA *data;
1681 int namelen;
1682 switch(optname)
1683 {
1684 case IPX_PTYPE:
1685 fd = get_sock_fd( s, 0, NULL );
1686 #ifdef SOL_IPX
1687 cs130_tom 1.1 if(getsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
1688 {
1689 return SOCKET_ERROR;
1690 }
1691 #else
1692 {
1693 struct ipx val;
1694 socklen_t len=sizeof(struct ipx);
1695
1696 if(getsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, &len) == -1 )
1697 return SOCKET_ERROR;
1698 *optval = (int)val.ipx_pt;
1699 }
1700 #endif
1701 TRACE("ptype: %d (fd: %d)\n", *(int*)optval, fd);
1702 release_sock_fd( s, fd );
1703
1704 return 0;
1705 case IPX_ADDRESS:
1706 /*
1707 * On a Win2000 system with one network card there are usually three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
1708 cs130_tom 1.1 * Using this call you can then retrieve info about this all. In case of Linux it is a bit different. Usually you have
1709 * only "one" device active and further it is not possible to query things like the linkspeed.
1710 */
1711 FIXME("IPX_ADDRESS\n");
1712 namelen = sizeof(struct WS_sockaddr);
1713 memset(&addr, 0, sizeof(struct WS_sockaddr));
1714 WS_getsockname(s, (struct WS_sockaddr*)&addr, &namelen);
1715
1716 data = (IPX_ADDRESS_DATA*)optval;
1717 memcpy(data->nodenum,&addr.sa_nodenum,sizeof(data->nodenum));
1718 memcpy(data->netnum,&addr.sa_netnum,sizeof(data->netnum));
1719 data->adapternum = 0;
1720 data->wan = FALSE; /* We are not on a wan for now .. */
1721 data->status = FALSE; /* Since we are not on a wan, the wan link isn't up */
1722 data->maxpkt = 1467; /* This value is the default one, at least on Win2k/WinXP */
1723 data->linkspeed = 100000; /* Set the line speed in 100bit/s to 10 Mbit; note 1MB = 1000kB in this case */
1724 return 0;
1725 case IPX_MAX_ADAPTER_NUM:
1726 FIXME("IPX_MAX_ADAPTER_NUM\n");
1727 *(int*)optval = 1; /* As noted under IPX_ADDRESS we have just one card. */
1728
1729 cs130_tom 1.1 return 0;
1730 default:
1731 FIXME("IPX optname:%x\n", optname);
1732 return SOCKET_ERROR;
1733 }
1734 }
1735 #endif
1736
1737 if( (fd = get_sock_fd( s, 0, NULL )) == -1)
1738 return SOCKET_ERROR;
1739
1740 if (!convert_sockopt(&level, &optname)) {
1741 SetLastError(WSAENOPROTOOPT); /* Unknown option */
1742 ret = SOCKET_ERROR;
1743 } else {
1744 struct timeval tv;
1745 struct linger lingval;
1746 INT len, *plen = optlen;
1747 char *pval = optval;
1748 if(level == SOL_SOCKET && is_timeout_option(optname)) {
1749 len = sizeof(tv);
1750 cs130_tom 1.1 plen = &len;
1751 pval = (char *) &tv;
1752 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1753 len = sizeof(lingval);
1754 plen = &len;
1755 pval = (char *) &lingval;
1756 }
1757 if (getsockopt(fd, (int) level, optname, pval, plen) != 0 ) {
1758 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1759 ret = SOCKET_ERROR;
1760 } else if(level == SOL_SOCKET && is_timeout_option(optname)) {
1761 if( *optlen >= sizeof(INT) ) {
1762 *optlen = sizeof(INT);
1763 *(INT*)optval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
1764 } else {
1765 SetLastError(WSAEFAULT);
1766 ret = SOCKET_ERROR;
1767 }
1768 } else if( level == SOL_SOCKET && optname == SO_LINGER) {
1769 if( *optlen >= sizeof( LINGER) ) {
1770 (( LINGER *) optval)->l_onoff = lingval.l_onoff;
1771 cs130_tom 1.1 (( LINGER *) optval)->l_linger = lingval.l_linger;
1772 } else {
1773 SetLastError(WSAEFAULT);
1774 ret = SOCKET_ERROR;
1775 }
1776 }
1777 }
1778 release_sock_fd( s, fd );
1779 return ret;
1780 }
1781
1782
1783 /***********************************************************************
1784 * htonl (WINSOCK.8)
1785 * htonl (WS2_32.8)
1786 */
1787 u_long WINAPI WS_htonl(u_long hostlong)
1788 {
1789 return htonl(hostlong);
1790 }
1791
1792 cs130_tom 1.1
1793 /***********************************************************************
1794 * htons (WINSOCK.9)
1795 * htons (WS2_32.9)
1796 */
1797 u_short WINAPI WS_htons(u_short hostshort)
1798 {
1799 return htons(hostshort);
1800 }
1801
1802 /***********************************************************************
1803 * WSAHtonl (WS2_32.46)
1804 * From MSDN decription of error codes, this function should also
1805 * check if WinSock has been initialized and the socket is a valid
1806 * socket. But why? This function only translates a host byte order
1807 * u_long into a network byte order u_long...
1808 */
1809 int WINAPI WSAHtonl(SOCKET s, u_long hostlong, u_long *lpnetlong)
1810 {
1811 if (lpnetlong)
1812 {
1813 cs130_tom 1.1 *lpnetlong = htonl(hostlong);
1814 return 0;
1815 }
1816 WSASetLastError(WSAEFAULT);
1817 return SOCKET_ERROR;
1818 }
1819
1820 /***********************************************************************
1821 * WSAHtons (WS2_32.47)
1822 * From MSDN decription of error codes, this function should also
1823 * check if WinSock has been initialized and the socket is a valid
1824 * socket. But why? This function only translates a host byte order
1825 * u_short into a network byte order u_short...
1826 */
1827 int WINAPI WSAHtons(SOCKET s, u_short hostshort, u_short *lpnetshort)
1828 {
1829
1830 if (lpnetshort)
1831 {
1832 *lpnetshort = htons(hostshort);
1833 return 0;
1834 cs130_tom 1.1 }
1835 WSASetLastError(WSAEFAULT);
1836 return SOCKET_ERROR;
1837 }
1838
1839
1840 /***********************************************************************
1841 * inet_addr (WINSOCK.10)
1842 * inet_addr (WS2_32.11)
1843 */
1844 u_long WINAPI WS_inet_addr(const char *cp)
1845 {
1846 return inet_addr(cp);
1847 }
1848
1849
1850 /***********************************************************************
1851 * ntohl (WINSOCK.14)
1852 * ntohl (WS2_32.14)
1853 */
1854 u_long WINAPI WS_ntohl(u_long netlong)
1855 cs130_tom 1.1 {
1856 return ntohl(netlong);
1857 }
1858
1859
1860 /***********************************************************************
1861 * ntohs (WINSOCK.15)
1862 * ntohs (WS2_32.15)
1863 */
1864 u_short WINAPI WS_ntohs(u_short netshort)
1865 {
1866 return ntohs(netshort);
1867 }
1868
1869
1870 /***********************************************************************
1871 * inet_ntoa (WS2_32.12)
1872 */
1873 char* WINAPI WS_inet_ntoa(struct WS_in_addr in)
1874 {
1875 /* use "buffer for dummies" here because some applications have a
1876 cs130_tom 1.1 * propensity to decode addresses in ws_hostent structure without
1877 * saving them first...
1878 */
1879 static char dbuffer[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
1880
1881 char* s = inet_ntoa(*((struct in_addr*)&in));
1882 if( s )
1883 {
1884 strcpy(dbuffer, s);
1885 return dbuffer;
1886 }
1887 SetLastError(wsaErrno());
1888 return NULL;
1889 }
1890
1891 /**********************************************************************
1892 * WSAIoctl (WS2_32.50)
1893 *
1894 *
1895 * FIXME: Only SIO_GET_INTERFACE_LIST option implemented.
1896 */
1897 cs130_tom 1.1 INT WINAPI WSAIoctl(SOCKET s,
1898 DWORD dwIoControlCode,
1899 LPVOID lpvInBuffer,
1900 DWORD cbInBuffer,
1901 LPVOID lpbOutBuffer,
1902 DWORD cbOutBuffer,
1903 LPDWORD lpcbBytesReturned,
1904 LPWSAOVERLAPPED lpOverlapped,
1905 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
1906 {
1907 int fd = get_sock_fd( s, 0, NULL );
1908
1909 if (fd == -1) return SOCKET_ERROR;
1910
1911 TRACE("%d, 0x%08lx, %p, %ld, %p, %ld, %p, %p, %p\n",
1912 s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpbOutBuffer,
1913 cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
1914
1915 switch( dwIoControlCode )
1916 {
1917 case SIO_GET_INTERFACE_LIST:
1918 cs130_tom 1.1 {
1919 INTERFACE_INFO* intArray = (INTERFACE_INFO*)lpbOutBuffer;
1920 DWORD size, numInt, apiReturn;
1921
1922 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
1923
1924 if (!lpbOutBuffer)
1925 {
1926 release_sock_fd( s, fd );
1927 WSASetLastError(WSAEFAULT);
1928 return SOCKET_ERROR;
1929 }
1930 if (!lpcbBytesReturned)
1931 {
1932 release_sock_fd( s, fd );
1933 WSASetLastError(WSAEFAULT);
1934 return SOCKET_ERROR;
1935 }
1936
1937 apiReturn = GetAdaptersInfo(NULL, &size);
1938 if (apiReturn == ERROR_NO_DATA)
1939 cs130_tom 1.1 {
1940 numInt = 0;
1941 }
1942 else if (apiReturn == ERROR_BUFFER_OVERFLOW)
1943 {
1944 PIP_ADAPTER_INFO table = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(),0,size);
1945
1946 if (table)
1947 {
1948 if (GetAdaptersInfo(table, &size) == NO_ERROR)
1949 {
1950 PIP_ADAPTER_INFO ptr;
1951
1952 if (size*sizeof(INTERFACE_INFO)/sizeof(IP_ADAPTER_INFO) > cbOutBuffer)
1953 {
1954 WARN("Buffer too small = %lu, cbOutBuffer = %lu\n", size, cbOutBuffer);
1955 HeapFree(GetProcessHeap(),0,table);
1956 release_sock_fd( s, fd );
1957 WSASetLastError(WSAEFAULT);
1958 return SOCKET_ERROR;
1959 }
1960 cs130_tom 1.1 for (ptr = table, numInt = 0; ptr;
1961 ptr = ptr->Next, intArray++, numInt++)
1962 {
1963 unsigned int addr, mask, bcast;
1964 struct ifreq ifInfo;
1965
1966 /* Socket Status Flags */
1967 strncpy(ifInfo.ifr_name, ptr->AdapterName, IFNAMSIZ);
1968 ifInfo.ifr_name[IFNAMSIZ-1] = '\0';
1969 if (ioctl(fd, SIOCGIFFLAGS, &ifInfo) < 0)
1970 {
1971 ERR("Error obtaining status flags for socket!\n");
1972 HeapFree(GetProcessHeap(),0,table);
1973 release_sock_fd( s, fd );
1974 WSASetLastError(WSAEINVAL);
1975 return SOCKET_ERROR;
1976 }
1977 else
1978 {
1979 /* set flags; the values of IFF_* are not the same
1980 under Linux and Windows, therefore must generate
1981 cs130_tom 1.1 new flags */
1982 intArray->iiFlags = 0;
1983 if (ifInfo.ifr_flags & IFF_BROADCAST)
1984 intArray->iiFlags |= WS_IFF_BROADCAST;
1985 #ifdef IFF_POINTOPOINT
1986 if (ifInfo.ifr_flags & IFF_POINTOPOINT)
1987 intArray->iiFlags |= WS_IFF_POINTTOPOINT;
1988 #endif
1989 if (ifInfo.ifr_flags & IFF_LOOPBACK)
1990 intArray->iiFlags |= WS_IFF_LOOPBACK;
1991 if (ifInfo.ifr_flags & IFF_UP)
1992 intArray->iiFlags |= WS_IFF_UP;
1993 if (ifInfo.ifr_flags & IFF_MULTICAST)
1994 intArray->iiFlags |= WS_IFF_MULTICAST;
1995 }
1996
1997 addr = inet_addr(ptr->IpAddressList.IpAddress.String);
1998 mask = inet_addr(ptr->IpAddressList.IpMask.String);
1999 bcast = addr | (addr & !mask);
2000 intArray->iiAddress.AddressIn.sin_family = AF_INET;
2001 intArray->iiAddress.AddressIn.sin_port = 0;
2002 cs130_tom 1.1 intArray->iiAddress.AddressIn.sin_addr.WS_s_addr =
2003 addr;
2004 intArray->iiNetmask.AddressIn.sin_family = AF_INET;
2005 intArray->iiNetmask.AddressIn.sin_port = 0;
2006 intArray->iiNetmask.AddressIn.sin_addr.WS_s_addr =
2007 mask;
2008 intArray->iiBroadcastAddress.AddressIn.sin_family =
2009 AF_INET;
2010 intArray->iiBroadcastAddress.AddressIn.sin_port = 0;
2011 intArray->iiBroadcastAddress.AddressIn.sin_addr.
2012 WS_s_addr = bcast;
2013 }
2014 }
2015 else
2016 {
2017 ERR("Unable to get interface table!\n");
2018 release_sock_fd( s, fd );
2019 HeapFree(GetProcessHeap(),0,table);
2020 WSASetLastError(WSAEINVAL);
2021 return SOCKET_ERROR;
2022 }
2023 cs130_tom 1.1 HeapFree(GetProcessHeap(),0,table);
2024 }
2025 else
2026 {
2027 release_sock_fd( s, fd );
2028 WSASetLastError(WSAEINVAL);
2029 return SOCKET_ERROR;
2030 }
2031 }
2032 else
2033 {
2034 ERR("Unable to get interface table!\n");
2035 release_sock_fd( s, fd );
2036 WSASetLastError(WSAEINVAL);
2037 return SOCKET_ERROR;
2038 }
2039 /* Calculate the size of the array being returned */
2040 *lpcbBytesReturned = sizeof(INTERFACE_INFO) * numInt;
2041 break;
2042 }
2043
2044 cs130_tom 1.1 case SIO_ADDRESS_LIST_CHANGE:
2045 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2046 /* FIXME: error and return code depend on whether socket was created
2047 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2048 break;
2049
2050 default:
2051 WARN("\tunsupported WS_IOCTL cmd (%08lx)\n", dwIoControlCode);
2052 release_sock_fd( s, fd );
2053 WSASetLastError(WSAEOPNOTSUPP);
2054 return SOCKET_ERROR;
2055 }
2056
2057 /* Function executed with no errors */
2058 release_sock_fd( s, fd );
2059 return 0;
2060 }
2061
2062
2063 /***********************************************************************
2064 * ioctlsocket (WS2_32.10)
2065 cs130_tom 1.1 */
2066 int WINAPI WS_ioctlsocket(SOCKET s, long cmd, u_long *argp)
2067 {
2068 int fd;
2069 long newcmd = cmd;
2070
2071 TRACE("socket %04x, cmd %08lx, ptr %p\n", s, cmd, argp);
2072
2073 switch( cmd )
2074 {
2075 case WS_FIONREAD:
2076 newcmd=FIONREAD;
2077 break;
2078
2079 case WS_FIONBIO:
2080 if( _get_sock_mask(s) )
2081 {
2082 /* AsyncSelect()'ed sockets are always nonblocking */
2083 if (*argp) return 0;
2084 SetLastError(WSAEINVAL);
2085 return SOCKET_ERROR;
2086 cs130_tom 1.1 }
2087 fd = get_sock_fd( s, 0, NULL );
2088 if (fd != -1)
2089 {
2090 int ret;
2091 if (*argp)
2092 {
2093 _enable_event(SOCKET2HANDLE(s), 0, FD_WINE_NONBLOCKING, 0);
2094 ret = fcntl( fd, F_SETFL, O_NONBLOCK );
2095 }
2096 else
2097 {
2098 _enable_event(SOCKET2HANDLE(s), 0, 0, FD_WINE_NONBLOCKING);
2099 ret = fcntl( fd, F_SETFL, 0 );
2100 }
2101 release_sock_fd( s, fd );
2102 if (!ret) return 0;
2103 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2104 }
2105 return SOCKET_ERROR;
2106
2107 cs130_tom 1.1 case WS_SIOCATMARK:
2108 newcmd=SIOCATMARK;
2109 break;
2110
2111 case WS__IOW('f',125,u_long):
2112 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2113 SetLastError(WSAEINVAL);
2114 return SOCKET_ERROR;
2115
2116 case SIOCGIFBRDADDR:
2117 case SIOCGIFNETMASK:
2118 case SIOCGIFADDR:
2119 /* These don't need any special handling. They are used by
2120 WsControl, and are here to suppress an unnecessary warning. */
2121 break;
2122
2123 default:
2124 /* Netscape tries hard to use bogus ioctl 0x667e */
2125 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd);
2126 break;
2127 }
2128 cs130_tom 1.1
2129 fd = get_sock_fd( s, 0, NULL );
2130 if (fd != -1)
2131 {
2132 if( ioctl(fd, newcmd, (char*)argp ) == 0 )
2133 {
2134 release_sock_fd( s, fd );
2135 return 0;
2136 }
2137 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
2138 release_sock_fd( s, fd );
2139 }
2140 return SOCKET_ERROR;
2141 }
2142
2143 /***********************************************************************
2144 * listen (WS2_32.13)
2145 */
2146 int WINAPI WS_listen(SOCKET s, int backlog)
2147 {
2148 int fd = get_sock_fd( s, GENERIC_READ, NULL );
2149 cs130_tom 1.1
2150 TRACE("socket %04x, backlog %d\n", s, backlog);
2151 if (fd != -1)
2152 {
2153 if (listen(fd, backlog) == 0)
2154 {
2155 release_sock_fd( s, fd );
2156 _enable_event(SOCKET2HANDLE(s), FD_ACCEPT,
2157 FD_WINE_LISTENING,
2158 FD_CONNECT|FD_WINE_CONNECTED);
2159 return 0;
2160 }
2161 SetLastError(wsaErrno());
2162 release_sock_fd( s, fd );
2163 }
2164 return SOCKET_ERROR;
2165 }
2166
2167 /***********************************************************************
2168 * recv (WS2_32.16)
2169 */
2170 cs130_tom 1.1 int WINAPI WS_recv(SOCKET s, char *buf, int len, int flags)
2171 {
2172 DWORD n, dwFlags = flags;
2173 WSABUF wsabuf;
2174
2175 wsabuf.len = len;
2176 wsabuf.buf = buf;
2177
2178 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, NULL, NULL, NULL, NULL) == SOCKET_ERROR )
2179 return SOCKET_ERROR;
2180 else
2181 return n;
2182 }
2183
2184 /***********************************************************************
2185 * recvfrom (WS2_32.17)
2186 */
2187 int WINAPI WS_recvfrom(SOCKET s, char *buf, INT len, int flags,
2188 struct WS_sockaddr *from, int *fromlen)
2189 {
2190 DWORD n, dwFlags = flags;
2191 cs130_tom 1.1 WSABUF wsabuf;
2192
2193 wsabuf.len = len;
2194 wsabuf.buf = buf;
2195
2196 if ( WSARecvFrom(s, &wsabuf, 1, &n, &dwFlags, from, fromlen, NULL, NULL) == SOCKET_ERROR )
2197 return SOCKET_ERROR;
2198 else
2199 return n;
2200 }
2201
2202 /***********************************************************************
2203 * select (WS2_32.18)
2204 */
2205 int WINAPI WS_select(int nfds, WS_fd_set *ws_readfds,
2206 WS_fd_set *ws_writefds, WS_fd_set *ws_exceptfds,
2207 const struct WS_timeval* ws_timeout)
2208 {
2209 int highfd = 0;
2210 fd_set readfds, writefds, exceptfds;
2211 fd_set *p_read, *p_write, *p_except;
2212 cs130_tom 1.1 int readfd[FD_SETSIZE], writefd[FD_SETSIZE], exceptfd[FD_SETSIZE];
2213 struct timeval timeout, *timeoutaddr = NULL;
2214
2215 TRACE("read %p, write %p, excp %p timeout %p\n",
2216 ws_readfds, ws_writefds, ws_exceptfds, ws_timeout);
2217
2218 p_read = fd_set_import(&readfds, ws_readfds, GENERIC_READ, &highfd, readfd);
2219 p_write = fd_set_import(&writefds, ws_writefds, GENERIC_WRITE, &highfd, writefd);
2220 p_except = fd_set_import(&exceptfds, ws_exceptfds, 0, &highfd, exceptfd);
2221 if (ws_timeout)
2222 {
2223 timeoutaddr = &timeout;
2224 timeout.tv_sec=ws_timeout->tv_sec;
2225 timeout.tv_usec=ws_timeout->tv_usec;
2226 }
2227
2228 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeoutaddr)) > 0 )
2229 {
2230 fd_set_export(&readfds, p_except, ws_readfds, readfd);
2231 fd_set_export(&writefds, p_except, ws_writefds, writefd);
2232
2233 cs130_tom 1.1 if (p_except && ws_exceptfds)
2234 {
2235 unsigned int i, j;
2236
2237 for (i = j = 0; i < ws_exceptfds->fd_count; i++)
2238 {
2239 int fd = exceptfd[i];
2240 SOCKET s = ws_exceptfds->fd_array[i];
2241 if (fd == -1) continue;
2242 if (FD_ISSET(fd, &exceptfds)) ws_exceptfds->fd_array[j++] = s;
2243 release_sock_fd( s, fd );
2244 }
2245 ws_exceptfds->fd_count = j;
2246 }
2247 return highfd;
2248 }
2249 fd_set_unimport(ws_readfds, readfd);
2250 fd_set_unimport(ws_writefds, writefd);
2251 fd_set_unimport(ws_exceptfds, exceptfd);
2252
2253 if( highfd == 0 ) return 0;
2254 cs130_tom 1.1 SetLastError(wsaErrno());
2255 return SOCKET_ERROR;
2256 }
2257
2258
2259 /***********************************************************************
2260 * send (WS2_32.19)
2261 */
2262 int WINAPI WS_send(SOCKET s, const char *buf, int len, int flags)
2263 {
2264 DWORD n;
2265 WSABUF wsabuf;
2266
2267 wsabuf.len = len;
2268 wsabuf.buf = (char*) buf;
2269
2270 if ( WSASendTo( s, &wsabuf, 1, &n, flags, NULL, 0, NULL, NULL) == SOCKET_ERROR )
2271 return SOCKET_ERROR;
2272 else
2273 return n;
2274 }
2275 cs130_tom 1.1
2276 /***********************************************************************
2277 * WSASend (WS2_32.72)
2278 */
2279 INT WINAPI WSASend( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2280 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2281 LPWSAOVERLAPPED lpOverlapped,
2282 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2283 {
2284 return WSASendTo( s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags,
2285 NULL, 0, lpOverlapped, lpCompletionRoutine );
2286 }
2287
2288 /***********************************************************************
2289 * WSASendDisconnect (WS2_32.73)
2290 */
2291 INT WINAPI WSASendDisconnect( SOCKET s, LPWSABUF lpBuffers )
2292 {
2293 return WS_shutdown( s, SD_SEND );
2294 }
2295
2296 cs130_tom 1.1
2297 /***********************************************************************
2298 * WSASendTo (WS2_32.74)
2299 */
2300 INT WINAPI WSASendTo( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
2301 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
2302 const struct WS_sockaddr *to, int tolen,
2303 LPWSAOVERLAPPED lpOverlapped,
2304 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
2305 {
2306 unsigned int i;
2307 int n, fd, err = WSAENOTSOCK, flags, ret;
2308 struct iovec* iovec;
2309 struct ws2_async *wsa;
2310 IO_STATUS_BLOCK* iosb;
2311
2312 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, to %p, tolen %d, ovl %p, func %p\n",
2313 s, lpBuffers, dwBufferCount, dwFlags,
2314 to, tolen, lpOverlapped, lpCompletionRoutine);
2315
2316 fd = get_sock_fd( s, GENERIC_WRITE, &flags );
2317 cs130_tom 1.1 TRACE( "fd=%d, flags=%x\n", fd, flags );
2318
2319 if ( fd == -1 ) return SOCKET_ERROR;
2320
2321 if (flags & FD_FLAG_SEND_SHUTDOWN)
2322 {
2323 WSASetLastError( WSAESHUTDOWN );
2324 goto err_close;
2325 }
2326
2327 if ( !lpNumberOfBytesSent )
2328 {
2329 err = WSAEFAULT;
2330 goto err_close;
2331 }
2332
2333 iovec = HeapAlloc(GetProcessHeap(), 0, dwBufferCount * sizeof(struct iovec) );
2334
2335 if ( !iovec )
2336 {
2337 err = WSAEFAULT;
2338 cs130_tom 1.1 goto err_close;
2339 }
2340
2341 for ( i = 0; i < dwBufferCount; i++ )
2342 {
2343 iovec[i].iov_base = lpBuffers[i].buf;
2344 iovec[i].iov_len = lpBuffers[i].len;
2345 }
2346
2347 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
2348 {
2349 wsa = WS2_make_async( s, fd, ws2m_write, iovec, dwBufferCount,
2350 &dwFlags, (struct WS_sockaddr*) to, &tolen,
2351 lpOverlapped, lpCompletionRoutine, &iosb );
2352 if ( !wsa )
2353 {
2354 err = WSAEFAULT;
2355 goto err_free;
2356 }
2357
2358 if ( ( ret = ws2_queue_async( wsa, iosb ) ) )
2359 cs130_tom 1.1 {
2360 err = NtStatusToWSAError( ret );
2361
2362 if ( !lpOverlapped )
2363 HeapFree( GetProcessHeap(), 0, iosb );
2364 HeapFree( GetProcessHeap(), 0, wsa );
2365 goto err_free;
2366 }
2367
2368 /* Try immediate completion */
2369 if ( lpOverlapped )
2370 {
2371 if ( WSAGetOverlappedResult( s, lpOverlapped,
2372 lpNumberOfBytesSent, FALSE, &dwFlags) )
2373 return 0;
2374
2375 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
2376 goto error;
2377 }
2378
2379 WSASetLastError( WSA_IO_PENDING );
2380 cs130_tom 1.1 return SOCKET_ERROR;
2381 }
2382
2383 if (_is_blocking(s))
2384 {
2385 /* FIXME: exceptfds? */
2386 int timeout = GET_SNDTIMEO(fd);
2387 if( !do_block(fd, POLLOUT, timeout)) {
2388 err = WSAETIMEDOUT;
2389 goto err_free; /* msdn says a timeout in send is fatal */
2390 }
2391 }
2392
2393 n = WS2_send( fd, iovec, dwBufferCount, to, tolen, dwFlags );
2394 if ( n == -1 )
2395 {
2396 err = wsaErrno();
2397 if ( err == WSAEWOULDBLOCK )
2398 _enable_event(SOCKET2HANDLE(s), FD_WRITE, 0, 0);
2399 goto err_free;
2400 }
2401 cs130_tom 1.1
2402 TRACE(" -> %i bytes\n", n);
2403 *lpNumberOfBytesSent = n;
2404
2405 HeapFree( GetProcessHeap(), 0, iovec );
2406 release_sock_fd( s, fd );
2407 return 0;
2408
2409 err_free:
2410 HeapFree( GetProcessHeap(), 0, iovec );
2411
2412 err_close:
2413 release_sock_fd( s, fd );
2414
2415 error:
2416 WARN(" -> ERROR %d\n", err);
2417 WSASetLastError(err);
2418 return SOCKET_ERROR;
2419 }
2420
2421 /***********************************************************************
2422 cs130_tom 1.1 * sendto (WS2_32.20)
2423 */
2424 int WINAPI WS_sendto(SOCKET s, const char *buf, int len, int flags,
2425 const struct WS_sockaddr *to, int tolen)
2426 {
2427 DWORD n;
2428 WSABUF wsabuf;
2429
2430 wsabuf.len = len;
2431 wsabuf.buf = (char*) buf;
2432
2433 if ( WSASendTo(s, &wsabuf, 1, &n, flags, to, tolen, NULL, NULL) == SOCKET_ERROR )
2434 return SOCKET_ERROR;
2435 else
2436 return n;
2437 }
2438
2439 /***********************************************************************
2440 * setsockopt (WS2_32.21)
2441 */
2442 int WINAPI WS_setsockopt(SOCKET s, int level, int optname,
2443 cs130_tom 1.1 const char *optval, int optlen)
2444 {
2445 int fd;
2446 int woptval;
2447 struct linger linger;
2448 struct timeval tval;
2449
2450 TRACE("socket: %04x, level %d, name %d, ptr %p, len %d\n",
2451 s, level, optname, optval, optlen);
2452
2453 /* SO_OPENTYPE does not require a valid socket handle. */
2454 if (level == WS_SOL_SOCKET && optname == WS_SO_OPENTYPE)
2455 {
2456 if (optlen < sizeof(int) || !optval)
2457 {
2458 SetLastError(WSAEFAULT);
2459 return SOCKET_ERROR;
2460 }
2461 get_per_thread_data()->opentype = *(int *)optval;
2462 TRACE("setting global SO_OPENTYPE to 0x%x\n", *(int *)optval );
2463 return 0;
2464 cs130_tom 1.1 }
2465
2466 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
2467 * socket. This will either not happen under windows or it is ignored in
2468 * windows (but it works in linux and therefore prevents the game from
2469 * finding games outside the current network) */
2470 if ( level==WS_SOL_SOCKET && optname==WS_SO_DONTROUTE )
2471 {
2472 FIXME("Does windows ignore SO_DONTROUTE?\n");
2473 return 0;
2474 }
2475
2476 #ifdef HAVE_IPX
2477 if(level == NSPROTO_IPX)
2478 {
2479 switch(optname)
2480 {
2481 case IPX_PTYPE:
2482 fd = get_sock_fd( s, 0, NULL );
2483 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(int*)optval, fd);
2484
2485 cs130_tom 1.1 /* We try to set the ipx type on ipx socket level. */
2486 #ifdef SOL_IPX
2487 if(setsockopt(fd, SOL_IPX, IPX_TYPE, optval, optlen) == -1)
2488 {
2489 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
2490 return SOCKET_ERROR;
2491 }
2492 #else
2493 {
2494 struct ipx val;
2495 /* Should we retrieve val using a getsockopt call and then
2496 * set the modified one? */
2497 val.ipx_pt = *optval;
2498 setsockopt(fd, 0, SO_DEFAULT_HEADERS, &val, sizeof(struct ipx));
2499 }
2500 #endif
2501 release_sock_fd( s, fd );
2502 return 0;
2503 case IPX_FILTERPTYPE:
2504 /* Sets the receive filter packet type, at the moment we don't support it */
2505 FIXME("IPX_FILTERPTYPE: %x\n", *optval);
2506 cs130_tom 1.1
2507 /* Returning 0 is better for now than returning a SOCKET_ERROR */
2508 return 0;
2509 default:
2510 FIXME("opt_name:%x\n", optname);
2511 return SOCKET_ERROR;
2512 }
2513 return 0;
2514 }
2515 #endif
2516
2517 /* Is a privileged and useless operation, so we don't. */
2518 if ((optname == WS_SO_DEBUG) && (level == WS_SOL_SOCKET))
2519 {
2520 FIXME("(%d,SOL_SOCKET,SO_DEBUG,%p(%ld)) attempted (is privileged). Ignoring.\n",s,optval,*(DWORD*)optval);
2521 return 0;
2522 }
2523
2524 if(optname == WS_SO_DONTLINGER && level == WS_SOL_SOCKET) {
2525 /* This is unique to WinSock and takes special conversion */
2526 linger.l_onoff = *((int*)optval) ? 0: 1;
2527 cs130_tom 1.1 linger.l_linger = 0;
2528 optname=SO_LINGER;
2529 optval = (char*)&linger;
2530 optlen = sizeof(struct linger);
2531 level = SOL_SOCKET;
2532 }
2533 else
2534 {
2535 if (!convert_sockopt(&level, &optname)) {
2536 ERR("Invalid level (%d) or optname (%d)\n", level, optname);
2537 SetLastError(WSAENOPROTOOPT);
2538 return SOCKET_ERROR;
2539 }
2540 if (optname == SO_LINGER && optval) {
2541 linger.l_onoff = ((LINGER*)optval)->l_onoff;
2542 linger.l_linger = ((LINGER*)optval)->l_linger;
2543 /* FIXME: what is documented behavior if SO_LINGER optval
2544 is null?? */
2545 optval = (char*)&linger;
2546 optlen = sizeof(struct linger);
2547 }
2548 cs130_tom 1.1 else if (optval && optlen < sizeof(int))
2549 {
2550 woptval= *((INT16 *) optval);
2551 optval= (char*) &woptval;
2552 optlen=sizeof(int);
2553 }
2554 if (level == SOL_SOCKET && is_timeout_option(optname))
2555 {
2556 if (optlen == sizeof(UINT32)) {
2557 /* WinSock passes miliseconds instead of struct timeval */
2558 tval.tv_usec = (*(PUINT32)optval % 1000) * 1000;
2559 tval.tv_sec = *(PUINT32)optval / 1000;
2560 /* min of 500 milisec */
2561 if (tval.tv_sec == 0 && tval.tv_usec < 500000)
2562 tval.tv_usec = 500000;
2563 optlen = sizeof(struct timeval);
2564 optval = (char*)&tval;
2565 } else if (optlen == sizeof(struct timeval)) {
2566 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen);
2567 } else {
2568 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen);
2569 cs130_tom 1.1 return 0;
2570 }
2571 }
2572 if (level == SOL_SOCKET && optname == SO_RCVBUF && *(int*)optval < 2048)
2573 {
2574 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(int*)optval );
2575 return 0;
2576 }
2577 }
2578
2579
2580 fd = get_sock_fd( s, 0, NULL );
2581 if (fd == -1) return SOCKET_ERROR;
2582
2583 if (setsockopt(fd, level, optname, optval, optlen) == 0)
2584 {
2585 release_sock_fd( s, fd );
2586 return 0;
2587 }
2588 TRACE("Setting socket error, %d\n", wsaErrno());
2589 SetLastError(wsaErrno());
2590 cs130_tom 1.1 release_sock_fd( s, fd );
2591 return SOCKET_ERROR;
2592 }
2593
2594 /***********************************************************************
2595 * shutdown (WS2_32.22)
2596 */
2597 int WINAPI WS_shutdown(SOCKET s, int how)
2598 {
2599 int fd, fd0 = -1, fd1 = -1, flags, err = WSAENOTSOCK;
2600 unsigned int clear_flags = 0;
2601
2602 fd = get_sock_fd( s, 0, &flags );
2603 TRACE("socket %04x, how %i %x\n", s, how, flags );
2604
2605 if (fd == -1)
2606 return SOCKET_ERROR;
2607
2608 switch( how )
2609 {
2610 case 0: /* drop receives */
2611 cs130_tom 1.1 clear_flags |= FD_READ;
2612 break;
2613 case 1: /* drop sends */
2614 clear_flags |= FD_WRITE;
2615 break;
2616 case 2: /* drop all */
2617 clear_flags |= FD_READ|FD_WRITE;
2618 default:
2619 clear_flags |= FD_WINE_LISTENING;
2620 }
2621
2622 if ( flags & FD_FLAG_OVERLAPPED ) {
2623
2624 switch ( how )
2625 {
2626 case SD_RECEIVE:
2627 fd0 = fd;
2628 break;
2629 case SD_SEND:
2630 fd1 = fd;
2631 break;
2632 cs130_tom 1.1 case SD_BOTH:
2633 default:
2634 fd0 = fd;
2635 fd1 = get_sock_fd( s, 0, NULL );
2636 break;
2637 }
2638
2639 if ( fd0 != -1 )
2640 {
2641 err = WS2_register_async_shutdown( s, fd0, ws2m_sd_read );
2642 if ( err )
2643 {
2644 release_sock_fd( s, fd0 );
2645 goto error;
2646 }
2647 }
2648 if ( fd1 != -1 )
2649 {
2650 err = WS2_register_async_shutdown( s, fd1, ws2m_sd_write );
2651 if ( err )
2652 {
2653 cs130_tom 1.1 release_sock_fd( s, fd1 );
2654 goto error;
2655 }
2656 }
2657 }
2658 else /* non-overlapped mode */
2659 {
2660 if ( shutdown( fd, how ) )
2661 {
2662 err = wsaErrno();
2663 release_sock_fd( s, fd );
2664 goto error;
2665 }
2666 release_sock_fd( s, fd );
2667 }
2668
2669 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2670 if ( how > 1) WSAAsyncSelect( s, 0, 0, 0 );
2671 return 0;
2672
2673 error:
2674 cs130_tom 1.1 _enable_event( SOCKET2HANDLE(s), 0, 0, clear_flags );
2675 WSASetLastError( err );
2676 return SOCKET_ERROR;
2677 }
2678
2679 /***********************************************************************
2680 * socket (WS2_32.23)
2681 */
2682 SOCKET WINAPI WS_socket(int af, int type, int protocol)
2683 {
2684 TRACE("af=%d type=%d protocol=%d\n", af, type, protocol);
2685
2686 return WSASocketA( af, type, protocol, NULL, 0,
2687 get_per_thread_data()->opentype ? 0 : WSA_FLAG_OVERLAPPED );
2688 }
2689
2690
2691 /***********************************************************************
2692 * gethostbyaddr (WS2_32.51)
2693 */
2694 struct WS_hostent* WINAPI WS_gethostbyaddr(const char *addr, int len, int type)
2695 cs130_tom 1.1 {
2696 struct WS_hostent *retval = NULL;
2697 struct hostent* host;
2698
2699 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2700 char *extrabuf;
2701 int ebufsize=1024;
2702 struct hostent hostentry;
2703 int locerr=ENOBUFS;
2704 host = NULL;
2705 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2706 while(extrabuf) {
2707 int res = gethostbyaddr_r(addr, len, type,
2708 &hostentry, extrabuf, ebufsize, &host, &locerr);
2709 if( res != ERANGE) break;
2710 ebufsize *=2;
2711 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2712 }
2713 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2714 #else
2715 EnterCriticalSection( &csWSgetXXXbyYYY );
2716 cs130_tom 1.1 host = gethostbyaddr(addr, len, type);
2717 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2718 #endif
2719 if( host != NULL ) retval = WS_dup_he(host);
2720 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2721 HeapFree(GetProcessHeap(),0,extrabuf);
2722 #else
2723 LeaveCriticalSection( &csWSgetXXXbyYYY );
2724 #endif
2725 TRACE("ptr %p, len %d, type %d ret %p\n", addr, len, type, retval);
2726 return retval;
2727 }
2728
2729 /***********************************************************************
2730 * gethostbyname (WS2_32.52)
2731 */
2732 struct WS_hostent* WINAPI WS_gethostbyname(const char* name)
2733 {
2734 struct WS_hostent *retval = NULL;
2735 struct hostent* host;
2736 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2737 cs130_tom 1.1 char *extrabuf;
2738 int ebufsize=1024;
2739 struct hostent hostentry;
2740 int locerr = ENOBUFS;
2741 #endif
2742 char buf[100];
2743 if( !name) {
2744 name = buf;
2745 if( gethostname( buf, 100) == -1) {
2746 SetLastError( WSAENOBUFS); /* appropriate ? */
2747 return retval;
2748 }
2749 }
2750 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2751 host = NULL;
2752 extrabuf=HeapAlloc(GetProcessHeap(),0,ebufsize) ;
2753 while(extrabuf) {
2754 int res = gethostbyname_r(name, &hostentry, extrabuf, ebufsize, &host, &locerr);
2755 if( res != ERANGE) break;
2756 ebufsize *=2;
2757 extrabuf=HeapReAlloc(GetProcessHeap(),0,extrabuf,ebufsize) ;
2758 cs130_tom 1.1 }
2759 if (!host) SetLastError((locerr < 0) ? wsaErrno() : wsaHerrno(locerr));
2760 #else
2761 EnterCriticalSection( &csWSgetXXXbyYYY );
2762 host = gethostbyname(name);
2763 if (!host) SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno(h_errno));
2764 #endif
2765 if (host) retval = WS_dup_he(host);
2766 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
2767 HeapFree(GetProcessHeap(),0,extrabuf);
2768 #else
2769 LeaveCriticalSection( &csWSgetXXXbyYYY );
2770 #endif
2771 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2772 return retval;
2773 }
2774
2775
2776 /***********************************************************************
2777 * getprotobyname (WS2_32.53)
2778 */
2779 cs130_tom 1.1 struct WS_protoent* WINAPI WS_getprotobyname(const char* name)
2780 {
2781 struct WS_protoent* retval = NULL;
2782 #ifdef HAVE_GETPROTOBYNAME
2783 struct protoent* proto;
2784 EnterCriticalSection( &csWSgetXXXbyYYY );
2785 if( (proto = getprotobyname(name)) != NULL )
2786 {
2787 retval = WS_dup_pe(proto);
2788 }
2789 else {
2790 MESSAGE("protocol %s not found; You might want to add "
2791 "this to /etc/protocols\n", debugstr_a(name) );
2792 SetLastError(WSANO_DATA);
2793 }
2794 LeaveCriticalSection( &csWSgetXXXbyYYY );
2795 #endif
2796 TRACE( "%s ret %p\n", debugstr_a(name), retval );
2797 return retval;
2798 }
2799
2800 cs130_tom 1.1
2801 /***********************************************************************
2802 * getprotobynumber (WS2_32.54)
2803 */
2804 struct WS_protoent* WINAPI WS_getprotobynumber(int number)
2805 {
2806 struct WS_protoent* retval = NULL;
2807 #ifdef HAVE_GETPROTOBYNUMBER
2808 struct protoent* proto;
2809 EnterCriticalSection( &csWSgetXXXbyYYY );
2810 if( (proto = getprotobynumber(number)) != NULL )
2811 {
2812 retval = WS_dup_pe(proto);
2813 }
2814 else {
2815 MESSAGE("protocol number %d not found; You might want to add "
2816 "this to /etc/protocols\n", number );
2817 SetLastError(WSANO_DATA);
2818 }
2819 LeaveCriticalSection( &csWSgetXXXbyYYY );
2820 #endif
2821 cs130_tom 1.1 TRACE("%i ret %p\n", number, retval);
2822 return retval;
2823 }
2824
2825
2826 /***********************************************************************
2827 * getservbyname (WS2_32.55)
2828 */
2829 struct WS_servent* WINAPI WS_getservbyname(const char *name, const char *proto)
2830 {
2831 struct WS_servent* retval = NULL;
2832 struct servent* serv;
2833 char *name_str;
2834 char *proto_str = NULL;
2835
2836 if (!(name_str = strdup_lower(name))) return NULL;
2837
2838 if (proto && *proto)
2839 {
2840 if (!(proto_str = strdup_lower(proto)))
2841 {
2842 cs130_tom 1.1 HeapFree( GetProcessHeap(), 0, name_str );
2843 return NULL;
2844 }
2845 }
2846
2847 EnterCriticalSection( &csWSgetXXXbyYYY );
2848 serv = getservbyname(name_str, proto_str);
2849 if( serv != NULL )
2850 {
2851 retval = WS_dup_se(serv);
2852 }
2853 else SetLastError(WSANO_DATA);
2854 LeaveCriticalSection( &csWSgetXXXbyYYY );
2855 HeapFree( GetProcessHeap(), 0, proto_str );
2856 HeapFree( GetProcessHeap(), 0, name_str );
2857 TRACE( "%s, %s ret %p\n", debugstr_a(name), debugstr_a(proto), retval );
2858 return retval;
2859 }
2860
2861
2862 /***********************************************************************
2863 cs130_tom 1.1 * getservbyport (WS2_32.56)
2864 */
2865 struct WS_servent* WINAPI WS_getservbyport(int port, const char *proto)
2866 {
2867 struct WS_servent* retval = NULL;
2868 #ifdef HAVE_GETSERVBYPORT
2869 struct servent* serv;
2870 char *proto_str = NULL;
2871
2872 if (proto && *proto)
2873 {
2874 if (!(proto_str = strdup_lower(proto))) return NULL;
2875 }
2876 EnterCriticalSection( &csWSgetXXXbyYYY );
2877 if( (serv = getservbyport(port, proto_str)) != NULL ) {
2878 retval = WS_dup_se(serv);
2879 }
2880 else SetLastError(WSANO_DATA);
2881 LeaveCriticalSection( &csWSgetXXXbyYYY );
2882 HeapFree( GetProcessHeap(), 0, proto_str );
2883 #endif
2884 cs130_tom 1.1 TRACE("%d (i.e. port %d), %s ret %p\n", port, (int)ntohl(port), debugstr_a(proto), retval);
2885 return retval;
2886 }
2887
2888
2889 /***********************************************************************
2890 * gethostname (WS2_32.57)
2891 */
2892 int WINAPI WS_gethostname(char *name, int namelen)
2893 {
2894 TRACE("name %p, len %d\n", name, namelen);
2895
2896 if (gethostname(name, namelen) == 0)
2897 {
2898 TRACE("<- '%s'\n", name);
2899 return 0;
2900 }
2901 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2902 TRACE("<- ERROR !\n");
2903 return SOCKET_ERROR;
2904 }
2905 cs130_tom 1.1
2906
2907 /* ------------------------------------- Windows sockets extensions -- *
2908 * *
2909 * ------------------------------------------------------------------- */
2910
2911 /***********************************************************************
2912 * WSAEnumNetworkEvents (WS2_32.36)
2913 */
2914 int WINAPI WSAEnumNetworkEvents(SOCKET s, WSAEVENT hEvent, LPWSANETWORKEVENTS lpEvent)
2915 {
2916 int ret;
2917
2918 TRACE("%08x, hEvent %p, lpEvent %08x\n", s, hEvent, (unsigned)lpEvent );
2919
2920 SERVER_START_REQ( get_socket_event )
2921 {
2922 req->handle = SOCKET2HANDLE(s);
2923 req->service = TRUE;
2924 req->c_event = hEvent;
2925 wine_server_set_reply( req, lpEvent->iErrorCode, sizeof(lpEvent->iErrorCode) );
2926 cs130_tom 1.1 if (!(ret = wine_server_call(req))) lpEvent->lNetworkEvents = reply->pmask & reply->mask;
2927 }
2928 SERVER_END_REQ;
2929 if (!ret) return 0;
2930 SetLastError(WSAEINVAL);
2931 return SOCKET_ERROR;
2932 }
2933
2934 /***********************************************************************
2935 * WSAEventSelect (WS2_32.39)
2936 */
2937 int WINAPI WSAEventSelect(SOCKET s, WSAEVENT hEvent, long lEvent)
2938 {
2939 int ret;
2940
2941 TRACE("%08x, hEvent %p, event %08x\n", s, hEvent, (unsigned)lEvent );
2942
2943 SERVER_START_REQ( set_socket_event )
2944 {
2945 req->handle = SOCKET2HANDLE(s);
2946 req->mask = lEvent;
2947 cs130_tom 1.1 req->event = hEvent;
2948 req->window = 0;
2949 req->msg = 0;
2950 ret = wine_server_call( req );
2951 }
2952 SERVER_END_REQ;
2953 if (!ret) return 0;
2954 SetLastError(WSAEINVAL);
2955 return SOCKET_ERROR;
2956 }
2957
2958 /**********************************************************************
2959 * WSAGetOverlappedResult (WS2_32.40)
2960 */
2961 BOOL WINAPI WSAGetOverlappedResult( SOCKET s, LPWSAOVERLAPPED lpOverlapped,
2962 LPDWORD lpcbTransfer, BOOL fWait,
2963 LPDWORD lpdwFlags )
2964 {
2965 DWORD r;
2966
2967 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
2968 cs130_tom 1.1 s, lpOverlapped, lpcbTransfer, fWait, lpdwFlags );
2969
2970 if ( lpOverlapped == NULL )
2971 {
2972 ERR( "Invalid pointer\n" );
2973 WSASetLastError(WSA_INVALID_PARAMETER);
2974 return FALSE;
2975 }
2976
2977 if ( fWait )
2978 {
2979 if (lpOverlapped->hEvent)
2980 while ( WaitForSingleObjectEx(lpOverlapped->hEvent,
2981 INFINITE, TRUE) == STATUS_USER_APC );
2982 else /* busy loop */
2983 while ( ((volatile OVERLAPPED*)lpOverlapped)->Internal == STATUS_PENDING )
2984 Sleep( 10 );
2985
2986 }
2987 else if ( lpOverlapped->Internal == STATUS_PENDING )
2988 {
2989 cs130_tom 1.1 /* Wait in order to give APCs a chance to run. */
2990 /* This is cheating, so we must set the event again in case of success -
2991 it may be a non-manual reset event. */
2992 while ( (r = WaitForSingleObjectEx(lpOverlapped->hEvent, 0, TRUE)) == STATUS_USER_APC );
2993 if ( r == WAIT_OBJECT_0 && lpOverlapped->hEvent )
2994 NtSetEvent( lpOverlapped->hEvent, NULL );
2995 }
2996
2997 if ( lpcbTransfer )
2998 *lpcbTransfer = lpOverlapped->InternalHigh;
2999
3000 if ( lpdwFlags )
3001 *lpdwFlags = lpOverlapped->Offset;
3002
3003 switch ( lpOverlapped->Internal )
3004 {
3005 case STATUS_SUCCESS:
3006 return TRUE;
3007 case STATUS_PENDING:
3008 WSASetLastError( WSA_IO_INCOMPLETE );
3009 if (fWait) ERR("PENDING status after waiting!\n");
3010 cs130_tom 1.1 return FALSE;
3011 default:
3012 WSASetLastError( NtStatusToWSAError( lpOverlapped->Internal ));
3013 return FALSE;
3014 }
3015 }
3016
3017
3018 /***********************************************************************
3019 * WSAAsyncSelect (WS2_32.101)
3020 */
3021 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, long lEvent)
3022 {
3023 int ret;
3024
3025 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s, hWnd, uMsg, lEvent );
3026
3027 SERVER_START_REQ( set_socket_event )
3028 {
3029 req->handle = SOCKET2HANDLE(s);
3030 req->mask = lEvent;
3031 cs130_tom 1.1 req->event = 0;
3032 req->window = hWnd;
3033 req->msg = uMsg;
3034 ret = wine_server_call( req );
3035 }
3036 SERVER_END_REQ;
3037 if (!ret) return 0;
3038 SetLastError(WSAEINVAL);
3039 return SOCKET_ERROR;
3040 }
3041
3042 /***********************************************************************
3043 * WSACreateEvent (WS2_32.31)
3044 *
3045 */
3046 WSAEVENT WINAPI WSACreateEvent(void)
3047 {
3048 /* Create a manual-reset event, with initial state: unsignaled */
3049 TRACE("\n");
3050
3051 return CreateEventW(NULL, TRUE, FALSE, NULL);
3052 cs130_tom 1.1 }
3053
3054 /***********************************************************************
3055 * WSACloseEvent (WS2_32.29)
3056 *
3057 */
3058 BOOL WINAPI WSACloseEvent(WSAEVENT event)
3059 {
3060 TRACE ("event=%p\n", event);
3061
3062 return CloseHandle(event);
3063 }
3064
3065 /***********************************************************************
3066 * WSASocketA (WS2_32.78)
3067 *
3068 */
3069 SOCKET WINAPI WSASocketA(int af, int type, int protocol,
3070 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3071 GROUP g, DWORD dwFlags)
3072 {
3073 cs130_tom 1.1 INT len;
3074 WSAPROTOCOL_INFOW info;
3075
3076 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3077 af, type, protocol, lpProtocolInfo, g, dwFlags);
3078
3079 if (!lpProtocolInfo) return WSASocketW(af, type, protocol, NULL, g, dwFlags);
3080
3081 memcpy(&info, lpProtocolInfo, FIELD_OFFSET(WSAPROTOCOL_INFOW, szProtocol));
3082 len = MultiByteToWideChar(CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3083 info.szProtocol, WSAPROTOCOL_LEN * sizeof(WCHAR) + 1);
3084
3085 if (!len)
3086 {
3087 WSASetLastError( WSAEINVAL);
3088 return SOCKET_ERROR;
3089 }
3090
3091 return WSASocketW(af, type, protocol, &info, g, dwFlags);
3092 }
3093
3094 cs130_tom 1.1 /***********************************************************************
3095 * WSASocketW (WS2_32.79)
3096 *
3097 */
3098 SOCKET WINAPI WSASocketW(int af, int type, int protocol,
3099 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3100 GROUP g, DWORD dwFlags)
3101 {
3102 SOCKET ret;
3103
3104 /*
3105 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3106 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3107 */
3108
3109 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%lx\n",
3110 af, type, protocol, lpProtocolInfo, g, dwFlags );
3111
3112 /* hack for WSADuplicateSocket */
3113 if (lpProtocolInfo && lpProtocolInfo->dwServiceFlags4 == 0xff00ff00) {
3114 ret = lpProtocolInfo->dwCatalogEntryId;
3115 cs130_tom 1.1 TRACE("\tgot duplicate %04x\n", ret);
3116 return ret;
3117 }
3118
3119 /* check the socket family */
3120 switch(af)
3121 {
3122 #ifdef HAVE_IPX
3123 case WS_AF_IPX: af = AF_IPX;
3124 #endif
3125 case AF_INET:
3126 case AF_UNSPEC:
3127 break;
3128 default:
3129 SetLastError(WSAEAFNOSUPPORT);
3130 return INVALID_SOCKET;
3131 }
3132
3133 /* check the socket type */
3134 switch(type)
3135 {
3136 cs130_tom 1.1 case WS_SOCK_STREAM:
3137 type=SOCK_STREAM;
3138 break;
3139 case WS_SOCK_DGRAM:
3140 type=SOCK_DGRAM;
3141 break;
3142 case WS_SOCK_RAW:
3143 type=SOCK_RAW;
3144 break;
3145 default:
3146 SetLastError(WSAESOCKTNOSUPPORT);
3147 return INVALID_SOCKET;
3148 }
3149
3150 /* check the protocol type */
3151 if ( protocol < 0 ) /* don't support negative values */
3152 {
3153 SetLastError(WSAEPROTONOSUPPORT);
3154 return INVALID_SOCKET;
3155 }
3156
3157 cs130_tom 1.1 if ( af == AF_UNSPEC) /* did they not specify the address family? */
3158 switch(protocol)
3159 {
3160 case IPPROTO_TCP:
3161 if (type == SOCK_STREAM) { af = AF_INET; break; }
3162 case IPPROTO_UDP:
3163 if (type == SOCK_DGRAM) { af = AF_INET; break; }
3164 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
3165 }
3166
3167 SERVER_START_REQ( create_socket )
3168 {
3169 req->family = af;
3170 req->type = type;
3171 req->protocol = protocol;
3172 req->access = GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE;
3173 req->flags = dwFlags;
3174 req->inherit = TRUE;
3175 set_error( wine_server_call( req ) );
3176 ret = HANDLE2SOCKET( reply->handle );
3177 }
3178 cs130_tom 1.1 SERVER_END_REQ;
3179 if (ret)
3180 {
3181 TRACE("\tcreated %04x\n", ret );
3182 return ret;
3183 }
3184
3185 if (GetLastError() == WSAEACCES) /* raw socket denied */
3186 {
3187 if (type == SOCK_RAW)
3188 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3189 else
3190 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3191 SetLastError(WSAESOCKTNOSUPPORT);
3192 }
3193
3194 WARN("\t\tfailed!\n");
3195 return INVALID_SOCKET;
3196 }
3197
3198 /***********************************************************************
3199 cs130_tom 1.1 * WSAJoinLeaf (WS2_32.58)
3200 *
3201 */
3202 SOCKET WINAPI WSAJoinLeaf(
3203 SOCKET s,
3204 const struct WS_sockaddr *addr,
3205 int addrlen,
3206 LPWSABUF lpCallerData,
3207 LPWSABUF lpCalleeData,
3208 LPQOS lpSQOS,
3209 LPQOS lpGQOS,
3210 DWORD dwFlags)
3211 {
3212 FIXME("stub.\n");
3213 return INVALID_SOCKET;
3214 }
3215
3216 /***********************************************************************
3217 * __WSAFDIsSet (WS2_32.151)
3218 */
3219 int WINAPI __WSAFDIsSet(SOCKET s, WS_fd_set *set)
3220 cs130_tom 1.1 {
3221 int i = set->fd_count;
3222
3223 TRACE("(%d,%8lx(%i))\n", s,(unsigned long)set, i);
3224
3225 while (i--)
3226 if (set->fd_array[i] == s) return 1;
3227 return 0;
3228 }
3229
3230 /***********************************************************************
3231 * WSAIsBlocking (WINSOCK.114)
3232 * WSAIsBlocking (WS2_32.114)
3233 */
3234 BOOL WINAPI WSAIsBlocking(void)
3235 {
3236 /* By default WinSock should set all its sockets to non-blocking mode
3237 * and poll in PeekMessage loop when processing "blocking" ones. This
3238 * function is supposed to tell if the program is in this loop. Our
3239 * blocking calls are truly blocking so we always return FALSE.
3240 *
3241 cs130_tom 1.1 * Note: It is allowed to call this function without prior WSAStartup().
3242 */
3243
3244 TRACE("\n");
3245 return FALSE;
3246 }
3247
3248 /***********************************************************************
3249 * WSACancelBlockingCall (WINSOCK.113)
3250 * WSACancelBlockingCall (WS2_32.113)
3251 */
3252 INT WINAPI WSACancelBlockingCall(void)
3253 {
3254 TRACE("\n");
3255 return 0;
3256 }
3257
3258 static INT WINAPI WSA_DefaultBlockingHook( FARPROC x )
3259 {
3260 FIXME("How was this called?\n");
3261 return x();
3262 cs130_tom 1.1 }
3263
3264
3265 /***********************************************************************
3266 * WSASetBlockingHook (WS2_32.109)
3267 */
3268 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
3269 {
3270 FARPROC prev = blocking_hook;
3271 blocking_hook = lpBlockFunc;
3272 TRACE("hook %p\n", lpBlockFunc);
3273 return prev;
3274 }
3275
3276
3277 /***********************************************************************
3278 * WSAUnhookBlockingHook (WS2_32.110)
3279 */
3280 INT WINAPI WSAUnhookBlockingHook(void)
3281 {
3282 blocking_hook = WSA_DefaultBlockingHook;
3283 cs130_tom 1.1 return 0;
3284 }
3285
3286
3287 /* ----------------------------------- end of API stuff */
3288
3289 /* ----------------------------------- helper functions -
3290 *
3291 * TODO: Merge WS_dup_..() stuff into one function that
3292 * would operate with a generic structure containing internal
3293 * pointers (via a template of some kind).
3294 */
3295
3296 static int list_size(char** l, int item_size)
3297 {
3298 int i,j = 0;
3299 if(l)
3300 { for(i=0;l[i];i++)
3301 j += (item_size) ? item_size : strlen(l[i]) + 1;
3302 j += (i + 1) * sizeof(char*); }
3303 return j;
3304 cs130_tom 1.1 }
3305
3306 static int list_dup(char** l_src, char** l_to, int item_size)
3307 {
3308 char *p;
3309 int i;
3310
3311 for (i = 0; l_src[i]; i++) ;
3312 p = (char *)(l_to + i + 1);
3313 for (i = 0; l_src[i]; i++)
3314 {
3315 int count = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
3316 memcpy(p, l_src[i], count);
3317 l_to[i] = p;
3318 p += count;
3319 }
3320 l_to[i] = NULL;
3321 return p - (char *)l_to;
3322 }
3323
3324 /* ----- hostent */
3325 cs130_tom 1.1
3326 /* duplicate hostent entry
3327 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
3328 * Dito for protoent and servent.
3329 */
3330 static struct WS_hostent *WS_dup_he(const struct hostent* p_he)
3331 {
3332 char *p;
3333 struct WS_hostent *p_to;
3334
3335 int size = (sizeof(*p_he) +
3336 strlen(p_he->h_name) + 1 +
3337 list_size(p_he->h_aliases, 0) +
3338 list_size(p_he->h_addr_list, p_he->h_length));
3339
3340 if (!(p_to = check_buffer_he(size))) return NULL;
3341 p_to->h_addrtype = p_he->h_addrtype;
3342 p_to->h_length = p_he->h_length;
3343
3344 p = (char *)(p_to + 1);
3345 p_to->h_name = p;
3346 cs130_tom 1.1 strcpy(p, p_he->h_name);
3347 p += strlen(p) + 1;
3348
3349 p_to->h_aliases = (char **)p;
3350 p += list_dup(p_he->h_aliases, p_to->h_aliases, 0);
3351
3352 p_to->h_addr_list = (char **)p;
3353 list_dup(p_he->h_addr_list, p_to->h_addr_list, p_he->h_length);
3354 return p_to;
3355 }
3356
3357 /* ----- protoent */
3358
3359 static struct WS_protoent *WS_dup_pe(const struct protoent* p_pe)
3360 {
3361 char *p;
3362 struct WS_protoent *p_to;
3363
3364 int size = (sizeof(*p_pe) +
3365 strlen(p_pe->p_name) + 1 +
3366 list_size(p_pe->p_aliases, 0));
3367 cs130_tom 1.1
3368 if (!(p_to = check_buffer_pe(size))) return NULL;
3369 p_to->p_proto = p_pe->p_proto;
3370
3371 p = (char *)(p_to + 1);
3372 p_to->p_name = p;
3373 strcpy(p, p_pe->p_name);
3374 p += strlen(p) + 1;
3375
3376 p_to->p_aliases = (char **)p;
3377 list_dup(p_pe->p_aliases, p_to->p_aliases, 0);
3378 return p_to;
3379 }
3380
3381 /* ----- servent */
3382
3383 static struct WS_servent *WS_dup_se(const struct servent* p_se)
3384 {
3385 char *p;
|
3407 cs130_tom 1.1 }
3408
3409 /* ----------------------------------- error handling */
3410
3411 UINT wsaErrno(void)
3412 {
3413 int loc_errno = errno;
3414 WARN("errno %d, (%s).\n", loc_errno, strerror(loc_errno));
3415
3416 switch(loc_errno)
3417 {
3418 case EINTR: return WSAEINTR;
3419 case EBADF: return WSAEBADF;
3420 case EPERM:
3421 case EACCES: return WSAEACCES;
3422 case EFAULT: return WSAEFAULT;
3423 case EINVAL: return WSAEINVAL;
3424 case EMFILE: return WSAEMFILE;
3425 case EWOULDBLOCK: return WSAEWOULDBLOCK;
3426 case EINPROGRESS: return WSAEINPROGRESS;
3427 case EALREADY: return WSAEALREADY;
3428 cs130_tom 1.1 case ENOTSOCK: return WSAENOTSOCK;
3429 case EDESTADDRREQ: return WSAEDESTADDRREQ;
3430 case EMSGSIZE: return WSAEMSGSIZE;
3431 case EPROTOTYPE: return WSAEPROTOTYPE;
3432 case ENOPROTOOPT: return WSAENOPROTOOPT;
3433 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
3434 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
3435 case EOPNOTSUPP: return WSAEOPNOTSUPP;
3436 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
3437 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
3438 case EADDRINUSE: return WSAEADDRINUSE;
3439 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
3440 case ENETDOWN: return WSAENETDOWN;
3441 case ENETUNREACH: return WSAENETUNREACH;
3442 case ENETRESET: return WSAENETRESET;
3443 case ECONNABORTED: return WSAECONNABORTED;
3444 case EPIPE:
3445 case ECONNRESET: return WSAECONNRESET;
3446 case ENOBUFS: return WSAENOBUFS;
3447 case EISCONN: return WSAEISCONN;
3448 case ENOTCONN: return WSAENOTCONN;
3449 cs130_tom 1.1 case ESHUTDOWN: return WSAESHUTDOWN;
3450 case ETOOMANYREFS: return WSAETOOMANYREFS;
3451 case ETIMEDOUT: return WSAETIMEDOUT;
3452 case ECONNREFUSED: return WSAECONNREFUSED;
3453 case ELOOP: return WSAELOOP;
3454 case ENAMETOOLONG: return WSAENAMETOOLONG;
3455 case EHOSTDOWN: return WSAEHOSTDOWN;
3456 case EHOSTUNREACH: return WSAEHOSTUNREACH;
3457 case ENOTEMPTY: return WSAENOTEMPTY;
3458 #ifdef EPROCLIM
3459 case EPROCLIM: return WSAEPROCLIM;
3460 #endif
3461 #ifdef EUSERS
3462 case EUSERS: return WSAEUSERS;
3463 #endif
3464 #ifdef EDQUOT
3465 case EDQUOT: return WSAEDQUOT;
3466 #endif
3467 #ifdef ESTALE
3468 case ESTALE: return WSAESTALE;
3469 #endif
3470 cs130_tom 1.1 #ifdef EREMOTE
3471 case EREMOTE: return WSAEREMOTE;
3472 #endif
3473
3474 /* just in case we ever get here and there are no problems */
3475 case 0: return 0;
3476 default:
3477 WARN("Unknown errno %d!\n", loc_errno);
3478 return WSAEOPNOTSUPP;
3479 }
3480 }
3481
3482 UINT wsaHerrno(int loc_errno)
3483 {
3484
3485 WARN("h_errno %d.\n", loc_errno);
3486
3487 switch(loc_errno)
3488 {
3489 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
3490 case TRY_AGAIN: return WSATRY_AGAIN;
3491 cs130_tom 1.1 case NO_RECOVERY: return WSANO_RECOVERY;
3492 case NO_DATA: return WSANO_DATA;
3493 case ENOBUFS: return WSAENOBUFS;
3494
3495 case 0: return 0;
3496 default:
3497 WARN("Unknown h_errno %d!\n", loc_errno);
3498 return WSAEOPNOTSUPP;
3499 }
3500 }
3501
3502
3503 /***********************************************************************
3504 * WSARecv (WS2_32.67)
3505 */
3506 int WINAPI WSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3507 LPDWORD NumberOfBytesReceived, LPDWORD lpFlags,
3508 LPWSAOVERLAPPED lpOverlapped,
3509 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
3510 {
3511 return WSARecvFrom(s, lpBuffers, dwBufferCount, NumberOfBytesReceived, lpFlags,
3512 cs130_tom 1.1 NULL, NULL, lpOverlapped, lpCompletionRoutine);
3513 }
3514
3515 /***********************************************************************
3516 * WSARecvFrom (WS2_32.69)
3517 */
3518 INT WINAPI WSARecvFrom( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
3519 LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct WS_sockaddr *lpFrom,
3520 LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
3521 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine )
3522
3523 {
3524 unsigned int i;
3525 int n, fd, err = WSAENOTSOCK, flags, ret;
3526 struct iovec* iovec;
3527 struct ws2_async *wsa;
3528 IO_STATUS_BLOCK* iosb;
3529
3530 TRACE("socket %04x, wsabuf %p, nbufs %ld, flags %ld, from %p, fromlen %ld, ovl %p, func %p\n",
3531 s, lpBuffers, dwBufferCount, *lpFlags, lpFrom,
3532 (lpFromlen ? *lpFromlen : -1L),
3533 cs130_tom 1.1 lpOverlapped, lpCompletionRoutine);
3534
3535 fd = get_sock_fd( s, GENERIC_READ, &flags );
3536 TRACE( "fd=%d, flags=%x\n", fd, flags );
3537
3538 if (fd == -1) return SOCKET_ERROR;
3539
3540 if (flags & FD_FLAG_RECV_SHUTDOWN)
3541 {
3542 WSASetLastError( WSAESHUTDOWN );
3543 goto err_close;
3544 }
3545
3546 iovec = HeapAlloc( GetProcessHeap(), 0, dwBufferCount * sizeof (struct iovec) );
3547 if ( !iovec )
3548 {
3549 err = WSAEFAULT;
3550 goto err_close;
3551 }
3552
3553 for (i = 0; i < dwBufferCount; i++)
3554 cs130_tom 1.1 {
3555 iovec[i].iov_base = lpBuffers[i].buf;
3556 iovec[i].iov_len = lpBuffers[i].len;
3557 }
3558
3559 if ( (lpOverlapped || lpCompletionRoutine) && flags & FD_FLAG_OVERLAPPED )
3560 {
3561 wsa = WS2_make_async( s, fd, ws2m_read, iovec, dwBufferCount,
3562 lpFlags, lpFrom, lpFromlen,
3563 lpOverlapped, lpCompletionRoutine, &iosb );
3564
3565 if ( !wsa )
3566 {
3567 err = WSAEFAULT;
3568 goto err_free;
3569 }
3570
3571 if ( ( ret = ws2_queue_async( wsa, iosb )) )
3572 {
3573 err = NtStatusToWSAError( ret );
3574
3575 cs130_tom 1.1 if ( !lpOverlapped )
3576 HeapFree( GetProcessHeap(), 0, iosb );
3577 HeapFree( GetProcessHeap(), 0, wsa );
3578 goto err_free;
3579 }
3580
3581 /* Try immediate completion */
3582 if ( lpOverlapped )
3583 {
3584 if ( WSAGetOverlappedResult( s, lpOverlapped,
3585 lpNumberOfBytesRecvd, FALSE, lpFlags) )
3586 return 0;
3587
3588 if ( (err = WSAGetLastError()) != WSA_IO_INCOMPLETE )
3589 goto error;
3590 }
3591
3592 WSASetLastError( WSA_IO_PENDING );
3593 return SOCKET_ERROR;
3594 }
3595
3596 cs130_tom 1.1 if ( _is_blocking(s) )
3597 {
3598 /* block here */
3599 /* FIXME: OOB and exceptfds? */
3600 int timeout = GET_RCVTIMEO(fd);
3601 if( !do_block(fd, POLLIN, timeout)) {
3602 err = WSAETIMEDOUT;
3603 /* a timeout is not fatal */
3604 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3605 goto err_free;
3606 }
3607 }
3608
3609 n = WS2_recv( fd, iovec, dwBufferCount, lpFrom, lpFromlen, lpFlags );
3610 if ( n == -1 )
3611 {
3612 err = wsaErrno();
3613 goto err_free;
3614 }
3615
3616 TRACE(" -> %i bytes\n", n);
3617 cs130_tom 1.1 *lpNumberOfBytesRecvd = n;
3618
3619 HeapFree(GetProcessHeap(), 0, iovec);
3620 release_sock_fd( s, fd );
3621 _enable_event(SOCKET2HANDLE(s), FD_READ, 0, 0);
3622
3623 return 0;
3624
3625 err_free:
3626 HeapFree(GetProcessHeap(), 0, iovec);
3627
3628 err_close:
3629 release_sock_fd( s, fd );
3630
3631 error:
3632 WARN(" -> ERROR %d\n", err);
3633 WSASetLastError( err );
3634 return SOCKET_ERROR;
3635 }
3636
3637 /***********************************************************************
3638 cs130_tom 1.1 * WSCInstallProvider (WS2_32.88)
3639 */
3640 INT WINAPI WSCInstallProvider( const LPGUID lpProviderId,
3641 LPCWSTR lpszProviderDllPath,
3642 const LPWSAPROTOCOL_INFOW lpProtocolInfoList,
3643 DWORD dwNumberOfEntries,
3644 LPINT lpErrno )
3645 {
3646 FIXME("(%s, %s, %p, %ld, %p): stub !\n", debugstr_guid(lpProviderId),
3647 debugstr_w(lpszProviderDllPath), lpProtocolInfoList,
3648 dwNumberOfEntries, lpErrno);
3649 *lpErrno = 0;
3650 return 0;
3651 }
3652
3653
3654 /***********************************************************************
3655 * WSCDeinstallProvider (WS2_32.83)
3656 */
3657 INT WINAPI WSCDeinstallProvider(LPGUID lpProviderId, LPINT lpErrno)
3658 {
3659 cs130_tom 1.1 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId), lpErrno);
3660 *lpErrno = 0;
3661 return 0;
3662 }
3663
3664
3665 /***********************************************************************
3666 * WSAAccept (WS2_32.26)
3667 */
3668 SOCKET WINAPI WSAAccept( SOCKET s, struct WS_sockaddr *addr, LPINT addrlen,
3669 LPCONDITIONPROC lpfnCondition, DWORD dwCallbackData)
3670 {
3671
3672 int ret = 0, size = 0;
3673 WSABUF CallerId, CallerData, CalleeId, CalleeData;
3674 /* QOS SQOS, GQOS; */
3675 GROUP g;
3676 SOCKET cs;
3677 SOCKADDR src_addr, dst_addr;
3678
3679 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %ld\n",
3680 cs130_tom 1.1 s, addr, addrlen, lpfnCondition, dwCallbackData);
3681
3682
3683 size = sizeof(src_addr);
3684 cs = WS_accept(s, &src_addr, &size);
3685
3686 if (cs == SOCKET_ERROR) return SOCKET_ERROR;
3687
3688 CallerId.buf = (char *)&src_addr;
3689 CallerId.len = sizeof(src_addr);
3690
3691 CallerData.buf = NULL;
3692 CallerData.len = (ULONG)NULL;
3693
3694 WS_getsockname(cs, &dst_addr, &size);
3695
3696 CalleeId.buf = (char *)&dst_addr;
3697 CalleeId.len = sizeof(dst_addr);
3698
3699
3700 ret = (*lpfnCondition)(&CallerId, &CallerData, NULL, NULL,
3701 cs130_tom 1.1 &CalleeId, &CalleeData, &g, dwCallbackData);
3702
3703 switch (ret)
3704 {
3705 case CF_ACCEPT:
3706 if (addr && addrlen)
3707 addr = memcpy(addr, &src_addr, (*addrlen > size) ? size : *addrlen );
3708 return cs;
3709 case CF_DEFER:
3710 SERVER_START_REQ( set_socket_deferred )
3711 {
3712 req->handle = SOCKET2HANDLE(s);
3713 req->deferred = SOCKET2HANDLE(cs);
3714 if ( !wine_server_call_err ( req ) )
3715 {
3716 SetLastError( WSATRY_AGAIN );
3717 WS_closesocket( cs );
3718 }
3719 }
3720 SERVER_END_REQ;
3721 return SOCKET_ERROR;
3722 cs130_tom 1.1 case CF_REJECT:
3723 WS_closesocket(cs);
3724 SetLastError(WSAECONNREFUSED);
3725 return SOCKET_ERROR;
3726 default:
3727 FIXME("Unknown return type from Condition function\n");
3728 SetLastError(WSAENOTSOCK);
3729 return SOCKET_ERROR;
3730 }
3731 }
3732
3733 /***********************************************************************
3734 * WSADuplicateSocketA (WS2_32.32)
3735 */
3736 int WINAPI WSADuplicateSocketA( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOA lpProtocolInfo )
3737 {
3738 HANDLE hProcess;
3739
3740 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3741 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3742 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
3743 cs130_tom 1.1 /* I don't know what the real Windoze does next, this is a hack */
3744 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
3745 * the target use the global duplicate, or we could copy a reference to us to the structure
3746 * and let the target duplicate it from us, but let's do it as simple as possible */
3747 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3748 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3749 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3750 0, FALSE, DUPLICATE_SAME_ACCESS);
3751 CloseHandle(hProcess);
3752 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3753 return 0;
3754 }
3755
3756 /***********************************************************************
3757 * WSADuplicateSocketW (WS2_32.33)
3758 */
3759 int WINAPI WSADuplicateSocketW( SOCKET s, DWORD dwProcessId, LPWSAPROTOCOL_INFOW lpProtocolInfo )
3760 {
3761 HANDLE hProcess;
3762
3763 TRACE("(%d,%lx,%p)\n", s, dwProcessId, lpProtocolInfo);
3764 cs130_tom 1.1
3765 memset(lpProtocolInfo, 0, sizeof(*lpProtocolInfo));
3766 hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
3767 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s),
3768 hProcess, (LPHANDLE)&lpProtocolInfo->dwCatalogEntryId,
3769 0, FALSE, DUPLICATE_SAME_ACCESS);
3770 CloseHandle(hProcess);
3771 lpProtocolInfo->dwServiceFlags4 = 0xff00ff00; /* magic */
3772 return 0;
3773 }
3774
3775 /***********************************************************************
3776 * WSAInstallServiceClassA (WS2_32.48)
3777 */
3778 int WINAPI WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info)
3779 {
3780 FIXME("Request to install service %s\n",debugstr_a(info->lpszServiceClassName));
3781 WSASetLastError(WSAEACCES);
3782 return SOCKET_ERROR;
3783 }
3784
3785 cs130_tom 1.1 /***********************************************************************
3786 * WSAInstallServiceClassW (WS2_32.49)
3787 */
3788 int WINAPI WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info)
3789 {
3790 FIXME("Request to install service %s\n",debugstr_w(info->lpszServiceClassName));
3791 WSASetLastError(WSAEACCES);
3792 return SOCKET_ERROR;
3793 }
3794
3795 /***********************************************************************
3796 * WSARemoveServiceClass (WS2_32.70)
3797 */
3798 int WINAPI WSARemoveServiceClass(LPGUID info)
3799 {
3800 FIXME("Request to remove service %p\n",info);
3801 WSASetLastError(WSATYPE_NOT_FOUND);
3802 return SOCKET_ERROR;
3803 }
3804
3805 /***********************************************************************
3806 cs130_tom 1.1 * WSAStringToAddressA (WS2_32.80)
3807 */
3808 INT WINAPI WSAStringToAddressA(LPSTR AddressString,
3809 INT AddressFamily,
3810 LPWSAPROTOCOL_INFOA lpProtocolInfo,
3811 LPSOCKADDR lpAddress,
3812 LPINT lpAddressLength)
3813 {
3814 INT res=0;
3815 LONG inetaddr;
3816 LPSTR workBuffer=NULL,ptrPort;
3817
3818 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString, AddressFamily, lpProtocolInfo,
3819 lpAddress, lpAddressLength );
3820
3821 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3822
3823 if (AddressString)
3824 {
3825 workBuffer = HeapAlloc( GetProcessHeap(), 0, strlen(AddressString)+1 );
3826 if (workBuffer)
3827 cs130_tom 1.1 {
3828 strcpy(workBuffer,AddressString);
3829 switch (AddressFamily)
3830 {
3831 case AF_INET:
3832 /* caller wants to know the size of the socket buffer */
3833 if (*lpAddressLength < sizeof(SOCKADDR_IN))
3834 {
3835 *lpAddressLength = sizeof(SOCKADDR_IN);
3836 res = WSAEFAULT;
3837 }
3838 else
3839 {
3840 /* caller wants to translate an AdressString into a SOCKADDR */
3841 if (lpAddress)
3842 {
3843 memset(lpAddress,0,sizeof(SOCKADDR_IN));
3844 ((LPSOCKADDR_IN)lpAddress)->sin_family = AF_INET;
3845 ptrPort = strchr(workBuffer,':');
3846 if (ptrPort)
3847 {
3848 cs130_tom 1.1 ((LPSOCKADDR_IN)lpAddress)->sin_port = (u_short)atoi(ptrPort+1);
3849 *ptrPort = '\0';
3850 }
3851 else
3852 ((LPSOCKADDR_IN)lpAddress)->sin_port = 0;
3853 inetaddr = inet_addr(workBuffer);
3854 if (inetaddr != INADDR_NONE)
3855 {
3856 ((LPSOCKADDR_IN)lpAddress)->sin_addr.WS_s_addr = inetaddr;
3857 res = 0;
3858 }
3859 else
3860 res = WSAEINVAL;
3861 }
3862 }
3863 if (lpProtocolInfo)
3864 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
3865 AddressString, AddressFamily,
3866 lpProtocolInfo, lpAddress, lpAddressLength);
3867
3868 break;
3869 cs130_tom 1.1 default:
3870 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
3871 AddressString, AddressFamily,
3872 lpProtocolInfo, lpAddress, lpAddressLength);
3873 }
3874 HeapFree( GetProcessHeap(), 0, workBuffer );
3875 }
3876 else
3877 res = WSA_NOT_ENOUGH_MEMORY;
3878 }
3879 else
3880 res = WSAEINVAL;
3881
3882 if (!res) return 0;
3883 WSASetLastError(res);
3884 return SOCKET_ERROR;
3885 }
3886
3887 /***********************************************************************
3888 * WSAStringToAddressW (WS2_32.81)
3889 *
3890 cs130_tom 1.1 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
3891 * If this should be the case, it would be required to map these digits
3892 * to Unicode digits (0-9) using FoldString first.
3893 */
3894 INT WINAPI WSAStringToAddressW(LPWSTR AddressString,
3895 INT AddressFamily,
3896 LPWSAPROTOCOL_INFOW lpProtocolInfo,
3897 LPSOCKADDR lpAddress,
3898 LPINT lpAddressLength)
3899 {
3900 INT sBuffer,res=0;
3901 LPSTR workBuffer=NULL;
3902 WSAPROTOCOL_INFOA infoA;
3903 LPWSAPROTOCOL_INFOA lpProtoInfoA = NULL;
3904
3905 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString), AddressFamily, lpProtocolInfo,
3906 lpAddress, lpAddressLength );
3907
3908 if (!lpAddressLength || !lpAddress) return SOCKET_ERROR;
3909
3910 /* if ProtocolInfo is available - convert to ANSI variant */
3911 cs130_tom 1.1 if (lpProtocolInfo)
3912 {
3913 lpProtoInfoA = &infoA;
3914 memcpy( lpProtoInfoA, lpProtocolInfo, FIELD_OFFSET( WSAPROTOCOL_INFOA, szProtocol ) );
3915
3916 if (!WideCharToMultiByte( CP_ACP, 0, lpProtocolInfo->szProtocol, -1,
3917 lpProtoInfoA->szProtocol, WSAPROTOCOL_LEN+1, NULL, NULL ))
3918 {
3919 WSASetLastError( WSAEINVAL);
3920 return SOCKET_ERROR;
3921 }
3922 }
3923
3924 if (AddressString)
3925 {
3926 /* Translate AddressString to ANSI code page - assumes that only
3927 standard digits 0-9 are used with this API call */
3928 sBuffer = WideCharToMultiByte( CP_ACP, 0, AddressString, -1, NULL, 0, NULL, NULL );
3929 workBuffer = HeapAlloc( GetProcessHeap(), 0, sBuffer );
3930
3931 if (workBuffer)
3932 cs130_tom 1.1 {
3933 WideCharToMultiByte( CP_ACP, 0, AddressString, -1, workBuffer, sBuffer, NULL, NULL );
3934 res = WSAStringToAddressA(workBuffer,AddressFamily,lpProtoInfoA,
3935 lpAddress,lpAddressLength);
3936 HeapFree( GetProcessHeap(), 0, workBuffer );
3937 return res;
3938 }
3939 else
3940 res = WSA_NOT_ENOUGH_MEMORY;
3941 }
3942 else
3943 res = WSAEINVAL;
3944
3945 WSASetLastError(res);
3946 return SOCKET_ERROR;
3947 }
3948
3949 /***********************************************************************
3950 * WSAAddressToStringA (WS2_32.27)
3951 *
3952 * See WSAAddressToStringW
3953 cs130_tom 1.1 */
3954 INT WINAPI WSAAddressToStringA( LPSOCKADDR sockaddr, DWORD len,
3955 LPWSAPROTOCOL_INFOA info, LPSTR string,
3956 LPDWORD lenstr )
3957 {
3958 INT size;
3959 CHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
3960 CHAR *p;
3961
3962 TRACE( "(%p, %lx, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
3963
3964 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
3965 if (!string || !lenstr) return SOCKET_ERROR;
3966
3967 /* sin_family is garanteed to be the first u_short */
3968 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
3969
3970 sprintf( buffer, "%u.%u.%u.%u:%u",
3971 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
3972 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
3973 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
3974 cs130_tom 1.1 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
3975 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
3976
3977 p = strchr( buffer, ':' );
3978 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
3979
3980 size = strlen( buffer );
3981
3982 if (*lenstr < size)
3983 {
3984 *lenstr = size;
3985 return SOCKET_ERROR;
3986 }
3987
3988 strcpy( string, buffer );
3989 return 0;
3990 }
3991
3992 /***********************************************************************
3993 * WSAAddressToStringW (WS2_32.28)
3994 *
3995 cs130_tom 1.1 * Convert a sockaddr address into a readable address string.
3996 *
3997 * PARAMS
3998 * sockaddr [I] Pointer to a sockaddr structure.
3999 * len [I] Size of the sockaddr structure.
4000 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4001 * string [I/O] Pointer to a buffer to receive the address string.
4002 * lenstr [I/O] Size of the receive buffer in WCHARs.
4003 *
4004 * RETURNS
4005 * Success: 0
4006 * Failure: SOCKET_ERROR
4007 *
4008 * NOTES
4009 * The 'info' parameter is ignored.
4010 *
4011 * BUGS
4012 * Only supports AF_INET addresses.
4013 */
4014 INT WINAPI WSAAddressToStringW( LPSOCKADDR sockaddr, DWORD len,
4015 LPWSAPROTOCOL_INFOW info, LPWSTR string,
4016 cs130_tom 1.1 LPDWORD lenstr )
4017 {
4018 INT size;
4019 WCHAR buffer[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4020 static const WCHAR format[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4021 WCHAR *p;
4022
4023 TRACE( "(%p, %lx, %p, %p, %p)\n", sockaddr, len, info, string, lenstr );
4024
4025 if (!sockaddr || len < sizeof(SOCKADDR_IN)) return SOCKET_ERROR;
4026 if (!string || !lenstr) return SOCKET_ERROR;
4027
4028 /* sin_family is garanteed to be the first u_short */
4029 if (((SOCKADDR_IN *)sockaddr)->sin_family != AF_INET) return SOCKET_ERROR;
4030
4031 sprintfW( buffer, format,
4032 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 24 & 0xff),
4033 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 16 & 0xff),
4034 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) >> 8 & 0xff),
4035 (unsigned int)(ntohl( ((SOCKADDR_IN *)sockaddr)->sin_addr.WS_s_addr ) & 0xff),
4036 ntohs( ((SOCKADDR_IN *)sockaddr)->sin_port ) );
4037 cs130_tom 1.1
4038 p = strchrW( buffer, ':' );
4039 if (!((SOCKADDR_IN *)sockaddr)->sin_port) *p = 0;
4040
4041 size = lstrlenW( buffer );
4042
4043 if (*lenstr < size)
4044 {
4045 *lenstr = size;
4046 return SOCKET_ERROR;
4047 }
4048
4049 lstrcpyW( string, buffer );
4050 return 0;
4051 }
4052
4053 /***********************************************************************
4054 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4055 */
4056 INT WINAPI WSAEnumNameSpaceProvidersA( LPDWORD len, LPWSANAMESPACE_INFOA buffer )
4057 {
4058 cs130_tom 1.1 FIXME( "(%p %p) Stub!\n", len, buffer );
4059 return 0;
4060 }
4061
4062 /***********************************************************************
4063 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4064 */
4065 INT WINAPI WSAEnumNameSpaceProvidersW( LPDWORD len, LPWSANAMESPACE_INFOW buffer )
4066 {
4067 FIXME( "(%p %p) Stub!\n", len, buffer );
4068 return 0;
4069 }
4070
4071 /***********************************************************************
4072 * WSAGetQOSByName (WS2_32.41)
4073 */
4074 BOOL WINAPI WSAGetQOSByName( SOCKET s, LPWSABUF lpQOSName, LPQOS lpQOS )
4075 {
4076 FIXME( "(0x%04x %p %p) Stub!\n", s, lpQOSName, lpQOS );
4077 return FALSE;
4078 }
4079 cs130_tom 1.1
4080 /***********************************************************************
4081 * WSAGetServiceClassInfoA (WS2_32.42)
4082 */
4083 INT WINAPI WSAGetServiceClassInfoA( LPGUID provider, LPGUID service, LPDWORD len,
4084 LPWSASERVICECLASSINFOA info )
4085 {
4086 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4087 len, info );
4088 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4089 return SOCKET_ERROR;
4090 }
4091
4092 /***********************************************************************
4093 * WSAGetServiceClassInfoW (WS2_32.43)
4094 */
4095 INT WINAPI WSAGetServiceClassInfoW( LPGUID provider, LPGUID service, LPDWORD len,
4096 LPWSASERVICECLASSINFOW info )
4097 {
4098 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider), debugstr_guid(service),
4099 len, info );
4100 cs130_tom 1.1 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4101 return SOCKET_ERROR;
4102 }
4103
4104 /***********************************************************************
4105 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4106 */
4107 INT WINAPI WSAGetServiceClassNameByClassIdA( LPGUID class, LPSTR service, LPDWORD len )
4108 {
4109 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4110 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4111 return SOCKET_ERROR;
4112 }
4113
4114 /***********************************************************************
4115 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4116 */
4117 INT WINAPI WSAGetServiceClassNameByClassIdW( LPGUID class, LPWSTR service, LPDWORD len )
4118 {
4119 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service, len );
4120 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4121 cs130_tom 1.1 return SOCKET_ERROR;
4122 }
4123
4124 /***********************************************************************
4125 * WSALookupServiceBeginA (WS2_32.59)
4126 */
4127 INT WINAPI WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions,
4128 DWORD dwControlFlags,
4129 LPHANDLE lphLookup)
4130 {
4131 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4132 lphLookup);
4133 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4134 return SOCKET_ERROR;
4135 }
4136
4137 /***********************************************************************
4138 * WSALookupServiceBeginW (WS2_32.60)
4139 */
4140 INT WINAPI WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions,
4141 DWORD dwControlFlags,
4142 cs130_tom 1.1 LPHANDLE lphLookup)
4143 {
4144 FIXME("(%p 0x%08lx %p) Stub!\n", lpqsRestrictions, dwControlFlags,
4145 lphLookup);
4146 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
4147 return SOCKET_ERROR;
4148 }
4149
4150 /***********************************************************************
4151 * WSALookupServiceBeginW (WS2_32.61)
4152 */
4153 INT WINAPI WSALookupServiceEnd( HANDLE lookup )
4154 {
4155 FIXME("(%p) Stub!\n", lookup );
4156 return 0;
4157 }
4158
4159 /***********************************************************************
4160 * WSALookupServiceNextA (WS2_32.62)
4161 */
4162 INT WINAPI WSALookupServiceNextA( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETA results )
4163 cs130_tom 1.1 {
4164 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4165 return 0;
4166 }
4167
4168 /***********************************************************************
4169 * WSALookupServiceNextW (WS2_32.63)
4170 */
4171 INT WINAPI WSALookupServiceNextW( HANDLE lookup, DWORD flags, LPDWORD len, LPWSAQUERYSETW results )
4172 {
4173 FIXME( "(%p 0x%08lx %p %p) Stub!\n", lookup, flags, len, results );
4174 return 0;
4175 }
4176
4177 /***********************************************************************
4178 * WSANtohl (WS2_32.64)
4179 */
4180 INT WINAPI WSANtohl( SOCKET s, u_long netlong, u_long* lphostlong )
4181 {
4182 TRACE( "(0x%04x 0x%08lx %p)\n", s, netlong, lphostlong );
4183
4184 cs130_tom 1.1 if (!lphostlong) return WSAEFAULT;
4185
4186 *lphostlong = ntohl( netlong );
4187 return 0;
4188 }
4189
4190 /***********************************************************************
4191 * WSANtohs (WS2_32.65)
4192 */
4193 INT WINAPI WSANtohs( SOCKET s, u_short netshort, u_short* lphostshort )
4194 {
4195 TRACE( "(0x%04x 0x%08x %p)\n", s, netshort, lphostshort );
4196
4197 if (!lphostshort) return WSAEFAULT;
4198
4199 *lphostshort = ntohs( netshort );
4200 return 0;
4201 }
4202
4203 /***********************************************************************
4204 * WSAProviderConfigChange (WS2_32.66)
4205 cs130_tom 1.1 */
4206 INT WINAPI WSAProviderConfigChange( LPHANDLE handle, LPWSAOVERLAPPED overlapped,
4207 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion )
4208 {
4209 FIXME( "(%p %p %p) Stub!\n", handle, overlapped, completion );
4210 return SOCKET_ERROR;
4211 }
4212
4213 /***********************************************************************
4214 * WSARecvDisconnect (WS2_32.68)
4215 */
4216 INT WINAPI WSARecvDisconnect( SOCKET s, LPWSABUF disconnectdata )
4217 {
4218 TRACE( "(0x%04x %p)\n", s, disconnectdata );
4219
4220 return WS_shutdown( s, 0 );
4221 }
4222
4223 /***********************************************************************
4224 * WSASetServiceA (WS2_32.76)
4225 */
4226 cs130_tom 1.1 INT WINAPI WSASetServiceA( LPWSAQUERYSETA query, WSAESETSERVICEOP operation, DWORD flags )
4227 {
4228 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4229 return 0;
4230 }
4231
4232 /***********************************************************************
4233 * WSASetServiceW (WS2_32.77)
4234 */
4235 INT WINAPI WSASetServiceW( LPWSAQUERYSETW query, WSAESETSERVICEOP operation, DWORD flags )
4236 {
4237 FIXME( "(%p 0x%08x 0x%08lx) Stub!\n", query, operation, flags );
4238 return 0;
4239 }
4240
4241 /***********************************************************************
4242 * WSCEnableNSProvider (WS2_32.84)
4243 */
4244 INT WINAPI WSCEnableNSProvider( LPGUID provider, BOOL enable )
4245 {
4246 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider), enable );
4247 cs130_tom 1.1 return 0;
4248 }
4249
4250 /***********************************************************************
4251 * WSCGetProviderPath (WS2_32.86)
4252 */
4253 INT WINAPI WSCGetProviderPath( LPGUID provider, LPWSTR path, LPINT len, LPINT errno )
4254 {
4255 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider), path, len, errno );
4256
4257 if (!errno || !provider || !len) return WSAEFAULT;
4258
4259 *errno = WSAEINVAL;
4260 return SOCKET_ERROR;
4261 }
4262
4263 /***********************************************************************
4264 * WSCInstallNameSpace (WS2_32.87)
4265 */
4266 INT WINAPI WSCInstallNameSpace( LPWSTR identifier, LPWSTR path, DWORD namespace,
4267 DWORD version, LPGUID provider )
4268 cs130_tom 1.1 {
4269 FIXME( "(%s %s 0x%08lx 0x%08lx %s) Stub!\n", debugstr_w(identifier), debugstr_w(path),
4270 namespace, version, debugstr_guid(provider) );
4271 return 0;
4272 }
4273
4274 /***********************************************************************
4275 * WSCUnInstallNameSpace (WS2_32.89)
4276 */
4277 INT WINAPI WSCUnInstallNameSpace( LPGUID lpProviderId )
4278 {
4279 FIXME("(%p) Stub!\n", lpProviderId);
4280 return NO_ERROR;
4281 }
4282
4283 /***********************************************************************
4284 * WSCWriteProviderOrder (WS2_32.91)
4285 */
4286 INT WINAPI WSCWriteProviderOrder( LPDWORD entry, DWORD number )
4287 {
4288 FIXME("(%p 0x%08lx) Stub!\n", entry, number);
4289 cs130_tom 1.1 return 0;
4290 }
|