updates.
[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 server 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). */
83
84 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
85                                unsigned long heartbeat,
86                                void *hb_context,
87                                SilcSocketConnectionHBCb hb_callback,
88                                void *timeout_queue)
89 {
90   SilcSocketConnectionHB hb = silc_calloc(1, sizeof(*hb));
91
92   hb->heartbeat = heartbeat;
93   hb->hb_context = hb_context;
94   hb->hb_callback = hb_callback;
95   hb->timeout_queue = timeout_queue;
96   hb->sock = sock;
97   hb->hb_task = silc_task_register(timeout_queue, sock->sock, 
98                                    silc_socket_heartbeat,
99                                    (void *)hb, heartbeat, 0,
100                                    SILC_TASK_TIMEOUT,
101                                    SILC_TASK_PRI_LOW);
102 }