socket.h
2.22 KB
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
#ifndef SCOT_SOCKET_H
#define SCOT_SOCKET_H
#include <scot_common.h>
#include <scot/scot_types.h>
#ifndef WIN32
# include <netdb.h>
# include <sys/socket.h>
# include <sys/un.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <stdlib.h>
# include <errno.h>
# define INVALID_SOCKET -1
# define SCOT_ERRNO errno
# define SCOT_H_ERRNO h_errno
# define SOCKET int
# define SCOT_SOCK_CLOSE close
#else
# include <winsock.h>
# define SCOT_ERRNO WSAGetLastError()
# define SCOT_H_ERRNO WSAGetLastError()
# define SCOT_SOCK_CLOSE closesocket
#endif /* WIN32 */
#include <scot/stream.h>
/*
* prevent direct access to the structure
* from other methods then the ones that are made for this.
* Should especially encourage the use of getter and setter
* functions
*/
#ifndef USE_STRUCT_SCOT_SOCKET
struct scot_socket
{
const char _ [sizeof (struct {
struct scot_stream socket;
struct sockaddr * sa;
SIZE_T addr_len;
})];
};
#else
struct scot_socket
{
struct scot_stream socket;
struct sockaddr * sa;
SIZE_T addr_len;
};
#endif /* USE_STRUCT_SCOT_SOCKET */
/* socket errors */
#define SCOT_SOCKET_NEW_FAIL 0
#define SCOT_SOCKET_LISTEN_FAIL 1
#define SCOT_SOCKET_ACCEPT_FAIL 2
#define SCOT_SOCKET_CONNECT_FAIL 3
#define SCOT_SOCKET_NO_VALID_HOST 4
#define SCOT_SOCKET_AF_NOT_IMPLEMENTED 5
/* socket warnings */
#define SCOT_SOCKET_UN_FILE_EXISTS 0
extern const char * scot_socket_errmsg[];
extern const char * scot_socket_wrnmsg[];
/*
* This function initializes scot sockets. This primary means it does
* nothing under a posix system an WSASartup on a Windows system.
*/
void scot_socket_init (uint16_t, uint16_t);
/*
* This finalizes the scot sockets system. Again does noting on posix
* and WSACleanup on Windows.
*/
void scot_socket_fini (void);
void scot_socket_listen (const struct scot_socket*);
struct scot_socket * scot_socket_accept (const struct scot_socket*);
void scot_socket_connect (const struct scot_socket*,
const char *);
void scot_socket_free (struct scot_socket*);
#endif /* SCOT_SOCKET_H */