Subversion Repositories Games.Carmageddon

Rev

Rev 18 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
#include "pd/net.h"
2
 
20 pmbaty 3
#include "brender/brender.h"
1 pmbaty 4
#include "dr_types.h"
5
#include "errors.h"
6
#include "globvrpb.h"
7
#include "harness/config.h"
8
#include "harness/hooks.h"
9
#include "harness/trace.h"
10
#include "harness/winsock.h"
11
#include "network.h"
12
#include "pd/net.h"
13
#include "pd/sys.h"
14
#include <ctype.h>
15
#include <stdlib.h>
16
#include <string.h>
17
 
18
// dethrace: have switched out IPX implementation for IP
19
 
20
tU32 gNetwork_init_flags;
21
tPD_net_game_info* gJoinable_games;
22
int gMatts_PC;
23
tU16 gSocket_number_pd_format;
24
//_IPX_ELEMENT gListen_elements[16];
25
char gLocal_ipx_addr_string[32];
26
//_IPX_ELEMENT gSend_elements[16];
27
struct sockaddr_in* gLocal_addr_ipx;
28
char gReceive_buffer[512];
29
tPD_net_player_info gRemote_net_player_info;
30
struct sockaddr_in* gBroadcast_addr_ipx;
31
tPD_net_player_info gLocal_net_player_info;
32
char gSend_buffer[512];
33
tIPX_netnum gNetworks[16];
34
struct sockaddr_in* gRemote_addr_ipx;
35
tU8* gSend_packet;
36
// W32 gListen_segment;
37
tU8* gSend_packet_ptr;
38
// W32 gSend_segment;
39
tU8* gListen_packet;
40
tU8* gListen_packet_ptr;
41
size_t gMsg_header_strlen;
42
int gNumber_of_networks;
43
int gNumber_of_hosts;
44
//_IPX_HEADER gLast_received_IPX_header;
45
tU16 gSocket_number_network_order;
46
// unsigned short gECB_offset;
47
tU16 gListen_selector;
48
tU16 gSend_selector;
49
 
50
struct sockaddr_in gLocal_addr;
51
struct sockaddr_in gRemote_addr;
52
struct sockaddr_in gBroadcast_addr;
53
struct sockaddr_in gLast_received_addr;
54
int gSocket;
55
 
56
#define MESSAGE_HEADER_STR "CW95MSG"
57
#define JOINABLE_GAMES_CAPACITY 16
58
 
59
// IDA: void __cdecl ClearupPDNetworkStuff()
60
void ClearupPDNetworkStuff(void) {
61
    LOG_TRACE("()");
62
    NOT_IMPLEMENTED();
63
}
64
 
65
// IDA: void __usercall MATTMessageCheck(char *pFunction_name@<EAX>, tNet_message *pMessage@<EDX>, int pAlleged_size@<EBX>)
66
void MATTMessageCheck(char* pFunction_name, tNet_message* pMessage, int pAlleged_size) {
67
    LOG_TRACE("(\"%s\", %p, %d)", pFunction_name, pMessage, pAlleged_size);
68
    NOT_IMPLEMENTED();
69
}
70
 
71
// IDA: int __usercall GetProfileText@<EAX>(char *pDest@<EAX>, int pDest_len@<EDX>, char *pFname@<EBX>, char *pKeyname@<ECX>)
72
int GetProfileText(char* pDest, int pDest_len, char* pFname, char* pKeyname) {
73
    //FILE* fp; // Pierre-Marie Baty -- unused variable
74
    //char in_buf[256]; // Pierre-Marie Baty -- unused variable
75
    //int i; // Pierre-Marie Baty -- unused variable
76
    //int j; // Pierre-Marie Baty -- unused variable
77
    //int len; // Pierre-Marie Baty -- unused variable
78
    LOG_TRACE("(\"%s\", %d, \"%s\", \"%s\")", pDest, pDest_len, pFname, pKeyname);
79
    NOT_IMPLEMENTED();
80
}
81
 
82
// IDA: int __cdecl GetSocketNumberFromProfileFile()
83
int GetSocketNumberFromProfileFile(void) {
84
    //char str[256]; // Pierre-Marie Baty -- unused variable
85
    //int sscanf_res; // Pierre-Marie Baty -- unused variable
86
    //tU32 socknum; // Pierre-Marie Baty -- unused variable
87
    LOG_TRACE("()");
88
    NOT_IMPLEMENTED();
89
}
90
 
91
// tU32 EthernetAddressToU32(SOCKADDR_IPX_* pAddr_ipx) {
92
//     LOG_TRACE("(%p)", pAddr_ipx);
93
//     NOT_IMPLEMENTED();
94
// }
95
 
96
void NetNowIPXLocalTarget2String(char* pString, struct sockaddr_in* pSock_addr_ipx) {
97
    LOG_TRACE("(\"%s\", %p)", pString, pSock_addr_ipx);
98
 
99
    char portbuf[10];
100
 
101
    inet_ntop(AF_INET, &pSock_addr_ipx->sin_addr, pString, 32);
102
    sprintf(portbuf, ":%d", ntohs(pSock_addr_ipx->sin_port));
103
    strcat(pString, portbuf);
104
}
105
 
106
// IDA: int __usercall GetMessageTypeFromMessage@<EAX>(char *pMessage_str@<EAX>)
107
int GetMessageTypeFromMessage(char* pMessage_str) {
108
    char* real_msg;
109
    int msg_type_int;
110
    LOG_TRACE("(\"%s\")", pMessage_str);
111
 
112
    real_msg = &pMessage_str[4];
113
    msg_type_int = 0;
114
 
115
    // FIXME: "CW95MSG" value is used in and depends on platform
116
    if (strncmp(real_msg, MESSAGE_HEADER_STR, gMsg_header_strlen) == 0) {
117
        if (isdigit(real_msg[gMsg_header_strlen])) {
118
            msg_type_int = real_msg[gMsg_header_strlen] - '0';
119
        }
120
        if (msg_type_int != 0 && msg_type_int < 3) {
121
            return msg_type_int;
122
        }
123
    }
124
    return 999;
125
}
126
 
127
int SameEthernetAddress(struct sockaddr_in* pAddr_ipx1, struct sockaddr_in* pAddr_ipx2) {
128
    LOG_TRACE("(%p, %p)", pAddr_ipx1, pAddr_ipx2);
129
 
130
    return memcmp(pAddr_ipx1, pAddr_ipx2, sizeof(struct sockaddr_in)) == 0;
131
}
132
 
133
/*SOCKADDR_IPX_* */ void* GetIPXAddrFromPlayerID(tPlayer_ID pPlayer_id) {
134
    //int i; // Pierre-Marie Baty -- unused variable
135
    //tU8* nodenum; // Pierre-Marie Baty -- unused variable
136
    NOT_IMPLEMENTED();
137
}
138
 
139
// IDA: void __usercall MakeMessageToSend(int pMessage_type@<EAX>)
140
void MakeMessageToSend(int pMessage_type) {
141
    LOG_TRACE("(%d)", pMessage_type);
142
 
143
#ifdef DETHRACE_FIX_BUGS
144
    sprintf(gSend_buffer, "XXXX%s%1d", MESSAGE_HEADER_STR, pMessage_type);
145
#else
146
    sprintf(gSend_buffer, "XXXX%s%0.1d", MESSAGE_HEADER_STR, pMessage_type);
147
#endif
148
}
149
 
150
// IDA: int __cdecl ReceiveHostResponses()
151
int ReceiveHostResponses(void) {
152
    char str[256];
153
    int i;
154
    int already_registered;
155
    LOG_TRACE("()");
156
 
157
    char addr_string[32];
158
    unsigned int sa_len;
159
    int wsa_error;
160
 
161
    sa_len = sizeof(gRemote_addr);
162
    while (1) {
163
        if (recvfrom(gSocket, gReceive_buffer, sizeof(gReceive_buffer), 0, (struct sockaddr*)&gRemote_addr, &sa_len) == -1) {
164
            break;
165
        }
166
        NetNowIPXLocalTarget2String(addr_string, gRemote_addr_ipx);
167
        dr_dprintf("ReceiveHostResponses(): Received string '%s' from %s", gReceive_buffer, addr_string);
168
 
169
        if (SameEthernetAddress(gLocal_addr_ipx, gRemote_addr_ipx)) {
170
            dr_dprintf("*** Discounting the above 'cos we sent it ***");
171
            continue;
172
        }
173
        if (GetMessageTypeFromMessage(gReceive_buffer) != 2) {
174
            dr_dprintf("*** Discounting the above 'cos it's not a host reply ***");
175
            continue;
176
        }
177
 
178
        dr_dprintf("*** It's a host reply! ***");
179
        already_registered = 0;
180
        for (i = 0; i < gNumber_of_hosts; i++) {
181
            if (SameEthernetAddress(&gJoinable_games[i].addr_in, gRemote_addr_ipx)) {
182
                already_registered = 1;
183
                break;
184
            }
185
        }
186
        if (already_registered) {
187
            dr_dprintf("That game already registered");
188
            gJoinable_games[i].last_response = PDGetTotalTime();
189
        } else {
190
            dr_dprintf("Adding joinable game to slot #%d", gNumber_of_hosts);
191
            gJoinable_games[gNumber_of_hosts].addr_in = *gRemote_addr_ipx;
192
            gJoinable_games[gNumber_of_hosts].last_response = PDGetTotalTime();
193
            gNumber_of_hosts++;
194
            dr_dprintf("Number of games found so far: %d", gNumber_of_hosts);
195
        }
196
        if (gNumber_of_hosts) {
197
            dr_dprintf("Currently registered net games:");
198
            for (i = 0; i < gNumber_of_hosts; i++) {
199
                NetNowIPXLocalTarget2String(str, &gJoinable_games[i].addr_in);
200
                dr_dprintf("%d: Host addr %s", i, str);
201
            }
202
        }
203
    }
204
    wsa_error = WSAGetLastError() != WSAEWOULDBLOCK;
205
    if (wsa_error == 0) {
206
        return 1;
207
    }
208
    dr_dprintf("ReceiveHostResponses(): Error on recvfrom() - WSAGetLastError=%d", wsa_error);
209
    return 0;
210
}
211
 
212
// IDA: int __cdecl BroadcastMessage()
213
int BroadcastMessage(void) {
214
    int i;
215
    int errors;
216
    char broadcast_addr_string[32];
217
    //char* real_msg; // Pierre-Marie Baty -- unused variable
218
    LOG_TRACE("()");
219
 
220
    errors = 0;
221
    for (i = 0; i < gNumber_of_networks; i++) {
222
        //*(_DWORD*)gBroadcast_addr_ipx->sa_netnum = gNetworks[i];
223
        NetNowIPXLocalTarget2String(broadcast_addr_string, gBroadcast_addr_ipx);
224
        dr_dprintf("Broadcasting on address '%s'", broadcast_addr_string);
225
        if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gBroadcast_addr, sizeof(gBroadcast_addr)) == -1) {
226
            dr_dprintf("BroadcastMessage(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
227
            errors = 1;
228
        }
229
    }
230
    return errors == 0;
231
}
232
 
233
// IDA: int __cdecl PDNetInitialise()
234
int PDNetInitialise(void) {
235
    //tU32 timenow; // Pierre-Marie Baty -- unused variable
236
    //char profile_string[32]; // Pierre-Marie Baty -- unused variable
237
    //char key_name[32]; // Pierre-Marie Baty -- unused variable
238
    //int sscanf_res; // Pierre-Marie Baty -- unused variable
239
    //int i; // Pierre-Marie Baty -- unused variable
240
    //tU32 netnum; // Pierre-Marie Baty -- unused variable
241
    //char str[256]; // Pierre-Marie Baty -- unused variable
242
    //int mess_num; // Pierre-Marie Baty -- unused variable
243
 
244
    struct linger so_linger;
245
    unsigned int sa_len;
246
    WSADATA wsadata;
247
    LOG_TRACE("()");
248
 
249
    sa_len = sizeof(struct sockaddr_in);
250
    dr_dprintf("PDNetInitialise()");
251
    int ipx_socket_num = 12286;
252
    // Dont bother to handle network.ini
253
    // if (gWin32_should_load_network_ini) {
254
    //     if (GetPrivateProfileStringA(
255
    //             "Network",
256
    //             "IPXSocket",
257
    //             Default,
258
    //             (LPSTR)ReturnedString,
259
    //             0x20u,
260
    //             gNetwork_profile_fname)) {
261
    //         v11 = sscanf(ReturnedString, "%x", &v9);
262
    //         if (v11) {
263
    //             if (v11 != -1) {
264
    //                 dr_dprintf("New IPX socket %4.4x", v9);
265
    //                 ipx_socket_num = sub_454930(v9);
266
    //             }
267
    //         }
268
    //     }
269
    // }
270
    gLocal_addr_ipx = &gLocal_addr;
271
    gRemote_addr_ipx = &gRemote_addr;
272
    gBroadcast_addr_ipx = &gBroadcast_addr;
273
    memset(&gLocal_addr, 0, sizeof(struct sockaddr_in));
274
    memset(&gRemote_addr, 0, sizeof(struct sockaddr_in));
275
    memset(&gBroadcast_addr, 0, sizeof(struct sockaddr_in));
276
 
277
    // gLocal_addr_ipx->sa_family = AF_IPX;
278
    // gRemote_addr_ipx->sa_family = AF_IPX;
279
    // gBroadcast_addr_ipx->sa_family = AF_IPX;
280
    // gLocal_addr_ipx->sa_socket = ipx_socket_num;
281
    // gRemote_addr_ipx->sa_socket = ipx_socket_num;
282
    // gBroadcast_addr_ipx->sa_socket = ipx_socket_num;
283
 
284
    // // IPX broadcast
285
    // gBroadcast_addr_ipx->sa_nodenum[0] = -1;
286
    // gBroadcast_addr_ipx->sa_nodenum[1] = -1;
287
    // gBroadcast_addr_ipx->sa_nodenum[2] = -1;
288
    // gBroadcast_addr_ipx->sa_nodenum[3] = -1;
289
    // gBroadcast_addr_ipx->sa_nodenum[4] = -1;
290
    // gBroadcast_addr_ipx->sa_nodenum[5] = -1;
291
 
292
    gLocal_addr.sin_family = AF_INET;
293
    gLocal_addr.sin_port = htons(12286);
294
    gLocal_addr.sin_addr.s_addr = INADDR_ANY;
295
    gRemote_addr.sin_family = AF_INET;
296
    gRemote_addr.sin_port = htons(12286);
297
    gBroadcast_addr.sin_family = AF_INET;
298
    gBroadcast_addr.sin_port = htons(12286);
299
 
300
    // original code was using MAKEWORD(1, 1)
301
    if (WSAStartup(MAKEWORD(2, 2), &wsadata) == -1) {
302
        dr_dprintf("PDNetInitialise(): WSAStartup() failed");
303
        return -1;
304
    }
305
 
306
    // gSocket = socket(6, 2, 1000);
307
    gSocket = socket(AF_INET, SOCK_DGRAM, 0);
308
    if (gSocket == -1) {
309
        dr_dprintf("PDNetInitialise(): Failed to create socket - WSAGetLastError=%d", WSAGetLastError());
310
        WSACleanup();
311
        return -1;
312
    }
313
    int broadcast = 1;
314
    setsockopt(gSocket, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast, sizeof(broadcast)); // Pierre-Marie Baty -- added portable type cast
315
    so_linger.l_onoff = 1;
316
    so_linger.l_linger = 0;
317
    setsockopt(gSocket, SOL_SOCKET, SO_LINGER, (void *) &so_linger, sizeof(so_linger)); // Pierre-Marie Baty -- added portable type cast
318
 
319
    unsigned long nobio = 1;
320
    if (ioctlsocket(gSocket, FIONBIO, &nobio) == -1) {
321
        dr_dprintf("Error on ioctlsocket() - WSAGetLastError=%d", WSAGetLastError());
322
        closesocket(gSocket);
323
        WSACleanup();
324
        return -1;
325
    }
326
 
327
    if (harness_game_config.no_bind == 0) {
328
        if (bind(gSocket, (struct sockaddr*)&gLocal_addr, sizeof(gLocal_addr)) == -1) {
329
            dr_dprintf("Error on bind() - WSAGetLastError=%d", WSAGetLastError());
330
            closesocket(gSocket);
331
            WSACleanup();
332
            return -1;
333
        }
334
    }
335
 
336
    int res = getsockname(gSocket, (struct sockaddr*)&gLocal_addr, &sa_len);
337
    NetNowIPXLocalTarget2String(gLocal_ipx_addr_string, gLocal_addr_ipx);
338
    // gNetworks[0] = *(tIPX_netnum*)gLocal_addr_ipx->sa_netnum;
339
    gNumber_of_networks = 1;
340
 
341
    // if (gWin32_should_load_network_ini) {
342
    //     for (i = 1; i <= 15; ++i) {
343
    //         sprintf(KeyName, "%s%d", "Network", i);
344
    //         if (GetPrivateProfileStringA(
345
    //                 "Network",
346
    //                 KeyName,
347
    //                 Default,
348
    //                 (LPSTR)ReturnedString,
349
    //                 0x20u,
350
    //                 gNetwork_profile_fname)) {
351
    //             v11 = sscanf(ReturnedString, "%x", &network_nbr);
352
    //             if (v11) {
353
    //                 if (v11 != -1) {
354
    //                     dr_dprintf("Found network number '%x' in INI file", network_nbr);
355
    //                     gNetworks[gNumber_of_networks].bNetwork[0] = HIBYTE(network_nbr);
356
    //                     gNetworks[gNumber_of_networks].bNetwork[1] = (network_nbr & 0xFF0000u) >> 16;
357
    //                     gNetworks[gNumber_of_networks].bNetwork[2] = BYTE1(network_nbr);
358
    //                     gNetworks[gNumber_of_networks].bNetwork[3] = network_nbr;
359
    //                     if (gNetworks[gNumber_of_networks].bNetwork[0] != gLocal_addr_ipx->sa_netnum[0]
360
    //                         || gNetworks[gNumber_of_networks].bNetwork[1] != gLocal_addr_ipx->sa_netnum[1]
361
    //                         || gNetworks[gNumber_of_networks].bNetwork[2] != gLocal_addr_ipx->sa_netnum[2]
362
    //                         || gNetworks[gNumber_of_networks].bNetwork[3] != gLocal_addr_ipx->sa_netnum[3]) {
363
    //                         ++gNumber_of_networks;
364
    //                     }
365
    //                 }
366
    //             }
367
    //         }
368
    //     }
369
    //     dr_dprintf("Total networks = %d", gNumber_of_networks);
370
    // }
371
    // byte_536440 = *gLocal_addr_ipx;
372
 
373
    dr_dprintf("Socket bound OK; local address is '%s'", gLocal_ipx_addr_string);
374
    gMsg_header_strlen = 7;
375
    if (strstr(gLocal_ipx_addr_string, "00a0240f9fac")) {
376
        gMatts_PC = 1;
377
    }
378
    return 0;
379
}
380
 
381
// IDA: int __cdecl PDNetShutdown()
382
int PDNetShutdown(void) {
383
    LOG_TRACE("()");
384
 
385
    dr_dprintf("PDNetShutdown()");
386
    if (gSocket != -1) {
387
        closesocket(gSocket);
388
    }
389
    gSocket = -1;
390
    return 0;
391
}
392
 
393
// IDA: void __cdecl PDNetStartProducingJoinList()
394
void PDNetStartProducingJoinList(void) {
395
    LOG_TRACE("()");
396
 
397
    dr_dprintf("PDNetStartProducingJoinList()");
398
    gNumber_of_hosts = 0;
399
    gJoinable_games = BrMemAllocate(sizeof(tPD_net_game_info) * JOINABLE_GAMES_CAPACITY, 0x80u);
400
    if (gJoinable_games == NULL) {
401
        PDFatalError("Can't allocate memory for joinable games");
402
    }
403
}
404
 
405
// IDA: void __cdecl PDNetEndJoinList()
406
void PDNetEndJoinList(void) {
407
    LOG_TRACE("()");
408
 
409
    dr_dprintf("PDNetEndJoinList()");
410
    if (gJoinable_games) {
411
        BrMemFree(gJoinable_games);
412
    }
413
    gJoinable_games = NULL;
414
}
415
 
416
// IDA: int __usercall PDNetGetNextJoinGame@<EAX>(tNet_game_details *pGame@<EAX>, int pIndex@<EDX>)
417
int PDNetGetNextJoinGame(tNet_game_details* pGame, int pIndex) {
418
    static tU32 next_broadcast_time = 0;
419
    int i;
420
    int j;
421
    int number_of_hosts_has_changed;
422
    //char str[256]; // Pierre-Marie Baty -- unused variable
423
    LOG_TRACE9("(%p, %d)", pGame, pIndex);
424
 
425
    dr_dprintf("PDNetGetNextJoinGame(): pIndex is %d", pIndex);
426
    if (pIndex == 0) {
427
        do {
428
            number_of_hosts_has_changed = 0;
429
            for (i = 0; i < gNumber_of_hosts; i++) {
430
                if (gJoinable_games[i].last_response + 10000 < (tU32) PDGetTotalTime()) { // Pierre-Marie Baty -- added type cast
431
                    number_of_hosts_has_changed = 1;
432
                    for (j = i; j < gNumber_of_hosts - 1; j++) {
433
                        memcpy(&gJoinable_games[j], &gJoinable_games[j + 1], sizeof(tPD_net_game_info));
434
                    }
435
                }
436
            }
437
            if (number_of_hosts_has_changed) {
438
                gNumber_of_hosts--;
439
            }
440
        } while (number_of_hosts_has_changed);
441
        if ((tU32) PDGetTotalTime() > next_broadcast_time) { // Pierre-Marie Baty -- added type cast
442
            next_broadcast_time = PDGetTotalTime() + 3000;
443
            MakeMessageToSend(1);
444
            if (BroadcastMessage() == 0) {
445
                dr_dprintf("PDNetGetNextJoinGame(): Error on BroadcastMessage()");
446
            }
447
        }
448
    }
449
    ReceiveHostResponses();
450
    if (gNumber_of_hosts <= pIndex) {
451
        return 0;
452
    }
453
    dr_dprintf("PDNetGetNextJoinGame(): Adding game.");
454
    memcpy(&pGame->pd_net_info.addr_in, &gJoinable_games[pIndex].addr_in, sizeof(pGame->pd_net_info.addr_in));
455
    return 1;
456
}
457
 
458
// IDA: void __usercall PDNetDisposeGameDetails(tNet_game_details *pDetails@<EAX>)
459
void PDNetDisposeGameDetails(tNet_game_details* pDetails) {
460
    LOG_TRACE("(%p)", pDetails);
461
    NOT_IMPLEMENTED();
462
}
463
 
464
// IDA: int __usercall PDNetHostGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pHost_name@<EDX>, void **pHost_address@<EBX>)
465
int PDNetHostGame(tNet_game_details* pDetails, char* pHost_name, void** pHost_address) {
466
    LOG_TRACE("(%p, \"%s\", %p)", pDetails, pHost_name, pHost_address);
467
 
468
    dr_dprintf("PDNetHostGame()");
469
    *pHost_address = &gLocal_addr;
470
    return 1;
471
}
472
 
473
// IDA: int __usercall PDNetJoinGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pPlayer_name@<EDX>)
474
int PDNetJoinGame(tNet_game_details* pDetails, char* pPlayer_name) {
475
    LOG_TRACE("(%p, \"%s\")", pDetails, pPlayer_name);
476
 
477
    dr_dprintf("PDNetJoinGame()");
478
    return 0;
479
}
480
 
481
// IDA: void __usercall PDNetLeaveGame(tNet_game_details *pDetails@<EAX>)
482
void PDNetLeaveGame(tNet_game_details* pDetails) {
483
    LOG_TRACE("(%p)", pDetails);
484
 
485
    dr_dprintf("PDNetLeaveGame()");
486
}
487
 
488
// IDA: void __usercall PDNetHostFinishGame(tNet_game_details *pDetails@<EAX>)
489
void PDNetHostFinishGame(tNet_game_details* pDetails) {
490
    LOG_TRACE("(%p)", pDetails);
491
 
492
    dr_dprintf("PDNetHostFinishGame()");
493
}
494
 
495
// IDA: tU32 __usercall PDNetExtractGameID@<EAX>(tNet_game_details *pDetails@<EAX>)
496
tU32 PDNetExtractGameID(tNet_game_details* pDetails) {
497
    LOG_TRACE("(%p)", pDetails);
498
 
499
    dr_dprintf("PDNetExtractGameID()");
500
    return ntohs(pDetails->pd_net_info.addr_in.sin_port); // PDGetTotalTime();
501
}
502
 
503
// IDA: tPlayer_ID __usercall PDNetExtractPlayerID@<EAX>(tNet_game_details *pDetails@<EAX>)
504
tPlayer_ID PDNetExtractPlayerID(tNet_game_details* pDetails) {
505
    LOG_TRACE("(%p)", pDetails);
506
 
507
    dr_dprintf("PDNetExtractPlayerID()");
508
    return ntohs(gLocal_addr_ipx->sin_port);
509
}
510
 
511
// IDA: void __usercall PDNetObtainSystemUserName(char *pName@<EAX>, int pMax_length@<EDX>)
512
void PDNetObtainSystemUserName(char* pName, int pMax_length) {
513
    int result;
514
    char* found;
515
    dr_dprintf("PDNetObtainSystemUserName()");
516
 
517
    result = gethostname(pName, pMax_length);
518
    if (result == 0) {
519
    }
520
 
521
    while (1) {
522
        found = strpbrk(pName, "_=(){}[]<>!$%^&*/:@~;'#,?\\|`\"");
523
        if (found == NULL) {
524
            break;
525
        }
526
        *found = '-';
527
    }
528
}
529
 
530
// IDA: int __usercall PDNetSendMessageToPlayer@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, tPlayer_ID pPlayer@<EBX>)
531
int PDNetSendMessageToPlayer(tNet_game_details* pDetails, tNet_message* pMessage, tPlayer_ID pPlayer) {
532
    //char str[256]; // Pierre-Marie Baty -- unused variable
533
    // SOCKADDR_IPX_* remote_addr_ipx;
534
    LOG_TRACE("(%p, %p, %d)", pDetails, pMessage, pPlayer);
535
    NOT_IMPLEMENTED();
536
}
537
 
538
// IDA: int __usercall PDNetSendMessageToAllPlayers@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
539
int PDNetSendMessageToAllPlayers(tNet_game_details* pDetails, tNet_message* pMessage) {
540
    //char str[256]; // Pierre-Marie Baty -- unused variable
541
    int i;
542
    LOG_TRACE("(%p, %p)", pDetails, pMessage);
543
 
544
    for (i = 0; i < gNumber_of_net_players; ++i) {
545
        if (i == gThis_net_player_index) {
546
            continue;
547
        }
548
        if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (struct sockaddr*)&gNet_players[i].pd_net_info.addr_in, sizeof(gNet_players[i].pd_net_info.addr_in)) == -1) {
549
            dr_dprintf("PDNetSendMessageToAllPlayers(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
550
            NetDisposeMessage(pDetails, pMessage);
551
            return 1;
552
        }
553
    }
554
    NetDisposeMessage(pDetails, pMessage);
555
    return 0;
556
}
557
 
558
// IDA: tNet_message* __usercall PDNetGetNextMessage@<EAX>(tNet_game_details *pDetails@<EAX>, void **pSender_address@<EDX>)
559
tNet_message* PDNetGetNextMessage(tNet_game_details* pDetails, void** pSender_address) {
560
    char* receive_buffer;
561
    char str[256];
562
    int msg_type;
563
    LOG_TRACE("(%p, %p)", pDetails, pSender_address);
564
 
565
    char addr_str[32];
566
    unsigned int sa_len;
567
    int res;
568
    tNet_message* msg;
569
 
570
    sa_len = sizeof(gRemote_addr);
571
    msg = NetAllocateMessage(512);
572
    receive_buffer = (char*)msg;
573
    res = recvfrom(gSocket, receive_buffer, 512, 0, (struct sockaddr*)&gRemote_addr, &sa_len);
574
    res = res != -1;
575
    if (res == 0) {
576
        res = WSAGetLastError() != WSAEWOULDBLOCK;
577
        if (res) {
578
            sprintf(str, "PDNetGetNextMessage(): Error on recvfrom() - WSAGetLastError=%d", res);
579
            PDFatalError(str);
580
        }
581
    } else {
582
        NetNowIPXLocalTarget2String(addr_str, gRemote_addr_ipx);
583
        if (!SameEthernetAddress(gLocal_addr_ipx, gRemote_addr_ipx)) {
584
            msg_type = GetMessageTypeFromMessage(receive_buffer);
585
            switch (msg_type) {
586
            case 1:
587
                if (gNet_mode == eNet_mode_host) {
588
                    dr_dprintf("PDNetGetNextMessage(): Received '%s' from '%s', replying to joiner", receive_buffer, addr_str);
589
                    MakeMessageToSend(2);
590
                    if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gRemote_addr, sizeof(gRemote_addr)) == -1) {
591
                        dr_dprintf("PDNetGetNextMessage(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
592
                    }
593
                }
594
                break;
595
            case 2:
596
                // no-op
597
                break;
598
            default:
599
                dr_dprintf("PDNetGetNextMessage(): res is %d, received message type %d from '%s', passing up", res, msg->contents.header.type, addr_str);
600
                memcpy(&gLast_received_addr, gRemote_addr_ipx, sizeof(gLast_received_addr));
601
                *pSender_address = &gLast_received_addr;
602
                return msg;
603
            }
604
        }
605
    }
606
    msg->guarantee_number = 0;
607
    NetDisposeMessage(pDetails, msg);
608
    return 0;
609
}
610
 
611
// IDA: tNet_message* __usercall PDNetAllocateMessage@<EAX>(tU32 pSize@<EAX>, tS32 pSize_decider@<EDX>)
612
tNet_message* PDNetAllocateMessage(tU32 pSize, tS32 pSize_decider) {
613
    LOG_TRACE("(%d, %d)", pSize, pSize_decider);
614
    NOT_IMPLEMENTED();
615
}
616
 
617
// IDA: void __usercall PDNetDisposeMessage(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
618
void PDNetDisposeMessage(tNet_game_details* pDetails, tNet_message* pMessage) {
619
    LOG_TRACE("(%p, %p)", pDetails, pMessage);
620
    NOT_IMPLEMENTED();
621
}
622
 
623
// IDA: void __usercall PDNetSetPlayerSystemInfo(tNet_game_player_info *pPlayer@<EAX>, void *pSender_address@<EDX>)
624
void PDNetSetPlayerSystemInfo(tNet_game_player_info* pPlayer, void* pSender_address) {
625
    LOG_TRACE("(%p, %p)", pPlayer, pSender_address);
626
 
627
    dr_dprintf("PDNetSetPlayerSystemInfo()");
628
    memcpy(&pPlayer->pd_net_info, pSender_address, sizeof(pPlayer->pd_net_info));
629
}
630
 
631
// IDA: void __usercall PDNetDisposePlayer(tNet_game_player_info *pPlayer@<EAX>)
632
void PDNetDisposePlayer(tNet_game_player_info* pPlayer) {
633
    LOG_TRACE("(%p)", pPlayer);
634
    NOT_IMPLEMENTED();
635
}
636
 
637
// IDA: int __usercall PDNetSendMessageToAddress@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, void *pAddress@<EBX>)
638
int PDNetSendMessageToAddress(tNet_game_details* pDetails, tNet_message* pMessage, void* pAddress) {
639
    char str[256];
640
    LOG_TRACE("(%p, %p, %p)", pDetails, pMessage, pAddress);
641
 
642
    NetNowIPXLocalTarget2String(str, (struct sockaddr_in*)pAddress);
20 pmbaty 643
    if (pDetails->pd_net_info.addr_in.sin_port == 0) {
644
        int i = 0;
645
    }
1 pmbaty 646
 
647
    if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (const struct sockaddr*)pAddress, sizeof(struct sockaddr)) == -1) {
648
        dr_dprintf("PDNetSendMessageToAddress(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
649
        NetDisposeMessage(pDetails, pMessage);
650
        return 1;
651
    }
652
 
653
    NetDisposeMessage(pDetails, pMessage);
654
    return 0;
655
}
656
 
657
// IDA: int __usercall PDNetInitClient@<EAX>(tNet_game_details *pDetails@<EAX>)
658
int PDNetInitClient(tNet_game_details* pDetails) {
659
    LOG_TRACE("(%p)", pDetails);
660
    NOT_IMPLEMENTED();
661
}
662
 
663
// IDA: int __cdecl PDNetGetHeaderSize()
664
int PDNetGetHeaderSize(void) {
665
    LOG_TRACE("()");
666
 
667
    return 0;
668
}