socket_un.c
3.31 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* some basic berkley socket stuff....far from beeing complete
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <stdlib.h>
#include <errno.h>
#define USE_STRUCT_SCOT_SOCKET
#include <scot/exception.h>
#include <scot/memory.h>
#include <scot/socket.h>
#include <scot_common.h>
#include <scot/scot_types.h>
struct scot_socket *
scot_socket_un_new (const char* path)
{
struct scot_socket * sock = NULL;
excenv_t * ee;
SOCKET handle;
TRY
{
handle = socket (PF_UNIX, SOCK_STREAM, 0);
if (handle == -1)
THROW (EXC (EXC_ERROR, errno, strerror (errno)));
if (SCOT_STR_LENGTH (path) > UNIX_PATH_MAX)
THROW (EXC (EXC_ERROR,
SCOT_UNX_PATH_TO_LONG,
scot_common_errmsg [SCOT_UNX_PATH_TO_LONG]));
sock = SCOT_MEM_GET (sizeof (struct scot_socket));
SCOT_MEM_ZERO (sock, sizeof (struct scot_socket));
sock->sa = SCOT_MEM_GET (sizeof (struct sockaddr_un));
SCOT_MEM_ZERO (sock->sa, sizeof (struct sockaddr_un));
((struct sockaddr_un *) sock->sa)->sun_family = AF_UNIX;
SCOT_STRN_COPY (
((struct sockaddr_un *) sock->sa)->sun_path,
path,
UNIX_PATH_MAX);
sock->socket.handle.sock = handle;
sock->socket.s_type = SCOT_STREAM_TYPE_SOCKET;
sock->addr_len =
sizeof (((struct sockaddr_un *) sock->sa)->sun_family) +
SCOT_STR_LENGTH (((struct sockaddr_un *) sock->sa)->sun_path);
}
CATCH (ee)
{
forward_all_exceptions (ee);
if (sock != NULL)
{
if (sock->socket.handle.sock <= 0)
SCOT_SOCK_CLOSE (sock->socket.handle.sock);
if (sock->sa != NULL)
SCOT_MEM_FREE (sock->sa);
SCOT_MEM_FREE (sock);
}
THROW (EXC (EXC_ERROR,
SCOT_SOCKET_NEW_FAIL,
scot_socket_errmsg [SCOT_SOCKET_NEW_FAIL]));
}
return sock;
}
const
char *
scot_socket_un_get_path (const struct scot_socket* s)
{
return ((struct sockaddr_un *) s->sa)->sun_path;
}
struct scot_socket *
scot_socket_un_accept (const struct scot_socket* s)
{
struct scot_socket * sock = NULL;
excenv_t * ee;
SOCKET handle;
struct sockaddr sa;
SIZE_T addr_len;
TRY
{
addr_len = s->addr_len;
handle = accept (s->socket.handle.sock, &sa, &addr_len);
if (handle == -1)
THROW (EXC (EXC_ERROR, errno, strerror (errno)));
sock = SCOT_MEM_GET (sizeof (struct scot_socket));
SCOT_MEM_ZERO (sock, sizeof (struct scot_socket));
sock->sa = SCOT_MEM_GET (sizeof (struct sockaddr_un));
SCOT_MEM_ZERO (sock->sa, sizeof (struct sockaddr_un));
sock->addr_len = addr_len;
sock->socket.handle.sock = handle;
sock->socket.s_type = SCOT_STREAM_TYPE_SOCKET;
SCOT_MEM_COPY (sock->sa, &sa, sizeof (struct sockaddr_un));
}
CATCH (ee)
{
forward_all_exceptions (ee);
if (sock != NULL)
{
if (sock->socket.handle.sock <= 0)
SCOT_SOCK_CLOSE (sock->socket.handle.sock);
if (sock->sa != NULL)
SCOT_MEM_FREE (sock->sa);
SCOT_MEM_FREE (sock);
}
THROW (EXC (EXC_ERROR,
SCOT_SOCKET_ACCEPT_FAIL,
scot_socket_errmsg [SCOT_SOCKET_ACCEPT_FAIL]));
}
return sock;
}
void
scot_socket_un_prep_con (struct scot_socket * s, const char * adr)
{
if (adr)
{
SCOT_STRN_COPY (
((struct sockaddr_un *) s->sa)->sun_path,
adr,
UNIX_PATH_MAX);
s->addr_len =
sizeof (((struct sockaddr_un *) s->sa)->sun_family) +
SCOT_STR_LENGTH (((struct sockaddr_un *) s->sa)->sun_path);
}
}