sock.h 4.62 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* sock.h
 * - General Socket Function Headers
 *
 * Copyright (c) 1999 the icecast team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#ifndef __SOCK_H
#define __SOCK_H

25
#include <stdarg.h>
Michael Smith's avatar
Michael Smith committed
26

27
#ifdef HAVE_WINSOCK2_H
28
#include <winsock2.h>
29
30
31
#endif

#ifdef HAVE_UNISTD_H
32
#include <unistd.h>
33
#elif _WIN32
34
#include <compat.h>
35
#endif
Jack Moffitt's avatar
Jack Moffitt committed
36

37
38
39
40
41
42
43
44
45
46
47
48
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#else
#ifndef _SYS_UIO_H
struct iovec
{
    void   *iov_base;
    size_t iov_len;
};
#endif
#endif

49
#if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON)
50
51
52
#define inet_aton(a,b) inet_pton(AF_INET, (a), (b))
#endif

53
54
55
56
57
58
#ifdef INET6_ADDRSTRLEN
#define MAX_ADDR_LEN INET6_ADDRSTRLEN
#else
#define MAX_ADDR_LEN 46
#endif

59
60
61
#ifndef sock_t
#define sock_t int
#endif
62

63
64
/* The following values are based on unix avoiding errno value clashes */
#define SOCK_SUCCESS 0
65
#define SOCK_ERROR (sock_t)-1
66
67
#define SOCK_TIMEOUT -2

Jack Moffitt's avatar
Jack Moffitt committed
68
69
70
/* sock connect macro */
#define sock_connect(h, p) sock_connect_wto(h, p, 0)

71
72
73
74
75
#ifdef _mangle
# define sock_initialize _mangle(sock_initialize)
# define sock_shutdown _mangle(sock_shutdown)
# define sock_get_localip _mangle(sock_get_localip)
# define sock_error _mangle(sock_error)
76
# define sock_set_error _mangle(sock_set_error)
77
78
79
80
81
82
83
84
85
# define sock_recoverable _mangle(sock_recoverable)
# define sock_stalled _mangle(sock_stalled)
# define sock_valid_socket _mangle(sock_valid_socket)
# define sock_set_blocking _mangle(sock_set_blocking)
# define sock_set_nolinger _mangle(sock_set_nolinger)
# define sock_set_nodelay _mangle(sock_set_nodelay)
# define sock_set_keepalive _mangle(sock_set_keepalive)
# define sock_close _mangle(sock_close)
# define sock_connect_wto _mangle(sock_connect_wto)
86
# define sock_connect_wto_bind _mangle(sock_connect_wto_bind)
87
88
89
90
91
92
93
94
95
96
97
# define sock_connect_non_blocking _mangle(sock_connect_non_blocking)
# define sock_connected _mangle(sock_connected)
# define sock_write_bytes _mangle(sock_write_bytes)
# define sock_write _mangle(sock_write)
# define sock_write_fmt _mangle(sock_write_fmt)
# define sock_write_string _mangle(sock_write_string)
# define sock_writev _mangle(sock_writev)
# define sock_read_bytes _mangle(sock_read_bytes)
# define sock_read_line _mangle(sock_read_line)
# define sock_get_server_socket _mangle(sock_get_server_socket)
# define sock_listen _mangle(sock_listen)
98
# define sock_set_send_buffer _mangle(sock_set_send_buffer)
99
100
101
# define sock_accept _mangle(sock_accept)
#endif

Jack Moffitt's avatar
Jack Moffitt committed
102
103
104
105
106
107
/* Misc socket functions */
void sock_initialize(void);
void sock_shutdown(void);
char *sock_get_localip(char *buff, int len);
int sock_error(void);
int sock_recoverable(int error);
108
int sock_stalled(int error);
Jack Moffitt's avatar
Jack Moffitt committed
109
int sock_valid_socket(sock_t sock);
110
int sock_active (sock_t sock);
111
int sock_set_blocking(sock_t sock, int block);
112
113
int sock_set_nolinger(sock_t sock);
int sock_set_keepalive(sock_t sock);
114
int sock_set_nodelay(sock_t sock);
115
void sock_set_send_buffer (sock_t sock, int win_size);
116
void sock_set_error(int val);
Jack Moffitt's avatar
Jack Moffitt committed
117
118
119
int sock_close(sock_t  sock);

/* Connection related socket functions */
120
sock_t sock_connect_wto(const char *hostname, int port, int timeout);
121
sock_t sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout);
122
123
sock_t sock_connect_non_blocking(const char *host, unsigned port);
int sock_connected(sock_t sock, int timeout);
Jack Moffitt's avatar
Jack Moffitt committed
124
125

/* Socket write functions */
126
int sock_write_bytes(sock_t sock, const void *buff, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
127
int sock_write(sock_t sock, const char *fmt, ...);
128
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap);
Jack Moffitt's avatar
Jack Moffitt committed
129
int sock_write_string(sock_t sock, const char *buff);
130
ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count);
131

Jack Moffitt's avatar
Jack Moffitt committed
132
133

/* Socket read functions */
134
int sock_read_bytes(sock_t sock, char *buff, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
135
136
137
int sock_read_line(sock_t sock, char *string, const int len);

/* server socket functions */
138
sock_t sock_get_server_socket(int port, const char *sinterface);
Jack Moffitt's avatar
Jack Moffitt committed
139
int sock_listen(sock_t serversock, int backlog);
140
sock_t sock_accept(sock_t serversock, char *ip, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
141
142
143
144
145
146

#ifdef _WIN32
int inet_aton(const char *s, struct in_addr *a);
#endif

#endif  /* __SOCK_H */