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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* symsocket.c:
*
* Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
* All rights reserved.
*
*/
static char rcsid[] =
"$Id$";
/*
* $Log$
* Revision 1.2 2008/02/14 00:57:58 james
* *** empty log message ***
*
* Revision 1.1 2008/02/13 18:05:06 james
* *** empty log message ***
*
*/
#include "project.h"
#include <sys/socket.h>
#include <sys/un.h>
#define BUF_SIZE 16384
#define MAX_TXN 4096
void
socket_free (Socket * s)
{
if (!s)
return;
if (s->read_buf)
slide_free (s->read_buf);
if (s->write_buf)
slide_free (s->write_buf);
close (s->fd);
}
Socket *
socket_listen (char *path)
{
int fd;
struct sockaddr_un *sun;
Socket *ret;
int n;
unlink (path);
fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return NULL;
n = strlen (path) + sizeof (struct sockaddr_un);
sun = (struct sockaddr_un *) malloc (n);
memset (sun, 0, n);
sun->sun_family = AF_UNIX;
strcpy (sun->sun_path, path);
if (bind (fd, (struct sockaddr *) sun, SUN_LEN (sun)) < 0)
{
free (sun);
close (fd);
return NULL;
}
free (sun);
if (listen (fd, 5) < 0)
{
close (fd);
return NULL;
}
set_nonblocking (fd);
ret = (Socket *) malloc (sizeof (Socket));
memset (ret, 0, sizeof (Socket));
ret->read_buf = NULL;
ret->write_buf = NULL;
ret->fd = fd;
return ret;
}
Socket *
socket_accept (Socket * l)
{
Socket *ret;
int len;
int fd;
struct sockaddr_un sun;
len = sizeof (struct sockaddr_un);
fd = accept (l->fd, (struct sockaddr *) &sun, &len);
if (fd < 0)
return NULL;
ret = (Socket *) malloc (sizeof (Socket));
memset (ret, 0, sizeof (Socket));
set_nonblocking (fd);
ret->fd = fd;
ret->read_buf = slide_new (BUF_SIZE);
ret->write_buf = slide_new (BUF_SIZE);
ret->msg = 0;
return ret;
}
/*Blocking for now*/
Socket *
socket_connect (char *path)
{
int n;
int fd;
struct sockaddr_un *sun;
Socket *ret;
fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return NULL;
n = strlen (path) + sizeof (struct sockaddr_un);
sun = (struct sockaddr_un *) malloc (n);
memset (sun, 0, n);
sun->sun_family = AF_UNIX;
strcpy (sun->sun_path, path);
if (connect (fd, (struct sockaddr *) sun, SUN_LEN (sun)))
{
free (sun);
close (fd);
return NULL;
}
free (sun);
set_nonblocking (fd);
ret = (Socket *) malloc (sizeof (Socket));
memset (ret, 0, sizeof (Socket));
ret->fd = fd;
ret->read_buf = slide_new (BUF_SIZE);
ret->write_buf = slide_new (BUF_SIZE);
ret->msg = 0;
return ret;
}
void
socket_consume_msg (Socket * s)
{
int n;
if (!s->msg)
return;
ipc_consume_message_in_slide(s->read_buf);
s->msg = ipc_check_for_message_in_slide (s->read_buf);
}
void
socket_pre_select (Socket * s, fd_set * rfds, fd_set * wfds)
{
char buf[1024];
int n;
/*Server socket */
if (SOCKET_IS_LISTENER (s))
{
FD_SET (s->fd, rfds);
return;
}
if (!SLIDE_EMPTY (s->write_buf))
FD_SET (s->fd, wfds);
if (!SLIDE_FULL (s->read_buf))
FD_SET (s->fd, rfds);
}
int
socket_post_select (Socket * s, fd_set * rfds, fd_set * wfds)
{
char buf[1024];
int n;
int error=0;
if ((!SLIDE_EMPTY (s->write_buf)) && FD_ISSET (s->fd, wfds))
{
n =
(SLIDE_BYTES (s->write_buf) >
MAX_TXN) ? MAX_TXN : SLIDE_BYTES (s->write_buf);
n = wrap_write (s->fd, SLIDE_RPTR (s->write_buf), n);
if (n > 0)
slide_consume (s->write_buf, n);
if (n<0) error=-1;
}
if (!SLIDE_FULL (s->read_buf) && FD_ISSET (s->fd, rfds))
{
n =
(SLIDE_SPACE (s->read_buf) >
MAX_TXN) ? MAX_TXN : SLIDE_SPACE (s->read_buf);
n = wrap_read (s->fd, SLIDE_WPTR (s->read_buf), n);
if (n > 0)
slide_added (s->read_buf, n);
if (n<0) error=-1;
}
s->msg = ipc_check_for_message_in_slide (s->read_buf);
return error;
}
int
socket_write (Socket * s, void *buf, int len)
{
int n;
slide_expand (s->write_buf, len);
memcpy (SLIDE_WPTR (s->write_buf), buf, len);
slide_added (s->write_buf, len);
n =
(SLIDE_BYTES (s->write_buf) >
MAX_TXN) ? MAX_TXN : SLIDE_BYTES (s->write_buf);
n = wrap_write (s->fd, SLIDE_RPTR (s->write_buf), n);
{
uint8_t *c=SLIDE_RPTR(s->write_buf);
}
if (n > 0)
slide_consume (s->write_buf, n);
if (n<0) return -1;
return len;
}
|