5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 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/Network Interface
24 * SILC Net API provides various network routines for applications. It can
25 * be used to create TCP/IP and UDP/IP connections and listeners. Various
26 * utility functions for resolving various information is also provided.
27 * The interface supports both IPv4 and IPv6.
31 * // Create TCP connection to example.com at port 25
32 * silc_net_tcp_connect(NULL, "example.com", 25, schedule, connected_cb, ctx);
34 * // Create UDP listener on local interface 10.2.1.7 on port 500
35 * SilcStream udpstream;
37 * udpstream = silc_net_udp_connect("10.2.1.7", 500, NULL, 0, schedule);
38 * silc_stream_set_notifier(udpstream, schedule, receive_callback, ctx);
47 /****s* silcutil/SilcNetListener
51 * typedef struct SilcNetListenerStruct *SilcNetListener;
55 * The network listenr context. This context is created with the
56 * silc_net_tcp_create_listener function and destroyed with
57 * silc_net_close_listener function.
60 typedef struct SilcNetListenerStruct *SilcNetListener;
62 /****f* silcutil/SilcNetCallback
66 * typedef void (*SilcNetCallback)(SilcResult status,
67 * SilcStream stream, void *context);
71 * A callback of this type is returned by silc_net_tcp_create_listener
72 * and silc_net_tcp_connect functions. For silc_net_tcp_create_listener
73 * this callback means that new incoming connection was accepted, and the
74 * `stream' is the socket stream representing the socket connection.
76 * For silc_net_tcp_connect this means that we have connected to the
77 * remote host and the `stream' is the socket stream for the socket
78 * connection. The SILC Stream API (such as silc_stream_read, etc.) can
79 * be used to read and write to the stream. The created stream is socket
80 * stream so various SilcSocketStream API functions can be used with
84 typedef void (*SilcNetCallback)(SilcResult status,
85 SilcStream stream, void *context);
87 /****f* silcutil/silc_net_tcp_create_listener
92 * silc_net_tcp_create_listener(const char **local_ip_addr,
93 * SilcUInt32 local_ip_count, int port,
94 * SilcBool lookup, SilcBool require_fqdn,
95 * SilcSchedule schedule,
96 * SilcNetCallback callback, void *context);
100 * This function creates TCP listener. This is used to create network
101 * listener for incoming connections, and `callback' will be called
102 * everytime new connection is received. If `local_ip_addr' is NULL 'any'
103 * address is used. If provided it can be used bind the listener to
104 * `local_ip_count' many IP addresses provided in `local_ip_addr' table.
105 * On success returns the SilcNetListener context, or NULL on error.
106 * If `require_fqdn' is TRUE the listener will require that the incoming
107 * connection has FQDN to be able to connect. If the `lookup' is TRUE
108 * then the incoming connection hostname will be resolved. If the `port'
109 * is zero (0), operating system will define it automatically.
111 * The `callback' always delivers valid new stream. It is not called
112 * with an error status. If `schedule' is NULL this will call
113 * silc_schedule_get_global to try to get global scheduler.
117 silc_net_tcp_create_listener(const char **local_ip_addr,
118 SilcUInt32 local_ip_count, int port,
119 SilcBool lookup, SilcBool require_fqdn,
120 SilcSchedule schedule,
121 SilcNetCallback callback, void *context);
123 /****f* silcutil/silc_net_tcp_create_listener2
128 * silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
129 * SilcUInt32 port_count,
130 * SilcBool ignore_port_error,
131 * SilcBool lookup, SilcBool require_fqdn,
132 * SilcSchedule schedule,
133 * SilcNetCallback callback, void *context);
137 * This function creates TCP listener. This is used to create network
138 * listener for incoming connections, and `callback' will be called
139 * everytime new connection is received. If `local_ip_addr' is NULL 'any'
140 * address is used. If `ports' is NULL or it contains a zero (0) port,
141 * operating system will define it automatically. This function can be
142 * used to bind to many ports at the same time. If `ignore_port_error'
143 * is TRUE this won't return NULL if at least one of the ports could
144 * be bound. Otherwise, NULL will be returned on error.
146 * If `require_fqdn' is TRUE the listener will require that the incoming
147 * connection has FQDN to be able to connect. If the `lookup' is TRUE
148 * then the incoming connection hostname will be resolved.
150 * The `callback' always delivers valid new stream. It is not called
151 * with an error status. If `schedule' is NULL this will call
152 * silc_schedule_get_global to try to get global scheduler.
156 silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
157 SilcUInt32 port_count,
158 SilcBool ignore_port_error,
159 SilcBool lookup, SilcBool require_fqdn,
160 SilcSchedule schedule,
161 SilcNetCallback callback, void *context);
163 /****f* silcutil/silc_net_listener_get_port
167 * SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
168 * SilcUInt32 *port_count);
172 * Returns the ports to where the `listener' is bound. This can be used
173 * to get the port if none was specified in silc_net_tcp_create_listener.
174 * Returns an array of ports of size of `port_count'. The caller must
175 * free the array with silc_free. There are as many ports in the array
176 * as there were IP addresses provided in silc_net_tcp_create_listener,
177 * or as there were ports provided in silc_net_tcp_create_listener2.
180 SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
181 SilcUInt32 *port_count);
183 /****f* silcutil/silc_net_listener_get_ip
187 * char **silc_net_listener_get_ip(SilcNetListener listener,
188 * SilcUInt32 *ip_count);
192 * Returns the IP's to where the `listener' is bound. Returns an array
193 * of IP addresses of size of `ip_count'. The caller must free the
194 * array and its strings with silc_free.
197 char **silc_net_listener_get_ip(SilcNetListener listener,
198 SilcUInt32 *ip_count);
200 /****f* silcutil/silc_net_listener_get_hostname
204 * char **silc_net_listener_get_hostname(SilcNetListener listener,
205 * SilcUInt32 *hostname_count);
209 * Returns the hostnames to where the `listener' is bound. Returns an
210 * array of hostnames of size of `hostname_count'. The caller must free
211 * the array and its strings with silc_free.
214 char **silc_net_listener_get_hostname(SilcNetListener listener,
215 SilcUInt32 *hostname_count);
217 /****f* silcutil/silc_net_close_listener
221 * void silc_net_close_listener(SilcNetListener listener);
225 * Closes the network listener indicated by `listener'.
228 void silc_net_close_listener(SilcNetListener listener);
230 /****f* silcutil/silc_net_tcp_connect
234 * SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
235 * const char *remote_ip_addr,
237 * SilcSchedule schedule,
238 * SilcNetCallback callback,
243 * Creates TCP/IP connection to the remote host indicated by `remote_host'
244 * which may be hostname or IP address, on the port indicated by
245 * `remote_port'. If the `local_ip_addr' is provided the local host is
246 * bound to that address before creating the connection. This is
247 * asynchronous call, and this function returns before the connection is
248 * actually established. The `callback' will be called after the
249 * connection is created to deliver the SilcStream for the created
250 * connection. This function supports IPv6 if the platform supports it.
252 * The returned SilcAsyncOperation context can be used to control the
253 * asynchronous connecting, such as to abort it. If it is aborted
254 * using silc_async_abort the `callback' will not be called. If NULL
255 * is returned the operation cannot be aborted. If `schedule' is NULL
256 * this will call silc_schedule_get_global to try to get global scheduler.
259 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
260 const char *remote_ip_addr,
262 SilcSchedule schedule,
263 SilcNetCallback callback,
266 /****f* silcutil/silc_net_udp_connect
271 * silc_net_udp_connect(const char *local_ip_addr, int local_port,
272 * const char *remote_ip_addr, int remote_port,
273 * SilcSchedule schedule);
277 * This function creates UDP stream. The UDP stream is bound to the
278 * `local_ip_addr' if it is specified. If `local_port' is non-zero the
279 * stream is bound to that port. If the `remote_ip_addr' and `remote_port'
280 * is also provided, packets may be sent to that address using
281 * silc_stream_write function and packets may be received using
282 * silc_stream_read function.
284 * If the remote address is not provided the stream is in connectionless
285 * state. This means that packets can be received only by using
286 * silc_net_udp_receive and sent only by using the function
289 * To receive packets the silc_stream_set_notifier must be called for the
290 * returned SilcStream. The packets are always received in the notifier
291 * callback when the SILC_STREAM_CAN_READ is returned to the callback.
292 * To read the packet use silc_stream_read if the `remote_ip_addr' was
293 * provided, and silc_net_udp_receive if it was not.
295 * Supports IPv6 if the platform supports it. If `schedule' is NULL this
296 * will call silc_schedule_get_global to try to get global scheduler.
300 * SilcStream udpstream;
302 * // Create UDP stream and prepare to receive packets
303 * udpstream = silc_net_udp_connect("10.2.1.7", 5000,
304 * "10.2.1.100, 5000, schedule);
305 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
307 * // Send packet to remote host
308 * silc_stream_write(udpstream, data, data_len);
310 * Create UDP listener:
312 * udpstream = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
313 * silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
316 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
317 const char *remote_ip_addr, int remote_port,
318 SilcSchedule schedule);
320 /****f* silcutil/silc_net_udp_receive
325 * silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
326 * SilcUInt32 remote_ip_addr_size, int *remote_port,
327 * unsigned char *ret_data, SilcUInt32 data_size)
331 * Receive a UDP packet from the `stream'. The IP address and port of
332 * the sender is returned into `remote_ip_addr' buffer and `remote_port'
333 * pointer. The packet data is returned into the `ret_data' buffer.
335 * Returns the length of the packet, or -1 on error or 0 in case of EOF.
336 * In case of error the silc_errno will indicate the error. If the
337 * error is SILC_ERR_WOULD_BLOCK data is not currently available from the
341 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
342 SilcUInt32 remote_ip_addr_size, int *remote_port,
343 unsigned char *ret_data, SilcUInt32 data_size);
345 /****f* silcutil/silc_net_udp_send
349 * int silc_net_udp_send(SilcStream stream,
350 * const char *remote_ip_addr, int remote_port,
351 * const unsigned char *data, SilcUInt32 data_len);
355 * Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
356 * This may be used with UDP streams that are not connected to any
357 * specific remote host. With those streams silc_stream_write cannot be
358 * used. In those cases, this function must be used. However, this may
359 * also be used even if the stream is in connected state.
361 * You can create the `stream' by calling silc_net_udp_connect.
363 * Returns the amount of data written, -1 if data could not be written
364 * at this moment, or -2 if error occurred. If -1 is returned the
365 * notifier callback will later be called with SILC_STREAM_CAN_WRITE
366 * status when stream is again ready for writing.
369 int silc_net_udp_send(SilcStream stream,
370 const char *remote_ip_addr, int remote_port,
371 const unsigned char *data, SilcUInt32 data_len);
373 /****f* silcutil/silc_net_close_connection
377 * void silc_net_close_connection(int sock);
381 * Closes the connection by closing the socket connection. This routine
382 * can only be used with POSIX compliant systems.
385 void silc_net_close_connection(int sock);
387 /****f* silcutil/silc_net_accept_connection
391 * int silc_net_accept_connection(int sock);
395 * Accepts a connection from a particular socket. This routine can only
396 * be used with POSIX compliant systems. This call is equivalent to
400 int silc_net_accept_connection(int sock);
402 /****f* silcutil/silc_net_set_socket_opt
406 * int silc_net_set_socket_opt(int sock, int level, int option, int on);
410 * Sets a option for a socket. This function can be used to set
411 * various options for the socket. Some of the options might be
412 * system specific. This routine can only be used with POSIX compliant
413 * systems. This call is equivalent to setsockopt(2);
416 int silc_net_set_socket_opt(int sock, int level, int option, int on);
418 /****f* silcutil/silc_net_get_socket_opt
422 * int silc_net_get_socket_opt(int sock, int level, int option,
423 * void *optval, int *opt_len);
427 * Return socket options to the `optval' and `opt_len'. This routine
428 * can only be used with POSIX compliant systems. This call is
429 * equivalent to getsockopt(2).
432 int silc_net_get_socket_opt(int sock, int level, int option,
433 void *optval, int *opt_len);
435 /****f* silcutil/silc_net_set_socket_nonblock
439 * int silc_net_set_socket_nonblock(SilcSocket sock);
443 * Sets the socket `sock' to non-blocking mode.
446 int silc_net_set_socket_nonblock(SilcSocket sock);
448 /****f* silcutil/silc_net_is_ip4
452 * SilcBool silc_net_is_ip4(const char *addr);
456 * Checks whether IP address sent as argument is valid IPv4 address.
459 SilcBool silc_net_is_ip4(const char *addr);
461 /****f* silcutil/silc_net_is_ip6
465 * SilcBool silc_net_is_ip6(const char *addr);
469 * Checks whether IP address sent as argument is valid IPv6 address.
472 SilcBool silc_net_is_ip6(const char *addr);
474 /****f* silcutil/silc_net_is_ip
478 * SilcBool silc_net_is_ip(const char *addr);
482 * Checks whether IP address sent as argument is valid IP address.
483 * This supports both IPv4 and IPv6 addresses.
486 SilcBool silc_net_is_ip(const char *addr);
488 /****f* silcutil/silc_net_addr2bin
492 * SilcBool silc_net_addr2bin(const char *addr, void *bin,
493 * SilcUInt32 bin_len);
497 * Converts the IP number string from numbers-and-dots notation to
498 * binary form in network byte order. The address can be either
499 * IPv4 or IPv6 address.
502 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
504 /****f* silcutil/silc_net_bin2addr
508 * SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len,
509 * char *addr, SilcUInt32 addr_size);
513 * Converts network byte ordered IP address into a numbers-and-dots
514 * string notation. The `bin' address can be either IPv4 or IPv6 address.
517 SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len,
518 char *addr, SilcUInt32 addr_size);
520 /****f* silcutil/SilcNetResolveCallback
524 * typedef void (*SilcNetResolveCallback)(const char *result,
529 * A callback function of this type is called after the asynchronous
530 * resolving operation has been completed. This callback is used
531 * when asynchronously resolving IP addresses and hostnames.
534 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
536 /****f* silcutil/silc_net_gethostbyname
540 * SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
541 * char *address, SilcUInt32 address_len);
545 * Resolves the IP address of the hostname indicated by the `name'.
546 * This returns TRUE and the IP address of the host to the `address'
547 * buffer, or FALSE if the address could not be resolved. This is
548 * synchronous function and will block the calling process. If the
549 * `prefer_ipv6' is TRUE then this will return IPv6 address if it
550 * finds. If FALSE if returns IPv4 address even if it found IPv6
554 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
555 char *address, SilcUInt32 address_len);
557 /****f* silcutil/silc_net_gethostbyname_async
561 * void silc_net_gethostbyname_async(const char *name,
562 * SilcBool prefer_ipv6,
563 * SilcSchedule schedule,
564 * SilcNetResolveCallback completion,
569 * Asynchronously resolves the IP address of the hostname indicated
570 * by the `name'. This function returns immediately, and the
571 * `completion' callback will be called after the resolving is
572 * completed. If `schedule' is NULL this will call silc_schedule_get_global
573 * to try to get global scheduler.
575 * If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
576 * finds. If FALSE if returns IPv4 address even if it found IPv6
580 void silc_net_gethostbyname_async(const char *name,
581 SilcBool prefer_ipv6,
582 SilcSchedule schedule,
583 SilcNetResolveCallback completion,
586 /****f* silcutil/silc_net_gethostbyaddr
590 * SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
591 * SilcUInt32 name_len);
595 * Resolves the hostname for the IP address indicated by the `addr'
596 * This returns TRUE and the resolved hostname to the `name' buffer,
597 * or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
598 * This is synchronous function and will block the calling process.
601 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
602 SilcUInt32 name_len);
604 /****f* silcutil/silc_net_gethostbyaddr_async
608 * void silc_net_gethostbyaddr_async(const char *addr,
609 * SilcSchedule schedule,
610 * SilcNetResolveCallback completion,
615 * Asynchronously resolves the hostname for the IP address indicated
616 * by the `addr'. This function returns immediately, and the
617 * `completion' callback will be called after the resolving is
618 * completed. If `schedule' is NULL this will call silc_schedule_get_global
619 * to try to get global scheduler.
622 void silc_net_gethostbyaddr_async(const char *addr,
623 SilcSchedule schedule,
624 SilcNetResolveCallback completion,
627 /****f* silcutil/silc_net_check_host_by_sock
631 * SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
636 * Performs lookups for remote name and IP address. This peforms reverse
637 * lookup as well to verify that the IP has FQDN.
640 SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
643 /****f* silcutil/silc_net_check_local_by_sock
647 * SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
652 * Performs lookups for local name and IP address. This peforms reverse
653 * lookup as well to verify that the IP has FQDN.
656 SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
659 /****f* silcutil/silc_net_get_remote_port
663 * SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
667 * Return remote port by socket.
670 SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
672 /****f* silcutil/silc_net_get_local_port
676 * SilcUInt16 silc_net_get_local_port(SilcSocket sock);
680 * Return local port by socket.
683 SilcUInt16 silc_net_get_local_port(SilcSocket sock);
685 /****f* silcutil/silc_net_localhost
689 * char *silc_net_localhost(void);
693 * Return name of localhost. This will also attempt to resolve
694 * the real hostname by the local host's IP address. If unsuccessful
695 * the first found hostname is returned. The caller must free
699 char *silc_net_localhost(void);
701 /****f* silcutil/silc_net_localip
705 * char *silc_net_localip(void)
709 * Return IP of localhost. The caller must free the returned IP.
712 char *silc_net_localip(void);
714 /****f* silcutil/silc_htonl
718 * SilcUInt32 silc_htonl(SilcUInt32 host);
722 * Converts integer `host' from host byte order to network byte order.
725 SilcUInt32 silc_htonl(SilcUInt32 host);
727 /****f* silcutil/silc_ntohl
731 * SilcUInt32 silc_ntohl(SilcUInt32 net);
735 * Converts integer `net' from network byte order to host byte order.
738 SilcUInt32 silc_ntohl(SilcUInt32 net);
740 /****f* silcutil/silc_htons
744 * SilcUInt16 silc_htons(SilcUInt16 host);
748 * Converts integer `host' from host byte order to network byte order.
751 SilcUInt16 silc_htons(SilcUInt16 host);
753 /****f* silcutil/silc_ntohs
757 * SilcUInt16 silc_ntohs(SilcUInt16 net);
761 * Converts integer `net' from network byte order to host byte order.
764 SilcUInt16 silc_ntohs(SilcUInt16 net);
766 #include "silcnet_i.h"
768 #endif /* SILCNET_H */