aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/browser_network.c (14051B)


      1 #include "linux_network.h"
      2 #include "linux_sync.h"
      3 #include "guest_heap.h"
      4 #include <pthread.h>
      5 #include <errno.h>
      6 #include <arpa/inet.h>
      7 #include <unistd.h>
      8 #include <emscripten.h>
      9 
     10 /* WebSocket callbacks run on the browser UI thread; only game workers wait. */
     11 int ao_socket_create(void);
     12 void ao_socket_connect(int handle, int port);
     13 int ao_socket_status(int handle);
     14 int ao_socket_available(int handle);
     15 int ao_socket_send(int handle, const void *data, int size);
     16 int ao_socket_receive(int handle, void *data, int size, int peek);
     17 void ao_socket_close(int handle);
     18 void ao_socket_watch(int enabled);
     19 
     20 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     21 static unsigned startup_count;
     22 static int watching;
     23 static struct browser_socket {
     24     uint32_t handle, selected, armed, recorded, error;
     25     int nonblocking;
     26     struct semaphore_handle *event;
     27     struct browser_socket *next;
     28 } *sockets;
     29 
     30 static struct browser_socket *find_socket_locked(uint32_t handle)
     31 {
     32     if (!startup_count) { MEM32(g_fs_base + 0x34) = 10093; return NULL; }
     33     for (struct browser_socket *socket = sockets; socket; socket = socket->next)
     34         if (socket->handle == handle) return socket;
     35     MEM32(g_fs_base + 0x34) = 10038;
     36     return NULL;
     37 }
     38 
     39 EMSCRIPTEN_KEEPALIVE void browser_socket_events(void)
     40 {
     41     /* The browser thread never waits for game workers holding either lock. */
     42     int error = pthread_mutex_trylock(&mutex);
     43     if (error == EBUSY) return;
     44     if (error) abort();
     45     for (struct browser_socket *socket = sockets; socket; socket = socket->next) {
     46         if (!socket->armed) continue;
     47         int status = ao_socket_status(socket->handle);
     48         uint32_t ready = (status & 2 ? 1 : 0) | (status & 12 ? 32 : 0);
     49         ready &= socket->armed;
     50         if (ready && socket->event && !linux_sync_event_try_change(socket->event, 1)) continue;
     51         socket->recorded |= ready;
     52         socket->armed &= ~ready;
     53         if (status & 8) socket->error = 10054;
     54     }
     55     if (pthread_mutex_unlock(&mutex)) abort();
     56 }
     57 
     58 static void create_socket(void)
     59 {
     60     uint32_t family = MEM32(g_esp + 4), type = MEM32(g_esp + 8), protocol = MEM32(g_esp + 12);
     61     g_esp += 16;
     62     g_eax = UINT32_MAX;
     63     if (family != 2 || type != 1 || (protocol && protocol != 6)) {
     64         fprintf(stderr, "Unsupported browser socket: %u %u %u\n", family, type, protocol);
     65         abort();
     66     }
     67     pthread_mutex_lock(&mutex);
     68     if (!startup_count) MEM32(g_fs_base + 0x34) = 10093;
     69     else {
     70         struct browser_socket *socket = calloc(1, sizeof(*socket));
     71         if (!socket) abort();
     72         socket->handle = ao_socket_create();
     73         socket->next = sockets;
     74         sockets = socket;
     75         g_eax = socket->handle;
     76     }
     77     pthread_mutex_unlock(&mutex);
     78 }
     79 
     80 static void close_socket(void)
     81 {
     82     pthread_mutex_lock(&mutex);
     83     struct browser_socket *socket = find_socket_locked(MEM32(g_esp + 4));
     84     g_eax = UINT32_MAX;
     85     if (socket) {
     86         struct browser_socket **link = &sockets;
     87         while (*link != socket) link = &(*link)->next;
     88         *link = socket->next;
     89         if (socket->event) linux_sync_event_release(socket->event);
     90         ao_socket_close(socket->handle);
     91         free(socket);
     92         g_eax = 0;
     93     }
     94     pthread_mutex_unlock(&mutex);
     95     g_esp += 8;
     96 }
     97 
     98 static void shutdown_socket(void)
     99 {
    100     /* WebSocket has no TCP half-close. ACE handles this error and closes the
    101      * socket normally; do not discard unread bytes or claim a half-close. */
    102     pthread_mutex_lock(&mutex);
    103     if (find_socket_locked(MEM32(g_esp + 4)))
    104         MEM32(g_fs_base + 0x34) = MEM32(g_esp + 8) > 2 ? 10022 : 10045;
    105     pthread_mutex_unlock(&mutex);
    106     g_eax = UINT32_MAX;
    107     g_esp += 12;
    108 }
    109 
    110 static void connect_socket(void)
    111 {
    112     uint32_t handle = MEM32(g_esp + 4), address = MEM32(g_esp + 8), size = MEM32(g_esp + 12);
    113     g_esp += 16;
    114     g_eax = UINT32_MAX;
    115     if (!address || size < 16) { MEM32(g_fs_base + 0x34) = 10014; return; }
    116     if (MEM16(address) != 2) { MEM32(g_fs_base + 0x34) = 10047; return; }
    117     pthread_mutex_lock(&mutex);
    118     struct browser_socket *socket = find_socket_locked(handle);
    119     int nonblocking = socket ? socket->nonblocking : 0;
    120     if (socket) ao_socket_connect(handle, ntohs(MEM16(address + 2)));
    121     pthread_mutex_unlock(&mutex);
    122     if (!socket) return;
    123     if (nonblocking) { MEM32(g_fs_base + 0x34) = 10035; return; }
    124     int status;
    125     while (!((status = ao_socket_status(handle)) & 13)) usleep(1000);
    126     if (status & 1) g_eax = 0;
    127     else MEM32(g_fs_base + 0x34) = 10061;
    128 }
    129 
    130 static void control_socket(void)
    131 {
    132     uint32_t command = MEM32(g_esp + 8), argument = MEM32(g_esp + 12);
    133     pthread_mutex_lock(&mutex);
    134     struct browser_socket *socket = find_socket_locked(MEM32(g_esp + 4));
    135     g_eax = UINT32_MAX;
    136     if (socket && argument) {
    137         if (command == 0x8004667e) socket->nonblocking = MEM32(argument) != 0;
    138         else if (command == 0x4004667f) MEM32(argument) = ao_socket_available(socket->handle);
    139         else { fprintf(stderr, "Unsupported browser ioctlsocket: %08x\n", command); abort(); }
    140         g_eax = 0;
    141     }
    142     pthread_mutex_unlock(&mutex);
    143     g_esp += 16;
    144 }
    145 
    146 static void local_socket_address(void)
    147 {
    148     uint32_t output = MEM32(g_esp + 8), length = MEM32(g_esp + 12);
    149     pthread_mutex_lock(&mutex);
    150     struct browser_socket *socket = find_socket_locked(MEM32(g_esp + 4));
    151     g_eax = UINT32_MAX;
    152     if (socket) {
    153         uint32_t error = 0;
    154         if (!output || !length || (int32_t)MEM32(length) < 16) error = 10014;
    155         else if (!(ao_socket_status(socket->handle) & 1)) error = 10022;
    156         else {
    157             /* WebSocket hides the transport endpoint. Report the local virtual
    158              * interface, with port zero because no TCP port is exposed. */
    159             memset((void *)ADDR(output), 0, 16);
    160             MEM16(output) = 2;
    161             MEM32(output + 4) = 0x0100007f;
    162             MEM32(length) = 16;
    163             g_eax = 0;
    164         }
    165         MEM32(g_fs_base + 0x34) = error;
    166     }
    167     pthread_mutex_unlock(&mutex);
    168     g_esp += 16;
    169 }
    170 
    171 static void transfer_socket(int sending)
    172 {
    173     uint32_t handle = MEM32(g_esp + 4), buffer = MEM32(g_esp + 8), flags = MEM32(g_esp + 16);
    174     int32_t size = MEM32(g_esp + 12);
    175     g_esp += 20;
    176     g_eax = UINT32_MAX;
    177     if (!buffer || size < 0) { MEM32(g_fs_base + 0x34) = 10014; return; }
    178     if (flags && (sending || flags != 2)) abort();
    179     pthread_mutex_lock(&mutex);
    180     struct browser_socket *socket = find_socket_locked(handle);
    181     int nonblocking = socket ? socket->nonblocking : 0;
    182     pthread_mutex_unlock(&mutex);
    183     if (!socket) return;
    184     int result;
    185     do {
    186         result = sending ? ao_socket_send(handle, (void *)ADDR(buffer), size) :
    187                            ao_socket_receive(handle, (void *)ADDR(buffer), size, flags == 2);
    188         if (result == -10035 && !nonblocking) usleep(1000);
    189     } while (result == -10035 && !nonblocking);
    190     if (result < 0) MEM32(g_fs_base + 0x34) = -result;
    191     else g_eax = result;
    192     if (!sending) {
    193         pthread_mutex_lock(&mutex);
    194         for (socket = sockets; socket; socket = socket->next)
    195             if (socket->handle == handle) { socket->armed |= socket->selected & 1; break; }
    196         pthread_mutex_unlock(&mutex);
    197     }
    198 }
    199 static void send_socket(void) { transfer_socket(1); }
    200 static void receive_socket(void) { transfer_socket(0); }
    201 
    202 static void event_select(void)
    203 {
    204     uint32_t handle = MEM32(g_esp + 4), event = MEM32(g_esp + 8), mask = MEM32(g_esp + 12);
    205     if (mask & ~0x21u) abort();
    206     pthread_mutex_lock(&mutex);
    207     struct browser_socket *socket = find_socket_locked(handle);
    208     g_eax = UINT32_MAX;
    209     if (socket) {
    210         if (socket->event) linux_sync_event_release(socket->event);
    211         socket->event = event && mask ? linux_sync_event_acquire(event) : NULL;
    212         socket->selected = socket->armed = mask;
    213         socket->recorded = socket->error = 0;
    214         socket->nonblocking = 1;
    215         if (mask && !watching) {
    216             ao_socket_watch(1);
    217             watching = 1;
    218         }
    219         g_eax = 0;
    220     }
    221     pthread_mutex_unlock(&mutex);
    222     g_esp += 16;
    223 }
    224 
    225 static void enum_events(void)
    226 {
    227     uint32_t event = MEM32(g_esp + 8), output = MEM32(g_esp + 12);
    228     pthread_mutex_lock(&mutex);
    229     struct browser_socket *socket = find_socket_locked(MEM32(g_esp + 4));
    230     g_eax = UINT32_MAX;
    231     if (socket && output) {
    232         memset((void *)ADDR(output), 0, 44);
    233         MEM32(output) = socket->recorded;
    234         MEM32(output + 24) = socket->error;
    235         socket->recorded = 0;
    236         if (event) {
    237             struct semaphore_handle *signal = linux_sync_event_acquire(event);
    238             if (!signal) abort();
    239             linux_sync_event_change(signal, 0);
    240             linux_sync_event_release(signal);
    241         }
    242         g_eax = 0;
    243     }
    244     pthread_mutex_unlock(&mutex);
    245     g_esp += 16;
    246 }
    247 
    248 static void select_sockets(void)
    249 {
    250     uint32_t sets[] = {MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16)};
    251     uint32_t timeout = MEM32(g_esp + 20), counts[3], ready[3][64];
    252     double deadline = timeout ? emscripten_get_now() + MEM32(timeout) * 1000.0 + MEM32(timeout + 4) / 1000.0 : INFINITY;
    253     for (unsigned i = 0; i < 3; ++i) {
    254         counts[i] = sets[i] ? MEM32(sets[i]) : 0;
    255         if (counts[i] > 64) abort();
    256     }
    257     g_esp += 24;
    258     for (;;) {
    259         uint32_t total = 0, found[3] = {0};
    260         for (unsigned i = 0; i < 3; ++i) for (unsigned j = 0; j < counts[i]; ++j) {
    261             uint32_t handle = MEM32(sets[i] + 4 + j * 4);
    262             int status = ao_socket_status(handle);
    263             if (status & (i == 0 ? 14 : i == 1 ? 1 : 8)) {
    264                 ready[i][found[i]++] = handle;
    265                 ++total;
    266             }
    267         }
    268         if (total || emscripten_get_now() >= deadline) {
    269             for (unsigned i = 0; i < 3; ++i) if (sets[i]) {
    270                 MEM32(sets[i]) = found[i];
    271                 memcpy((void *)ADDR(sets[i] + 4), ready[i], found[i] * 4);
    272             }
    273             g_eax = total;
    274             return;
    275         }
    276         usleep(1000);
    277     }
    278 }
    279 
    280 static void socket_in_set(void)
    281 {
    282     uint32_t handle = MEM32(g_esp + 4), set = MEM32(g_esp + 8);
    283     g_eax = 0;
    284     for (unsigned i = 0; set && i < MEM32(set); ++i)
    285         if (MEM32(set + 4 + i * 4) == handle) { g_eax = 1; break; }
    286     g_esp += 12;
    287 }
    288 
    289 static void network_long(void) { g_eax = __builtin_bswap32(MEM32(g_esp + 4)); g_esp += 8; }
    290 static void network_short(void) { g_eax = __builtin_bswap16(MEM16(g_esp + 4)); g_esp += 8; }
    291 static void address_string(void)
    292 {
    293     struct in_addr address = {MEM32(g_esp + 4)};
    294     /* Keep the result in the guest thread's own storage, as on Linux. */
    295     g_eax = g_fs_base + 0x2100;
    296     if (!inet_ntop(AF_INET, &address, (char *)ADDR(g_eax), 16)) abort();
    297     MEM32(g_fs_base + 0x34) = 0;
    298     g_esp += 8;
    299 }
    300 static void last_error(void) { g_eax = MEM32(g_fs_base + 0x34); g_esp += 4; }
    301 static void set_error(void) { MEM32(g_fs_base + 0x34) = MEM32(g_esp + 4); g_esp += 8; }
    302 
    303 static void startup(void)
    304 {
    305     uint32_t data = MEM32(g_esp + 8);
    306     uint16_t requested = MEM16(g_esp + 4);
    307     g_eax = 10014;
    308     if (data) {
    309         MEM16(data) = requested;
    310         MEM16(data + 2) = 0x0202;
    311         strcpy((char *)ADDR(data + 4), "WinSock 2.0");
    312         strcpy((char *)ADDR(data + 261), "Running");
    313         MEM16(data + 390) = MEM16(data + 392) = 0;
    314         pthread_mutex_lock(&mutex);
    315         ++startup_count;
    316         pthread_mutex_unlock(&mutex);
    317         g_eax = 0;
    318     }
    319     g_esp += 12;
    320 }
    321 
    322 static void cleanup(void)
    323 {
    324     pthread_mutex_lock(&mutex);
    325     g_eax = UINT32_MAX;
    326     if (startup_count) {
    327         if (!--startup_count) {
    328             while (sockets) {
    329                 struct browser_socket *socket = sockets;
    330                 sockets = socket->next;
    331                 if (socket->event) linux_sync_event_release(socket->event);
    332                 ao_socket_close(socket->handle);
    333                 free(socket);
    334             }
    335             if (watching) {
    336                 ao_socket_watch(0);
    337                 watching = 0;
    338             }
    339         }
    340         g_eax = 0;
    341     } else MEM32(g_fs_base + 0x34) = 10093;
    342     pthread_mutex_unlock(&mutex);
    343     g_esp += 4;
    344 }
    345 
    346 static void host_name(void)
    347 {
    348     if (pthread_mutex_lock(&mutex)) abort();
    349     uint32_t output = MEM32(g_esp + 4);
    350     int32_t size = MEM32(g_esp + 8);
    351     char name[256];
    352     uint32_t error = 0;
    353     if (!startup_count) error = 10093; /* WSANOTINITIALISED */
    354     else if (!output || size <= 0) error = 10014; /* WSAEFAULT */
    355     else if (gethostname(name, sizeof(name))) error = 10050; /* WSAENETDOWN */
    356     else if ((size_t)size <= strlen(name)) error = 10014;
    357     else memcpy((void *)ADDR(output), name, strlen(name) + 1);
    358 
    359     MEM32(g_fs_base + 0x34) = error;
    360     g_eax = error ? UINT32_MAX : 0;
    361     g_esp += 12;
    362     if (pthread_mutex_unlock(&mutex)) abort();
    363 }
    364 
    365 recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal)
    366 {
    367     static const struct { const char *name; uint32_t ordinal; recomp_func_t function; } functions[] = {
    368         {"gethostname", 57, host_name},
    369         {"inet_ntoa", 12, address_string},
    370         {"WSAStartup", 115, startup}, {"WSACleanup", 116, cleanup},
    371         {"WSAGetLastError", 111, last_error}, {"WSASetLastError", 112, set_error},
    372         {"htonl", 8, network_long}, {"htons", 9, network_short},
    373         {"ntohl", 14, network_long}, {"ntohs", 15, network_short},
    374         {"socket", 23, create_socket}, {"closesocket", 3, close_socket},
    375         {"shutdown", 22, shutdown_socket},
    376         {"getsockname", 6, local_socket_address},
    377         {"ioctlsocket", 10, control_socket}, {"connect", 4, connect_socket},
    378         {"send", 19, send_socket}, {"recv", 16, receive_socket},
    379         {"select", 18, select_sockets}, {"__WSAFDIsSet", 151, socket_in_set},
    380         {"WSAEventSelect", 0, event_select}, {"WSAEnumNetworkEvents", 0, enum_events},
    381     };
    382     for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
    383         if (name ? !strcmp(name, functions[i].name) : ordinal && ordinal == functions[i].ordinal)
    384             return functions[i].function;
    385     return NULL;
    386 }