Added preliminary Symbian support.
[silc.git] / lib / silcutil / silcnet.h
1 /*
2
3   silcnet.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2006 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Net Interface
21  *
22  * DESCRIPTION
23  *
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
27  * provided.
28  *
29  * On WIN32 systems the SILC Net API must initialized by calling the
30  * silc_net_win32_init and uninitialized when the application ends by
31  * calling the silc_net_win32_uninit function. The initializing must be
32  * done in order to assure that the SILC Net API works correctly.
33  *
34  ***/
35
36 #ifndef SILCNET_H
37 #define SILCNET_H
38
39 /* Prototypes */
40
41 /****s* silcutil/SilcNetAPI/SilcNetListener
42  *
43  * NAME
44  *
45  *    typedef struct SilcNetListenerStruct *SilcNetListener;
46  *
47  * DESCRIPTION
48  *
49  *    The network listenr context.  This context is created with the
50  *    silc_net_create_listener function and destroyed with
51  *    silc_net_close_listener function.
52  *
53  ***/
54 typedef struct SilcNetListenerStruct *SilcNetListener;
55
56 /****d* silcutil/SilcNetAPI/SilcNetStatus
57  *
58  * NAME
59  *
60  *    typedef enum { ... } SilcNetStatus;
61  *
62  * DESCRIPTION
63  *
64  *    Status to indicate the result of the network operation creation.  This
65  *    type is returned in the SilcNetCallback callback function.
66  *
67  * SOURCE
68  */
69 typedef enum {
70   SILC_NET_OK,                         /* Everything Ok */
71   SILC_NET_UNKNOWN_IP,                 /* Unknown IP address */
72   SILC_NET_UNKNOWN_HOST,               /* Unknown hostname */
73   SILC_NET_HOST_UNREACHABLE,           /* Destination unreachable */
74   SILC_NET_CONNECTION_REFUSED,         /* Connection refused */
75   SILC_NET_CONNECTION_TIMEOUT,         /* Connection timedout */
76   SILC_NET_NO_MEMORY,                  /* System out of memory */
77   SILC_NET_ERROR,                      /* Unknown error */
78 } SilcNetStatus;
79 /***/
80
81 /****f* silcutil/SilcNetAPI/SilcNetCallback
82  *
83  * SYNOPSIS
84  *
85  *    typedef void (*SilcNetCallback)(SilcNetStatus status,
86  *                                    SilcStream stream, void *context);
87  *
88  * DESCRIPTION
89  *
90  *    A callback of this type is returned by silc_net_tcp_create_listener
91  *    and silc_net_tcp_connect functions.  For silc_net_tcp_create_listener
92  *    this callback means that new incoming connection was accepted, and the
93  *    `stream' is the socket stream representing the socket connection.
94  *
95  *    For silc_net_tcp_connect this means that we have connected to the
96  *    remote host and the `stream' is the socket stream for the socket
97  *    connection.  The SILC Stream API (such as silc_stream_read, etc.) can
98  *    be used to read and write to the stream.  The created stream is socket
99  *    stream so various SilcSocketStream API functions can be used with
100  *    the `stream'.
101  *
102  ***/
103 typedef void (*SilcNetCallback)(SilcNetStatus status,
104                                 SilcStream stream, void *context);
105
106 /****f* silcutil/SilcNetAPI/silc_net_tcp_create_listener
107  *
108  * SYNOPSIS
109  *
110  *    SilcNetListener
111  *    silc_net_tcp_create_listener(const char **local_ip_addr,
112  *                                 SilcUInt32 local_ip_count, int port,
113  *                                 SilcBool lookup, SilcBool require_fqdn,
114  *                                 SilcSchedule schedule,
115  *                                 SilcNetCallback callback, void *context);
116  *
117  * DESCRIPTION
118  *
119  *    This function creates TCP listener.  This is used to create network
120  *    listener for incoming connections, and `callback' will be called
121  *    everytime new connection is received.  If `local_ip_addr' is NULL any
122  *    address is used.  If provided it can be used bind the listener to
123  *    `local_ip_count' many IP addresses provided in `local_ip_addr' table.
124  *    On success returns the SilcNetListener context, or NULL on error.
125  *    If `require_fqdn' is TRUE the listener will require that the incoming
126  *    connection has FQDN to be able to connect.  If the `lookup' is TRUE
127  *    then the incoming connection hostname will be resolved.  If the `port'
128  *    is zero (0), operating system will define it automatically.
129  *
130  *    The `callback' always delivers valid new stream.  It is not called
131  *    with an error status.
132  *
133  ***/
134 SilcNetListener
135 silc_net_tcp_create_listener(const char **local_ip_addr,
136                              SilcUInt32 local_ip_count, int port,
137                              SilcBool lookup, SilcBool require_fqdn,
138                              SilcSchedule schedule,
139                              SilcNetCallback callback, void *context);
140
141 /****f* silcutil/SilcNetAPI/silc_net_listener_get_port
142  *
143  * SYNOPSIS
144  *
145  *    SilcUInt16 silc_net_listener_get_port(SilcNetListener listener);
146  *
147  * DESCRIPTION
148  *
149  *    Returns the ports to where the `listener' is bound.  This can be used
150  *    to get the port if none was specified in silc_net_tcp_create_listener.
151  *    Returns an array of ports of size of `port_count'.  The caller must
152  *    free the array with silc_free.  There are as many ports in the array
153  *    as there were IP addresses provided in silc_net_tcp_create_listener.
154  *
155  ***/
156 SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
157                                        SilcUInt32 *port_count);
158
159 /****f* silcutil/SilcNetAPI/silc_net_close_listener
160  *
161  * SYNOPSIS
162  *
163  *    void silc_net_close_listener(SilcNetListener listener);
164  *
165  * DESCRIPTION
166  *
167  *    Closes the network listener indicated by `listener'.
168  *
169  ***/
170 void silc_net_close_listener(SilcNetListener listener);
171
172 /****f* silcutil/SilcNetAPI/silc_net_tcp_connect
173  *
174  * SYNOPSIS
175  *
176  *    SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
177  *                                            const char *remote_ip_addr,
178  *                                            int remote_port,
179  *                                            SilcSchedule schedule,
180  *                                            SilcNetCallback callback,
181  *                                            void *context);
182  *
183  * DESCRIPTION
184  *
185  *    Creates TCP/IP connection to the remote host indicated by `remote_host'
186  *    which may be hostname or IP address, on the port indicated by
187  *    `remote_port'.  If the `local_ip_addr' is provided the local host is
188  *    bound to that address before creating the connection.  This is
189  *    asynchronous call, and this function returns before the connection is
190  *    actually established.  The `callback' will be called after the
191  *    connection is created to deliver the SilcStream for the created
192  *    connection.  This function supports IPv6 if the platform supports it.
193  *
194  *    The returned SilcAsyncOperation context can be used to control the
195  *    asynchronous connecting, such as to abort it.  If it is aborted
196  *    using silc_async_abort the `callback' will not be called.  If NULL
197  *    is returned the operation cannot be aborted.
198  *
199  */
200 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
201                                         const char *remote_ip_addr,
202                                         int remote_port,
203                                         SilcSchedule schedule,
204                                         SilcNetCallback callback,
205                                         void *context);
206
207 /****f* silcutil/SilcNetAPI/silc_net_udp_connect
208  *
209  * SYNOPSIS
210  *
211  *    SilcStream
212  *    silc_net_udp_connect(const char *local_ip_addr, int local_port,
213  *                         const char *remote_ip_addr, int remote_port,
214  *                         SilcSchedule schedule);
215  *
216  * DESCRIPTION
217  *
218  *    This function creates UDP stream.  The UDP stream is bound to the
219  *    `local_ip_addr' if it is specified.  If `local_port' is non-zero the
220  *    stream is bound to that port.  If the `remote_ip_addr' and `remote_port'
221  *    is also provided, packets may be sent to that address using
222  *    silc_stream_write function and packets may be received using
223  *    silc_stream_read function.
224  *
225  *    If the remote address is not provided the stream is in connectionless
226  *    state.  This means that packets can be received only by using
227  *    silc_net_udp_receive and sent only by using the function
228  *    silc_net_udp_send.
229  *
230  *    To receive packets the silc_stream_set_notifier must be called for the
231  *    returned SilcStream.  The packets are always received in the notifier
232  *    callback when the SILC_STREAM_CAN_READ is returned to the callback
233  *    To read the packet use silc_stream_read if the remote address was
234  *    provided, and silc_net_udp_receive if it was not.
235  *
236  *    Supports IPv6 if the platform supports it.
237  *
238  * EXAMPLE
239  *
240  *    SilcStream udpstream;
241  *
242  *    // Create UDP stream and prepare to receive packets
243  *    udpstream = silc_net_udp_connect("10.2.1.7", 5000,
244  *                                     "10.2.1.100, 5000, schedule);
245  *    silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
246  *
247  *    // Send packet to remote host
248  *    silc_stream_write(udpstream, data, data_len);
249  *
250  *    Create UDP listener:
251  *
252  *    udpstream = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
253  *    silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
254  *
255  ***/
256 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
257                                 const char *remote_ip_addr, int remote_port,
258                                 SilcSchedule schedule);
259
260 /****f* silcutil/SilcNetAPI/silc_net_udp_receive
261  *
262  * SYNOPSIS
263  *
264  *    int
265  *    silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
266  *                         SilcUInt32 remote_ip_addr_size, int *remote_port,
267  *                         unsigned char *ret_data, SilcUInt32 data_size)
268  *
269  * DESCRIPTION
270  *
271  *    Receive a UDP packet from the `stream'.  The IP address and port of
272  *    the sender is returned into `remote_ip_addr' buffer and `remote_port'
273  *    pointer.  The packet data is returned into the `ret_data' buffer.
274  *
275  *    Returns the length of the packet, or -1 on error or 0 in case of EOF.
276  *
277  ***/
278 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
279                          SilcUInt32 remote_ip_addr_size, int *remote_port,
280                          unsigned char *ret_data, SilcUInt32 data_size);
281
282 /****f* silcutil/SilcNetAPI/silc_net_udp_send
283  *
284  * SYNOPSIS
285  *
286  *    int silc_net_udp_send(SilcStream stream,
287  *                          const char *remote_ip_addr, int remote_port,
288  *                          const unsigned char *data, SilcUInt32 data_len);
289  *
290  * DESCRIPTION
291  *
292  *    Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
293  *    This may be used with UDP streams that are not connected to any
294  *    specific remote host.  With those stream silc_stream_write cannot be
295  *    used.  In those cases, this function must be used.  This may also be
296  *    used even if the stream is connected.
297  *
298  *    Returns the amount of data written, -1 if data could not be written
299  *    at this moment, or -2 if error occurred.  If -1 is returned the
300  *    notifier callback will later be called with SILC_STREAM_CAN_WRITE
301  *    status when stream is again ready for writing.
302  *
303  ***/
304 int silc_net_udp_send(SilcStream stream,
305                       const char *remote_ip_addr, int remote_port,
306                       const unsigned char *data, SilcUInt32 data_len);
307
308 /****f* silcutil/SilcNetAPI/silc_net_close_connection
309  *
310  * SYNOPSIS
311  *
312  *    void silc_net_close_connection(int sock);
313  *
314  * DESCRIPTION
315  *
316  *    Closes the connection by closing the socket connection.  This routine
317  *    can only be used with POSIX compliant systems.
318  *
319  ***/
320 void silc_net_close_connection(int sock);
321
322 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
323  *
324  * SYNOPSIS
325  *
326  *    int silc_net_accept_connection(int sock);
327  *
328  * DESCRIPTION
329  *
330  *    Accepts a connection from a particular socket.  This routine can only
331  *    be used with POSIX compliant systems.  This call is equivalent to
332  *    accept(2).
333  *
334  ***/
335 int silc_net_accept_connection(int sock);
336
337 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
338  *
339  * SYNOPSIS
340  *
341  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
342  *
343  * DESCRIPTION
344  *
345  *    Sets a option for a socket.  This function can be used to set
346  *    various options for the socket.  Some of the options might be
347  *    system specific.  This routine can only be used with POSIX compliant
348  *    systems.  This call is equivalent to setsockopt(2);
349  *
350  ***/
351 int silc_net_set_socket_opt(int sock, int level, int option, int on);
352
353 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
354  *
355  * SYNOPSIS
356  *
357  *    int silc_net_get_socket_opt(int sock, int level, int option,
358  *                                void *optval, int *opt_len);
359  *
360  * DESCRIPTION
361  *
362  *    Return socket options to the `optval' and `opt_len'.  This routine
363  *    can only be used with POSIX compliant systems.  This call is
364  *    equivalent to getsockopt(2).
365  *
366  ***/
367 int silc_net_get_socket_opt(int sock, int level, int option,
368                             void *optval, int *opt_len);
369
370 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
371  *
372  * SYNOPSIS
373  *
374  *    int silc_net_set_socket_nonblock(SilcSocket sock);
375  *
376  * DESCRIPTION
377  *
378  *    Sets the socket `sock' to non-blocking mode.
379  *
380  ***/
381 int silc_net_set_socket_nonblock(SilcSocket sock);
382
383 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
384  *
385  * SYNOPSIS
386  *
387  *    SilcBool silc_net_is_ip4(const char *addr);
388  *
389  * DESCRIPTION
390  *
391  *    Checks whether IP address sent as argument is valid IPv4 address.
392  *
393  ***/
394 SilcBool silc_net_is_ip4(const char *addr);
395
396 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
397  *
398  * SYNOPSIS
399  *
400  *    SilcBool silc_net_is_ip6(const char *addr);
401  *
402  * DESCRIPTION
403  *
404  *    Checks whether IP address sent as argument is valid IPv6 address.
405  *
406  ***/
407 SilcBool silc_net_is_ip6(const char *addr);
408
409 /****f* silcutil/SilcNetAPI/silc_net_is_ip
410  *
411  * SYNOPSIS
412  *
413  *    SilcBool silc_net_is_ip(const char *addr);
414  *
415  * DESCRIPTION
416  *
417  *    Checks whether IP address sent as argument is valid IP address.
418  *    This supports both IPv4 and IPv6 addresses.
419  *
420  ***/
421 SilcBool silc_net_is_ip(const char *addr);
422
423 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
424  *
425  * SYNOPSIS
426  *
427  *    SilcBool silc_net_addr2bin(const char *addr, void *bin,
428  *                               SilcUInt32 bin_len);
429  *
430  * DESCRIPTION
431  *
432  *    Converts the IP number string from numbers-and-dots notation to
433  *    binary form in network byte order.  The address can be either
434  *    IPv4 or IPv6 address.
435  *
436  ***/
437 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
438
439 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
440  *
441  * SYNOPSIS
442  *
443  *    typedef void (*SilcNetResolveCallback)(const char *result,
444  *                                           void *context);
445  *
446  * DESCRIPTION
447  *
448  *    A callback function of this type is called after the asynchronous
449  *    resolving operation has been completed.  This callback is used
450  *    when asynchronously resolving IP addresses and hostnames.
451  *
452  ***/
453 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
454
455 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
456  *
457  * SYNOPSIS
458  *
459  *    SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
460  *                                char *address, SilcUInt32 address_len);
461  *
462  * DESCRIPTION
463  *
464  *    Resolves the IP address of the hostname indicated by the `name'.
465  *    This returns TRUE and the IP address of the host to the `address'
466  *    buffer, or FALSE if the address could not be resolved.  This is
467  *    synchronous function and will block the calling process.  If the
468  *    `prefer_ipv6' is TRUE then this will return IPv6 address if it
469  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
470  *    address also.
471  *
472  ***/
473 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
474                                 char *address, SilcUInt32 address_len);
475
476 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
477  *
478  * SYNOPSIS
479  *
480  *    void silc_net_gethostbyname_async(const char *name,
481  *                                      SilcBool prefer_ipv6,
482  *                                      SilcSchedule schedule,
483  *                                      SilcNetResolveCallback completion,
484  *                                      void *context)
485  *
486  * DESCRIPTION
487  *
488  *    Asynchronously resolves the IP address of the hostname indicated
489  *    by the `name'.  This function returns immediately, and the
490  *    `completion' callback will be called after the resolving is
491  *    completed.
492  *
493  *    If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
494  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
495  *    address also.
496  *
497  ***/
498 void silc_net_gethostbyname_async(const char *name,
499                                   SilcBool prefer_ipv6,
500                                   SilcSchedule schedule,
501                                   SilcNetResolveCallback completion,
502                                   void *context);
503
504 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
505  *
506  * SYNOPSIS
507  *
508  *   SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
509  *                               SilcUInt32 name_len);
510  *
511  * DESCRIPTION
512  *
513  *    Resolves the hostname for the IP address indicated by the `addr'
514  *    This returns TRUE and the resolved hostname to the `name' buffer,
515  *    or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
516  *    This is synchronous function and will block the calling process.
517  *
518  ***/
519 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
520                                 SilcUInt32 name_len);
521
522 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
523  *
524  * SYNOPSIS
525  *
526  *    void silc_net_gethostbyaddr_async(const char *addr,
527  *                                      SilcSchedule schedule,
528  *                                      SilcNetResolveCallback completion,
529  *                                      void *context)
530  *
531  * DESCRIPTION
532  *
533  *    Asynchronously resolves the hostname for the IP address indicated
534  *    by the `addr'.  This function returns immediately, and the
535  *    `completion' callback will be called after the resolving is
536  *    completed.
537  *
538  ***/
539 void silc_net_gethostbyaddr_async(const char *addr,
540                                   SilcSchedule schedule,
541                                   SilcNetResolveCallback completion,
542                                   void *context);
543
544 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
545  *
546  * SYNOPSIS
547  *
548  *    SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
549  *                                         char **ip);
550  *
551  * DESCRIPTION
552  *
553  *    Performs lookups for remote name and IP address. This peforms reverse
554  *    lookup as well to verify that the IP has FQDN.
555  *
556  ***/
557 SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
558                                      char **ip);
559
560 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
561  *
562  * SYNOPSIS
563  *
564  *    SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
565  *                                          char **ip);
566  *
567  * DESCRIPTION
568  *
569  *    Performs lookups for local name and IP address. This peforms reverse
570  *    lookup as well to verify that the IP has FQDN.
571  *
572  ***/
573 SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
574                                       char **ip);
575
576 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
577  *
578  * SYNOPSIS
579  *
580  *    SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
581  *
582  * DESCRIPTION
583  *
584  *    Return remote port by socket.
585  *
586  ***/
587 SilcUInt16 silc_net_get_remote_port(SilcSocket sock);
588
589 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
590  *
591  * SYNOPSIS
592  *
593  *    SilcUInt16 silc_net_get_local_port(SilcSocket sock);
594  *
595  * DESCRIPTION
596  *
597  *    Return local port by socket.
598  *
599  ***/
600 SilcUInt16 silc_net_get_local_port(SilcSocket sock);
601
602 /****f* silcutil/SilcNetAPI/silc_net_localhost
603  *
604  * SYNOPSIS
605  *
606  *    char *silc_net_localhost(void);
607  *
608  * DESCRIPTION
609  *
610  *    Return name of localhost.  This will also attempt to resolve
611  *    the real hostname by the local host's IP address.  If unsuccessful
612  *    the first found hostname is returned.  The caller must free
613  *    returned hostname.
614  *
615  ***/
616 char *silc_net_localhost(void);
617
618 /****f* silcutil/SilcNetAPI/silc_net_localip
619  *
620  * SYNOPSIS
621  *
622  *    char *silc_net_localip(void)
623  *
624  * DESCRIPTION
625  *
626  *    Return IP of localhost.  The caller must free the returned IP.
627  *
628  ***/
629 char *silc_net_localip(void);
630
631 /****f* silcutil/SilcNetAPI/silc_net_win32_init
632  *
633  * SYNOPSIS
634  *
635  *    SilcBool silc_net_win32_init(void);
636  *
637  * DESCRIPTION
638  *
639  *    This is WIN32 system specific function and is used to initialize
640  *    the network.  This must be called by all WIN32 applications.  It
641  *    is usually called at the application's main() or WinMain() before
642  *    calling any other SILC routine.  The application must also call
643  *    the silc_net_win32_uninit when exiting the application.  Returns
644  *    FALSE on error.  The network will not work if this function returns
645  *    FALSE.
646  *
647  * NOTES
648  *
649  *    This routines is available only on Win32 platform.
650  *
651  ***/
652 SilcBool silc_net_win32_init(void);
653
654 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
655  *
656  * SYNOPSIS
657  *
658  *    void silc_net_win32_init(void);
659  *
660  * DESCRIPTION
661  *
662  *    This is WIN32 system specific function and is used to uninitialize
663  *    the network.  This must be called by all WIN32 applications.  It
664  *    is usually called when the application is exiting.  After calling
665  *    this function the SILC Net API routines will not work anymore.
666  *
667  * NOTES
668  *
669  *    This routines is available only on Win32 platform.
670  *
671  ***/
672 void silc_net_win32_uninit(void);
673
674 #include "silcnet_i.h"
675
676 #endif /* SILCNET_H */