summaryrefslogtreecommitdiff
path: root/other/ldistfp/src/network.h
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/ldistfp/src/network.h
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/ldistfp/src/network.h')
-rw-r--r--other/ldistfp/src/network.h287
1 files changed, 287 insertions, 0 deletions
diff --git a/other/ldistfp/src/network.h b/other/ldistfp/src/network.h
new file mode 100644
index 0000000..253ecc9
--- /dev/null
+++ b/other/ldistfp/src/network.h
@@ -0,0 +1,287 @@
1/* scut's leet network library ;)
2 * 1999 (c) scut
3 *
4 * networking code
5 */
6
7#ifndef SCUT_NETWORK_H
8#define SCUT_NETWORK_H
9
10#include <sys/socket.h>
11#include <netinet/in.h>
12#include <stdio.h>
13
14#define NET_READTIMEOUT 180
15#define NET_CONNTIMEOUT 60
16#define NET_IDENTTIMEOUT 15
17
18
19typedef struct bound {
20 int bs; /* bound socket */
21 unsigned short port; /* port we bound to */
22 struct sockaddr bsa; /* bs_in */
23} bound;
24
25extern int net_readtimeout;
26extern int net_conntimeout;
27extern int net_identtimeout;
28
29
30/* net_socks_connect
31 *
32 * relays through an open socks 5 server (NO AUTH type)
33 * returns a socket descriptor which is already connected
34 */
35
36int net_socks_connect (char *socks, unsigned short int sport,
37 char *server, unsigned short int port, int sec);
38
39
40/* net_socks_put_s5info
41 *
42 * insert socks 5 compatible relay information into socket s5s,
43 * used to relay over more then just one socks server
44 */
45
46int net_socks_put_s5info (int s5s, char *server,
47 unsigned short int port, int sec);
48
49
50/* net_parseip
51 *
52 * read an ip in the format "1.1.1.1:299" or "blabla:481" into
53 * the char pointer *ip and into the port *port
54 *
55 * return 0 on failure
56 * return 1 on success
57 */
58
59int net_parseip (char *inp, char **ip, unsigned short int *port);
60
61
62/* net_getlocalip
63 *
64 * give back the main IP of the local machine
65 *
66 * return the local IP address as string on success
67 * return NULL on failure
68 */
69
70char *net_getlocalip (void);
71
72
73/* net_peeraddress
74 *
75 * return a pointer to a string representation of the remote IP address of
76 * the already connected socket `socket'
77 *
78 * return NULL on failure
79 * return string pointer on succes
80 */
81
82char * net_peeraddress (int socket);
83
84
85/* net_descriptify
86 *
87 * descriptify a socket `socket' ;)
88 *
89 * return -1 on failure
90 * return file descriptor on success
91 */
92
93FILE *net_descriptify (int socket);
94
95
96/* net_ident
97 *
98 * ident a connection identified by the host:port pairs on both sides,
99 * returning the ident in *ident
100 *
101 * return 1 on success
102 * return -1 on failure
103 */
104
105int net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport,
106 struct sockaddr_in *remotes, unsigned short int remoteport);
107
108
109/* net_accept
110 *
111 * accept a connection from socket s, and stores the connection
112 * into cs.
113 * wait a maximum amount of maxsec seconds for connections
114 * maxsec can also be zero (infinite wait, until connection)
115 *
116 * return 0 if no connection has been made within maxsec seconds
117 * return -1 if an error appears
118 * return the socket number if a connection has been made
119 */
120
121int net_accept (int s, struct sockaddr_in *cs, int maxsec);
122
123
124/* net_bind
125 *
126 * bind a socket to an ip:port on the local machine,
127 * `ip' can be either NULL (bind to all IP's on the host), or a pointer
128 * to a virtual host name, or a real IP, or "*" for any.
129 * `port' can be either 0 (ephemeral port), or any free port.
130 *
131 * return NULL on failure
132 * return pointer to bound structure on success
133 */
134
135bound *net_bind (char *ip, unsigned short int port);
136
137
138/* net_boundfree
139 *
140 * free the bound structure pointed to by `bf'
141 *
142 * return in any case
143 */
144
145void net_boundfree (bound *bf);
146
147
148/* net_resolve
149 *
150 * resolve a hostname pointed to by `host' into a s_addr return value
151 *
152 * return the correct formatted `s_addr' for this host on success
153 * return 0 on failure
154 */
155
156unsigned long int net_resolve (char *host);
157
158
159/* net_assignaddr
160 *
161 * assign an IP address and port to a socket
162 * sourceip can be an IP or hostname that is available on the local host,
163 * or NULL/"*" to let the kernel choose one, same applies to sourceport,
164 * it can either be zero (ephemeral port choosen by the kernel) or a
165 * valid port number
166 *
167 * return 1 on success
168 * return 0 on failure
169 */
170
171int net_assignaddr (int sd, char *sourceip, unsigned short int sourceport);
172
173
174/* net_connect
175 *
176 * connect to the given `server' and `port' with a max timeout of `sec'.
177 * initialize the sockaddr_in struct `cs' correctly (ipv4), accept any
178 * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname.
179 * create a new socket and return either -1 if failed or
180 * the connected socket if connection has been established within the
181 * timeout limit.
182 *
183 * the routine is still IPv4 biased :-/
184 * with `sourceip'/`sourceportÄ you MAY specify the source IP and source port
185 * to use for the connection, but you can set the ip or port to NULL/0,
186 * to choose the default IP and an ephemeral port. this was added later in
187 * this library, so please update your sources.
188 *
189 * return -1 on failure
190 * return socket if success
191 */
192
193int net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, char *sourceip,
194 unsigned short int sourceport, int sec);
195
196
197/* net_rtimeout
198 *
199 * waits max `sec' seconds for fd to become readable
200 *
201 * return -1 on error (errno set)
202 * return 1 on readability
203 */
204
205int net_rtimeout (int fd, int sec);
206
207
208/* net_rbuf
209 *
210 * allocate memory and read socket data to `dst' until the connection
211 * gets closed.
212 *
213 * return n if success (n = number of bytes read)
214 * return -1 if failed
215 */
216
217long int net_rbuf (int fd, char **dst);
218#define NET_BSIZE 4096 /* blocksize for pre-allocation */
219
220
221/* net_rbuft
222 *
223 * read `dsize' bytes into `dst' from `fd', with timeout
224 *
225 * return 1 on success
226 * return -1 on failure
227 */
228int net_rbuft (int fd, char *dst, unsigned long int dsize);
229
230
231/* net_rlinet
232 *
233 * read a line from socket descriptor with timeout to buffer
234 * if sec = 0, then only a continuous stream of data is required, not
235 * an overall timeout.
236 *
237 * return -1 on timeout
238 * return 0 on connection close
239 * return length of readen line (including '\n')
240 *
241 * net_rlineta
242 * same as net_rlinet, but allocs the memory itself
243 */
244
245int net_rlineta (int fd, char **buf, int sec);
246int net_rlinet (int fd, char *buf, int bufsize, int sec);
247
248
249/* net_tline
250 *
251 * return length if string `buf' with a maximum length of `bufsize'
252 * contains '\n'
253 *
254 * return -1 if no '\n' in string
255 */
256
257int net_tline (char *buf, int bufsize);
258
259
260/* net_write
261 *
262 * print a formatted string to a socket, see syntax of printf
263 *
264 * return in any case
265 */
266
267void net_write (int fd, const char *str, ...);
268
269
270/* net_printip
271 *
272 * print an IP address stored in the struct in_addr pointed to by `ia' to a
273 * string `str' with a maximum length of `len'.
274 *
275 * return 0 on success
276 * return 1 on failure
277 *
278 * net_printipa behaves the same way, except it allocates memory and let
279 * `*str' point to the string
280 */
281
282int net_printip (struct in_addr *ia, char *str, size_t len);
283int net_printipa (struct in_addr *ia, char **str);
284
285
286#endif
287