updates. New data types.
[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   (*new_socket)->users++;
43 }
44
45 /* Free's the Socket connection object. */
46
47 void silc_socket_free(SilcSocketConnection sock)
48 {
49   sock->users--;
50   SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users + 1,
51                   sock->users));
52   if (sock->users < 1) {
53     silc_buffer_free(sock->inbuf);
54     silc_buffer_free(sock->outbuf);
55     if (sock->hb) {
56       silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
57       silc_free(sock->hb->hb_context);
58       silc_free(sock->hb);
59     }
60
61     memset(sock, 'F', sizeof(*sock));
62     silc_free(sock);
63   }
64 }
65
66 /* Increase the reference counter. */
67
68 SilcSocketConnection silc_socket_dup(SilcSocketConnection sock)
69 {
70   sock->users++;
71   SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users - 1,
72                   sock->users));
73   return sock;
74 }
75
76 /* Internal timeout callback to perform heartbeat */
77
78 SILC_TASK_CALLBACK(silc_socket_heartbeat)
79 {
80   SilcSocketConnectionHB hb = (SilcSocketConnectionHB)context;
81
82   if (!hb->heartbeat)
83     return;
84
85   if (hb->hb_callback)
86     hb->hb_callback(hb->sock, hb->hb_context);
87
88   hb->hb_task = silc_task_register(hb->timeout_queue, hb->sock->sock, 
89                                    silc_socket_heartbeat,
90                                    context, hb->heartbeat, 0,
91                                    SILC_TASK_TIMEOUT,
92                                    SILC_TASK_PRI_LOW);
93 }
94
95 /* Sets the heartbeat timeout and prepares the socket for performing
96    heartbeat in `heartbeat' intervals (seconds). The `hb_context' is
97    allocated by the application and will be sent as argument to the
98    `hb_callback' function that is called when the `heartbeat' timeout
99    expires.  The callback `hb_context' won't be touched by the library
100    but will be freed automatically when calling silc_socket_free.  The
101    `timeout_queue' is the application's scheduler timeout queue. */
102
103 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
104                                uint32 heartbeat,
105                                void *hb_context,
106                                SilcSocketConnectionHBCb hb_callback,
107                                void *timeout_queue)
108 {
109
110   if (sock->hb) {
111     silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
112     silc_free(sock->hb->hb_context);
113     silc_free(sock->hb);
114   }
115
116   sock->hb = silc_calloc(1, sizeof(*sock->hb));
117   sock->hb->heartbeat = heartbeat;
118   sock->hb->hb_context = hb_context;
119   sock->hb->hb_callback = hb_callback;
120   sock->hb->timeout_queue = timeout_queue;
121   sock->hb->sock = sock;
122   sock->hb->hb_task = silc_task_register(timeout_queue, sock->sock,
123                                          silc_socket_heartbeat,
124                                          (void *)sock->hb, heartbeat, 0,
125                                          SILC_TASK_TIMEOUT,
126                                          SILC_TASK_PRI_LOW);
127 }