5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
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.
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.
22 #include "silcincludes.h"
24 /* Allocates a new socket connection object. The allocated object is
25 returned to the new_socket argument. */
27 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
28 SilcSocketConnection *new_socket)
30 SILC_LOG_DEBUG(("Allocating new socket connection object"));
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++;
45 /* Free's the Socket connection object. */
47 void silc_socket_free(SilcSocketConnection sock)
50 SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users + 1,
52 if (sock->users < 1) {
53 silc_buffer_free(sock->inbuf);
54 silc_buffer_free(sock->outbuf);
56 silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
57 silc_free(sock->hb->hb_context);
61 memset(sock, 'F', sizeof(*sock));
66 /* Increase the reference counter. */
68 SilcSocketConnection silc_socket_dup(SilcSocketConnection sock)
71 SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users - 1,
76 /* Internal timeout callback to perform heartbeat */
78 SILC_TASK_CALLBACK(silc_socket_heartbeat)
80 SilcSocketConnectionHB hb = (SilcSocketConnectionHB)context;
86 hb->hb_callback(hb->sock, hb->hb_context);
88 hb->hb_task = silc_task_register(hb->timeout_queue, hb->sock->sock,
89 silc_socket_heartbeat,
90 context, hb->heartbeat, 0,
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. */
103 void silc_socket_set_heartbeat(SilcSocketConnection sock,
106 SilcSocketConnectionHBCb hb_callback,
111 silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
112 silc_free(sock->hb->hb_context);
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,