update
[silc.git] / lib / silccore / silcsockconn.c
1 /*
2
3   silcsockconn.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Allocates a new socket connection object. The allocated object is 
25    returned to the new_socket argument. */
26
27 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data, 
28                        SilcSocketConnection *new_socket)
29 {
30   SILC_LOG_DEBUG(("Allocating new socket connection object"));
31
32   /* Set the pointers. Incoming and outgoing data buffers
33      are allocated by the application when they are first used. */
34   *new_socket = silc_calloc(1, sizeof(**new_socket));
35   (*new_socket)->sock = sock;
36   (*new_socket)->type = type;
37   (*new_socket)->user_data = user_data;
38   (*new_socket)->protocol = NULL;
39   (*new_socket)->flags = 0;
40   (*new_socket)->inbuf = NULL;
41   (*new_socket)->outbuf = NULL;
42 }
43
44 /* Free's the Socket connection object. */
45
46 void silc_socket_free(SilcSocketConnection sock)
47 {
48   if (sock) {
49     silc_buffer_free(sock->inbuf);
50     silc_buffer_free(sock->outbuf);
51     if (sock->hb) {
52       silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
53       silc_free(sock->hb->hb_context);
54       silc_free(sock->hb);
55     }
56
57     memset(sock, 'F', sizeof(*sock));
58     silc_free(sock);
59   }
60 }
61
62 /* Internal timeout callback to perform heartbeat */
63
64 SILC_TASK_CALLBACK(silc_socket_heartbeat)
65 {
66   SilcSocketConnectionHB hb = (SilcSocketConnectionHB)context;
67
68   if (!hb->heartbeat)
69     return;
70
71   if (hb->hb_callback)
72     hb->hb_callback(hb->sock, hb->hb_context);
73
74   hb->hb_task = silc_task_register(hb->timeout_queue, hb->sock->sock, 
75                                    silc_socket_heartbeat,
76                                    context, hb->heartbeat, 0,
77                                    SILC_TASK_TIMEOUT,
78                                    SILC_TASK_PRI_LOW);
79 }
80
81 /* Sets the heartbeat timeout and prepares the socket for performing
82    heartbeat in `heartbeat' intervals (seconds). The `hb_context' is
83    allocated by the application and will be sent as argument to the
84    `hb_callback' function that is called when the `heartbeat' timeout
85    expires.  The callback `hb_context' won't be touched by the library
86    but will be freed automatically when calling silc_socket_free.  The
87    `timeout_queue' is the application's scheduler timeout queue. */
88
89 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
90                                unsigned long heartbeat,
91                                void *hb_context,
92                                SilcSocketConnectionHBCb hb_callback,
93                                void *timeout_queue)
94 {
95   SilcSocketConnectionHB hb = silc_calloc(1, sizeof(*hb));
96
97   hb->heartbeat = heartbeat;
98   hb->hb_context = hb_context;
99   hb->hb_callback = hb_callback;
100   hb->timeout_queue = timeout_queue;
101   hb->sock = sock;
102   hb->hb_task = silc_task_register(timeout_queue, sock->sock, 
103                                    silc_socket_heartbeat,
104                                    (void *)hb, heartbeat, 0,
105                                    SILC_TASK_TIMEOUT,
106                                    SILC_TASK_PRI_LOW);
107 }