socket_stream.c
5.69 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
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2004,
2005, 2006, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <mailutils/stream.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
struct _socket_stream
{
mu_stream_t fstream;
char *filename;
int ec; /* Last error code if fstream == NULL */
};
static void
_s_destroy (mu_stream_t stream)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
if (s->filename)
free (s->filename);
mu_stream_destroy (&s->fstream, mu_stream_get_owner (s->fstream));
free (s);
}
static int
_s_read (mu_stream_t stream, char *optr, size_t osize,
mu_off_t offset, size_t *nbytes)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_read (s->fstream, optr, osize, offset, nbytes);
}
static int
_s_readline (mu_stream_t stream, char *optr, size_t osize,
mu_off_t offset, size_t *nbytes)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_readline (s->fstream, optr, osize, offset, nbytes);
}
static int
_s_write (mu_stream_t stream, const char *iptr, size_t isize,
mu_off_t offset, size_t *nbytes)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_write (s->fstream, iptr, isize, offset, nbytes);
}
static int
_s_open (mu_stream_t stream)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
int fd, rc;
FILE *fp;
struct sockaddr_un addr;
char *fstr;
int flags;
if (!s)
return EINVAL;
fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return errno;
memset (&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
strncpy (addr.sun_path, s->filename, sizeof addr.sun_path - 1);
addr.sun_path[sizeof addr.sun_path - 1] = 0;
if (connect (fd, (struct sockaddr *) &addr, sizeof(addr)))
{
close (fd);
return errno;
}
mu_stream_get_flags(stream, &flags);
if (flags & MU_STREAM_WRITE)
fstr = "w";
else if (flags & MU_STREAM_RDWR)
fstr = "w+";
else if (flags & MU_STREAM_READ)
fstr = "r";
else
fstr = "w+";
fp = fdopen (fd, fstr);
if (!fp)
{
close (fd);
return errno;
}
rc = mu_stdio_stream_create (&s->fstream, fp, flags);
if (rc)
{
fclose (fp);
return rc;
}
rc = mu_stream_open (s->fstream);
if (rc)
{
mu_stream_destroy (&s->fstream, mu_stream_get_owner (s->fstream));
fclose (fp);
}
return rc;
}
static int
_s_close (mu_stream_t stream)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_close (s->fstream);
}
static int
_s_flush (mu_stream_t stream)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_flush (s->fstream);
}
int
_s_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_wait (s->fstream, pflags, tvp);
}
int
_s_strerror (mu_stream_t stream, const char **pstr)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_strerror (s->fstream, pstr);
}
static int
_s_get_transport2 (mu_stream_t stream,
mu_transport_t *pin, mu_transport_t *pout)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
return mu_stream_get_transport2 (s->fstream, pin, pout);
}
int
_s_shutdown (mu_stream_t stream, int how)
{
struct _socket_stream *s = mu_stream_get_owner (stream);
int flag;
mu_transport_t trans;
if (s->fstream == NULL)
return EINVAL;
mu_stream_get_transport(s->fstream, &trans);
switch (how)
{
case MU_STREAM_READ:
flag = SHUT_RD;
break;
case MU_STREAM_WRITE:
flag = SHUT_WR;
}
if (shutdown ((int) trans, flag))
return errno;
return 0;
}
int
mu_socket_stream_create (mu_stream_t *stream, const char *filename, int flags)
{
struct _socket_stream *s;
int rc;
if (stream == NULL)
return MU_ERR_OUT_PTR_NULL;
s = calloc (1, sizeof (struct _socket_stream));
if (s == NULL)
return ENOMEM;
if ((s->filename = strdup (filename)) == NULL)
{
free (s);
return ENOMEM;
}
rc = mu_stream_create (stream, flags | MU_STREAM_NO_CHECK, s);
if (rc)
{
free (s);
free (s->filename);
return rc;
}
mu_stream_set_open (*stream, _s_open, s);
mu_stream_set_close (*stream, _s_close, s);
mu_stream_set_get_transport2 (*stream, _s_get_transport2, s);
mu_stream_set_read (*stream, _s_read, s);
mu_stream_set_readline (*stream, _s_readline, s);
mu_stream_set_write (*stream, _s_write, s);
mu_stream_set_flush (*stream, _s_flush, s);
mu_stream_set_destroy (*stream, _s_destroy, s);
mu_stream_set_strerror (*stream, _s_strerror, s);
mu_stream_set_wait (*stream, _s_wait, s);
mu_stream_set_shutdown (*stream, _s_shutdown, s);
return 0;
}