File: cddb_net.c

package info (click to toggle)
libcddb 1.3.2-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 2,276 kB
  • sloc: sh: 10,946; ansic: 4,721; makefile: 44
file content (341 lines) | stat: -rw-r--r-- 8,602 bytes parent folder | download | duplicates (5)
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
/*
    $Id: cddb_net.c,v 1.19 2009/03/01 03:28:07 jcaratzas Exp $

    Copyright (C) 2003, 2004, 2005 Kris Verbeeck <airborne@advalvas.be>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA  02111-1307, USA.
*/

#include "cddb/cddb_ni.h"

#include <errno.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include <setjmp.h>
#include <signal.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_TIME_H
#include <time.h>
#endif
#if defined(HAVE_SYS_TIME_H) && defined(TIME_WITH_SYS_TIME)
#include <sys/time.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif


/* Utility functions */


/**
 * Checks whether bytes can be read/written from/to the socket within
 * the specified time out period.
 *
 * @param sock     The socket to read from.
 * @param timeout  Number of seconds after which to time out.
 * @param to_write TRUE if we have to check for writing, FALSE for
 *                 reading.
 * @return TRUE if reading/writing is possible, FALSE otherwise.
 */
static int sock_ready(int sock, int timeout, int to_write)
{
    fd_set fds;
    struct timeval tv;
    int rv;

    //cddb_log_debug("sock_ready()");
    /* set up select time out */
    tv.tv_sec = timeout;
    tv.tv_usec = 0;
    /* set up file descriptor set */
    FD_ZERO(&fds);
    FD_SET(sock, &fds);
    /* wait for data to become available */
    if (to_write) {
        rv = select(sock + 1, NULL, &fds, NULL, &tv) ;
    } else {
        rv = select(sock + 1, &fds, NULL, NULL, &tv) ;
    }
    if (rv <= 0) {
        if (rv == 0) {
            errno = ETIMEDOUT;
        }
        return FALSE;
    }
    return TRUE;
}
#define sock_can_read(s,t) sock_ready(s, t, FALSE)
#define sock_can_write(s,t) sock_ready(s, t, TRUE)


/* Socket-based work-alikes */


char *sock_fgets(char *s, int size, cddb_conn_t *c)
{
    int rv;
    time_t now, end, timeout;
    char *p = s;

    cddb_log_debug("sock_fgets()");
    timeout = c->timeout;
    end = time(NULL) + timeout;
    size--;                      /* save one for terminating null */
    while (size) {
        now = time(NULL);
        timeout = end - now;
        if (timeout <= 0) {
            errno = ETIMEDOUT;
            return NULL;        /* time out */
        }
        /* can we read from the socket? */
        if (!sock_can_read(c->socket, timeout)) {
            /* error or time out */
            return NULL;
        }
        /* read one byte */
        rv = recv(c->socket, p, 1, 0);
        if (rv == -1) {
            /* recv() error */
            return NULL;
        } else if (rv == 0) {
            /* EOS reached */
            break;
        } else if (*p == CHR_LF) {
            /* EOL reached, stop reading */
            p++;
            break;
        }
        p++;
        size--;
    }
    if (p == s) {
        cddb_log_debug("...read = Empty");
        return NULL;
    }
    *p = CHR_EOS;
    cddb_log_debug("...read = '%s'", s);
    return s;
}

size_t sock_fwrite(const void *ptr, size_t size, size_t nmemb, cddb_conn_t *c)
{
    size_t total_size, to_send;
    time_t now, end, timeout;
    int rv;
    const char *p = (const char *)ptr;

    cddb_log_debug("sock_fwrite()");
    total_size = size * nmemb;
    to_send = total_size;
    timeout = c->timeout;
    end = time(NULL) + timeout;
    while (to_send) {
        now = time(NULL);
        timeout = end - now;
        if (timeout <= 0) {
            /* time out */
            errno = ETIMEDOUT;
            break;
        }
        /* can we write to the socket? */
        if (!sock_can_write(c->socket, timeout)) {
            /* error or time out */
            break;
        }
        /* try sending data */
        rv = send(c->socket, p, to_send, 0);
        if (rv == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
            /* error */
            break;
        } else {
            to_send -= rv;
            p += rv;
        }
    }
    return (total_size - to_send) / size;
}

int sock_fprintf(cddb_conn_t *c, const char *format, ...)
{
    int rv;
    va_list args;

    cddb_log_debug("sock_fprintf()");
    va_start(args, format);
    rv = sock_vfprintf(c, format, args);
    va_end(args);
    return rv;
}

int sock_vfprintf(cddb_conn_t *c, const char *format, va_list ap)
{
    char *buf;
    int rv;
   
    cddb_log_debug("sock_vfprintf()");
    buf = (char*)malloc(c->buf_size);
    rv = vsnprintf(buf, c->buf_size, format, ap);
    cddb_log_debug("...buf = '%s'", buf);
    if (rv < 0 || rv >= c->buf_size) {
        /* buffer too small */
        cddb_errno_log_crit(c, CDDB_ERR_LINE_SIZE);
        free(buf);
        return -1;
    }
    rv = sock_fwrite(buf, sizeof(char), rv, c);
    free(buf);
    return rv;
}

/* Time-out enabled work-alikes */

#ifdef HAVE_ALARM
/* time-out jump buffer */
static jmp_buf timeout_expired;

/* time-out signal handler */
static void alarm_handler(int signum)
{
    longjmp(timeout_expired, 1);
}
#endif

struct hostent *timeout_gethostbyname(const char *hostname, int timeout)
{
#ifdef HAVE_ALARM
    struct hostent *he = NULL;
    struct sigaction action;
    struct sigaction old;

    /* no signal before setjmp */
    alarm(0);

    /* register signal handler */
    memset(&action, 0, sizeof(action));
    action.sa_handler = alarm_handler;
    sigaction(SIGALRM, &action, &old);

    /* save stack state */
    if (!setjmp(timeout_expired)) {
        alarm(timeout);         /* set time-out alarm */
        he = gethostbyname(hostname); /* execute DNS query */
        alarm(0);               /* reset alarm timer */
    } else {
        errno = ETIMEDOUT;
    }
    sigaction(SIGALRM, &old, NULL); /* restore previous signal handler */

    return he;
#else
    return gethostbyname(hostname); /* execute DNS query directly */
#endif
}

int timeout_connect(int sockfd, const struct sockaddr *addr, 
                    size_t len, int timeout)
{
    int got_error = 0;

    /* set socket to non-blocking */
#ifdef BEOS
    int on = 1;

    if (setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &on, sizeof(on)) == -1) {
        /* error while trying to set socket to non-blocking */
        return -1;
    }
#elif defined WIN32
    unsigned long arg = 1;

    if (ioctlsocket(sockfd, FIONBIO, &arg))
        return -1;
#else
    int flags;

    flags = fcntl(sockfd, F_GETFL, 0);
    flags |= O_NONBLOCK;        /* add non-blocking flag */
    if (fcntl(sockfd, F_SETFL, flags) == -1) {
        return -1;
    }
#endif /* BEOS */

    /* try connecting */
    if (connect(sockfd, addr, len) == -1) {
        /* check whether we can continue */
        if (errno == EINPROGRESS) {
            int rv;
            fd_set wfds;
            struct timeval tv;
            size_t l;

            /* set up select time out */
            tv.tv_sec = timeout;
            tv.tv_usec = 0;

            /* set up file descriptor set */
            FD_ZERO(&wfds);
            FD_SET(sockfd, &wfds);

            /* wait for connect to finish */
            rv = select(sockfd + 1, NULL, &wfds, NULL, &tv);
            switch (rv) {
            case 0:             /* time out */
                errno = ETIMEDOUT;
            case -1:            /* select error */
                got_error = -1;
            default:
                /* we got connected, check error condition */
                l = sizeof(rv);
                getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &rv, &l);
                if (rv) {
                    /* something went wrong, simulate normal connect behaviour */
                    errno = rv;
                    got_error = -1;
                }
            }
        }
    } else {
        /* connect failed */
        got_error = -1;
    }
    return got_error;
}