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 /****f* silcutil/SilcNetAPI/SilcNetCallback
55 * typedef void (*SilcNetCallback)(SilcResult status,
56 * SilcStream stream, void *context);
60 * A callback of this type is returned by silc_net_tcp_create_listener
61 * and silc_net_tcp_connect functions. For silc_net_tcp_create_listener
62 * this callback means that new incoming connection was accepted, and the
63 * `stream' is the socket stream representing the socket connection.
65 * For silc_net_tcp_connect this means that we have connected to the
66 * remote host and the `stream' is the socket stream for the socket
67 * connection. The SILC Stream API (such as silc_stream_read, etc.) can
68 * be used to read and write to the stream. The created stream is socket
69 * stream so various SilcSocketStream API functions can be used with
73 typedef void (*SilcNetCallback)(SilcResult status,
74 SilcStream stream, void *context);
76 /****f* silcutil/SilcNetAPI/silc_net_tcp_create_listener
81 * silc_net_tcp_create_listener(const char **local_ip_addr,
82 * SilcUInt32 local_ip_count, int port,
83 * SilcBool lookup, SilcBool require_fqdn,
84 * SilcSchedule schedule,
85 * SilcNetCallback callback, void *context);
89 * This function creates TCP listener. This is used to create network
90 * listener for incoming connections, and `callback' will be called
91 * everytime new connection is received. If `local_ip_addr' is NULL 'any'
92 * address is used. If provided it can be used bind the listener to
93 * `local_ip_count' many IP addresses provided in `local_ip_addr' table.
94 * On success returns the SilcNetListener context, or NULL on error.
95 * If `require_fqdn' is TRUE the listener will require that the incoming
96 * connection has FQDN to be able to connect. If the `lookup' is TRUE
97 * then the incoming connection hostname will be resolved. If the `port'
98 * is zero (0), operating system will define it automatically.
100 * The `callback' always delivers valid new stream. It is not called
101 * with an error status.
105 silc_net_tcp_create_listener(const char **local_ip_addr,
106 SilcUInt32 local_ip_count, int port,
107 SilcBool lookup, SilcBool require_fqdn,
108 SilcSchedule schedule,
109 SilcNetCallback callback, void *context);
111 /****f* silcutil/SilcNetAPI/silc_net_tcp_create_listener2
116 * silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
117 * SilcUInt32 port_count,
118 * SilcBool ignore_port_error,
119 * SilcBool lookup, SilcBool require_fqdn,
120 * SilcSchedule schedule,
121 * SilcNetCallback callback, void *context);
125 * This function creates TCP listener. This is used to create network
126 * listener for incoming connections, and `callback' will be called
127 * everytime new connection is received. If `local_ip_addr' is NULL 'any'
128 * address is used. If `ports' is NULL or it contains a zero (0) port,
129 * operating system will define it automatically. This function can be
130 * used to bind to many ports at the same time. If `ignore_port_error'
131 * is TRUE this won't return NULL if at least one of the ports could
132 * be bound. Otherwise, NULL will be returned on error.
134 * If `require_fqdn' is TRUE the listener will require that the incoming
135 * connection has FQDN to be able to connect. If the `lookup' is TRUE
136 * then the incoming connection hostname will be resolved.
138 * The `callback' always delivers valid new stream. It is not called
139 * with an error status.
143 silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
144 SilcUInt32 port_count,
145 SilcBool ignore_port_error,
146 SilcBool lookup, SilcBool require_fqdn,
147 SilcSchedule schedule,
148 SilcNetCallback callback, void *context);
150 /****f* silcutil/SilcNetAPI/silc_net_listener_get_port
154 * SilcUInt16 silc_net_listener_get_port(SilcNetListener listener);
158 * Returns the ports to where the `listener' is bound. This can be used
159 * to get the port if none was specified in silc_net_tcp_create_listener.
160 * Returns an array of ports of size of `port_count'. The caller must
161 * free the array with silc_free. There are as many ports in the array
162 * as there were IP addresses provided in silc_net_tcp_create_listener,
163 * as there were ports provided in silc_net_tcp_create_listener2.
166 SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
167 SilcUInt32 *port_count);
169 /****f* silcutil/SilcNetAPI/silc_net_listener_get_ip
173 * char **silc_net_listener_get_ip(SilcNetListener listener,
174 * SilcUInt32 *ip_count);
178 * Returns the IP's to where the `listener' is bound. Returns an array
179 * of IP addresses of size of `port_count'. The caller must free the
180 * array and its strings with silc_free.
183 char **silc_net_listener_get_ip(SilcNetListener listener,
184 SilcUInt32 *ip_count);
186 /****f* silcutil/SilcNetAPI/silc_net_listener_get_hostname
190 * char **silc_net_listener_get_hostname(SilcNetListener listener,
191 * SilcUInt32 *hostname_count);
195 * Returns the hostnames to where the `listener' is bound. Returns an
196 * array of hostnames of size of `port_count'. The caller must free the
197 * array and its strings with silc_free.
200 char **silc_net_listener_get_hostname(SilcNetListener listener,
201 SilcUInt32 *hostname_count);
203 /****f* silcutil/SilcNetAPI/silc_net_close_listener
207 * void silc_net_close_listener(SilcNetListener listener);
211 * Closes the network listener indicated by `listener'.
214 void silc_net_close_listener(SilcNetListener listener);
216 /****f* silcutil/SilcNetAPI/silc_net_tcp_connect
220 * SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
221 * const char *remote_ip_addr,
223 * SilcSchedule schedule,
224 * SilcNetCallback callback,
229 * Creates TCP/IP connection to the remote host indicated by `remote_host'
230 * which may be hostname or IP address, on the port indicated by
231 * `remote_port'. If the `local_ip_addr' is provided the local host is
232 * bound to that address before creating the connection. This is
233 * asynchronous call, and this function returns before the connection is
234 * actually established. The `callback' will be called after the
235 * connection is created to deliver the SilcStream for the created
236 * connection. This function supports IPv6 if the platform supports it.
238 * The returned SilcAsyncOperation context can be used to control the
239 * asynchronous connecting, such as to abort it. If it is aborted
240 * using silc_async_abort the `callback' will not be called. If NULL
241 * is returned the operation cannot be aborted.
244 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
245 const char *remote_ip_addr,
247 SilcSchedule schedule,
248 SilcNetCallback callback,
251 /****f* silcutil/SilcNetAPI/silc_net_udp_connect
256 * silc_net_udp_connect(const char *local_ip_addr, int local_port,
257 * const char *remote_ip_addr, int remote_port,
258 * SilcSchedule schedule);
262 * This function creates UDP stream. The UDP stream is bound to the
263 * `local_ip_addr' if it is specified. If `local_port' is non-zero the
264 * stream is bound to that port. If the `remote_ip_addr' and `remote_port'
265 * is also provided, packets may be sent to that address using
266 * silc_stream_write function and packets may be received using
267 * silc_stream_read function.
269 * If the remote address is not provided the stream is in connectionless
270 * state. This means that packets can be received only by using
271 * silc_net_udp_receive and sent only by using the function
274 * To receive packets the silc_stream_set_notifier must be called for the
275 * returned SilcStream. The packets are always received in the notifier
276 * callback when the SILC_STREAM_CAN_READ is returned to the callback
277 * To read the packet use silc_stream_read if the remote address was
278 * provided, and silc_net_udp_receive if it was not.
280 * Supports IPv6 if the platform supports it.
284 * SilcStream udpstream;
286 * // Create UDP stream and prepare to receive packets
287 * udpstream = silc_net_udp_connect("10.2.1.7", 5000,
288 * "10.2.1.100, 5000, schedule);
289 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
291 * // Send packet to remote host
292 * silc_stream_write(udpstream, data, data_len);
294 * Create UDP listener:
296 * udpstream = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
297 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
300 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
301 const char *remote_ip_addr, int remote_port,
302 SilcSchedule schedule);
304 /****f* silcutil/SilcNetAPI/silc_net_udp_receive
309 * silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
310 * SilcUInt32 remote_ip_addr_size, int *remote_port,
311 * unsigned char *ret_data, SilcUInt32 data_size)
315 * Receive a UDP packet from the `stream'. The IP address and port of
316 * the sender is returned into `remote_ip_addr' buffer and `remote_port'
317 * pointer. The packet data is returned into the `ret_data' buffer.
319 * Returns the length of the packet, or -1 on error or 0 in case of EOF.
322 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
323 SilcUInt32 remote_ip_addr_size, int *remote_port,
324 unsigned char *ret_data, SilcUInt32 data_size);
326 /****f* silcutil/SilcNetAPI/silc_net_udp_send
330 * int silc_net_udp_send(SilcStream stream,
331 * const char *remote_ip_addr, int remote_port,
332 * const unsigned char *data, SilcUInt32 data_len);
336 * Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
337 * This may be used with UDP streams that are not connected to any
338 * specific remote host. With those stream silc_stream_write cannot be
339 * used. In those cases, this function must be used. This may also be
340 * used even if the stream is connected.
342 * Returns the amount of data written, -1 if data could not be written
343 * at this moment, or -2 if error occurred. If -1 is returned the
344 * notifier callback will later be called with SILC_STREAM_CAN_WRITE
345 * status when stream is again ready for writing.
348 int silc_net_udp_send(SilcStream stream,
349 const char *remote_ip_addr, int remote_port,
350 const unsigned char *data, SilcUInt32 data_len);
352 /****f* silcutil/SilcNetAPI/silc_net_close_connection
356 * void silc_net_close_connection(int sock);
360 * Closes the connection by closing the socket connection. This routine
361 * can only be used with POSIX compliant systems.
364 void silc_net_close_connection(int sock);
366 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
370 * int silc_net_accept_connection(int sock);
374 * Accepts a connection from a particular socket. This routine can only
375 * be used with POSIX compliant systems. This call is equivalent to
379 int silc_net_accept_connection(int sock);
381 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
385 * int silc_net_set_socket_opt(int sock, int level, int option, int on);
389 * Sets a option for a socket. This function can be used to set
390 * various options for the socket. Some of the options might be
391 * system specific. This routine can only be used with POSIX compliant
392 * systems. This call is equivalent to setsockopt(2);
395 int silc_net_set_socket_opt(int sock, int level, int option, int on);
397 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
401 * int silc_net_get_socket_opt(int sock, int level, int option,
402 * void *optval, int *opt_len);
406 * Return socket options to the `optval' and `opt_len'. This routine
407 * can only be used with POSIX compliant systems. This call is
408 * equivalent to getsockopt(2).
411 int silc_net_get_socket_opt(int sock, int level, int option,
412 void *optval, int *opt_len);
414 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
418 * int silc_net_set_socket_nonblock(SilcSocket sock);
422 * Sets the socket `sock' to non-blocking mode.
425 int silc_net_set_socket_nonblock(SilcSocket sock);
427 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
431 * SilcBool silc_net_is_ip4(const char *addr);
435 * Checks whether IP address sent as argument is valid IPv4 address.
438 SilcBool silc_net_is_ip4(const char *addr);
440 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
444 * SilcBool silc_net_is_ip6(const char *addr);
448 * Checks whether IP address sent as argument is valid IPv6 address.
451 SilcBool silc_net_is_ip6(const char *addr);
453 /****f* silcutil/SilcNetAPI/silc_net_is_ip
457 * SilcBool silc_net_is_ip(const char *addr);
461 * Checks whether IP address sent as argument is valid IP address.
462 * This supports both IPv4 and IPv6 addresses.
465 SilcBool silc_net_is_ip(const char *addr);
467 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
471 * SilcBool silc_net_addr2bin(const char *addr, void *bin,
472 * SilcUInt32 bin_len);
476 * Converts the IP number string from numbers-and-dots notation to
477 * binary form in network byte order. The address can be either
478 * IPv4 or IPv6 address.
481 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
483 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
487 * typedef void (*SilcNetResolveCallback)(const char *result,
492 * A callback function of this type is called after the asynchronous
493 * resolving operation has been completed. This callback is used
494 * when asynchronously resolving IP addresses and hostnames.
497 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
499 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
503 * SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
504 * char *address, SilcUInt32 address_len);
508 * Resolves the IP address of the hostname indicated by the `name'.
509 * This returns TRUE and the IP address of the host to the `address'
510 * buffer, or FALSE if the address could not be resolved. This is
511 * synchronous function and will block the calling process. If the
512 * `prefer_ipv6' is TRUE then this will return IPv6 address if it
513 * finds. If FALSE if returns IPv4 address even if it found IPv6
517 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
518 char *address, SilcUInt32 address_len);
520 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
524 * void silc_net_gethostbyname_async(const char *name,
525 * SilcBool prefer_ipv6,
526 * SilcSchedule schedule,
527 * SilcNetResolveCallback completion,
532 * Asynchronously resolves the IP address of the hostname indicated
533 * by the `name'. This function returns immediately, and the
534 * `completion' callback will be called after the resolving is
537 * If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
538 * finds. If FALSE if returns IPv4 address even if it found IPv6
542 void silc_net_gethostbyname_async(const char *name,
543 SilcBool prefer_ipv6,
544 SilcSchedule schedule,
545 SilcNetResolveCallback completion,
548 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
552 * SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
553 * SilcUInt32 name_len);
557 * Resolves the hostname for the IP address indicated by the `addr'
558 * This returns TRUE and the resolved hostname to the `name' buffer,
559 * or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
560 * This is synchronous function and will block the calling process.
563 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
564 SilcUInt32 name_len);
566 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
570 * void silc_net_gethostbyaddr_async(const char *addr,
571 * SilcSchedule schedule,
572 * SilcNetResolveCallback completion,
577 * Asynchronously resolves the hostname for the IP address indicated
578 * by the `addr'. This function returns immediately, and the
579 * `completion' callback will be called after the resolving is
583 void silc_net_gethostbyaddr_async(const char *addr,
584 SilcSchedule schedule,
585 SilcNetResolveCallback completion,
588 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
592 * SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
597 * Performs lookups for remote name and IP address. This peforms reverse
598 * lookup as well to verify that the IP has FQDN.
601 SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
604 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
608 * SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
613 * Performs lookups for local name and IP address. This peforms reverse
614 * lookup as well to verify that the IP has FQDN.
617 SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
620 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
624 * SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
628 * Return remote port by socket.
631 SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
633 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
637 * SilcUInt16 silc_net_get_local_port(SilcSocket sock);
641 * Return local port by socket.
644 SilcUInt16 silc_net_get_local_port(SilcSocket sock);
646 /****f* silcutil/SilcNetAPI/silc_net_localhost
650 * char *silc_net_localhost(void);
654 * Return name of localhost. This will also attempt to resolve
655 * the real hostname by the local host's IP address. If unsuccessful
656 * the first found hostname is returned. The caller must free
660 char *silc_net_localhost(void);
662 /****f* silcutil/SilcNetAPI/silc_net_localip
666 * char *silc_net_localip(void)
670 * Return IP of localhost. The caller must free the returned IP.
673 char *silc_net_localip(void);
675 #include "silcnet_i.h"
677 #endif /* SILCNET_H */