updates.
[silc.git] / lib / silcutil / silcnet.h
1 /****h* silcutil/silcnet.h
2  *
3  * NAME
4  *
5  * silcnet.h
6  *
7  * COPYRIGHT
8  *
9  * Author: Pekka Riikonen <priikone@silcnet.org>
10  *
11  * Copyright (C) 1997 - 2001 Pekka Riikonen
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  */
24
25 #ifndef SILCNET_H
26 #define SILCNET_H
27
28 /* Prototypes */
29
30 /****f* silcutil/SilcNetAPI/silc_net_create_server
31  *
32  * SYNOPSIS
33  *
34  *    int silc_net_create_server(int port, char *ip_addr);
35  *
36  * DESCRIPTION
37  *
38  *    This function creates server or daemon or listener or what ever. This
39  *    does not fork a new process, it must be done by the caller if caller
40  *    wants to create a child process. This is used by the SILC server. 
41  *    If argument `ip_addr' is NULL `any' address will be used. Returns 
42  *    the created socket or -1 on error.
43  *
44  ***/
45 int silc_net_create_server(int port, char *ip_addr);
46
47 /****f* silcutil/SilcNetAPI/silc_net_close_server
48  *
49  * SYNOPSIS
50  *
51  *    void silc_net_close_server(int sock);
52  *
53  * DESCRIPTION
54  *
55  *    Closes the server by closing the socket connection.
56  *
57  ***/
58 void silc_net_close_server(int sock);
59
60 /****f* silcutil/SilcNetAPI/silc_net_create_connection
61  *
62  * SYNOPSIS
63  *
64  *    int silc_net_create_connection(int port, char *host);
65  *
66  * DESCRIPTION
67  *
68  *    Creates a connection (TCP/IP) to a remote host. Returns the connection
69  *    socket or -1 on error. This blocks the process while trying to create
70  *    the connection.
71  *
72  ***/
73 int silc_net_create_connection(int port, char *host);
74
75 /****f* silcutil/SilcNetAPI/silc_net_create_connection_async
76  *
77  * SYNOPSIS
78  *
79  *    int silc_net_create_connection_async(int port, char *host);
80  *
81  * DESCRIPTION
82  *
83  *    Creates a connection (TCP/IP) to a remote host. Returns the connection
84  *    socket or -1 on error. This creates non-blocking socket hence the
85  *    connection returns directly. To get the result of the connect() one
86  *    must select() the socket and read the result after it's ready.
87  *
88  ***/
89 int silc_net_create_connection_async(int port, char *host);
90
91 /****f* silcutil/SilcNetAPI/silc_net_close_connection
92  *
93  * SYNOPSIS
94  *
95  *    void silc_net_close_connection(int sock);
96  *
97  * DESCRIPTION
98  *
99  *    Closes the connection by closing the socket connection.
100  *
101  ***/
102 void silc_net_close_connection(int sock);
103
104 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
105  *
106  * SYNOPSIS
107  *
108  *    int silc_net_accept_connection(int sock);
109  *
110  * DESCRIPTION
111  *
112  *    Accepts a connection from a particular socket.
113  *
114  ***/
115 int silc_net_accept_connection(int sock);
116
117 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
118  *
119  * SYNOPSIS
120  *
121  *    int silc_net_set_socket_nonblock(int sock);
122  *
123  * DESCRIPTION
124  *
125  *    Sets the socket to non-blocking mode.
126  *
127  ***/
128 int silc_net_set_socket_nonblock(int sock);
129
130 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
131  *
132  * SYNOPSIS
133  *
134  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
135  *
136  * DESCRIPTION
137  *
138  *    Sets a option for a socket.  This function can be used to set
139  *    various options for the socket.  Some of the options might be
140  *    system specific.
141  *
142  ***/
143 int silc_net_set_socket_opt(int sock, int level, int option, int on);
144
145 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
146  *
147  * SYNOPSIS
148  *
149  *    int silc_net_get_socket_opt(int sock, int level, int option, 
150  *                                void *optval, int *opt_len);
151  *
152  * DESCRIPTION
153  *
154  *    Return socket options to the `optval' and `opt_len'.
155  *
156  ***/
157 int silc_net_get_socket_opt(int sock, int level, int option, 
158                             void *optval, int *opt_len);
159
160 /****f* silcutil/SilcNetAPI/silc_net_is_ip
161  *
162  * SYNOPSIS
163  *
164  *    bool silc_net_is_ip(const char *addr);
165  *
166  * DESCRIPTION
167  *
168  *    Checks whether IP address sent as argument is valid IP address.
169  *
170  ***/
171 bool silc_net_is_ip(const char *addr);
172
173 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
174  *
175  * SYNOPSIS
176  *
177  *    bool silc_net_addr2bin(const char *addr, unsigned char *bin,
178  *                           uint32 bin_len);
179  *
180  * DESCRIPTION
181  *
182  *    Converts the IP number string from numbers-and-dots notation to
183  *    binary form.
184  *
185  ***/
186 bool silc_net_addr2bin(const char *addr, unsigned char *bin,
187                        uint32 bin_len);
188
189 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
190  *
191  * SYNOPSIS
192  *
193  *    bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
194  *
195  * DESCRIPTION
196  *
197  *    Performs lookups for remote name and IP address. This peforms reverse
198  *    lookup as well to verify that the IP has FQDN.
199  *
200  ***/
201 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
202
203 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
204  *
205  * SYNOPSIS
206  *
207  *    bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
208  *
209  * DESCRIPTION
210  *
211  *    Performs lookups for local name and IP address. This peforms reverse
212  *    lookup as well to verify that the IP has FQDN.
213  *
214  ***/
215 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
216
217 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
218  *
219  * SYNOPSIS
220  *
221  *    uint16 silc_net_get_remote_port(int sock);
222  *
223  * DESCRIPTION
224  *
225  *    Return remote port by socket.
226  *
227  ***/
228 uint16 silc_net_get_remote_port(int sock);
229
230 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
231  *
232  * SYNOPSIS
233  *
234  *    uint16 silc_net_get_local_port(int sock);
235  *
236  * DESCRIPTION
237  *
238  *    Return local port by socket.
239  *
240  ***/
241 uint16 silc_net_get_local_port(int sock);
242
243 /****f* silcutil/SilcNetAPI/silc_net_localhost
244  *
245  * SYNOPSIS
246  *
247  *    char *silc_net_localhost();
248  *
249  * DESCRIPTION
250  *
251  *    Return name of localhost.  This will also attempt to resolve
252  *    the real hostname by the local host's IP address.  If unsuccessful
253  *    the first found hostname is returned.
254  *
255  ***/
256 char *silc_net_localhost();
257
258 #ifdef WIN32
259
260 /****f* silcutil/SilcNetAPI/silc_net_win32_init
261  *
262  * SYNOPSIS
263  *
264  *    bool silc_net_win32_init(void);
265  *
266  * DESCRIPTION
267  *
268  *    This is WIN32 system specific function and is used to initialize
269  *    the network.  This must be called by all WIN32 applications.  It
270  *    is usually called at the application's main() or WinMain() before
271  *    calling any other SILC routine.  The application must also call
272  *    the silc_net_win32_uninit when exiting the application.  Returns
273  *    FALSE on error.  The network will not work if this function returns
274  *    FALSE.
275  *
276  ***/
277 bool silc_net_win32_init(void);
278
279 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
280  *
281  * SYNOPSIS
282  *
283  *    void silc_net_win32_init(void);
284  *
285  * DESCRIPTION
286  *
287  *    This is WIN32 system specific function and is used to uninitialize
288  *    the network.  This must be called by all WIN32 applications.  It
289  *    is usually called when the application is exiting.  After calling
290  *    this function the SILC Net API routines will not work anymore.
291  *
292  ***/
293 void silc_net_win32_init(void);
294
295 #endif
296
297 #endif