89de51aace822bc5f023e9f77a4013172b85100c
[silc.git] / lib / silcutil / silcsockconn.h
1 /*
2  
3   silcsockconn.h
4  
5   Author: Pekka Riikonen <priikone@silnet.org>
6  
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 /****h* silcutil/SilcSocketConnectionAPI
22  *
23  * DESCRIPTION
24  *
25  * Implementation of the Socket Connection object. The SilcSocketConnection
26  * is used by all applications to represent a socket based connection
27  * to the network. The Socket Connection object handles inbound and outbound
28  * data buffers, can perform keepalive actions for the connection and
29  * supports connection based protocols as well.
30  *
31  ***/
32
33 #ifndef SILCSOCKCONN_H
34 #define SILCSOCKCONN_H
35
36 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnection
37  *
38  * NAME
39  * 
40  *    typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
41  *
42  * DESCRIPTION
43  *
44  *    This context is forward declaration for the SilcSocketConnectionStruct.
45  *    This is allocated by the silc_socket_alloc and freed by the
46  *    silc_socket_free function. The silc_socket_dup can be used to
47  *    increase the reference counter of the context. The data is freed
48  *    by the silc_socket_free function only after the reference counter
49  *    hits zero.
50  *
51  ***/
52 typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
53
54 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionHB
55  *
56  * NAME
57  * 
58  *    typedef struct SilcSocketConnectionHB *SilcSocketConnectionHB;
59  *
60  * DESCRIPTION
61  *
62  *    This context is the heartbeat context for the SilcSockeConnection.
63  *    It is meant to hold the keepalive information for the connection.
64  *    This is allocated internally and freed internally by the 
65  *    interface routines.
66  *
67  ***/
68 typedef struct SilcSocketConnectionHBStruct *SilcSocketConnectionHB;
69
70 /****d* silcutil/SilcSocketConnectionAPI/SilcSocketType
71  *
72  * NAME
73  * 
74  *    typedef enum { ... } SilcSocketType;
75  *
76  * DESCRIPTION
77  *
78  *    Socket types. These identifies the socket connection. There
79  *    are four different types; unknown, client, server and router.
80  *    Unknown connections are connections that hasn't advanced long
81  *    enough so that we might know which type of connection it is.
82  *    It is the applications responsibility to update the type 
83  *    information when it becomes available.
84  *
85  * SOURCE
86  */
87 typedef enum {
88   SILC_SOCKET_TYPE_UNKNOWN = 0,
89   SILC_SOCKET_TYPE_CLIENT = 1,
90   SILC_SOCKET_TYPE_SERVER = 2,
91   SILC_SOCKET_TYPE_ROUTER = 3
92 } SilcSocketType;
93 /***/
94
95 /* Socket flags */
96 #define SILC_SF_NONE             0
97 #define SILC_SF_INBUF_PENDING    1
98 #define SILC_SF_OUTBUF_PENDING   2
99 #define SILC_SF_DISCONNECTING    3
100 #define SILC_SF_DISCONNECTED     4
101 #define SILC_SF_HOST_LOOKUP      5
102
103 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionStruct
104  *
105  * NAME
106  * 
107  *    struct SilcSocketConnectionStruct { ... };
108  *
109  * DESCRIPTION
110  *
111  *    This object holds information about the connected sockets to the server.
112  *    This is quite important object since this is referenced by the server all
113  *    the time when figuring out what the connection is supposed to be doing
114  *    and to whom we should send a message. This structure is the structure
115  *    for the SilcSocketConnection forward declaration.
116  *
117  *    Following short description of the fields:
118  *
119  *    int sock
120  *
121  *      The actual connected socket. This is usually saved when accepting
122  *      new connection to the server.
123  *
124  *    SilcSocketType type
125  *
126  *      Type of the socket. This identifies the type of the connection. This
127  *      is mainly used to identify whether the connection is a client or a
128  *      server connection.
129  *
130  *    void *user_data
131  *
132  *      This is a pointer to a data that is is saved here at the same
133  *      time a new connection object is allocated. Usually this is a 
134  *      back-pointer to some important data for fast referencing. For
135  *      SILC server this is a pointer to the ID list and for SILC client
136  *      to object holding active connections (windows).
137  *
138  *    SilcProtocol protocol
139  *
140  *      Protocol object for the socket. Currently only one protocol can be
141  *      executing at a time for a particular socket.
142  *
143  *    uint32 flags
144  *
145  *      Socket flags that indicate the status of the socket. This can
146  *      indicate several different status that can affect the use of the
147  *      socket object.
148  *
149  *    int users
150  *
151  *      Reference counter. When allocated it is set to one (1) and it won't
152  *      be freed until it hits zero (0).
153  *
154  *    char *hostname
155  *    char *ip
156  *    uint16 port
157  *
158  *      Resolved hostname, IP address and port of the connection who owns
159  *      this object.
160  *
161  *    SilcBuffer inbuf
162  *    SilcBuffer outbuf
163  *
164  *      Incoming and outgoing buffers for the particular socket connection.
165  *      Incoming data from the socket is put after decryption in to the
166  *      inbuf buffer and outgoing data after encryption is put to the outbuf
167  *      buffer.
168  *
169  *    SilcSocketConnectionHB hb
170  *
171  *      The heartbeat context.  If NULL, heartbeat is not performed.
172  *
173  ***/
174 struct SilcSocketConnectionStruct {
175   int sock;
176   SilcSocketType type;
177   void *user_data;
178   SilcProtocol protocol;
179   uint32 flags;
180   int users;
181
182   char *hostname;
183   char *ip;
184   uint16 port;
185
186   SilcBuffer inbuf;
187   SilcBuffer outbuf;
188
189   SilcSocketConnectionHB hb;
190 };
191
192 /* Macros */
193
194 /* Amount of bytes to be read from the socket connection at once. */
195 #define SILC_SOCKET_READ_SIZE 16384
196
197 /* Default socket buffer size. */
198 #define SILC_SOCKET_BUF_SIZE 1024
199
200 /* Generic manipulation of flags */
201 #define SF_SET(x, f) (x)->flags |= (1L << (f))
202 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
203 #define SF_IS(x, f) ((x)->flags & (1L << (f)))
204
205 /* Setting/Unsetting flags */
206 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
207 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
208 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
209 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
210 #define SILC_SET_HOST_LOOKUP(x) SF_SET((x), SILC_SF_HOST_LOOKUP)
211 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
212 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
213 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
214 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
215 #define SILC_UNSET_HOST_LOOKUP(x) SF_UNSET((x), SILC_SF_HOST_LOOKUP)
216
217 /* Checking for flags */
218 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
219 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
220 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
221 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
222 #define SILC_IS_HOST_LOOKUP(x) SF_IS((x), SILC_SF_HOST_LOOKUP)
223
224 /* Prototypes */
225
226 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_alloc
227  *
228  * SYNOPSIS
229  *
230  *    void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
231  *                           SilcSocketConnection *new_socket);
232  *
233  * DESCRIPTION
234  *
235  *    Allocates a new socket connection object. The allocated object is 
236  *    returned to the new_socket argument. The `sock' is the socket
237  *    for the connection, the `type' the initial type of the connection and
238  *    the `user_data' a application specific pointer.
239  *
240  ***/
241 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
242                        SilcSocketConnection *new_socket);
243
244 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_free
245  *
246  * SYNOPSIS
247  *
248  *    void silc_socket_free(SilcSocketConnection sock);
249  *
250  * DESCRIPTION
251  *
252  *    Frees the socket connection context. This frees it only if the
253  *    reference counter of the socket is zero, otherwise it decreases the
254  *    reference counter.
255  *
256  ***/
257 void silc_socket_free(SilcSocketConnection sock);
258
259 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_dup
260  *
261  * SYNOPSIS
262  *
263  *    SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
264  *
265  * DESCRIPTION
266  *
267  *    Duplicates the socket context. This actually does not duplicate
268  *    any data, instead this increases the reference counter of the
269  *    context. The reference counter is decreased by calling the
270  *    silc_socket_free function and it frees the data when the counter
271  *    hits zero.
272  *
273  ***/
274 SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
275
276 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_read
277  *
278  * SYNOPSIS
279  *
280  *    int silc_socket_read(SilcSocketConnection sock);
281  *
282  * DESCRIPTION
283  *
284  *    Reads data from the socket connection into the incoming data buffer.
285  *    It reads as much as possible from the socket connection. This returns
286  *    amount of bytes read or -1 on error or -2 on case where all of the
287  *    data could not be read at once. Implementation of this function
288  *    may be platform specific.
289  *
290  ***/
291 int silc_socket_read(SilcSocketConnection sock);
292
293 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_write
294  *
295  * SYNOPSIS
296  *
297  *    int silc_socket_read(SilcSocketConnection sock);
298  *
299  * DESCRIPTION
300  *
301  *    Writes data from the outgoing buffer to the socket connection. If the
302  *    data cannot be written at once, it must be written at later time. 
303  *    The data is written from the data section of the buffer, not from head
304  *    or tail section. This automatically pulls the data section towards end
305  *    after writing the data. Implementation of this function may be
306  *    platform specific.
307  *
308  ***/
309 int silc_socket_write(SilcSocketConnection sock);
310
311 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionHBCb
312  *
313  * SYNOPSIS
314  *
315  *    typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
316  *                                             void *context);
317  *
318  * DESCRIPTION
319  *
320  *    Heartbeat callback function. This is the function in the application
321  *    that this library will call when it is time to send the keepalive
322  *    packet SILC_PACKET_HEARTBEAT.
323  *
324  ***/
325 typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
326                                          void *context);
327
328 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_set_heartbeat
329  *
330  * SYNOPSIS
331  *
332  *    void silc_socket_set_heartbeat(SilcSocketConnection sock, 
333  *                                   uint32 heartbeat,
334  *                                   void *hb_context,
335  *                                   SilcSocketConnectionHBCb hb_callback,
336  *                                   SilcSchedule schedule);
337  *
338  * DESCRIPTION
339  *
340  *    Sets the heartbeat timeout and prepares the socket for performing
341  *    heartbeat in `heartbeat' intervals (seconds). The `hb_context' is
342  *    allocated by the application and will be sent as argument to the
343  *    `hb_callback' function that is called when the `heartbeat' timeout
344  *    expires.  The callback `hb_context' won't be touched by the library
345  *    but will be freed automatically when calling silc_socket_free.  The
346  *    `schedule' is the application's scheduler.
347  *
348  ***/
349 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
350                                uint32 heartbeat,
351                                void *hb_context,
352                                SilcSocketConnectionHBCb hb_callback,
353                                SilcSchedule schedule);
354
355 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketHostLookupCb
356  *
357  * SYNOPSIS
358  *
359  *    typedef void (*SilcSocketHostLookupCb)(SilcSocketConnection sock,
360  *                                           void *context);
361  *
362  * DESCRIPTION
363  *
364  *    Asynchronous host lookup callback function that will be called
365  *    when the lookup is performed.
366  *
367  ***/
368 typedef void (*SilcSocketHostLookupCb)(SilcSocketConnection sock,
369                                        void *context);
370
371 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_host_lookup
372  *
373  * SYNOPSIS
374  *
375  *    void silc_socket_host_lookup(SilcSocketConnection sock,
376  *                                 bool port_lookup,
377  *                                 SilcSocketHostLookupCb callback,
378  *                                 void *context,
379  *                                 SilcSchedule schedule);
380  *
381  * DESCRIPTION
382  *
383  *    Performs asynchronous host name and IP address lookups for the
384  *    specified socket connection. This may be called when the socket
385  *    connection is created and the full IP address and fully qualified
386  *    domain name information is desired. The `callback' with `context'
387  *    will be called after the lookup is performed. The `schedule'
388  *    is the application's scheduler which the lookup routine needs. 
389  *    If the socket connection is freed during the lookup the library
390  *    will automatically cancel the lookup and the `callback' will not be
391  *    called.
392  *
393  *    If `port_lookup' is TRUE then the remote port of the socket 
394  *    connection is resolved. After the information is resolved they
395  *    are accessible using sock->ip and sock->hostname pointers. Note
396  *    that if the both IP and FQDN could not be resolved the sock->hostname
397  *    includes the IP address of the remote host. The resolved port is 
398  *    available in sock->port.
399  *
400  ***/
401 void silc_socket_host_lookup(SilcSocketConnection sock,
402                              bool port_lookup,
403                              SilcSocketHostLookupCb callback,
404                              void *context,
405                              SilcSchedule schedule);
406
407 #endif