summaryrefslogtreecommitdiff
path: root/other/reverb/network.h
blob: 8dc816d8d521890a7562ebb236aca892c79b7612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* scut's leet network library ;)
 * 1999 (c) scut
 *
 * networking code 
 */

#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>

#ifndef SCUT_NETWORK_H
#define SCUT_NETWORK_H

#define NET_READTIMEOUT	180
#define NET_CONNTIMEOUT	60


typedef	struct bound {
	int			bs;	/* bound socket */
	unsigned short		port;	/* port we bound to */
	struct sockaddr		bsa;	/* bs_in */
} bound;

extern int	net_readtimeout;
extern int	net_conntimeout;


/* net_parseip
 *
 * reads an ip in the format "1.1.1.1:299" or "blabla:481" into
 * the char pointer *ip and into the port *port
 *
 * returns 0 on failure
 * returns 1 on success
 */
int	net_parseip (char *inp, char **ip, unsigned short int *port);


/* net_accept
 * accepts a connection from socket s, and stores the connection
 * into cs.
 * wait a maximum amount of maxsec seconds for connections
 * maxsec can also be zero (infinite wait, until connection)
 *
 * returns 0 if no connection has been made within maxsec seconds
 * returns -1 if an error appears
 * returns the socket number if a connection has been made
 */

int	net_accept (int s, struct sockaddr_in *cs, int maxsec);


/* net_bind
 * binds a socket to an ip:port on the local machine,
 * ip can be either NULL (bind to all IP's on the host), or a pointer
 * to a virtual host name, or a real IP, or "*" for any.
 * port can be either 0 (ephemeral port), or any free port.
 *
 * returns NULL on failure
 *         pointer to bound structure on success
 */
bound 	*net_bind (char *ip, unsigned short int port);
void	net_boundfree (bound *bf);


/* net_resolve
 * resolves host into s_addr
 */
unsigned long int	net_resolve(char *host);


/* net_connect
 * connects to the given server and port with a max timeout of sec
 * initializes the sockaddr_in struct correctly (ipv4), accepts any
 * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname
 * creates a new socket and returns either -1 if failed or
 * the socket if connection has been established within the timeout limit
 * routine is still IPv4 biased :-/
 * with sourceip/sourceport you MAY specify the source IP and source port
 * to use for the connection, but you can set the ip or port to NULL/0,
 * to choose the default IP and an ephemeral port. this was added later in
 * this library, so please update your sources.
 *
 * returns -1 on failure
 * returns socket if success
 */
int	net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, int sec);


/* net_rtimeout
 * waits max <sec> seconds for fd to become readable
 * returns -1 on error (errno set)
 * returns 1 on readability
 */
int	net_rtimeout(int fd, int sec);


/* net_rbuf
 * allocates memory and reads to *dst until connection close
 * returns n if success (n = number of bytes read)
 * returns -1 if failed
 */
long int	net_rbuf(int fd, char **dst);
#define NET_BSIZE  4096


/* net_rbuft
 * reads dsize bytes into dst from fd, with timeout
 * returns 1 on success
 * returns -1 on failure
 */
int	net_rbuft(int fd, char *dst, unsigned long int dsize);


/* net_rlinet
 * reads a line from socket descriptor with timeout to buffer
 * if sec = 0, then only a continuous stream of data is required, not
 * an overall timeout.
 * returns -1 on timeout
 * returns 0  on connection close
 * returns length of readen line (including '\n')
 */
int	net_rlinet(int fd, char *buf, int bufsize, int sec);


/* net_tline
 * returns length if string contains '\n'
 * returns -1 if no '\n' in string
 */
int	net_tline(char *buf, int bufsize);


/* net_write
 * prints a formatted string to a socket
 */
void	net_write(int fd, const char *str, ...);

#endif