ESP8266-based WiFi serial modem emulator ROM
1/*
2 * WiFiPPP
3 * Copyright (c) 2021 joshua stein <jcs@jcs.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "wifippp.h"
19#include <WiFiClient.h>
20#include <WiFiServer.h>
21#include "SocksClient.h"
22
23#define SOCKS_PORT 1080
24
25WiFiServer socks_server(SOCKS_PORT);
26
27#define MAX_SOCKS_CLIENTS 5
28static SocksClient *socks_clients[MAX_SOCKS_CLIENTS] = { nullptr };
29
30void
31socks_setup(void)
32{
33 socks_server = WiFiServer(settings->ppp_server_ip, SOCKS_PORT);
34 socks_server.begin();
35}
36
37void
38socks_process(void)
39{
40 int i;
41
42 if (socks_server.hasClient()) {
43 int slot = -1;
44
45 for (i = 0; i < MAX_SOCKS_CLIENTS; i++) {
46 if (!socks_clients[i] || socks_clients[i]->done()) {
47 if (socks_clients[i])
48 delete socks_clients[i];
49 slot = i;
50 break;
51 }
52 }
53
54#ifdef SOCKS_TRACE
55 syslog.logf(LOG_DEBUG, "new SOCKS client, slot %d", slot);
56#endif
57
58 if (slot > -1) {
59 WiFiClient client = socks_server.available();
60 if (client.connected())
61 socks_clients[slot] = new SocksClient(slot,
62 client);
63 else
64 syslog.logf(LOG_ERR, "found slot %d for new "
65 "connection but not connected", slot);
66 }
67 }
68
69 for (i = 0; i < MAX_SOCKS_CLIENTS; i++) {
70 if (!socks_clients[i])
71 continue;
72
73 if (socks_clients[i]->done()) {
74 delete socks_clients[i];
75 socks_clients[i] = nullptr;
76 continue;
77 }
78
79 socks_clients[i]->process();
80 }
81}