tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

acceptread.c (7206B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include <prio.h>
      7 #include <prprf.h>
      8 #include <prinit.h>
      9 #include <prnetdb.h>
     10 #include <prinrval.h>
     11 #include <prthread.h>
     12 
     13 #include <plerror.h>
     14 
     15 #include <stdlib.h>
     16 
     17 #ifdef DEBUG
     18 #  define PORT_INC_DO +100
     19 #else
     20 #  define PORT_INC_DO
     21 #endif
     22 #ifdef IS_64
     23 #  define PORT_INC_3264 +200
     24 #else
     25 #  define PORT_INC_3264
     26 #endif
     27 
     28 #define DEFAULT_PORT 12273 PORT_INC_DO PORT_INC_3264
     29 #define GET "GET / HTTP/1.0\n\n"
     30 static PRFileDesc *std_out, *err_out;
     31 static PRIntervalTime write_dally, accept_timeout;
     32 
     33 static PRStatus PrintAddress(const PRNetAddr* address) {
     34  char buffer[100];
     35  PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
     36  if (PR_FAILURE == rv) {
     37    PL_FPrintError(err_out, "PR_NetAddrToString");
     38  } else
     39    PR_fprintf(std_out, "Accepted connection from (0x%p)%s:%d\n", address,
     40               buffer, address->inet.port);
     41  return rv;
     42 } /* PrintAddress */
     43 
     44 static void ConnectingThread(void* arg) {
     45  PRInt32 nbytes;
     46  char buf[1024];
     47  PRFileDesc* sock;
     48  PRNetAddr peer_addr, *addr;
     49 
     50  addr = (PRNetAddr*)arg;
     51 
     52  sock = PR_NewTCPSocket();
     53  if (sock == NULL) {
     54    PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
     55    PR_ProcessExit(1);
     56  }
     57 
     58  if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
     59    PL_FPrintError(err_out, "PR_Connect (client) failed");
     60    PR_ProcessExit(1);
     61  }
     62  if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE) {
     63    PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
     64    PR_ProcessExit(1);
     65  }
     66 
     67  /*
     68  ** Then wait between the connection coming up and sending the expected
     69  ** data. At some point in time, the server should fail due to a timeou
     70  ** on the AcceptRead() operation, which according to the document is
     71  ** only due to the read() portion.
     72  */
     73  PR_Sleep(write_dally);
     74 
     75  nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
     76  if (nbytes == -1) {
     77    PL_FPrintError(err_out, "PR_Send (client) failed");
     78  }
     79 
     80  nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
     81  if (nbytes == -1) {
     82    PL_FPrintError(err_out, "PR_Recv (client) failed");
     83  } else {
     84    PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
     85    buf[sizeof(buf) - 1] = '\0';
     86    PR_fprintf(std_out, "%s\n", buf);
     87  }
     88 
     89  if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH)) {
     90    PL_FPrintError(err_out, "PR_Shutdown (client) failed");
     91  }
     92 
     93  if (PR_FAILURE == PR_Close(sock)) {
     94    PL_FPrintError(err_out, "PR_Close (client) failed");
     95  }
     96 
     97  return;
     98 } /* ConnectingThread */
     99 
    100 #define BUF_SIZE 117
    101 static void AcceptingThread(void* arg) {
    102  PRStatus rv;
    103  PRInt32 bytes;
    104  PRSize buf_size = BUF_SIZE;
    105  PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
    106  PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
    107  PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
    108  PRSocketOptionData sock_opt;
    109 
    110  if (NULL == listen_sock) {
    111    PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
    112    PR_ProcessExit(1);
    113  }
    114  sock_opt.option = PR_SockOpt_Reuseaddr;
    115  sock_opt.value.reuse_addr = PR_TRUE;
    116  rv = PR_SetSocketOption(listen_sock, &sock_opt);
    117  if (PR_FAILURE == rv) {
    118    PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
    119    PR_ProcessExit(1);
    120  }
    121  rv = PR_Bind(listen_sock, listen_addr);
    122  if (PR_FAILURE == rv) {
    123    PL_FPrintError(err_out, "PR_Bind (server) failed");
    124    PR_ProcessExit(1);
    125  }
    126  rv = PR_Listen(listen_sock, 10);
    127  if (PR_FAILURE == rv) {
    128    PL_FPrintError(err_out, "PR_Listen (server) failed");
    129    PR_ProcessExit(1);
    130  }
    131  bytes = PR_AcceptRead(listen_sock, &accept_sock, &accept_addr, buf, buf_size,
    132                        accept_timeout);
    133 
    134  if (-1 == bytes) {
    135    PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
    136  } else {
    137    PrintAddress(accept_addr);
    138    PR_fprintf(std_out, "(Server) read [0x%p..0x%p) %s\n", buf, &buf[BUF_SIZE],
    139               buf);
    140    bytes = PR_Write(accept_sock, buf, bytes);
    141    rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
    142    if (PR_FAILURE == rv) {
    143      PL_FPrintError(err_out, "PR_Shutdown (server) failed");
    144    }
    145  }
    146 
    147  if (-1 != bytes) {
    148    rv = PR_Close(accept_sock);
    149    if (PR_FAILURE == rv) {
    150      PL_FPrintError(err_out, "PR_Close (server) failed");
    151    }
    152  }
    153 
    154  rv = PR_Close(listen_sock);
    155  if (PR_FAILURE == rv) {
    156    PL_FPrintError(err_out, "PR_Close (server) failed");
    157  }
    158 } /* AcceptingThread */
    159 
    160 int main(int argc, char** argv) {
    161  PRHostEnt he;
    162  PRStatus status;
    163  PRIntn next_index;
    164  PRUint16 port_number;
    165  char netdb_buf[PR_NETDB_BUF_SIZE];
    166  PRNetAddr client_addr, server_addr;
    167  PRThread *client_thread, *server_thread;
    168  PRIntervalTime delta = PR_MillisecondsToInterval(500);
    169 
    170  err_out = PR_STDERR;
    171  std_out = PR_STDOUT;
    172  accept_timeout = PR_SecondsToInterval(2);
    173 
    174  if (argc != 2 && argc != 3) {
    175    port_number = DEFAULT_PORT;
    176  } else {
    177    port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
    178  }
    179 
    180  status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
    181  if (PR_SUCCESS != status) {
    182    PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
    183    PR_ProcessExit(1);
    184  }
    185  if (argc < 3) {
    186    status = PR_InitializeNetAddr(PR_IpAddrLoopback, port_number, &client_addr);
    187    if (PR_SUCCESS != status) {
    188      PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
    189      PR_ProcessExit(1);
    190    }
    191  } else {
    192    status = PR_GetHostByName(argv[1], netdb_buf, sizeof(netdb_buf), &he);
    193    if (status == PR_FAILURE) {
    194      PL_FPrintError(err_out, "PR_GetHostByName failed");
    195      PR_ProcessExit(1);
    196    }
    197    next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
    198    if (next_index == -1) {
    199      PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
    200      PR_ProcessExit(1);
    201    }
    202  }
    203 
    204  for (write_dally = 0; write_dally < accept_timeout + (2 * delta);
    205       write_dally += delta) {
    206    PR_fprintf(std_out, "Testing w/ write_dally = %d msec\n",
    207               PR_IntervalToMilliseconds(write_dally));
    208    server_thread = PR_CreateThread(PR_USER_THREAD, AcceptingThread,
    209                                    &server_addr, PR_PRIORITY_NORMAL,
    210                                    PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
    211    if (server_thread == NULL) {
    212      PL_FPrintError(err_out, "PR_CreateThread (server) failed");
    213      PR_ProcessExit(1);
    214    }
    215 
    216    PR_Sleep(delta); /* let the server pot thicken */
    217 
    218    client_thread = PR_CreateThread(PR_USER_THREAD, ConnectingThread,
    219                                    &client_addr, PR_PRIORITY_NORMAL,
    220                                    PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
    221    if (client_thread == NULL) {
    222      PL_FPrintError(err_out, "PR_CreateThread (client) failed");
    223      PR_ProcessExit(1);
    224    }
    225 
    226    if (PR_JoinThread(client_thread) == PR_FAILURE) {
    227      PL_FPrintError(err_out, "PR_JoinThread (client) failed");
    228    }
    229 
    230    if (PR_JoinThread(server_thread) == PR_FAILURE) {
    231      PL_FPrintError(err_out, "PR_JoinThread (server) failed");
    232    }
    233  }
    234 
    235  return 0;
    236 }