u_short port;
SOCKET sock = -1;
signed int result;
unsigned int readlen;
int locip;
int outmsglen;
char peeraddr;
struct sockaddr name;
char recvbuf[8192];
char outbuf[];
locip = *(_DWORD *)(a1 + 12);
port = *(_WORD *)(a1 + 16);
if ( get_version_co_initialize_instance(port, 17) ) {
while ( !call_WaitForSingleObject(a1, 0, a1) ) {
sock = socket(2, 2, 17);
if ( sock ) {
if ( sock != -1 ) {
build_sockaddr(0, locip, (int)&name, port);
if ( !bind(sock, &name, 16) ) {
while ( 1 ) {
if ( call_WaitForSingleObject(a1, 0, a1) )
goto CLOSE_SOCK_AND_EXIT;
result = call_recvfrom(6, (int)&peeraddr, (int)&readlen, sock,
recvbuf, 8192);
if ( result & 1 && !(result & 0x40) ) break;
/* recvfrom successful */
if (result & 4 ) {
server_handle_recv(outbuf, recvbuf, readlen, (int)&peeraddr,
(int)&name, 17, (void *)sock, (int)&outmsglen);
if (outmsglen )
result = call_select_and_sendto(5, sock, outbuf,
outmsglen, (const struct sockaddr *)&peeraddr);
}
}
}
}
if (sock && sock != -1 ) {
closesocket(sock);
sock = -1;
}
}
Sleep(5000);
}
}
CLOSE_SOCK_AND_EXIT:
if (sock && sock != -1 )
closesocket(sock);
ExitThread(0);
}
SOURCE
LISTING 9: P2P UDP server thread