This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
net.h
220 lines (180 loc) · 6.99 KB
/
net.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#ifndef NET_H
#define NET_H
#include <config.h>
#include "monitor.h"
/**
* General purpose Network and Socket methods.
*
* @author Jan-Henrik Haukeland, <[email protected]>
* @author Christian Hopp, <[email protected]>
* @author Martin Pala, <[email protected]>
*
* @file
*/
/**
* Standard seconds to wait for a socket connection or for socket read
* i/o before aborting
*/
#define NET_TIMEOUT 5
/**
* Check if the hostname resolves
* @param hostname The host to check
* @return TRUE if hostname resolves, otherwise FALSE
*/
int check_host(const char *hostname);
/**
* Verify that the socket is ready for i|o
* @param socket A socket
* @return TRUE if the socket is ready, otherwise FALSE.
*/
int check_socket(int socket);
/**
* Verify that the udp server is up. The given socket must be a
* connected udp socket if we should be able to test the udp server.
* The test is conducted by sending a datagram to the server and
* check for a returned ICMP error when reading from the socket.
* @param socket A socket
* @return TRUE if the socket is ready, otherwise FALSE.
*/
int check_udp_socket(int socket);
/**
* Create a non-blocking socket against hostname:port with the given
* protocol. The protocol should be either SOCK_STREAM or SOCK_DGRAM.
* @param hostname The host to open a socket at
* @param port The port number to connect to
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM)
* @param timeout If not connected within timeout seconds abort and return -1
* @return The socket or -1 if an error occured.
*/
int create_socket(const char *hostname, int port, int protocol, int timeout);
/**
* Open a socket using the given Port_T structure. The protocol,
* destination and type are selected appropriately.
* @param p connection description
* @return The socket or -1 if an error occured.
*/
int create_generic_socket(Port_T p);
/**
* Create a non-blocking UNIX socket.
* @param pathname The pathname to use for the unix socket
* @param timeout If not connected within timeout seconds abort and return -1
* @return The socket or -1 if an error occured.
*/
int create_unix_socket(const char *pathname, int timeout);
/**
* Create a blocking server socket and bind it to the specified local
* port number, with the specified backlog. Set a socket option to
* make the port reusable again. If a bind address is given the socket
* will only accept connect requests to this addresses. If the bind
* address is NULL it will accept connections on any/all local
* addresses
* @param port The localhost port number to open
* @param backlog The maximum queue length for incomming connections
* @param bindAddr the local address the server will bind to
* @return The socket ready for accept, or -1 if an error occured.
*/
int create_server_socket(int port, int backlog, const char *bindAddr);
/**
* Shutdown a socket and close the descriptor.
* @param socket The socket to shutdown and close
* @return TRUE if the close succeed otherwise FALSE
*/
int close_socket(int socket);
/**
* Enable nonblocking i|o on the given socket.
* @param socket A socket
* @return TRUE if success, otherwise FALSE
*/
int set_noblock(int socket);
/**
* Disable nonblocking i|o on the given socket
* @param socket A socket
* @return TRUE if success, otherwise FALSE
*/
int set_block(int socket);
/**
* Check if data is available, if not, wait timeout seconds for data
* to be present.
* @param socket A socket
* @param timeout How long to wait before timeout (value in seconds)
* @return Return TRUE if the event occured, otherwise FALSE.
*/
int can_read(int socket, int timeout);
/**
* Check if data can be sent to the socket, if not, wait timeout
* seconds for the socket to be ready.
* @param socket A socket
* @param timeout How long to wait before timeout (value in seconds)
* @return Return TRUE if the event occured, otherwise FALSE.
*/
int can_write(int socket, int timeout);
/**
* Write <code>size</code> bytes from the <code>buffer</code> to the
* <code>socket</code>
* @param socket the socket to write to
* @param buffer The buffer to write
* @param size Number of bytes to send
* @param timeout Seconds to wait for data to be written
* @return The number of bytes sent or -1 if an error occured.
*/
int sock_write(int socket, const void *buffer, int size, int timeout);
/**
* Read up to size bytes from the <code>socket</code> into the
* <code>buffer</code>. If data is not available wait for
* <code>timeout</code> seconds.
* @param socket the Socket to read data from
* @param buffer The buffer to write the data to
* @param size Number of bytes to read from the socket
* @param timeout Seconds to wait for data to be available
* @return The number of bytes read or -1 if an error occured.
*/
int sock_read(int socket, void *buffer, int size, int timeout);
/**
* Write <code>size</code> bytes from the <code>buffer</code> to the
* <code>socket</code>. The given socket <b>must</b> be a connected
* UDP socket
* @param socket the socket to write to
* @param buffer The buffer to write
* @param size Number of bytes to send
* @param timeout Seconds to wait for data to be written
* @return The number of bytes sent or -1 if an error occured.
*/
int udp_write(int socket, void *b, int len, int timeout);
/**
* Create a ICMP socket against hostname, send echo and wait for response.
* The 'count' echo requests is send and we expect at least one reply.
* @param hostname The host to open a socket at
* @param timeout If response will not come within timeout seconds abort
* @param count How many pings to send
* @return response time on succes, -1 on error
*/
double icmp_echo(const char *hostname, int timeout, int count);
#endif