| 1 |
/************************************************************************ |
| 2 |
* network.h Header file for low-level networking routines * |
| 3 |
* Copyright (C) 1998-2022 Ben Webb * |
| 4 |
* Email: [email protected] * |
| 5 |
* WWW: https://dopewars.sourceforge.io/ * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU General Public License * |
| 9 |
* as published by the Free Software Foundation; either version 2 * |
| 10 |
* of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This program is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU General Public License * |
| 18 |
* along with this program; if not, write to the Free Software * |
| 19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, * |
| 20 |
* MA 02111-1307, USA. * |
| 21 |
************************************************************************/ |
| 22 |
|
| 23 |
#ifndef __DP_NETWORK_H__ |
| 24 |
#define __DP_NETWORK_H__ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include <config.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
/* Various includes necessary for select() calls */ |
| 31 |
#ifdef CYGWIN |
| 32 |
#include <winsock2.h> |
| 33 |
#include <windows.h> |
| 34 |
#else |
| 35 |
#include <sys/types.h> |
| 36 |
/* Be careful not to include both sys/time.h and time.h on those systems |
| 37 |
* which don't like it */ |
| 38 |
#if TIME_WITH_SYS_TIME |
| 39 |
#include <sys/time.h> |
| 40 |
#include <time.h> |
| 41 |
#else |
| 42 |
#ifdef HAVE_SYS_TIME_H |
| 43 |
#include <sys/time.h> |
| 44 |
#else |
| 45 |
#include <time.h> |
| 46 |
#endif |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef HAVE_UNISTD_H |
| 50 |
#include <unistd.h> |
| 51 |
#endif |
| 52 |
#endif |
| 53 |
|
| 54 |
#include <glib.h> |
| 55 |
|
| 56 |
#include "error.h" |
| 57 |
|
| 58 |
#ifdef NETWORKING |
| 59 |
#include <curl/curl.h> |
| 60 |
|
| 61 |
#ifndef SOCKET_ERROR |
| 62 |
#define SOCKET_ERROR -1 |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef CYGWIN |
| 66 |
/* Need GUI main loop to handle timers/events */ |
| 67 |
#include "gtkport/gtkport.h" |
| 68 |
#else |
| 69 |
#define dp_g_source_remove g_source_remove |
| 70 |
#define dp_g_io_add_watch g_io_add_watch |
| 71 |
#define dp_g_timeout_add g_timeout_add |
| 72 |
#endif |
| 73 |
|
| 74 |
typedef struct _CurlConnection { |
| 75 |
CURLM *multi; |
| 76 |
CURL *h; |
| 77 |
gboolean running; |
| 78 |
gchar *data; |
| 79 |
size_t data_size; |
| 80 |
char Terminator; /* Character that separates messages */ |
| 81 |
char StripChar; /* Char that should be removed |
| 82 |
* from messages */ |
| 83 |
GPtrArray *headers; |
| 84 |
|
| 85 |
guint timer_event; |
| 86 |
GSourceFunc timer_cb; |
| 87 |
GIOFunc socket_cb; |
| 88 |
} CurlConnection; |
| 89 |
|
| 90 |
typedef struct _ConnBuf { |
| 91 |
gchar *Data; /* bytes waiting to be read/written */ |
| 92 |
gint Length; /* allocated length of the "Data" buffer */ |
| 93 |
gint DataPresent; /* number of bytes currently in "Data" */ |
| 94 |
} ConnBuf; |
| 95 |
|
| 96 |
typedef struct _NetworkBuffer NetworkBuffer; |
| 97 |
|
| 98 |
typedef void (*NBCallBack) (NetworkBuffer *NetBuf, gboolean Read, |
| 99 |
gboolean Write, gboolean Exception, |
| 100 |
gboolean CallNow); |
| 101 |
|
| 102 |
typedef void (*NBUserPasswd) (NetworkBuffer *NetBuf, gpointer data); |
| 103 |
|
| 104 |
/* Information about a SOCKS server */ |
| 105 |
typedef struct _SocksServer { |
| 106 |
gchar *name; /* hostname */ |
| 107 |
int port; /* port number */ |
| 108 |
int version; /* desired protocol version (usually |
| 109 |
* 4 or 5) */ |
| 110 |
gboolean numuid; /* if TRUE, send numeric user IDs rather |
| 111 |
* than names */ |
| 112 |
char *user; /* if not blank, override the username |
| 113 |
* with this */ |
| 114 |
gchar *authuser; /* if set, the username for SOCKS5 auth */ |
| 115 |
gchar *authpassword; /* if set, the password for SOCKS5 auth */ |
| 116 |
} SocksServer; |
| 117 |
|
| 118 |
/* The status of a network buffer */ |
| 119 |
typedef enum { |
| 120 |
NBS_PRECONNECT, /* Socket is not yet connected */ |
| 121 |
NBS_SOCKSCONNECT, /* A CONNECT request is being sent to a |
| 122 |
* SOCKS server */ |
| 123 |
NBS_CONNECTED /* Socket is connected */ |
| 124 |
} NBStatus; |
| 125 |
|
| 126 |
/* Status of a SOCKS v5 negotiation */ |
| 127 |
typedef enum { |
| 128 |
NBSS_METHODS, /* Negotiation of available methods */ |
| 129 |
NBSS_USERPASSWD, /* Username-password request is being sent */ |
| 130 |
NBSS_CONNECT /* CONNECT request is being sent */ |
| 131 |
} NBSocksStatus; |
| 132 |
|
| 133 |
/* Handles reading and writing messages from/to a network connection */ |
| 134 |
struct _NetworkBuffer { |
| 135 |
int fd; /* File descriptor of the socket */ |
| 136 |
GIOChannel *ioch; /* GLib representation of the descriptor */ |
| 137 |
gint InputTag; /* Identifier for GLib event routines */ |
| 138 |
NBCallBack CallBack; /* Function called when the socket |
| 139 |
* status changes */ |
| 140 |
gpointer CallBackData; /* Data accessible to the callback |
| 141 |
* function */ |
| 142 |
char Terminator; /* Character that separates messages */ |
| 143 |
char StripChar; /* Char that should be removed |
| 144 |
* from messages */ |
| 145 |
ConnBuf ReadBuf; /* New data, waiting for the application */ |
| 146 |
ConnBuf WriteBuf; /* Data waiting to be written to the wire */ |
| 147 |
ConnBuf negbuf; /* Output for protocol negotiation |
| 148 |
* (e.g. SOCKS) */ |
| 149 |
gboolean WaitConnect; /* TRUE if a non-blocking connect is in |
| 150 |
* progress */ |
| 151 |
NBStatus status; /* Status of the connection (if any) */ |
| 152 |
NBSocksStatus sockstat; /* Status of SOCKS negotiation (if any) */ |
| 153 |
SocksServer *socks; /* If non-NULL, a SOCKS server to use */ |
| 154 |
NBUserPasswd userpasswd; /* Function to supply username and |
| 155 |
* password for SOCKS5 authentication */ |
| 156 |
gpointer userpasswddata; /* data to pass to the above function */ |
| 157 |
gchar *host; /* If non-NULL, the host to connect to */ |
| 158 |
unsigned port; /* If non-NULL, the port to connect to */ |
| 159 |
LastError *error; /* Any error from the last operation */ |
| 160 |
}; |
| 161 |
|
| 162 |
void InitNetworkBuffer(NetworkBuffer *NetBuf, char Terminator, |
| 163 |
char StripChar, SocksServer *socks); |
| 164 |
void SetNetworkBufferCallBack(NetworkBuffer *NetBuf, NBCallBack CallBack, |
| 165 |
gpointer CallBackData); |
| 166 |
void SetNetworkBufferUserPasswdFunc(NetworkBuffer *NetBuf, |
| 167 |
NBUserPasswd userpasswd, |
| 168 |
gpointer data); |
| 169 |
gboolean IsNetworkBufferActive(NetworkBuffer *NetBuf); |
| 170 |
void BindNetworkBufferToSocket(NetworkBuffer *NetBuf, int fd); |
| 171 |
gboolean StartNetworkBufferConnect(NetworkBuffer *NetBuf, |
| 172 |
const gchar *bindaddr, |
| 173 |
gchar *RemoteHost, unsigned RemotePort); |
| 174 |
void ShutdownNetworkBuffer(NetworkBuffer *NetBuf); |
| 175 |
void SetSelectForNetworkBuffer(NetworkBuffer *NetBuf, fd_set *readfds, |
| 176 |
fd_set *writefds, fd_set *errorfds, |
| 177 |
int *MaxSock); |
| 178 |
gboolean RespondToSelect(NetworkBuffer *NetBuf, fd_set *readfds, |
| 179 |
fd_set *writefds, fd_set *errorfds, |
| 180 |
gboolean *DoneOK); |
| 181 |
gboolean NetBufHandleNetwork(NetworkBuffer *NetBuf, gboolean ReadReady, |
| 182 |
gboolean WriteReady, gboolean ErrorReady, |
| 183 |
gboolean *DoneOK); |
| 184 |
gboolean ReadDataFromWire(NetworkBuffer *NetBuf); |
| 185 |
gboolean WriteDataToWire(NetworkBuffer *NetBuf); |
| 186 |
void QueueMessageForSend(NetworkBuffer *NetBuf, gchar *data); |
| 187 |
gint CountWaitingMessages(NetworkBuffer *NetBuf); |
| 188 |
gchar *GetWaitingMessage(NetworkBuffer *NetBuf); |
| 189 |
void SendSocks5UserPasswd(NetworkBuffer *NetBuf, gchar *user, |
| 190 |
gchar *password); |
| 191 |
gchar *GetWaitingData(NetworkBuffer *NetBuf, int numbytes); |
| 192 |
gchar *PeekWaitingData(NetworkBuffer *NetBuf, int numbytes); |
| 193 |
gchar *ExpandWriteBuffer(ConnBuf *conn, int numbytes, LastError **error); |
| 194 |
void CommitWriteBuffer(NetworkBuffer *NetBuf, ConnBuf *conn, gchar *addpt, |
| 195 |
guint addlen); |
| 196 |
|
| 197 |
#define DOPE_CURL_ERROR dope_curl_error_quark() |
| 198 |
GQuark dope_curl_error_quark(void); |
| 199 |
|
| 200 |
#define DOPE_CURLM_ERROR dope_curlm_error_quark() |
| 201 |
GQuark dope_curlm_error_quark(void); |
| 202 |
|
| 203 |
void CurlInit(CurlConnection *conn); |
| 204 |
void CurlCleanup(CurlConnection *conn); |
| 205 |
gboolean OpenCurlConnection(CurlConnection *conn, char *URL, char *body, |
| 206 |
GError **err); |
| 207 |
void CloseCurlConnection(CurlConnection *conn); |
| 208 |
gboolean CurlConnectionPerform(CurlConnection *conn, int *still_running, |
| 209 |
GError **err); |
| 210 |
gboolean CurlConnectionSocketAction(CurlConnection *conn, curl_socket_t fd, |
| 211 |
int action, int *still_running, |
| 212 |
GError **err); |
| 213 |
char *CurlNextLine(CurlConnection *conn, char *ch); |
| 214 |
void SetCurlCallback(CurlConnection *conn, GSourceFunc timer_cb, |
| 215 |
GIOFunc socket_cb); |
| 216 |
|
| 217 |
int CreateTCPSocket(LastError **error); |
| 218 |
gboolean BindTCPSocket(int sock, const gchar *addr, unsigned port, |
| 219 |
LastError **error); |
| 220 |
void StartNetworking(void); |
| 221 |
void StopNetworking(void); |
| 222 |
|
| 223 |
#ifdef CYGWIN |
| 224 |
#define CloseSocket(sock) closesocket(sock) |
| 225 |
void SetReuse(SOCKET sock); |
| 226 |
void SetBlocking(SOCKET sock, gboolean blocking); |
| 227 |
#else |
| 228 |
#define CloseSocket(sock) close(sock) |
| 229 |
void SetReuse(int sock); |
| 230 |
void SetBlocking(int sock, gboolean blocking); |
| 231 |
#endif |
| 232 |
|
| 233 |
void AddB64Enc(GString *str, gchar *unenc); |
| 234 |
|
| 235 |
#endif /* NETWORKING */ |
| 236 |
|
| 237 |
#endif /* __DP_NETWORK_H__ */ |