sock.h 2.67 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

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

Jack Moffitt's avatar
Jack Moffitt committed
27
#ifdef _WIN32
28
#include <winsock2.h>
Jack Moffitt's avatar
Jack Moffitt committed
29
30
#endif

31
32
33
34
35
36
37
38
39
40
41
42
#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

43
44
typedef int sock_t;

45
46
/* The following values are based on unix avoiding errno value clashes */
#define SOCK_SUCCESS 0
Jack Moffitt's avatar
Jack Moffitt committed
47
#define SOCK_ERROR -1
48
49
#define SOCK_TIMEOUT -2

Jack Moffitt's avatar
Jack Moffitt committed
50
51
52
53
54
55
56
57
58
59
60
61
#define SOCK_BLOCK 0
#define SOCK_NONBLOCK 1

/* sock connect macro */
#define sock_connect(h, p) sock_connect_wto(h, p, 0)

/* 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);
62
int sock_stalled (int error);
Jack Moffitt's avatar
Jack Moffitt committed
63
64
int sock_valid_socket(sock_t sock);
int sock_set_blocking(sock_t sock, const int block);
65
66
int sock_set_nolinger(sock_t sock);
int sock_set_keepalive(sock_t sock);
Jack Moffitt's avatar
Jack Moffitt committed
67
68
69
70
int sock_close(sock_t  sock);

/* Connection related socket functions */
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout);
71
72
int sock_connect_non_blocking (const char *host, const unsigned port);
int sock_connected (int sock, unsigned timeout);
Jack Moffitt's avatar
Jack Moffitt committed
73
74

/* Socket write functions */
75
int sock_write_bytes(sock_t sock, const void *buff, const size_t len);
Jack Moffitt's avatar
Jack Moffitt committed
76
77
int sock_write(sock_t sock, const char *fmt, ...);
int sock_write_string(sock_t sock, const char *buff);
78
79
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count);

Jack Moffitt's avatar
Jack Moffitt committed
80
81
82
83
84
85

/* Socket read functions */
int sock_read_bytes(sock_t sock, char *buff, const int len);
int sock_read_line(sock_t sock, char *string, const int len);

/* server socket functions */
86
sock_t sock_get_server_socket(const int port, char *sinterface);
Jack Moffitt's avatar
Jack Moffitt committed
87
88
89
90
91
92
93
94
95
96
97
98
99
int sock_listen(sock_t serversock, int backlog);
int sock_accept(sock_t serversock, char *ip, int len);

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

#endif  /* __SOCK_H */