updates.
[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,
113  *                                 int port, 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.
127  *
128  ***/
129 SilcNetListener
130 silc_net_tcp_create_listener(const char **local_ip_addr,
131                              SilcUInt32 local_ip_count,
132                              int port, SilcBool require_fqdn,
133                              SilcSchedule schedule,
134                              SilcNetCallback callback, void *context);
135
136 /****f* silcutil/SilcNetAPI/silc_net_close_listener
137  *
138  * SYNOPSIS
139  *
140  *    void silc_net_close_listener(SilcNetListener listener);
141  *
142  * DESCRIPTION
143  *
144  *    Closes the network listener indicated by `listener'.
145  *
146  ***/
147 void silc_net_close_listener(SilcNetListener listener);
148
149 /****f* silcutil/SilcNetAPI/silc_net_tcp_connect
150  *
151  * SYNOPSIS
152  *
153  *    SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
154  *                                            const char *remote_ip_addr,
155  *                                            int remote_port,
156  *                                            SilcSchedule schedule,
157  *                                            SilcNetCallback callback,
158  *                                            void *context);
159  *
160  * DESCRIPTION
161  *
162  *    Creates TCP/IP connection to the remote host indicated by `remote_host'
163  *    which may be hostname or IP address, on the port indicated by
164  *    `remote_port'.  If the `local_ip_addr' is provided the local host is
165  *    bound to that address before creating the connection.  This is
166  *    asynchronous call, and this function returns before the connection is
167  *    actually established.  The `callback' will be called after the
168  *    connection is created to deliver the SilcStream for the created
169  *    connection.
170  *
171  *    The returned SilcAsyncOperation context can be used to control the
172  *    asynchronous connecting, such as to abort it.  If it is aborted
173  *    using silc_async_abort the `callback' will not be called.  If NULL
174  *    is returned the operation cannot be aborted.
175  *
176  */
177 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
178                                         const char *remote_ip_addr,
179                                         int remote_port,
180                                         SilcSchedule schedule,
181                                         SilcNetCallback callback,
182                                         void *context);
183
184 /****f* silcutil/SilcNetAPI/silc_net_udp_connect
185  *
186  * SYNOPSIS
187  *
188  *    SilcStream
189  *    silc_net_udp_connect(const char *local_ip_addr, int local_port,
190  *                         const char *remote_ip_addr, int remote_port,
191  *                         SilcSchedule schedule);
192  *
193  * DESCRIPTION
194  *
195  *    This function creates UDP stream.  The UDP stream is bound to the
196  *    `local_ip_addr' if it is specified.  If `local_port' is non-zero the
197  *    stream is bound to that port.  If the `remote_ip_addr' and `remote_port'
198  *    is also provided, packets may be sent to that address using
199  *    silc_stream_write function and packets may be received using
200  *    silc_stream_read function.
201  *
202  *    If the remote address is not provided then packets may only be received
203  *    by using silc_net_udp_receive and sent only by using the function
204  *    silc_net_udp_send.
205  *
206  *    To receive packets the silc_stream_set_notifier must be called for the
207  *    returned SilcStream.  The packets are always received in the notifier
208  *    callback when the SILC_STREAM_CAN_READ is returned to the callback
209  *    To read the packet use silc_stream_read if the remote address was
210  *    provided, and silc_net_udp_receive if it was not.
211  *
212  * EXAMPLE
213  *
214  *    SilcStream udpstream;
215  *
216  *    // Create UDP stream and prepare to receive packets
217  *    udpstream = silc_net_udp_connect("10.2.1.7", 5000,
218  *                                     "10.2.1.100, 5000, schedule);
219  *    silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
220  *
221  *    // Send packet to remote host
222  *    silc_stream_write(udpstream, data, data_len);
223  *
224  ***/
225 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
226                                 const char *remote_ip_addr, int remote_port,
227                                 SilcSchedule schedule);
228
229 /****f* silcutil/SilcNetAPI/silc_net_udp_receive
230  *
231  * SYNOPSIS
232  *
233  *    int
234  *    silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
235  *                         SilcUInt32 remote_ip_addr_size, int *remote_port,
236  *                         unsigned char *ret_data, SilcUInt32 data_size)
237  *
238  * DESCRIPTION
239  *
240  *    Receive a UDP packet from the `stream'.  The IP address and port of
241  *    the sender is returned into `remote_ip_addr' buffer and `remote_port'
242  *    pointer.  The packet data is returned into the `ret_data' buffer.
243  *
244  *    Returns the length of the packet, or -1 on error or 0 in case of EOF.
245  *
246  ***/
247 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
248                          SilcUInt32 remote_ip_addr_size, int *remote_port,
249                          unsigned char *ret_data, SilcUInt32 data_size);
250
251 /****f* silcutil/SilcNetAPI/silc_net_udp_send
252  *
253  * SYNOPSIS
254  *
255  *    void silc_net_udp_send(SilcStream stream,
256  *                           const char *remote_ip_addr, int remote_port,
257  *                           const unsigned char *data, SilcUInt32 data_len);
258  *
259  * DESCRIPTION
260  *
261  *    Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
262  *    This may be used with UDP streams that are not connected to any
263  *    specific remote host.  With those stream silc_stream_write cannot be
264  *    used.  In those cases, this function must be used.  This may also be
265  *    used even if the stream is connected.
266  *
267  *    This function always succeeds, however there is no guarantee that the
268  *    packet is delivered, as UDP is unreliable transport protocol.
269  *
270  ***/
271 void silc_net_udp_send(SilcStream stream,
272                        const char *remote_ip_addr, int remote_port,
273                        const unsigned char *data, SilcUInt32 data_len);
274
275 /****f* silcutil/SilcNetAPI/silc_net_close_connection
276  *
277  * SYNOPSIS
278  *
279  *    void silc_net_close_connection(int sock);
280  *
281  * DESCRIPTION
282  *
283  *    Closes the connection by closing the socket connection.
284  *
285  ***/
286 void silc_net_close_connection(int sock);
287
288 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
289  *
290  * SYNOPSIS
291  *
292  *    int silc_net_accept_connection(int sock);
293  *
294  * DESCRIPTION
295  *
296  *    Accepts a connection from a particular socket.
297  *
298  ***/
299 int silc_net_accept_connection(int sock);
300
301 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
302  *
303  * SYNOPSIS
304  *
305  *    int silc_net_set_socket_nonblock(int sock);
306  *
307  * DESCRIPTION
308  *
309  *    Sets the socket to non-blocking mode.
310  *
311  ***/
312 int silc_net_set_socket_nonblock(int sock);
313
314 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
315  *
316  * SYNOPSIS
317  *
318  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
319  *
320  * DESCRIPTION
321  *
322  *    Sets a option for a socket.  This function can be used to set
323  *    various options for the socket.  Some of the options might be
324  *    system specific.
325  *
326  ***/
327 int silc_net_set_socket_opt(int sock, int level, int option, int on);
328
329 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
330  *
331  * SYNOPSIS
332  *
333  *    int silc_net_get_socket_opt(int sock, int level, int option,
334  *                                void *optval, int *opt_len);
335  *
336  * DESCRIPTION
337  *
338  *    Return socket options to the `optval' and `opt_len'.
339  *
340  ***/
341 int silc_net_get_socket_opt(int sock, int level, int option,
342                             void *optval, int *opt_len);
343
344 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
345  *
346  * SYNOPSIS
347  *
348  *    SilcBool silc_net_is_ip4(const char *addr);
349  *
350  * DESCRIPTION
351  *
352  *    Checks whether IP address sent as argument is valid IPv4 address.
353  *
354  ***/
355 SilcBool silc_net_is_ip4(const char *addr);
356
357 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
358  *
359  * SYNOPSIS
360  *
361  *    SilcBool silc_net_is_ip6(const char *addr);
362  *
363  * DESCRIPTION
364  *
365  *    Checks whether IP address sent as argument is valid IPv6 address.
366  *
367  ***/
368 SilcBool silc_net_is_ip6(const char *addr);
369
370 /****f* silcutil/SilcNetAPI/silc_net_is_ip
371  *
372  * SYNOPSIS
373  *
374  *    SilcBool silc_net_is_ip(const char *addr);
375  *
376  * DESCRIPTION
377  *
378  *    Checks whether IP address sent as argument is valid IP address.
379  *    This supports both IPv4 and IPv6 addresses.
380  *
381  ***/
382 SilcBool silc_net_is_ip(const char *addr);
383
384 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
385  *
386  * SYNOPSIS
387  *
388  *    SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
389  *
390  * DESCRIPTION
391  *
392  *    Converts the IP number string from numbers-and-dots notation to
393  *    binary form in network byte order.  The address can be either
394  *    IPv4 or IPv6 address.
395  *
396  ***/
397 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
398
399 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
400  *
401  * SYNOPSIS
402  *
403  *    typedef void (*SilcNetResolveCallback)(const char *result,
404  *                                           void *context);
405  *
406  * DESCRIPTION
407  *
408  *    A callback function of this type is called after the asynchronous
409  *    resolving operation has been completed.  This callback is used
410  *    when asynchronously resolving IP addresses and hostnames.
411  *
412  ***/
413 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
414
415 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
416  *
417  * SYNOPSIS
418  *
419  *    SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
420  *                                char *address, SilcUInt32 address_len);
421  *
422  * DESCRIPTION
423  *
424  *    Resolves the IP address of the hostname indicated by the `name'.
425  *    This returns TRUE and the IP address of the host to the `address'
426  *    buffer, or FALSE if the address could not be resolved.  This is
427  *    synchronous function and will block the calling process.  If the
428  *    `prefer_ipv6' is TRUE then this will return IPv6 address if it
429  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
430  *    address also.
431  *
432  ***/
433 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
434                                 char *address, SilcUInt32 address_len);
435
436 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
437  *
438  * SYNOPSIS
439  *
440  *    void silc_net_gethostbyname_async(const char *name,
441  *                                      SilcBool prefer_ipv6,
442  *                                      SilcSchedule schedule,
443  *                                      SilcNetResolveCallback completion,
444  *                                      void *context)
445  *
446  * DESCRIPTION
447  *
448  *    Asynchronously resolves the IP address of the hostname indicated
449  *    by the `name'.  This function returns immediately, and the
450  *    `completion' callback will be called after the resolving is
451  *    completed.
452  *
453  *    If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
454  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
455  *    address also.
456  *
457  ***/
458 void silc_net_gethostbyname_async(const char *name,
459                                   SilcBool prefer_ipv6,
460                                   SilcSchedule schedule,
461                                   SilcNetResolveCallback completion,
462                                   void *context);
463
464 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
465  *
466  * SYNOPSIS
467  *
468  *   SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
469  *                               SilcUInt32 name_len);
470  *
471  * DESCRIPTION
472  *
473  *    Resolves the hostname for the IP address indicated by the `addr'
474  *    This returns TRUE and the resolved hostname to the `name' buffer,
475  *    or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
476  *    This is synchronous function and will block the calling process.
477  *
478  ***/
479 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
480                                 SilcUInt32 name_len);
481
482 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
483  *
484  * SYNOPSIS
485  *
486  *    void silc_net_gethostbyaddr_async(const char *addr,
487  *                                      SilcSchedule schedule,
488  *                                      SilcNetResolveCallback completion,
489  *                                      void *context)
490  *
491  * DESCRIPTION
492  *
493  *    Asynchronously resolves the hostname for the IP address indicated
494  *    by the `addr'.  This function returns immediately, and the
495  *    `completion' callback will be called after the resolving is
496  *    completed.
497  *
498  ***/
499 void silc_net_gethostbyaddr_async(const char *addr,
500                                   SilcSchedule schedule,
501                                   SilcNetResolveCallback completion,
502                                   void *context);
503
504 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
505  *
506  * SYNOPSIS
507  *
508  *    SilcBool silc_net_check_host_by_sock(int sock, char **hostname,
509  *                                         char **ip);
510  *
511  * DESCRIPTION
512  *
513  *    Performs lookups for remote name and IP address. This peforms reverse
514  *    lookup as well to verify that the IP has FQDN.
515  *
516  ***/
517 SilcBool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
518
519 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
520  *
521  * SYNOPSIS
522  *
523  *    SilcBool silc_net_check_local_by_sock(int sock, char **hostname,
524  *                                          char **ip);
525  *
526  * DESCRIPTION
527  *
528  *    Performs lookups for local name and IP address. This peforms reverse
529  *    lookup as well to verify that the IP has FQDN.
530  *
531  ***/
532 SilcBool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
533
534 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
535  *
536  * SYNOPSIS
537  *
538  *    SilcUInt16 silc_net_get_remote_port(int sock);
539  *
540  * DESCRIPTION
541  *
542  *    Return remote port by socket.
543  *
544  ***/
545 SilcUInt16 silc_net_get_remote_port(int sock);
546
547 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
548  *
549  * SYNOPSIS
550  *
551  *    SilcUInt16 silc_net_get_local_port(int sock);
552  *
553  * DESCRIPTION
554  *
555  *    Return local port by socket.
556  *
557  ***/
558 SilcUInt16 silc_net_get_local_port(int sock);
559
560 /****f* silcutil/SilcNetAPI/silc_net_localhost
561  *
562  * SYNOPSIS
563  *
564  *    char *silc_net_localhost(void);
565  *
566  * DESCRIPTION
567  *
568  *    Return name of localhost.  This will also attempt to resolve
569  *    the real hostname by the local host's IP address.  If unsuccessful
570  *    the first found hostname is returned.  The caller must free
571  *    returned hostname.
572  *
573  ***/
574 char *silc_net_localhost(void);
575
576 /****f* silcutil/SilcNetAPI/silc_net_localip
577  *
578  * SYNOPSIS
579  *
580  *    char *silc_net_localip(void)
581  *
582  * DESCRIPTION
583  *
584  *    Return IP of localhost.  The caller must free the returned IP.
585  *
586  ***/
587 char *silc_net_localip(void);
588
589 /****f* silcutil/SilcNetAPI/silc_net_win32_init
590  *
591  * SYNOPSIS
592  *
593  *    SilcBool silc_net_win32_init(void);
594  *
595  * DESCRIPTION
596  *
597  *    This is WIN32 system specific function and is used to initialize
598  *    the network.  This must be called by all WIN32 applications.  It
599  *    is usually called at the application's main() or WinMain() before
600  *    calling any other SILC routine.  The application must also call
601  *    the silc_net_win32_uninit when exiting the application.  Returns
602  *    FALSE on error.  The network will not work if this function returns
603  *    FALSE.
604  *
605  * NOTES
606  *
607  *    This routines is available only on Win32 platform.
608  *
609  ***/
610 SilcBool silc_net_win32_init(void);
611
612 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
613  *
614  * SYNOPSIS
615  *
616  *    void silc_net_win32_init(void);
617  *
618  * DESCRIPTION
619  *
620  *    This is WIN32 system specific function and is used to uninitialize
621  *    the network.  This must be called by all WIN32 applications.  It
622  *    is usually called when the application is exiting.  After calling
623  *    this function the SILC Net API routines will not work anymore.
624  *
625  * NOTES
626  *
627  *    This routines is available only on Win32 platform.
628  *
629  ***/
630 void silc_net_win32_uninit(void);
631
632 #include "silcnet_i.h"
633
634 #endif /* SILCNET_H */