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 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;
44 /* Free's the Socket connection object. */
46 void silc_socket_free(SilcSocketConnection sock)
49 silc_buffer_free(sock->inbuf);
50 silc_buffer_free(sock->outbuf);
52 silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
53 silc_free(sock->hb->hb_context);
57 memset(sock, 'F', sizeof(*sock));
62 /* Internal timeout callback to perform heartbeat */
64 SILC_TASK_CALLBACK(silc_socket_heartbeat)
66 SilcSocketConnectionHB hb = (SilcSocketConnectionHB)context;
72 hb->hb_callback(hb->sock, hb->hb_context);
74 hb->hb_task = silc_task_register(hb->timeout_queue, hb->sock->sock,
75 silc_socket_heartbeat,
76 context, hb->heartbeat, 0,
81 /* Sets the heartbeat timeout and prepares the socket for performing
82 heartbeat in `heartbeat' intervals (seconds). */
84 void silc_socket_set_heartbeat(SilcSocketConnection sock,
85 unsigned long heartbeat,
87 SilcSocketConnectionHBCb hb_callback,
90 SilcSocketConnectionHB hb = silc_calloc(1, sizeof(*hb));
92 hb->heartbeat = heartbeat;
93 hb->hb_context = hb_context;
94 hb->hb_callback = hb_callback;
95 hb->timeout_queue = timeout_queue;
97 hb->hb_task = silc_task_register(timeout_queue, sock->sock,
98 silc_socket_heartbeat,
99 (void *)hb, heartbeat, 0,