1 /*
2 * LASH
3 *
4 * Copyright (C) 2002 Robert Ham <rah@bash.sh>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <string.h>
25 #include <arpa/inet.h>
26 #include <pthread.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <lash/lash.h>
31 #include <lash/internal_headers.h>
32
33 #include "conn.h"
34
35 void
conn_init(conn_t * conn)36 conn_init(conn_t * conn)
37 {
38 static unsigned long id = 1;
39
40 conn->id = id;
41 id++;
42 conn->socket = 0;
43 pthread_mutex_init(&conn->lock, NULL);
44 }
45
46 void
conn_free(conn_t * conn)47 conn_free(conn_t * conn)
48 {
49 if (conn->socket)
50 close(conn->socket);
51 conn->socket = 0;
52 pthread_mutex_destroy(&conn->lock);
53 }
54
55 conn_t *
conn_new()56 conn_new()
57 {
58 conn_t *conn;
59
60 conn = lash_malloc(sizeof(conn_t));
61 conn_init(conn);
62 return conn;
63 }
64
65 void
conn_destroy(conn_t * conn)66 conn_destroy(conn_t * conn)
67 {
68 conn_free(conn);
69 free(conn);
70 }
71
72 void
conn_lock(conn_t * conn)73 conn_lock(conn_t * conn)
74 {
75 pthread_mutex_lock(&conn->lock);
76 }
77
78 void
conn_unlock(conn_t * conn)79 conn_unlock(conn_t * conn)
80 {
81 pthread_mutex_unlock(&conn->lock);
82 }
83
84 const char *
conn_get_str_id(conn_t * conn)85 conn_get_str_id(conn_t * conn)
86 {
87 static char *str_id = NULL;
88 static size_t str_id_len = 0;
89
90 const char *host;
91 const char *port;
92 size_t new_str_id_len;
93
94 host = lash_lookup_peer_name(conn->socket);
95 port = lash_lookup_peer_port(conn->socket);
96
97 new_str_id_len = strlen(host) + 1 + strlen(port) + 1;
98
99 if (new_str_id_len > str_id_len) {
100 str_id_len = new_str_id_len;
101
102 if (!str_id)
103 str_id = lash_malloc(str_id_len);
104 else
105 str_id = lash_realloc(str_id, str_id_len);
106 }
107
108 sprintf(str_id, "%s:%s", host, port);
109
110 return str_id;
111 }
112
113 /*
114 * stuff for pings
115 */
116
117 time_t
conn_get_recv_stamp(conn_t * conn)118 conn_get_recv_stamp(conn_t * conn)
119 {
120 return conn->recv_stamp;
121 }
122
123 int
conn_get_pinged(conn_t * conn)124 conn_get_pinged(conn_t * conn)
125 {
126 return conn->ping_stamp != (time_t) - 1;
127 }
128
129 int
conn_recv_timed_out(conn_t * conn,time_t time)130 conn_recv_timed_out(conn_t * conn, time_t time)
131 {
132 return time - conn->recv_stamp > CONN_TIMEOUT;
133 }
134
135 int
conn_ping_timed_out(conn_t * conn,time_t time)136 conn_ping_timed_out(conn_t * conn, time_t time)
137 {
138 if (conn_get_pinged(conn)) {
139 return time - conn->ping_stamp > CONN_TIMEOUT;
140 } else
141 return 0;
142 }
143
144 void
conn_set_recv_stamp(conn_t * conn)145 conn_set_recv_stamp(conn_t * conn)
146 {
147 conn->ping_stamp = (time_t) - 1;
148 conn->recv_stamp = time(NULL);
149 if (conn->recv_stamp == (time_t) - 1) {
150 fprintf(stderr,
151 "%s: could not get time to set recieve stamp for connection '%ld', aborting!: %s\n",
152 __FUNCTION__, conn->id, strerror(errno));
153 abort();
154 }
155 }
156
157 void
conn_set_ping_stamp(conn_t * conn)158 conn_set_ping_stamp(conn_t * conn)
159 {
160 conn->ping_stamp = time(NULL);
161 if (conn->ping_stamp == (time_t) - 1) {
162 fprintf(stderr,
163 "%s: could not get time to set ping transmission stamp for connection '%ld', aborting!: %s\n",
164 __FUNCTION__, conn->id, strerror(errno));
165 abort();
166 }
167 }
168
169 /* EOF */
170