5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/SILC Net Interface
24 * SILC Net API provides various network routines for applications. It
25 * can be used to create TCP/IP and UDP/IP connections and listeners.
26 * Various utility functions for resolving various information is also
36 /****s* silcutil/SilcNetAPI/SilcNetListener
40 * typedef struct SilcNetListenerStruct *SilcNetListener;
44 * The network listenr context. This context is created with the
45 * silc_net_create_listener function and destroyed with
46 * silc_net_close_listener function.
49 typedef struct SilcNetListenerStruct *SilcNetListener;
51 /****d* silcutil/SilcNetAPI/SilcNetStatus
55 * typedef enum { ... } SilcNetStatus;
59 * Status to indicate the result of the network operation creation. This
60 * type is returned in the SilcNetCallback callback function.
65 SILC_NET_OK, /* Everything Ok */
66 SILC_NET_UNKNOWN_IP, /* Unknown IP address */
67 SILC_NET_UNKNOWN_HOST, /* Unknown hostname */
68 SILC_NET_HOST_UNREACHABLE, /* Destination unreachable */
69 SILC_NET_CONNECTION_REFUSED, /* Connection refused */
70 SILC_NET_CONNECTION_TIMEOUT, /* Connection timedout */
71 SILC_NET_NO_MEMORY, /* System out of memory */
72 SILC_NET_ERROR, /* Unknown error */
76 /****f* silcutil/SilcNetAPI/SilcNetCallback
80 * typedef void (*SilcNetCallback)(SilcNetStatus status,
81 * SilcStream stream, void *context);
85 * A callback of this type is returned by silc_net_tcp_create_listener
86 * and silc_net_tcp_connect functions. For silc_net_tcp_create_listener
87 * this callback means that new incoming connection was accepted, and the
88 * `stream' is the socket stream representing the socket connection.
90 * For silc_net_tcp_connect this means that we have connected to the
91 * remote host and the `stream' is the socket stream for the socket
92 * connection. The SILC Stream API (such as silc_stream_read, etc.) can
93 * be used to read and write to the stream. The created stream is socket
94 * stream so various SilcSocketStream API functions can be used with
98 typedef void (*SilcNetCallback)(SilcNetStatus status,
99 SilcStream stream, void *context);
101 /****f* silcutil/SilcNetAPI/silc_net_tcp_create_listener
106 * silc_net_tcp_create_listener(const char **local_ip_addr,
107 * SilcUInt32 local_ip_count, int port,
108 * SilcBool lookup, SilcBool require_fqdn,
109 * SilcSchedule schedule,
110 * SilcNetCallback callback, void *context);
114 * This function creates TCP listener. This is used to create network
115 * listener for incoming connections, and `callback' will be called
116 * everytime new connection is received. If `local_ip_addr' is NULL 'any'
117 * address is used. If provided it can be used bind the listener to
118 * `local_ip_count' many IP addresses provided in `local_ip_addr' table.
119 * On success returns the SilcNetListener context, or NULL on error.
120 * If `require_fqdn' is TRUE the listener will require that the incoming
121 * connection has FQDN to be able to connect. If the `lookup' is TRUE
122 * then the incoming connection hostname will be resolved. If the `port'
123 * is zero (0), operating system will define it automatically.
125 * The `callback' always delivers valid new stream. It is not called
126 * with an error status.
130 silc_net_tcp_create_listener(const char **local_ip_addr,
131 SilcUInt32 local_ip_count, int port,
132 SilcBool lookup, SilcBool require_fqdn,
133 SilcSchedule schedule,
134 SilcNetCallback callback, void *context);
136 /****f* silcutil/SilcNetAPI/silc_net_listener_get_port
140 * SilcUInt16 silc_net_listener_get_port(SilcNetListener listener);
144 * Returns the ports to where the `listener' is bound. This can be used
145 * to get the port if none was specified in silc_net_tcp_create_listener.
146 * Returns an array of ports of size of `port_count'. The caller must
147 * free the array with silc_free. There are as many ports in the array
148 * as there were IP addresses provided in silc_net_tcp_create_listener.
151 SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
152 SilcUInt32 *port_count);
154 /****f* silcutil/SilcNetAPI/silc_net_listener_get_ip
158 * char **silc_net_listener_get_ip(SilcNetListener listener,
159 * SilcUInt32 *ip_count);
163 * Returns the IP's to where the `listener' is bound. Returns an array
164 * of IP addresses of size of `port_count'. The caller must free the
165 * array and its strings with silc_free.
168 char **silc_net_listener_get_ip(SilcNetListener listener,
169 SilcUInt32 *ip_count);
171 /****f* silcutil/SilcNetAPI/silc_net_listener_get_hostname
175 * char **silc_net_listener_get_hostname(SilcNetListener listener,
176 * SilcUInt32 *hostname_count);
180 * Returns the hostnames to where the `listener' is bound. Returns an
181 * array of hostnames of size of `port_count'. The caller must free the
182 * array and its strings with silc_free.
185 char **silc_net_listener_get_hostname(SilcNetListener listener,
186 SilcUInt32 *hostname_count);
188 /****f* silcutil/SilcNetAPI/silc_net_close_listener
192 * void silc_net_close_listener(SilcNetListener listener);
196 * Closes the network listener indicated by `listener'.
199 void silc_net_close_listener(SilcNetListener listener);
201 /****f* silcutil/SilcNetAPI/silc_net_tcp_connect
205 * SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
206 * const char *remote_ip_addr,
208 * SilcSchedule schedule,
209 * SilcNetCallback callback,
214 * Creates TCP/IP connection to the remote host indicated by `remote_host'
215 * which may be hostname or IP address, on the port indicated by
216 * `remote_port'. If the `local_ip_addr' is provided the local host is
217 * bound to that address before creating the connection. This is
218 * asynchronous call, and this function returns before the connection is
219 * actually established. The `callback' will be called after the
220 * connection is created to deliver the SilcStream for the created
221 * connection. This function supports IPv6 if the platform supports it.
223 * The returned SilcAsyncOperation context can be used to control the
224 * asynchronous connecting, such as to abort it. If it is aborted
225 * using silc_async_abort the `callback' will not be called. If NULL
226 * is returned the operation cannot be aborted.
229 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
230 const char *remote_ip_addr,
232 SilcSchedule schedule,
233 SilcNetCallback callback,
236 /****f* silcutil/SilcNetAPI/silc_net_udp_connect
241 * silc_net_udp_connect(const char *local_ip_addr, int local_port,
242 * const char *remote_ip_addr, int remote_port,
243 * SilcSchedule schedule);
247 * This function creates UDP stream. The UDP stream is bound to the
248 * `local_ip_addr' if it is specified. If `local_port' is non-zero the
249 * stream is bound to that port. If the `remote_ip_addr' and `remote_port'
250 * is also provided, packets may be sent to that address using
251 * silc_stream_write function and packets may be received using
252 * silc_stream_read function.
254 * If the remote address is not provided the stream is in connectionless
255 * state. This means that packets can be received only by using
256 * silc_net_udp_receive and sent only by using the function
259 * To receive packets the silc_stream_set_notifier must be called for the
260 * returned SilcStream. The packets are always received in the notifier
261 * callback when the SILC_STREAM_CAN_READ is returned to the callback
262 * To read the packet use silc_stream_read if the remote address was
263 * provided, and silc_net_udp_receive if it was not.
265 * Supports IPv6 if the platform supports it.
269 * SilcStream udpstream;
271 * // Create UDP stream and prepare to receive packets
272 * udpstream = silc_net_udp_connect("10.2.1.7", 5000,
273 * "10.2.1.100, 5000, schedule);
274 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
276 * // Send packet to remote host
277 * silc_stream_write(udpstream, data, data_len);
279 * Create UDP listener:
281 * udpstream = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
282 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
285 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
286 const char *remote_ip_addr, int remote_port,
287 SilcSchedule schedule);
289 /****f* silcutil/SilcNetAPI/silc_net_udp_receive
294 * silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
295 * SilcUInt32 remote_ip_addr_size, int *remote_port,
296 * unsigned char *ret_data, SilcUInt32 data_size)
300 * Receive a UDP packet from the `stream'. The IP address and port of
301 * the sender is returned into `remote_ip_addr' buffer and `remote_port'
302 * pointer. The packet data is returned into the `ret_data' buffer.
304 * Returns the length of the packet, or -1 on error or 0 in case of EOF.
307 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
308 SilcUInt32 remote_ip_addr_size, int *remote_port,
309 unsigned char *ret_data, SilcUInt32 data_size);
311 /****f* silcutil/SilcNetAPI/silc_net_udp_send
315 * int silc_net_udp_send(SilcStream stream,
316 * const char *remote_ip_addr, int remote_port,
317 * const unsigned char *data, SilcUInt32 data_len);
321 * Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
322 * This may be used with UDP streams that are not connected to any
323 * specific remote host. With those stream silc_stream_write cannot be
324 * used. In those cases, this function must be used. This may also be
325 * used even if the stream is connected.
327 * Returns the amount of data written, -1 if data could not be written
328 * at this moment, or -2 if error occurred. If -1 is returned the
329 * notifier callback will later be called with SILC_STREAM_CAN_WRITE
330 * status when stream is again ready for writing.
333 int silc_net_udp_send(SilcStream stream,
334 const char *remote_ip_addr, int remote_port,
335 const unsigned char *data, SilcUInt32 data_len);
337 /****f* silcutil/SilcNetAPI/silc_net_get_error_string
341 * const char silc_net_get_error_string(SilcNetStatus error);
345 * Return `error' as a string.
348 const char *silc_net_get_error_string(SilcNetStatus error);
350 /****f* silcutil/SilcNetAPI/silc_net_close_connection
354 * void silc_net_close_connection(int sock);
358 * Closes the connection by closing the socket connection. This routine
359 * can only be used with POSIX compliant systems.
362 void silc_net_close_connection(int sock);
364 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
368 * int silc_net_accept_connection(int sock);
372 * Accepts a connection from a particular socket. This routine can only
373 * be used with POSIX compliant systems. This call is equivalent to
377 int silc_net_accept_connection(int sock);
379 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
383 * int silc_net_set_socket_opt(int sock, int level, int option, int on);
387 * Sets a option for a socket. This function can be used to set
388 * various options for the socket. Some of the options might be
389 * system specific. This routine can only be used with POSIX compliant
390 * systems. This call is equivalent to setsockopt(2);
393 int silc_net_set_socket_opt(int sock, int level, int option, int on);
395 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
399 * int silc_net_get_socket_opt(int sock, int level, int option,
400 * void *optval, int *opt_len);
404 * Return socket options to the `optval' and `opt_len'. This routine
405 * can only be used with POSIX compliant systems. This call is
406 * equivalent to getsockopt(2).
409 int silc_net_get_socket_opt(int sock, int level, int option,
410 void *optval, int *opt_len);
412 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
416 * int silc_net_set_socket_nonblock(SilcSocket sock);
420 * Sets the socket `sock' to non-blocking mode.
423 int silc_net_set_socket_nonblock(SilcSocket sock);
425 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
429 * SilcBool silc_net_is_ip4(const char *addr);
433 * Checks whether IP address sent as argument is valid IPv4 address.
436 SilcBool silc_net_is_ip4(const char *addr);
438 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
442 * SilcBool silc_net_is_ip6(const char *addr);
446 * Checks whether IP address sent as argument is valid IPv6 address.
449 SilcBool silc_net_is_ip6(const char *addr);
451 /****f* silcutil/SilcNetAPI/silc_net_is_ip
455 * SilcBool silc_net_is_ip(const char *addr);
459 * Checks whether IP address sent as argument is valid IP address.
460 * This supports both IPv4 and IPv6 addresses.
463 SilcBool silc_net_is_ip(const char *addr);
465 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
469 * SilcBool silc_net_addr2bin(const char *addr, void *bin,
470 * SilcUInt32 bin_len);
474 * Converts the IP number string from numbers-and-dots notation to
475 * binary form in network byte order. The address can be either
476 * IPv4 or IPv6 address.
479 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
481 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
485 * typedef void (*SilcNetResolveCallback)(const char *result,
490 * A callback function of this type is called after the asynchronous
491 * resolving operation has been completed. This callback is used
492 * when asynchronously resolving IP addresses and hostnames.
495 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
497 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
501 * SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
502 * char *address, SilcUInt32 address_len);
506 * Resolves the IP address of the hostname indicated by the `name'.
507 * This returns TRUE and the IP address of the host to the `address'
508 * buffer, or FALSE if the address could not be resolved. This is
509 * synchronous function and will block the calling process. If the
510 * `prefer_ipv6' is TRUE then this will return IPv6 address if it
511 * finds. If FALSE if returns IPv4 address even if it found IPv6
515 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
516 char *address, SilcUInt32 address_len);
518 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
522 * void silc_net_gethostbyname_async(const char *name,
523 * SilcBool prefer_ipv6,
524 * SilcSchedule schedule,
525 * SilcNetResolveCallback completion,
530 * Asynchronously resolves the IP address of the hostname indicated
531 * by the `name'. This function returns immediately, and the
532 * `completion' callback will be called after the resolving is
535 * If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
536 * finds. If FALSE if returns IPv4 address even if it found IPv6
540 void silc_net_gethostbyname_async(const char *name,
541 SilcBool prefer_ipv6,
542 SilcSchedule schedule,
543 SilcNetResolveCallback completion,
546 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
550 * SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
551 * SilcUInt32 name_len);
555 * Resolves the hostname for the IP address indicated by the `addr'
556 * This returns TRUE and the resolved hostname to the `name' buffer,
557 * or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
558 * This is synchronous function and will block the calling process.
561 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
562 SilcUInt32 name_len);
564 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
568 * void silc_net_gethostbyaddr_async(const char *addr,
569 * SilcSchedule schedule,
570 * SilcNetResolveCallback completion,
575 * Asynchronously resolves the hostname for the IP address indicated
576 * by the `addr'. This function returns immediately, and the
577 * `completion' callback will be called after the resolving is
581 void silc_net_gethostbyaddr_async(const char *addr,
582 SilcSchedule schedule,
583 SilcNetResolveCallback completion,
586 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
590 * SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
595 * Performs lookups for remote name and IP address. This peforms reverse
596 * lookup as well to verify that the IP has FQDN.
599 SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
602 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
606 * SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
611 * Performs lookups for local name and IP address. This peforms reverse
612 * lookup as well to verify that the IP has FQDN.
615 SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
618 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
622 * SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
626 * Return remote port by socket.
629 SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
631 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
635 * SilcUInt16 silc_net_get_local_port(SilcSocket sock);
639 * Return local port by socket.
642 SilcUInt16 silc_net_get_local_port(SilcSocket sock);
644 /****f* silcutil/SilcNetAPI/silc_net_localhost
648 * char *silc_net_localhost(void);
652 * Return name of localhost. This will also attempt to resolve
653 * the real hostname by the local host's IP address. If unsuccessful
654 * the first found hostname is returned. The caller must free
658 char *silc_net_localhost(void);
660 /****f* silcutil/SilcNetAPI/silc_net_localip
664 * char *silc_net_localip(void)
668 * Return IP of localhost. The caller must free the returned IP.
671 char *silc_net_localip(void);
673 #include "silcnet_i.h"
675 #endif /* SILCNET_H */