107 |
|
|
108 |
|
|
109 |
/** |
/** |
110 |
* Test INET Socket connection to host on port with the |
* Test INET Socket connection to host on port with the specified |
111 |
* specified socket protocol type. The protocol is normaly |
* socket protocol type. The protocol should be either SOCK_STREAM or |
112 |
* either SOCK_STREAM or SOCK_DGRAM. |
* SOCK_DGRAM. |
113 |
* @param hostname The host to connect to |
* @param hostname The host to connect to |
114 |
* @param port The portnumber to connect to |
* @param port The portnumber to connect to |
115 |
* @param protocol The socket protocol to use |
* @param protocol The socket protocol to use |
179 |
|
|
180 |
/** |
/** |
181 |
* Verify that the udp socket is ready for i|o. The given socket must |
* Verify that the udp socket is ready for i|o. The given socket must |
182 |
* be a connected udp socket. The test is conducted by sending one |
* be a connected udp socket if we should be able to test the udp |
183 |
* byte to the server and check for a returned ICMP error when reading |
* server. The test is conducted by sending one byte to the server and |
184 |
* from the socket. A better test would be to send an empty SYN udp |
* check for a returned ICMP error when reading from the socket. A |
185 |
* package to avoid possibly raising an error from the server we are |
* better test would be to send an empty SYN udp package to avoid |
186 |
* testing but assembling an udp by hand requires SOCKET_RAW and |
* possibly raising an error from the server we are testing but |
187 |
* running the program as root. |
* assembling an udp by hand requires SOCKET_RAW and running the |
188 |
|
* program as root. |
189 |
* @param socket A socket |
* @param socket A socket |
190 |
* @return TRUE if the socket is ready, otherwise FALSE. |
* @return TRUE if the socket is ready, otherwise FALSE. |
191 |
*/ |
*/ |
215 |
|
|
216 |
/** |
/** |
217 |
* Create a non-blocking socket against hostname:port with the given |
* Create a non-blocking socket against hostname:port with the given |
218 |
* protocol. The protocol is normaly either SOCK_STREAM or |
* protocol. The protocol should be either SOCK_STREAM or SOCK_DGRAM. |
|
* SOCK_DGRAM. |
|
219 |
* @param hostname The host to open a socket at |
* @param hostname The host to open a socket at |
220 |
* @param port The port number to connect to |
* @param port The port number to connect to |
221 |
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
259 |
|
|
260 |
|
|
261 |
/** |
/** |
262 |
* Open a socket with given Port_T structure |
* Open a socket using the given Port_T structure. The protocol, |
263 |
* The protocol, destination and type are selected appropriately |
* destination and type are selected appropriately. |
264 |
* @param pp connection description |
* @param p connection description |
265 |
* @return The socket or -1 if an error occured. |
* @return The socket or -1 if an error occured. |
266 |
*/ |
*/ |
267 |
int create_generic_socket(Port_T p) { |
int create_generic_socket(Port_T p) { |
287 |
|
|
288 |
|
|
289 |
/** |
/** |
290 |
* Create a non-blocking socket against pathname with the given |
* Create a non-blocking UNIX socket. |
291 |
* protocol. |
* @param pathname The pathname to use for the unix socket |
|
* @param pathname The unix socket to connect to |
|
|
* @param protocol Socket protocol to use (SOCK_STREAM|SOCK_DGRAM) |
|
292 |
* @return The socket or -1 if an error occured. |
* @return The socket or -1 if an error occured. |
293 |
*/ |
*/ |
294 |
int create_unix_socket(char *pathname) { |
int create_unix_socket(char *pathname) { |
323 |
|
|
324 |
|
|
325 |
/** |
/** |
326 |
* Creates a blocking server socket (SOCK_STREAM type) and binds it to |
* Creates a blocking server socket (SOCK_STREAM type) and bind it to |
327 |
* the specified local port number, with the specified backlog. Set a |
* the specified local port number, with the specified backlog. Set a |
328 |
* socket option to make the port reusable again. This is useful if |
* socket option to make the port reusable again. If a bind address is |
329 |
* the server has been shut down, and then restarted right away while |
* given the socket will only accept connect requests to this |
330 |
* sockets are still active on its port. If a bind address is given |
* addresses. If the bind address is NULL it will accept connections |
331 |
* the socket will only accept connect requests to this addresses. If |
* on any/all local addresses |
|
* the bind address is NULL it will default accepting connections on |
|
|
* any/all local addresses |
|
332 |
* @param port The localhost port number to open |
* @param port The localhost port number to open |
333 |
* @param backlog The maximum queue length for incomming connections |
* @param backlog The maximum queue length for incomming connections |
334 |
* @param bindAddr the local address the server will bind to |
* @param bindAddr the local address the server will bind to |
389 |
*/ |
*/ |
390 |
int close_socket(int socket) { |
int close_socket(int socket) { |
391 |
|
|
392 |
int rv= TRUE; |
int r; |
393 |
|
|
394 |
if((shutdown(socket, 2) < 0)) { |
shutdown(socket, 2); |
|
rv= FALSE; |
|
|
} |
|
395 |
|
|
396 |
if(close(socket) < 0) { |
do { |
397 |
rv= FALSE; |
r= close(socket); |
398 |
} |
} while(r == -1 && errno == EINTR); |
399 |
|
|
400 |
return rv; |
|
401 |
|
return r; |
402 |
|
|
403 |
} |
} |
404 |
|
|
553 |
|
|
554 |
} |
} |
555 |
|
|
|
|
|
|
/** |
|
|
* Get the local host name |
|
|
* @return the name of the localhost. |
|
|
* NULL if lookup failed |
|
|
*/ |
|
|
char *get_localhostname() { |
|
|
|
|
|
char hostname[256]; |
|
|
|
|
|
if (gethostname(hostname, sizeof(hostname)) < 0) { |
|
|
|
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
return (strdup(hostname)); |
|
|
|
|
|
} |
|
|
|
|
556 |
|
|
557 |
/* ----------------------------------------------------------------- Private */ |
/* ----------------------------------------------------------------- Private */ |
558 |
|
|