sock.h 4.88 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
2
3
/* sock.h
 * - General Socket Function Headers
 *
4
5
6
7
8
 * Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
 *                    Brendan Cully <brendan@xiph.org>,
 *                    Karl Heyes <karl@xiph.org>,
 *                    Jack Moffitt <jack@icecast.org>,
 *                    Ed "oddsock" Zaleski <oddsock@xiph.org>
Jack Moffitt's avatar
Jack Moffitt committed
9
 *
10
11
12
13
14
15
 * 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,
Jack Moffitt's avatar
Jack Moffitt committed
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
18
19
20
21
22
23
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
Jack Moffitt's avatar
Jack Moffitt committed
24
25
26
27
28
29
 *
 */

#ifndef __SOCK_H
#define __SOCK_H

30
#include <stdarg.h>
Michael Smith's avatar
Michael Smith committed
31

32
#ifdef HAVE_WINSOCK2_H
33
#include <winsock2.h>
34
35
36
#endif

#ifdef HAVE_UNISTD_H
37
#include <unistd.h>
38
#elif _WIN32
39
#include <compat.h>
40
#endif
Jack Moffitt's avatar
Jack Moffitt committed
41

42
43
44
45
46
47
48
49
50
51
52
53
#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

54
#if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON)
55
56
57
#define inet_aton(a,b) inet_pton(AF_INET, (a), (b))
#endif

58
59
60
61
62
63
#ifdef INET6_ADDRSTRLEN
#define MAX_ADDR_LEN INET6_ADDRSTRLEN
#else
#define MAX_ADDR_LEN 46
#endif

64
65
66
#ifndef sock_t
#define sock_t int
#endif
67

68
69
/* The following values are based on unix avoiding errno value clashes */
#define SOCK_SUCCESS 0
70
#define SOCK_ERROR (sock_t)-1
71
72
#define SOCK_TIMEOUT -2

Jack Moffitt's avatar
Jack Moffitt committed
73
74
75
/* sock connect macro */
#define sock_connect(h, p) sock_connect_wto(h, p, 0)

76
77
78
79
80
#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)
81
# define sock_set_error _mangle(sock_set_error)
82
83
84
85
86
87
88
89
90
# 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)
91
# define sock_connect_wto_bind _mangle(sock_connect_wto_bind)
92
93
94
95
96
97
98
99
100
101
102
# 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)
103
# define sock_set_send_buffer _mangle(sock_set_send_buffer)
104
105
106
# define sock_accept _mangle(sock_accept)
#endif

Jack Moffitt's avatar
Jack Moffitt committed
107
108
109
110
111
112
/* 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);
113
int sock_stalled(int error);
Jack Moffitt's avatar
Jack Moffitt committed
114
int sock_valid_socket(sock_t sock);
115
int sock_active (sock_t sock);
116
int sock_set_blocking(sock_t sock, int block);
117
118
int sock_set_nolinger(sock_t sock);
int sock_set_keepalive(sock_t sock);
119
int sock_set_nodelay(sock_t sock);
120
void sock_set_send_buffer (sock_t sock, int win_size);
121
void sock_set_error(int val);
Jack Moffitt's avatar
Jack Moffitt committed
122
123
124
int sock_close(sock_t  sock);

/* Connection related socket functions */
125
sock_t sock_connect_wto(const char *hostname, int port, int timeout);
126
sock_t sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout);
127
128
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
129
130

/* Socket write functions */
131
int sock_write_bytes(sock_t sock, const void *buff, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
132
int sock_write(sock_t sock, const char *fmt, ...);
133
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap);
Jack Moffitt's avatar
Jack Moffitt committed
134
int sock_write_string(sock_t sock, const char *buff);
135
ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count);
136

Jack Moffitt's avatar
Jack Moffitt committed
137
138

/* Socket read functions */
139
int sock_read_bytes(sock_t sock, char *buff, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
140
141
142
int sock_read_line(sock_t sock, char *string, const int len);

/* server socket functions */
143
sock_t sock_get_server_socket(int port, const char *sinterface);
Jack Moffitt's avatar
Jack Moffitt committed
144
int sock_listen(sock_t serversock, int backlog);
145
sock_t sock_accept(sock_t serversock, char *ip, size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
146
147
148
149
150
151

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

#endif  /* __SOCK_H */