22 |
#define NET_H |
#define NET_H |
23 |
|
|
24 |
#include <config.h> |
#include <config.h> |
|
|
|
25 |
#include "monitor.h" |
#include "monitor.h" |
26 |
|
|
27 |
|
|
28 |
|
/** |
29 |
|
* General purpose Network and Socket methods. |
30 |
|
* |
31 |
|
* @author Jan-Henrik Haukeland, <hauk@tildeslash.com> |
32 |
|
* @author Christian Hopp <chopp@iei.tu-clausthal.de> |
33 |
|
* |
34 |
|
* @version \$Id$ |
35 |
|
* |
36 |
|
* @file |
37 |
|
*/ |
38 |
|
|
39 |
#define TRUE 1 |
#define TRUE 1 |
40 |
#define FALSE 0 |
#define FALSE 0 |
41 |
#define SELECT_TIMEOUT 5 |
#define SELECT_TIMEOUT 5 |
42 |
#define LINGER_TIMEOUT 10 |
#define LINGER_TIMEOUT 10 |
43 |
|
|
44 |
int set_block(int); |
|
45 |
int set_noblock(int); |
/** |
46 |
int check_socket(int); |
* Test INET Socket connection to host on port with the |
47 |
int close_socket(int); |
* specified socket protocol type. The protocol is normaly |
48 |
int check_host(char *); |
* either SOCK_STREAM or SOCK_DGRAM. |
49 |
char *get_localhostname(); |
* @param hostname The host to connect to |
50 |
char *get_remote_host(int); |
* @param port The portnumber to connect to |
51 |
int check_udp_socket(int); |
* @param protocol The socket protocol to use |
52 |
int set_sotimeout(int, int); |
* @return TRUE if the connect succeeded otherwise FALSE. |
53 |
int create_generic_socket(Port_T); |
*/ |
54 |
int check_connect(char*, int, int); |
int check_connect(char *hostname, int port, int protocol); |
55 |
int create_socket(char*, int, int); |
|
56 |
int create_unix_socket(char*, int); |
|
57 |
int sock_recv(int, char *, int, int); |
/** |
58 |
int sock_send(int, const char *, int, int); |
* Check if the hostname resolves |
59 |
int create_server_socket(int, int, char *bindAddr); |
* @param hostname The host to check |
60 |
|
* @return TRUE if hostname resolves, otherwise FALSE |
61 |
|
*/ |
62 |
|
int check_host(char *hostname); |
63 |
|
|
64 |
|
|
65 |
|
/** |
66 |
|
* Verify that the socket is ready for i|o |
67 |
|
* @param socket A socket |
68 |
|
* @return TRUE if the socket is ready, otherwise FALSE. |
69 |
|
*/ |
70 |
|
int check_socket(int socket); |
71 |
|
|
72 |
|
|
73 |
|
/** |
74 |
|
* Verify that the udp socket is ready for i|o. The given socket must |
75 |
|
* be a connected udp socket. The test is conducted by sending one |
76 |
|
* byte to the server and check for a returned ICMP error when reading |
77 |
|
* from the socket. A better test would be to send an empty SYN udp |
78 |
|
* package to avoid possibly raising an error from the server we are |
79 |
|
* testing but assembling an udp by hand requires SOCKET_RAW and |
80 |
|
* running the program as root. |
81 |
|
* @param socket A socket |
82 |
|
* @return TRUE if the socket is ready, otherwise FALSE. |
83 |
|
*/ |
84 |
|
int check_udp_socket(int socket); |
85 |
|
|
86 |
|
|
87 |
|
/** |
88 |
|
* Create a non-blocking socket against hostname:port with the given |
89 |
|
* protocol. The protocol is normaly either SOCK_STREAM or |
90 |
|
* SOCK_DGRAM. |
91 |
|
* @param hostname The host to open a socket at |
92 |
|
* @param port The port number to connect to |
93 |
|
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
94 |
|
* @return The socket or -1 if an error occured. |
95 |
|
*/ |
96 |
|
int create_socket(char *hostname, int port, int protocol); |
97 |
|
|
98 |
|
|
99 |
|
/** |
100 |
|
* Open a socket with given Port_T structure |
101 |
|
* The protocol, destination and type are selected appropriately |
102 |
|
* @param pp connection description |
103 |
|
* @return The socket or -1 if an error occured. |
104 |
|
*/ |
105 |
|
int create_generic_socket(Port_T p); |
106 |
|
|
107 |
|
|
108 |
|
/** |
109 |
|
* Create a non-blocking socket against pathname with the given |
110 |
|
* protocol. |
111 |
|
* @param pathname The unix socket to connect to |
112 |
|
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
113 |
|
* @return The socket or -1 if an error occured. |
114 |
|
*/ |
115 |
|
int create_unix_socket(char *pathname); |
116 |
|
|
117 |
|
|
118 |
|
/** |
119 |
|
* Creates a blocking server socket (SOCK_STREAM type) and binds it to |
120 |
|
* the specified local port number, with the specified backlog. Set a |
121 |
|
* socket option to make the port reusable again. This is useful if |
122 |
|
* the server has been shut down, and then restarted right away while |
123 |
|
* sockets are still active on its port. If a bind address is given |
124 |
|
* the socket will only accept connect requests to this addresses. If |
125 |
|
* the bind address is NULL it will default accepting connections on |
126 |
|
* any/all local addresses |
127 |
|
* @param port The localhost port number to open |
128 |
|
* @param backlog The maximum queue length for incomming connections |
129 |
|
* @param bindAddr the local address the server will bind to |
130 |
|
* @return The socket ready for accept, or -1 if an error occured. |
131 |
|
*/ |
132 |
|
int create_server_socket(int port, int backlog, char *bindAddr); |
133 |
|
|
134 |
|
|
135 |
|
/** |
136 |
|
* Shutdown a socket and close the descriptor. |
137 |
|
* @param socket The socket to shutdown and close |
138 |
|
* @return TRUE if the close succeed otherwise FALSE |
139 |
|
*/ |
140 |
|
int close_socket(int socket); |
141 |
|
|
142 |
|
|
143 |
|
/** |
144 |
|
* Enable nonblocking i|o on the given socket. |
145 |
|
* @param socket A socket |
146 |
|
* @return TRUE if success, otherwise FALSE |
147 |
|
*/ |
148 |
|
int set_noblock(int socket); |
149 |
|
|
150 |
|
|
151 |
|
/** |
152 |
|
* Disable nonblocking i|o on the given socket |
153 |
|
* @param socket A socket |
154 |
|
* @return TRUE if success, otherwise FALSE |
155 |
|
*/ |
156 |
|
int set_block(int socket); |
157 |
|
|
158 |
|
|
159 |
|
/** |
160 |
|
* Check if data is available, if not, wait timeout seconds for data |
161 |
|
* to be present. |
162 |
|
* @param socket A socket |
163 |
|
* @param timeout How long to wait before timeout (value in seconds) |
164 |
|
* @return Return TRUE if the event occured, otherwise FALSE. |
165 |
|
*/ |
166 |
|
int can_read(int socket, int timeout); |
167 |
|
|
168 |
|
|
169 |
|
/** |
170 |
|
* Check if data can be sent to the socket, if not, wait timeout |
171 |
|
* seconds for the socket to be ready. |
172 |
|
* @param socket A socket |
173 |
|
* @param timeout How long to wait before timeout (value in seconds) |
174 |
|
* @return Return TRUE if the event occured, otherwise FALSE. |
175 |
|
*/ |
176 |
|
int can_write(int socket, int timeout); |
177 |
|
|
178 |
|
|
179 |
|
/** |
180 |
|
* Write <code>size</code> bytes from the <code>buffer</code> to the |
181 |
|
* <code>socket</code> |
182 |
|
* @param socket the socket to write to |
183 |
|
* @param buffer The buffer to write |
184 |
|
* @param size Number of bytes to send |
185 |
|
* @return The number of bytes sent or -1 if an error |
186 |
|
* occured. Returning 0 means EOF |
187 |
|
*/ |
188 |
|
int sock_write(int socket, void *buffer, int size); |
189 |
|
|
190 |
|
|
191 |
|
/** |
192 |
|
* Read up to size bytes from the <code>socket</code> into the |
193 |
|
* <code>buffer</code>. If data is not available wait for |
194 |
|
* <code>timeout</code> seconds. |
195 |
|
* @param socket the Socket to read data from |
196 |
|
* @param buffer The buffer to write the data to |
197 |
|
* @param size Number of bytes to read from the socket |
198 |
|
* @param timeout Seconds to wait for data to be available |
199 |
|
* @return The number of bytes read or -1 if an error |
200 |
|
* occured. Returning 0 means EOF |
201 |
|
*/ |
202 |
|
int sock_read(int socket, void *buffer, int size, int timeout); |
203 |
|
|
204 |
|
|
205 |
|
/** |
206 |
|
* Get the local host name |
207 |
|
* @return the name of the localhost. |
208 |
|
* NULL if lookup failed |
209 |
|
*/ |
210 |
|
char *get_localhostname(); |
211 |
|
|
212 |
|
|
213 |
#endif |
#endif |