summaryrefslogtreecommitdiff
path: root/include/cddb/cddb_net.h
blob: 60c5464b5bc39cc0bbc0f426f4babb687b233614 (plain)
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
/*
    $Id: cddb_net.h,v 1.11 2005/03/11 21:29:29 airborne 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.
*/

#ifndef CDDB_NET_H
#define CDDB_NET_H 1

#ifdef __cplusplus
    extern "C" {
#endif


#include <stdarg.h>

#if defined( UNDER_CE )
#   include <winsock.h>
#elif defined( WIN32 )
#   include <winsock2.h>
#   include <ws2tcpip.h>
#endif 

#include <cddb/cddb_ni.h>
#include <cddb/cddb_config.h>

#if defined(CDDB_NEED_SYS_SOCKET_H) || defined(HAVE_SYS_SOCKET_H)
#include <sys/socket.h>
#endif


/* --- socket-based work-alikes --- */


/**
 * This function performs the same task as the standard fgets except
 * for the fact that it might time-out if the socket read takes too
 * long.  In case of a time out, errno will be set to ETIMEDOUT.
 *
 * @param s       The string buffer.
 * @param size    Size of the buffer.
 * @param c       The CDDB connection structure.
 * @return The string that was read or NULL on error or EOF when no
 *         characters were read.
 */
char *sock_fgets(char *s, int size, cddb_conn_t *c);

/**
 * This function performs the same task as the standard fwrite except
 * for the fact that it might time-out if the socket write takes too
 * long.  In case of a time out, errno will be set to ETIMEDOUT.
 *
 * @param ptr     Pointer to data record.
 * @param size    Size of data record.
 * @param nmemb   The number of data records to write.
 * @param c       The CDDB connection structure.
 * @return The number of records written.
 */
size_t sock_fwrite(const void *ptr, size_t size, size_t nmemb, cddb_conn_t *c);

/**
 * This function performs the same task as the standard fprintf except
 * for the fact that it might time-out if the socket write takes too
 * long.  In case of a time out, errno will be set to ETIMEDOUT.
 *
 * @param c       The CDDB connection structure.
 * @param format  Pointer to data record.
 * @return The number of characters written.
 */
int sock_fprintf(cddb_conn_t *c, const char *format, ...);

/**
 * This function performs the same task as the standard vfprintf
 * except for the fact that it might time-out if the socket write
 * takes too long.  In case of a time out, errno will be set to
 * ETIMEDOUT.
 *
 * @param c       The CDDB connection structure.
 * @param format  Pointer to data record.
 * @param ap      Variable argument list.
 * @return The number of characters written.
 */
int sock_vfprintf(cddb_conn_t *c, const char *format, va_list ap);

/* --- time-out enabled work-alikes --- */

/**
 * This function performs the same task as the standard gethostbyname
 * except for the fact that it might time-out if the query takes too
 * long.  In case of a time out, errno will be set to ETIMEDOUT.
 *
 * @param hostname The hostname that needs to be resolved.
 * @param timeout  Number of seconds after which to time out.
 * @return The host entity for given host name or NULL if not found or
 *         timed out (errno will be set).
 */
struct hostent *timeout_gethostbyname(const char *hostname, int timeout);

/**
 * This function performs the same task as the standard connect except
 * for the fact that it might time-out if the connect takes too long.
 * In case of a time out, errno will be set to ETIMEDOUT.
 * 
 * @param sockfd   The socket.
 * @param addr     The address to connect to.
 * @param len      The size of the address structure.
 * @param timeout  Number of seconds after which to time out.
 * @return Zero on success, -1 on failure (errno will be set).
 */
int timeout_connect(int sockfd, const struct sockaddr *addr, size_t len, 
                    int timeout);


#ifdef __cplusplus
    }
#endif

#endif /* CDDB_NET_H */