File: net_io.c

package info (click to toggle)
c-icap 1:0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,600 kB
  • sloc: ansic: 27,220; sh: 4,415; makefile: 242; perl: 95; awk: 10
file content (398 lines) | stat: -rw-r--r-- 11,431 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 *  Copyright (C) 2004-2008 Christos Tsantilas
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA.
 */

#include "common.h"
#include "c-icap.h"
#include <errno.h>
#include "net_io.h"
#include "debug.h"
#include "net_io.h"
#include "util.h"
#ifdef USE_OPENSSL
#include "net_io_ssl.h"
#endif

#include <assert.h>


#ifdef USE_IPV6
void ci_fill_sockaddr(ci_sockaddr_t * addr)
{
    addr->ci_sin_family = addr->sockaddr.ss_family;
    if (addr->ci_sin_family == AF_INET6) {
        addr->ci_sin_port =
            ((struct sockaddr_in6 *) &(addr->sockaddr))->sin6_port;
        addr->ci_sin_addr =
            &(((struct sockaddr_in6 *) &(addr->sockaddr))->sin6_addr);
        addr->ci_inaddr_len = sizeof(struct in6_addr);
    } else {
        addr->ci_sin_port =
            ((struct sockaddr_in *) &(addr->sockaddr))->sin_port;
        addr->ci_sin_addr =
            &(((struct sockaddr_in *) &(addr->sockaddr))->sin_addr);
        addr->ci_inaddr_len = sizeof(struct in_addr);
    }
}

void ci_copy_sockaddr(ci_sockaddr_t * dest, ci_sockaddr_t * src)
{
    memcpy(dest, src, sizeof(ci_sockaddr_t));
    if (dest->ci_sin_family == AF_INET6)
        dest->ci_sin_addr =
            &(((struct sockaddr_in6 *) &(dest->sockaddr))->sin6_addr);
    else
        dest->ci_sin_addr =
            &(((struct sockaddr_in *) &(dest->sockaddr))->sin_addr);
}

#else
void ci_fill_sockaddr(ci_sockaddr_t * addr)
{
    addr->ci_sin_family = addr->sockaddr.sin_family;
    addr->ci_sin_port = addr->sockaddr.sin_port;
    addr->ci_sin_addr = &(addr->sockaddr.sin_addr);
    addr->ci_inaddr_len = sizeof(struct in_addr);
}

void ci_copy_sockaddr(ci_sockaddr_t * dest, ci_sockaddr_t * src)
{
    memcpy(dest, src, sizeof(ci_sockaddr_t));
    dest->ci_sin_addr = &(dest->sockaddr.sin_addr);
}
#endif

void ci_fill_ip_t(ci_ip_t *ip_dest, ci_sockaddr_t *src)
{
    ip_dest->family = src->ci_sin_family;
    memcpy(&ip_dest->address,src->ci_sin_addr,sizeof(ci_in_addr_t));

#ifdef USE_IPV6
    if (ip_dest->family == AF_INET6)
        ci_ipv6_inaddr_hostnetmask(ip_dest->netmask);
    else
        ci_ipv4_inaddr_hostnetmask(ip_dest->netmask);
#else
    ci_ipv4_inaddr_hostnetmask(ip_dest->netmask);
#endif
}

#ifdef USE_IPV6

void ci_sockaddr_set_port(ci_sockaddr_t * addr, int port)
{
    if (addr->sockaddr.ss_family == AF_INET) {
        ((struct sockaddr_in *) &(addr->sockaddr))->sin_port = htons(port);
        addr->ci_sin_port =
            ((struct sockaddr_in6 *) &(addr->sockaddr))->sin6_port;
    } else {
        ((struct sockaddr_in6 *) &(addr->sockaddr))->sin6_port = htons(port);
        addr->ci_sin_port =
            ((struct sockaddr_in *) &(addr->sockaddr))->sin_port;
    }
}

#else

void ci_sockaddr_set_port(ci_sockaddr_t * addr, int port)
{
    addr->sockaddr.sin_port = htons(port);
    addr->ci_sin_port = addr->sockaddr.sin_port;
}
#endif



const char *ci_sockaddr_t_to_ip(ci_sockaddr_t * addr, char *ip, int maxlen)
{
    return ci_inet_ntoa(addr->ci_sin_family, addr->ci_sin_addr, ip, maxlen);
}


/*
  Needed check in configure.in for inet_pton and inet_ntop ?
  For Linux and Solaris exists.
  But I did not found these functions in win32 for example .
*/

int ci_inet_aton(int af, const char *cp, void *addr)
{
#ifdef USE_IPV6
    return inet_pton(af, cp, addr);
#else
#ifdef HAVE_INET_ATON
    return inet_aton(cp, (struct in_addr *) addr);
#else
    ((struct in_addr *) addr)->s_addr = inet_addr(cp);
    if (((struct in_addr *) addr)->s_addr == 0xffffffff
            && strcmp(cp, "255.255.255.255") != 0)
        return 0;             /*0xffffffff =255.255.255.255 which is a valid address */
    return 1;
#endif
#endif                          /*USE_IPV6 */
}



const char *ci_inet_ntoa(int af, const void *src, char *dst, int cnt)
{
#ifdef USE_IPV6
    return inet_ntop(af, src, dst, cnt);
#else
    unsigned char *addr_bytes;
    addr_bytes = (unsigned char *) src;
    snprintf(dst, cnt, "%d.%d.%d.%d", addr_bytes[0], addr_bytes[1],
             addr_bytes[2], addr_bytes[3]);
    dst[cnt - 1] = '\0';
    return (const char *) dst;
#endif
}


void ci_copy_connection(ci_connection_t * dest, ci_connection_t * src)
{
    dest->fd = src->fd;
    ci_copy_sockaddr(&dest->claddr, &src->claddr);
    ci_copy_sockaddr(&dest->srvaddr, &src->srvaddr);
#if defined(USE_OPENSSL)
    dest->bio = src->bio;
#endif
    dest->flags = src->flags;
}

void ci_connection_reset(ci_connection_t *conn)
{
    conn->fd = -1;
#if defined(USE_OPENSSL)
    conn->bio = NULL;
#endif
    conn->flags = 0;
}

int ci_host_to_sockaddr_t(const char *servername, ci_sockaddr_t * addr, int proto)
{
    int ret = 0;
    struct addrinfo hints, *res;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = proto;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    if ((ret = getaddrinfo(servername, NULL, &hints, &res)) != 0) {
        ci_debug_printf(5, "Error getting addrinfo for '%s':%s\n", servername, gai_strerror(ret));
        return 0;
    }
    //fill the addr..... and
    memcpy(&(addr->sockaddr), res->ai_addr, CI_SOCKADDR_SIZE);
    freeaddrinfo(res);
    ci_fill_sockaddr(addr);
    return 1;
}

ci_connection_t *ci_connection_create()
{
    ci_connection_t *connection = malloc(sizeof(ci_connection_t));
    if (connection)
        ci_connection_reset(connection);
    return connection;
}

/**
 * Close and free the given connection
 */
void ci_connection_destroy(ci_connection_t *connection)
{
    if ( connection ) {
        if (connection->fd >= 0)
            ci_connection_hard_close(connection);
        free(connection);
    }
}

int ci_connect_to_nonblock(ci_connection_t *connection, const char *servername, int port, int proto)
{
    unsigned int addrlen = 0;
    char errBuf[512];

    if (!connection)
        return -1;

    if (connection->fd < 0) {
        if (!ci_host_to_sockaddr_t(servername, &(connection->srvaddr), proto)) {
            ci_debug_printf(1, "Error getting address info for host '%s'\n", servername);
            return -1;
        }
        ci_sockaddr_set_port(&(connection->srvaddr), port);

        connection->fd = socket(connection->srvaddr.ci_sin_family, SOCK_STREAM, 0);
        if (connection->fd == -1) {
            ci_debug_printf(1, "Error opening socket :%d:%s....\n",
                            errno,
                            ci_strerror(errno,  errBuf, sizeof(errBuf)));
            return -1;
        }

#ifdef USE_IPV6
        if (connection->srvaddr.ci_sin_family == AF_INET6)
            addrlen = sizeof(struct sockaddr_in6);
        else
#endif
            addrlen = sizeof(struct sockaddr_in);

        // Sets the fd to non-block mode
        ci_connection_set_nonblock(connection);

        int ret;
        ret = connect(connection->fd, (struct sockaddr *) &(connection->srvaddr.sockaddr), addrlen);
        if (ret < 0 && errno != EINPROGRESS) {
            ci_debug_printf(1, "Error connecting to host  '%s': %s \n",
                            servername,
                            ci_strerror(errno,  errBuf, sizeof(errBuf)));
            close(connection->fd);
            connection->fd = -1;
            return -1;
        }

        return 0;
    }

    if (!(connection->flags & CI_CONNECTION_CONNECTED)) {
        int errcode = 0;
        socklen_t len = sizeof(errcode);
        if (getsockopt(connection->fd, SOL_SOCKET, SO_ERROR, &errcode, &len) != 0)
            errcode = errno;

        if (errcode) {
            ci_debug_printf(1, "Error while connecting to host '%s': %s\n",
                            servername,
                            ci_strerror(errcode,  errBuf, sizeof(errBuf)));
            return -1;
        }

        if (!ci_connection_init(connection, ci_connection_client_side)) {
            ci_debug_printf(1, "Error initializing connection to '%s': %s\n",
                            servername,
                            ci_strerror(errno,  errBuf, sizeof(errBuf)));
            return -1;
        }
        connection->flags &= CI_CONNECTION_CONNECTED;
    }

    return 1;
}

ci_connection_t *ci_connect_to(const char *servername, int port, int proto, int timeout)
{
    int ret;
    ci_connection_t *connection = ci_connection_create();
    if (!connection) {
        ci_debug_printf(1, "Failed to allocate memory for ci_connection_t object\n");
        return NULL;
    }

    ret = ci_connect_to_nonblock(connection, servername, port, proto);
    if (ret < 0)
        return NULL;

    do {
        ret = ci_wait_for_data(connection->fd, timeout, ci_wait_for_write);
    } while (ret > 0 && (ret & ci_wait_should_retry)); //while iterrupted by signal

    if (ret > 0)
        ret = ci_connect_to_nonblock(connection, servername, port, proto);

    if (ret <= 0) {
        ci_debug_printf(1, "Connection to '%s:%d' failed/timedout\n",
                        servername, port);
        ci_connection_destroy(connection);
        return NULL;
    }

    return connection;
}

int ci_connection_wait(ci_connection_t *conn, int secs, int what_wait)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_wait_tls(conn, secs, what_wait);
#endif
    return ci_wait_for_data(conn->fd, secs, what_wait);
}

int ci_connection_read(ci_connection_t *conn, void *buf, size_t count, int timeout)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_read_tls(conn, buf, count, timeout);
#endif
    return ci_read(conn->fd, buf, count, timeout);
}

int ci_connection_write(ci_connection_t *conn, void *buf, size_t count, int timeout)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_write_tls(conn, buf, count, timeout);
#endif
    return ci_write(conn->fd, buf, count, timeout);
}

int ci_connection_read_nonblock(ci_connection_t *conn, void *buf, size_t count)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_read_nonblock_tls(conn, buf, count);
#endif
    return ci_read_nonblock(conn->fd, buf, count);
}

int ci_connection_write_nonblock(ci_connection_t *conn, void *buf, size_t count)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_write_nonblock_tls(conn, buf, count);
#endif
    return ci_write_nonblock(conn->fd, buf, count);
}

int ci_connection_linger_close(ci_connection_t *conn, int timeout)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_linger_close_tls(conn, timeout);
#endif
    return ci_linger_close(conn->fd, timeout);
}

int ci_connection_hard_close(ci_connection_t *conn)
{
    assert(conn);
#ifdef USE_OPENSSL
    if (conn->bio)
        return ci_connection_hard_close_tls(conn);
#endif
    close(conn->fd);
    conn->fd = -1;
    return 1;
}