Commit 69a7eacb 69a7eacb090fe733c8ffe108a5b0f8841abde1cf by Sam Roberts

Modified stream creation and opening. The *_stream_create() functions now

take the specific arguments appropriate to them and setup a stream_t
so that stream_open(), which now takes no arguments, can perform whatever
actions are appropriate when opening that particular kind of stream.

I have tested the changes with the example programs and sieve, but not with
the imap or pop servers.
1 parent 7cf404b6
...@@ -481,7 +481,7 @@ notify_user (const char *user, const char *device, const char *path, off_t offse ...@@ -481,7 +481,7 @@ notify_user (const char *user, const char *device, const char *path, off_t offse
481 return; 481 return;
482 } 482 }
483 483
484 if ((status = memory_stream_create (&stream))) 484 if ((status = memory_stream_create (&stream, 0, 0)))
485 { 485 {
486 syslog (LOG_ERR, "can't create temporary stream: %s", 486 syslog (LOG_ERR, "can't create temporary stream: %s",
487 strerror (status)); 487 strerror (status));
......
...@@ -78,7 +78,7 @@ imap4d_append0 (mailbox_t mbox, int flags, char *text) ...@@ -78,7 +78,7 @@ imap4d_append0 (mailbox_t mbox, int flags, char *text)
78 if (mailbox_open (tmp, MU_STREAM_READ) != 0) 78 if (mailbox_open (tmp, MU_STREAM_READ) != 0)
79 return 1; 79 return 1;
80 80
81 if (memory_stream_create (&stream)) 81 if (memory_stream_create (&stream, 0, 0))
82 { 82 {
83 mailbox_close (tmp); 83 mailbox_close (tmp);
84 return 1; 84 return 1;
......
...@@ -1174,7 +1174,7 @@ fetch_header_fields (message_t msg, char **arg, unsigned long start, ...@@ -1174,7 +1174,7 @@ fetch_header_fields (message_t msg, char **arg, unsigned long start,
1174 stream_t stream = NULL; 1174 stream_t stream = NULL;
1175 int status; 1175 int status;
1176 1176
1177 status = memory_stream_create (&stream); 1177 status = memory_stream_create (&stream, 0, 0);
1178 if (status != 0) 1178 if (status != 0)
1179 imap4d_bye (ERR_NO_MEM); 1179 imap4d_bye (ERR_NO_MEM);
1180 1180
...@@ -1275,7 +1275,7 @@ fetch_header_fields_not (message_t msg, char **arg, unsigned long start, ...@@ -1275,7 +1275,7 @@ fetch_header_fields_not (message_t msg, char **arg, unsigned long start,
1275 stream_t stream = NULL; 1275 stream_t stream = NULL;
1276 int status; 1276 int status;
1277 1277
1278 status = memory_stream_create (&stream); 1278 status = memory_stream_create (&stream, 0, 0);
1279 if (status) 1279 if (status)
1280 imap4d_bye (ERR_NO_MEM); 1280 imap4d_bye (ERR_NO_MEM);
1281 1281
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
18 #ifndef _MAILUTILS_STREAM_H 18 #ifndef _MAILUTILS_STREAM_H
19 # define _MAILUTILS_STREAM_H 19 # define _MAILUTILS_STREAM_H
20 20
21 #include <stdio.h>
21 #include <sys/types.h> 22 #include <sys/types.h>
22 23
23 #include <mailutils/property.h> 24 #include <mailutils/property.h>
...@@ -43,66 +44,41 @@ typedef struct _stream *stream_t; ...@@ -43,66 +44,41 @@ typedef struct _stream *stream_t;
43 #define MU_STREAM_APPEND 0x00000008 44 #define MU_STREAM_APPEND 0x00000008
44 #define MU_STREAM_CREAT 0x00000010 45 #define MU_STREAM_CREAT 0x00000010
45 #define MU_STREAM_NONBLOCK 0x00000020 46 #define MU_STREAM_NONBLOCK 0x00000020
46 /* Stream will be destroy on stream_destroy whitout checking the owner. */ 47 /* Stream will be destroy on stream_destroy without checking the owner. */
47 #define MU_STREAM_NO_CHECK 0x00000040 48 #define MU_STREAM_NO_CHECK 0x00000040
48 #define MU_STREAM_SEEKABLE 0x00000080 49 #define MU_STREAM_SEEKABLE 0x00000080
50 #define MU_STREAM_NO_CLOSE 0x00000100
49 51
50 extern int stream_create __P ((stream_t *, int flags, void *owner)); 52 /* Functions useful to users of the pre-defined stream types. */
51 extern void stream_destroy __P ((stream_t *, void *owner));
52 extern int stream_set_destroy __P ((stream_t, void (*_destroy) __P ((stream_t)), void *owner));
53 extern void * stream_get_owner __P ((stream_t));
54 53
55 extern int stream_open __P ((stream_t, const char *, int, int)); 54 extern int file_stream_create __P ((stream_t *stream, const char* filename, int flags));
56 extern int stream_set_open __P ((stream_t, int (*_open) 55 extern int tcp_stream_create __P ((stream_t *stream, const char* host, int port, int flags));
57 __P ((stream_t, const char *, int, int)), 56 extern int mapfile_stream_create __P ((stream_t *stream, const char* filename, int flags));
58 void *owner)); 57 extern int memory_stream_create __P ((stream_t *stream, const char* filename, int flags));
58 extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream, const char *encoding));
59 extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream, const char *encoding));
59 60
60 extern int stream_close __P ((stream_t)); 61 extern void stream_destroy __P ((stream_t *, void *owner));
61 extern int stream_set_close __P ((stream_t, int (*_close) __P ((stream_t)),
62 void *owner));
63 62
63 extern int stream_open __P ((stream_t));
64 extern int stream_close __P ((stream_t));
64 extern int stream_is_seekable __P ((stream_t)); 65 extern int stream_is_seekable __P ((stream_t));
65
66 extern int stream_get_fd __P ((stream_t , int *)); 66 extern int stream_get_fd __P ((stream_t , int *));
67 extern int stream_set_fd __P ((stream_t, int (*_get_fd)(stream_t, int *), 67 extern int stream_read __P ((stream_t, char *, size_t, off_t, size_t *));
68 void *owner)); 68 extern int stream_readline __P ((stream_t, char *, size_t, off_t, size_t *));
69
70 extern int stream_read __P ((stream_t, char *, size_t,
71 off_t, size_t *));
72 extern int stream_set_read __P ((stream_t, int
73 (*_read) __P ((stream_t, char *, size_t,
74 off_t, size_t *)),
75 void *owner));
76
77 extern int stream_readline __P ((stream_t, char *, size_t,
78 off_t, size_t *));
79 extern int stream_set_readline __P ((stream_t, int
80 (*_readline) __P ((stream_t, char *,
81 size_t, off_t,
82 size_t *)),
83 void *owner));
84
85 extern int stream_size __P ((stream_t, off_t *)); 69 extern int stream_size __P ((stream_t, off_t *));
86 extern int stream_set_size __P ((stream_t, int
87 (*_size) __P ((stream_t, off_t *)),
88 void *owner));
89
90 extern int stream_truncate __P ((stream_t, off_t)); 70 extern int stream_truncate __P ((stream_t, off_t));
91 extern int stream_set_truncate __P ((stream_t, int 71 extern int stream_write __P ((stream_t, const char *, size_t, off_t, size_t *));
92 (*_truncate) __P ((stream_t, off_t)),
93 void *owner));
94
95 extern int stream_write __P ((stream_t, const char *, size_t,
96 off_t, size_t *));
97 extern int stream_set_write __P ((stream_t, int
98 (*_write) __P ((stream_t, const char *,
99 size_t, off_t, size_t *)),
100 void *owner));
101
102 extern int stream_setbufsiz __P ((stream_t stream, size_t size)); 72 extern int stream_setbufsiz __P ((stream_t stream, size_t size));
103 extern int stream_flush __P ((stream_t)); 73 extern int stream_flush __P ((stream_t));
104 extern int stream_set_flush __P ((stream_t, int (*_flush) 74
105 __P ((stream_t)), void *owner)); 75
76 /* Functions useful to implementors of new stream types. */
77
78 extern int stream_create __P ((stream_t *stream, int flags, void* owner));
79
80 extern void* stream_get_owner __P ((stream_t));
81 extern void stream_set_owner __P ((stream_t, void* owner));
106 82
107 extern int stream_get_flags __P ((stream_t, int *pflags)); 83 extern int stream_get_flags __P ((stream_t, int *pflags));
108 extern int stream_set_flags __P ((stream_t, int flags)); 84 extern int stream_set_flags __P ((stream_t, int flags));
...@@ -116,18 +92,39 @@ extern int stream_set_property __P ((stream_t, property_t, void *)); ...@@ -116,18 +92,39 @@ extern int stream_set_property __P ((stream_t, property_t, void *));
116 #define MU_STREAM_STATE_CLOSE 8 92 #define MU_STREAM_STATE_CLOSE 8
117 extern int stream_get_state __P ((stream_t, int *pstate)); 93 extern int stream_get_state __P ((stream_t, int *pstate));
118 94
119 /* Misc. */ 95 extern int stream_set_destroy __P ((stream_t,
120 extern int file_stream_create __P ((stream_t *stream)); 96 void (*_destroy) __P ((stream_t)), void *owner));
121 extern int mapfile_stream_create __P ((stream_t *stream)); 97
122 extern int memory_stream_create __P ((stream_t *stream)); 98 extern int stream_set_open __P ((stream_t,
123 extern int tcp_stream_create __P ((stream_t *stream)); 99 int (*_open) __P ((stream_t)), void *owner));
124 extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream, 100
125 const char *encoding)); 101 extern int stream_set_close __P ((stream_t,
126 extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream, 102 int (*_close) __P ((stream_t)), void *owner));
127 const char *encoding)); 103
104 extern int stream_set_fd __P ((stream_t,
105 int (*_get_fd)(stream_t, int *), void *owner));
106
107 extern int stream_set_read __P ((stream_t,
108 int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)), void *owner));
109
110 extern int stream_set_readline __P ((stream_t,
111 int (*_readline) __P ((stream_t, char *, size_t, off_t, size_t *)), void *owner));
112
113 extern int stream_set_size __P ((stream_t,
114 int (*_size) __P ((stream_t, off_t *)), void *owner));
115
116 extern int stream_set_truncate __P ((stream_t,
117 int (*_truncate) __P ((stream_t, off_t)), void *owner));
118
119 extern int stream_set_write __P ((stream_t,
120 int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *)), void *owner));
121
122 extern int stream_set_flush __P ((stream_t,
123 int (*_flush) __P ((stream_t)), void *owner));
128 124
129 #ifdef __cplusplus 125 #ifdef __cplusplus
130 } 126 }
131 #endif 127 #endif
132 128
133 #endif /* _MAILUTILS_STREAM_H */ 129 #endif /* _MAILUTILS_STREAM_H */
130
......
...@@ -92,8 +92,8 @@ int message_create_attachment(const char *content_type, const char *encoding, co ...@@ -92,8 +92,8 @@ int message_create_attachment(const char *content_type, const char *encoding, co
92 sprintf(header, MSG_HDR, content_type, name, encoding, name); 92 sprintf(header, MSG_HDR, content_type, name, encoding, name);
93 if ( ( ret = header_create( &hdr, header, strlen(header), *newmsg ) ) == 0 ) { 93 if ( ( ret = header_create( &hdr, header, strlen(header), *newmsg ) ) == 0 ) {
94 message_get_body(*newmsg, &body); 94 message_get_body(*newmsg, &body);
95 if ( ( ret = file_stream_create(&fstream) ) == 0 ) { 95 if ( ( ret = file_stream_create(&fstream, filename, MU_STREAM_READ) ) == 0 ) {
96 if ( ( ret = stream_open(fstream, filename, 0, MU_STREAM_READ) ) == 0 ) { 96 if ( ( ret = stream_open(fstream) ) == 0 ) {
97 if ( ( ret = filter_create(&tstream, fstream, encoding, MU_FILTER_ENCODE, MU_STREAM_READ) ) == 0 ) { 97 if ( ( ret = filter_create(&tstream, fstream, encoding, MU_FILTER_ENCODE, MU_STREAM_READ) ) == 0 ) {
98 body_set_stream(body, tstream, *newmsg); 98 body_set_stream(body, tstream, *newmsg);
99 message_set_header(*newmsg, hdr, NULL); 99 message_set_header(*newmsg, hdr, NULL);
...@@ -255,8 +255,8 @@ int message_save_attachment(message_t msg, const char *filename, void **data) ...@@ -255,8 +255,8 @@ int message_save_attachment(message_t msg, const char *filename, void **data)
255 ret = message_attachment_filename(msg, &fname); 255 ret = message_attachment_filename(msg, &fname);
256 else 256 else
257 fname = filename; 257 fname = filename;
258 if ( fname && ( ret = file_stream_create(&info->fstream) ) == 0 ) { 258 if ( fname && ( ret = file_stream_create(&info->fstream, fname, MU_STREAM_WRITE|MU_STREAM_CREAT) ) == 0 ) {
259 if ( ( ret = stream_open(info->fstream, fname, 0, MU_STREAM_WRITE|MU_STREAM_CREAT) ) == 0 ) { 259 if ( ( ret = stream_open(info->fstream) ) == 0 ) {
260 header_get_value(hdr, "Content-Transfer-Encoding", NULL, 0, &size); 260 header_get_value(hdr, "Content-Transfer-Encoding", NULL, 0, &size);
261 if ( size ) { 261 if ( size ) {
262 if ( ( content_encoding = alloca(size+1) ) == NULL ) 262 if ( ( content_encoding = alloca(size+1) ) == NULL )
......
...@@ -147,16 +147,16 @@ body_get_stream (body_t body, stream_t *pstream) ...@@ -147,16 +147,16 @@ body_get_stream (body_t body, stream_t *pstream)
147 if (body->stream == NULL) 147 if (body->stream == NULL)
148 { 148 {
149 int fd; 149 int fd;
150 int status = stream_create (&(body->stream), MU_STREAM_RDWR, body); 150 int status = stream_create (&body->stream, MU_STREAM_RDWR, body);
151 if (status != 0) 151 if (status != 0)
152 return status; 152 return status;
153 status = file_stream_create (&(body->fstream)); 153 status = file_stream_create (&body->fstream, body->filename, MU_STREAM_RDWR);
154 if (status != 0) 154 if (status != 0)
155 return status; 155 return status;
156 fd = lazy_create (body); 156 fd = lazy_create (body);
157 if (fd == -1) 157 if (fd == -1)
158 return errno; 158 return errno;
159 status = stream_open (body->fstream, body->filename, 0, MU_STREAM_RDWR); 159 status = stream_open (body->fstream);
160 close (fd); 160 close (fd);
161 if (status != 0) 161 if (status != 0)
162 return status; 162 return status;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 21
22 #include <assert.h>
22 #include <errno.h> 23 #include <errno.h>
23 #include <stdio.h> 24 #include <stdio.h>
24 #include <stdlib.h> 25 #include <stdlib.h>
...@@ -36,14 +37,20 @@ struct _file_stream ...@@ -36,14 +37,20 @@ struct _file_stream
36 { 37 {
37 FILE *file; 38 FILE *file;
38 int offset; 39 int offset;
40
41 char *filename;
39 }; 42 };
40 43
41 static void 44 static void
42 _file_destroy (stream_t stream) 45 _file_destroy (stream_t stream)
43 { 46 {
44 struct _file_stream *fs = stream_get_owner (stream); 47 struct _file_stream *fs = stream_get_owner (stream);
45 if (fs->file) 48
46 fclose (fs->file); 49 stream_close (stream);
50
51 if (fs->filename)
52 free (fs->filename);
53
47 free (fs); 54 free (fs);
48 } 55 }
49 56
...@@ -222,24 +229,42 @@ _file_close (stream_t stream) ...@@ -222,24 +229,42 @@ _file_close (stream_t stream)
222 { 229 {
223 struct _file_stream *fs = stream_get_owner (stream); 230 struct _file_stream *fs = stream_get_owner (stream);
224 int err = 0; 231 int err = 0;
232
233 if (!stream)
234 return EINVAL;
235
225 if (fs->file) 236 if (fs->file)
226 { 237 {
238 int flags = 0;
239
240 stream_get_flags (stream, &flags);
241
242 if ((flags & MU_STREAM_NO_CLOSE) == 0)
243 {
227 if (fclose (fs->file) != 0) 244 if (fclose (fs->file) != 0)
228 err = errno; 245 err = errno;
246 }
247
229 fs->file = NULL; 248 fs->file = NULL;
230 } 249 }
231 return err; 250 return err;
232 } 251 }
233 252
234 static int 253 static int
235 _file_open (stream_t stream, const char *filename, int port, int flags) 254 _file_open (stream_t stream)
236 { 255 {
237 struct _file_stream *fs = stream_get_owner (stream); 256 struct _file_stream *fs = stream_get_owner (stream);
238 int flg; 257 int flg;
239 int fd; 258 int fd;
240 const char *mode; 259 const char *mode;
260 char* filename = 0;
261 int flags = 0;
262
263 assert(fs);
241 264
242 (void)port; /* Ignored. */ 265 filename = fs->filename;
266
267 assert(filename);
243 268
244 if (fs->file) 269 if (fs->file)
245 { 270 {
...@@ -247,6 +272,8 @@ _file_open (stream_t stream, const char *filename, int port, int flags) ...@@ -247,6 +272,8 @@ _file_open (stream_t stream, const char *filename, int port, int flags)
247 fs->file = NULL; 272 fs->file = NULL;
248 } 273 }
249 274
275 stream_get_flags(stream, &flags);
276
250 /* Map the flags to the system equivalent. */ 277 /* Map the flags to the system equivalent. */
251 if (flags & MU_STREAM_WRITE && flags & MU_STREAM_READ) 278 if (flags & MU_STREAM_WRITE && flags & MU_STREAM_READ)
252 return EINVAL; 279 return EINVAL;
...@@ -327,15 +354,14 @@ _file_open (stream_t stream, const char *filename, int port, int flags) ...@@ -327,15 +354,14 @@ _file_open (stream_t stream, const char *filename, int port, int flags)
327 if (fs->file == NULL) 354 if (fs->file == NULL)
328 { 355 {
329 int ret = errno; 356 int ret = errno;
330 free (fs);
331 return ret; 357 return ret;
332 } 358 }
333 stream_set_flags (stream, flags |MU_STREAM_NO_CHECK); 359
334 return 0; 360 return 0;
335 } 361 }
336 362
337 int 363 int
338 file_stream_create (stream_t *stream) 364 file_stream_create (stream_t *stream, const char* filename, int flags)
339 { 365 {
340 struct _file_stream *fs; 366 struct _file_stream *fs;
341 int ret; 367 int ret;
...@@ -347,10 +373,17 @@ file_stream_create (stream_t *stream) ...@@ -347,10 +373,17 @@ file_stream_create (stream_t *stream)
347 if (fs == NULL) 373 if (fs == NULL)
348 return ENOMEM; 374 return ENOMEM;
349 375
350 ret = stream_create (stream, MU_STREAM_NO_CHECK, fs); 376 if ((fs->filename = strdup(filename)) == NULL)
377 {
378 free (fs);
379 return ENOMEM;
380 }
381
382 ret = stream_create (stream, flags|MU_STREAM_NO_CHECK, fs);
351 if (ret != 0) 383 if (ret != 0)
352 { 384 {
353 free (fs); 385 free (fs);
386 free (fs->filename);
354 return ret; 387 return ret;
355 } 388 }
356 389
...@@ -364,5 +397,46 @@ file_stream_create (stream_t *stream) ...@@ -364,5 +397,46 @@ file_stream_create (stream_t *stream)
364 stream_set_size (*stream, _file_size, fs); 397 stream_set_size (*stream, _file_size, fs);
365 stream_set_flush (*stream, _file_flush, fs); 398 stream_set_flush (*stream, _file_flush, fs);
366 stream_set_destroy (*stream, _file_destroy, fs); 399 stream_set_destroy (*stream, _file_destroy, fs);
400
367 return 0; 401 return 0;
368 } 402 }
403
404 int
405 stdio_stream_create (stream_t *stream, FILE* file, int flags)
406 {
407 struct _file_stream *fs;
408 int ret;
409
410 if (stream == NULL)
411 return EINVAL;
412
413 if (file == NULL)
414 return EINVAL;
415
416 fs = calloc (1, sizeof (struct _file_stream));
417 if (fs == NULL)
418 return ENOMEM;
419
420 fs->file = file;
421
422 ret = stream_create (stream, flags|MU_STREAM_NO_CHECK, fs);
423 if (ret != 0)
424 {
425 free (fs);
426 return ret;
427 }
428
429 /* We don't need to open the FILE, just return success. */
430
431 stream_set_open (*stream, NULL, fs);
432 stream_set_close (*stream, _file_close, fs);
433 stream_set_fd (*stream, _file_get_fd, fs);
434 stream_set_read (*stream, _file_read, fs);
435 stream_set_readline (*stream, _file_readline, fs);
436 stream_set_write (*stream, _file_write, fs);
437 stream_set_flush (*stream, _file_flush, fs);
438 stream_set_destroy (*stream, _file_destroy, fs);
439
440 return 0;
441 }
442
......
...@@ -80,11 +80,11 @@ filter_write (stream_t stream, const char *buffer, size_t buflen, ...@@ -80,11 +80,11 @@ filter_write (stream_t stream, const char *buffer, size_t buflen,
80 } 80 }
81 81
82 static int 82 static int
83 filter_open (stream_t stream, const char *filename, int port, int flags) 83 filter_open (stream_t stream)
84 { 84 {
85 filter_t filter = stream_get_owner (stream); 85 filter_t filter = stream_get_owner (stream);
86 stream_set_flags (stream, flags); 86
87 return stream_open (filter->stream, filename, port, flags); 87 return stream_open (filter->stream);
88 } 88 }
89 89
90 static int 90 static int
......
...@@ -434,9 +434,9 @@ folder_imap_open (folder_t folder, int flags) ...@@ -434,9 +434,9 @@ folder_imap_open (folder_t folder, int flags)
434 { 434 {
435 CHECK_ERROR (f_imap, ENOMEM); 435 CHECK_ERROR (f_imap, ENOMEM);
436 } 436 }
437 status = memory_stream_create (&f_imap->string.stream); 437 status = memory_stream_create (&f_imap->string.stream, NULL, MU_STREAM_RDWR);
438 CHECK_ERROR (f_imap, status); 438 CHECK_ERROR (f_imap, status);
439 stream_open (f_imap->string.stream, NULL, 0, MU_STREAM_RDWR); 439 stream_open (f_imap->string.stream);
440 } 440 }
441 else 441 else
442 { 442 {
...@@ -451,7 +451,7 @@ folder_imap_open (folder_t folder, int flags) ...@@ -451,7 +451,7 @@ folder_imap_open (folder_t folder, int flags)
451 /* Create the networking stack. */ 451 /* Create the networking stack. */
452 if (folder->stream == NULL) 452 if (folder->stream == NULL)
453 { 453 {
454 status = tcp_stream_create (&(folder->stream)); 454 status = tcp_stream_create (&folder->stream, host, port, folder->flags);
455 CHECK_ERROR (f_imap, status); 455 CHECK_ERROR (f_imap, status);
456 /* Ask for the stream internal buffering mechanism scheme. */ 456 /* Ask for the stream internal buffering mechanism scheme. */
457 stream_setbufsiz (folder->stream, BUFSIZ); 457 stream_setbufsiz (folder->stream, BUFSIZ);
...@@ -463,7 +463,7 @@ folder_imap_open (folder_t folder, int flags) ...@@ -463,7 +463,7 @@ folder_imap_open (folder_t folder, int flags)
463 463
464 case IMAP_OPEN_CONNECTION: 464 case IMAP_OPEN_CONNECTION:
465 /* Establish the connection. */ 465 /* Establish the connection. */
466 status = stream_open (folder->stream, host, port, folder->flags); 466 status = stream_open (folder->stream);
467 CHECK_EAGAIN (f_imap, status); 467 CHECK_EAGAIN (f_imap, status);
468 /* Can't recover bailout. */ 468 /* Can't recover bailout. */
469 CHECK_ERROR_CLOSE (folder, f_imap, status); 469 CHECK_ERROR_CLOSE (folder, f_imap, status);
......
...@@ -864,10 +864,10 @@ fill_blurb (header_t header) ...@@ -864,10 +864,10 @@ fill_blurb (header_t header)
864 864
865 if (header->mstream == NULL) 865 if (header->mstream == NULL)
866 { 866 {
867 status = memory_stream_create (&(header->mstream)); 867 status = memory_stream_create (&header->mstream, NULL, MU_STREAM_RDWR);
868 if (status != 0) 868 if (status != 0)
869 return status; 869 return status;
870 stream_open (header->mstream, NULL, 0, MU_STREAM_RDWR); 870 stream_open (header->mstream);
871 header->stream_len = 0; 871 header->stream_len = 0;
872 } 872 }
873 873
...@@ -940,10 +940,15 @@ header_write (stream_t os, const char *buf, size_t buflen, ...@@ -940,10 +940,15 @@ header_write (stream_t os, const char *buf, size_t buflen,
940 940
941 if (header->mstream == NULL) 941 if (header->mstream == NULL)
942 { 942 {
943 status = memory_stream_create (&(header->mstream)); 943 status = memory_stream_create (&header->mstream, NULL, MU_STREAM_RDWR);
944 if (status != 0) 944 if (status != 0)
945 return status; 945 return status;
946 stream_open (header->mstream, NULL, 0, MU_STREAM_RDWR); 946 status = stream_open (header->mstream);
947 if (status != 0)
948 {
949 stream_destroy(&header->mstream, NULL);
950 return status;
951 }
947 header->stream_len = 0; 952 header->stream_len = 0;
948 } 953 }
949 954
......
...@@ -20,5 +20,4 @@ observer0.h \ ...@@ -20,5 +20,4 @@ observer0.h \
20 property0.h \ 20 property0.h \
21 registrar0.h \ 21 registrar0.h \
22 stream0.h \ 22 stream0.h \
23 tcp0.h \
24 url0.h 23 url0.h
......
...@@ -58,7 +58,7 @@ struct _stream ...@@ -58,7 +58,7 @@ struct _stream
58 struct rbuffer rbuffer; 58 struct rbuffer rbuffer;
59 59
60 void (*_destroy) __P ((stream_t)); 60 void (*_destroy) __P ((stream_t));
61 int (*_open) __P ((stream_t, const char *, int port, int flags)); 61 int (*_open) __P ((stream_t));
62 int (*_close) __P ((stream_t)); 62 int (*_close) __P ((stream_t));
63 int (*_get_fd) __P ((stream_t, int *)); 63 int (*_get_fd) __P ((stream_t, int *));
64 int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)); 64 int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *));
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _TCP0_H
19 #define _TCP0_H
20
21 #define TCP_STATE_INIT 1
22 #define TCP_STATE_RESOLVE 2
23 #define TCP_STATE_RESOLVING 3
24 #define TCP_STATE_CONNECTING 4
25 #define TCP_STATE_CONNECTED 5
26
27 struct _tcp_instance {
28 int fd;
29 char *host;
30 int port;
31 int state;
32 unsigned long address;
33 };
34
35 #endif /* _TCP0_H */
...@@ -44,6 +44,7 @@ struct _mapfile_stream ...@@ -44,6 +44,7 @@ struct _mapfile_stream
44 int flags; 44 int flags;
45 char *ptr; 45 char *ptr;
46 size_t size; 46 size_t size;
47 char* filename;
47 }; 48 };
48 49
49 static void 50 static void
...@@ -57,6 +58,7 @@ _mapfile_destroy (stream_t stream) ...@@ -57,6 +58,7 @@ _mapfile_destroy (stream_t stream)
57 munmap (mfs->ptr, mfs->size); 58 munmap (mfs->ptr, mfs->size);
58 close (mfs->fd); 59 close (mfs->fd);
59 } 60 }
61 free (mfs->filename);
60 free (mfs); 62 free (mfs);
61 } 63 }
62 64
...@@ -259,13 +261,15 @@ _mapfile_close (stream_t stream) ...@@ -259,13 +261,15 @@ _mapfile_close (stream_t stream)
259 } 261 }
260 262
261 static int 263 static int
262 _mapfile_open (stream_t stream, const char *filename, int port, int flags) 264 _mapfile_open (stream_t stream)
263 { 265 {
264 struct _mapfile_stream *mfs = stream_get_owner (stream); 266 struct _mapfile_stream *mfs = stream_get_owner (stream);
265 int mflag, flg; 267 int mflag, flg;
266 struct stat st; 268 struct stat st;
269 char* filename = mfs->filename;
270 int flags;
267 271
268 (void)port; /* Ignored. */ 272 stream_get_flags (stream, &flags);
269 273
270 /* Close any previous file. */ 274 /* Close any previous file. */
271 if (mfs->ptr != MAP_FAILED) 275 if (mfs->ptr != MAP_FAILED)
...@@ -331,7 +335,7 @@ _mapfile_open (stream_t stream, const char *filename, int port, int flags) ...@@ -331,7 +335,7 @@ _mapfile_open (stream_t stream, const char *filename, int port, int flags)
331 #endif /* _POSIX_MAPPED_FILES */ 335 #endif /* _POSIX_MAPPED_FILES */
332 336
333 int 337 int
334 mapfile_stream_create (stream_t *stream) 338 mapfile_stream_create (stream_t *stream, const char* filename, int flags)
335 { 339 {
336 #ifndef _POSIX_MAPPED_FILES 340 #ifndef _POSIX_MAPPED_FILES
337 return ENOSYS; 341 return ENOSYS;
...@@ -339,19 +343,27 @@ mapfile_stream_create (stream_t *stream) ...@@ -339,19 +343,27 @@ mapfile_stream_create (stream_t *stream)
339 struct _mapfile_stream *fs; 343 struct _mapfile_stream *fs;
340 int ret; 344 int ret;
341 345
342 if (stream == NULL) 346 if (stream == NULL || filename == NULL)
343 return EINVAL; 347 return EINVAL;
344 348
345 fs = calloc (1, sizeof (struct _mapfile_stream)); 349 fs = calloc (1, sizeof (struct _mapfile_stream));
346 if (fs == NULL) 350 if (fs == NULL)
347 return ENOMEM; 351 return ENOMEM;
348 352
353 fs->filename = strdup (filename);
354 if (!fs->filename)
355 {
356 free (fs);
357 return ENOMEM;
358 }
359
349 fs->fd = -1; 360 fs->fd = -1;
350 fs->ptr = MAP_FAILED; 361 fs->ptr = MAP_FAILED;
351 362
352 ret = stream_create (stream, MU_STREAM_NO_CHECK, fs); 363 ret = stream_create (stream, flags | MU_STREAM_NO_CHECK, fs);
353 if (ret != 0) 364 if (ret != 0)
354 { 365 {
366 free (fs->filename);
355 free (fs); 367 free (fs);
356 return ret; 368 return ret;
357 } 369 }
......
...@@ -345,34 +345,34 @@ mbox_open (mailbox_t mailbox, int flags) ...@@ -345,34 +345,34 @@ mbox_open (mailbox_t mailbox, int flags)
345 /* Try to mmap () the file first. */ 345 /* Try to mmap () the file first. */
346 if (status == 0) 346 if (status == 0)
347 { 347 {
348 status = mapfile_stream_create (&(mailbox->stream)); 348 status = mapfile_stream_create (&mailbox->stream, mud->name, mailbox->flags);
349 if (status == 0) 349 if (status == 0)
350 { 350 {
351 status = stream_open (mailbox->stream, mud->name, 0, 351 status = stream_open (mailbox->stream);
352 mailbox->flags);
353 if (status != 0)
354 stream_destroy (&mailbox->stream, NULL);
355 } 352 }
356 } 353 }
357 354
358 /* Fall back to normal file if mmap() failed. */ 355 /* Fall back to normal file if mmap() failed. */
359 if (status != 0) 356 if (status != 0)
360 { 357 {
361 status = file_stream_create (&(mailbox->stream)); 358 status = file_stream_create (&mailbox->stream, mud->name, mailbox->flags);
362 if (status != 0) 359 if (status != 0)
363 return status; 360 return status;
364 status = stream_open (mailbox->stream, mud->name, 0, mailbox->flags); 361 status = stream_open (mailbox->stream);
365 } 362 }
366 /* All failed, bail out. */ 363 /* All failed, bail out. */
367 if (status != 0) 364 if (status != 0)
365 {
366 stream_destroy (&mailbox->stream, NULL);
368 return status; 367 return status;
368 }
369 /* Even on top, of normal FILE *, lets agressively cache. But this 369 /* Even on top, of normal FILE *, lets agressively cache. But this
370 may not be suitable for system tight on memory. */ 370 may not be suitable for system tight on memory. */
371 stream_setbufsiz (mailbox->stream, BUFSIZ); 371 stream_setbufsiz (mailbox->stream, BUFSIZ);
372 } 372 }
373 else 373 else
374 { 374 {
375 status = stream_open (mailbox->stream, mud->name, 0, mailbox->flags); 375 status = stream_open (mailbox->stream);
376 if (status != 0) 376 if (status != 0)
377 return status; 377 return status;
378 } 378 }
......
...@@ -1164,18 +1164,25 @@ mh_message_stream_open (struct _mh_message *mhm) ...@@ -1164,18 +1164,25 @@ mh_message_stream_open (struct _mh_message *mhm)
1164 char *filename = NULL; 1164 char *filename = NULL;
1165 int status; 1165 int status;
1166 1166
1167 if (file_stream_create (&mhm->stream))
1168 return errno;
1169
1170 filename = _mh_message_name (mhm, mhm->deleted); 1167 filename = _mh_message_name (mhm, mhm->deleted);
1171 1168
1172 if (!filename) 1169 if (!filename)
1173 return ENOMEM; 1170 return ENOMEM;
1174 1171
1175 status = stream_open (mhm->stream, filename, 0, mhd->mailbox->flags); 1172 status = file_stream_create (&mhm->stream, filename, mhd->mailbox->flags);
1176 1173
1177 free (filename); 1174 free (filename);
1178 1175
1176 if (status != 0)
1177 return status;
1178
1179 status = stream_open (mhm->stream);
1180
1181 if (status != 0)
1182 {
1183 stream_destroy (&mhm->stream, NULL);
1184 }
1185
1179 if (status == 0) 1186 if (status == 0)
1180 status = mh_scan_message (mhm); 1187 status = mh_scan_message (mhm);
1181 1188
......
...@@ -587,7 +587,7 @@ pop_open (mailbox_t mbox, int flags) ...@@ -587,7 +587,7 @@ pop_open (mailbox_t mbox, int flags)
587 /* Create the networking stack. */ 587 /* Create the networking stack. */
588 if (mbox->stream == NULL) 588 if (mbox->stream == NULL)
589 { 589 {
590 status = tcp_stream_create (&(mbox->stream)); 590 status = tcp_stream_create (&mbox->stream, host, port, mbox->flags);
591 CHECK_ERROR(mpd, status); 591 CHECK_ERROR(mpd, status);
592 /* Using the awkward stream_t buffering. */ 592 /* Using the awkward stream_t buffering. */
593 stream_setbufsiz (mbox->stream, BUFSIZ); 593 stream_setbufsiz (mbox->stream, BUFSIZ);
...@@ -610,7 +610,7 @@ pop_open (mailbox_t mbox, int flags) ...@@ -610,7 +610,7 @@ pop_open (mailbox_t mbox, int flags)
610 case POP_OPEN_CONNECTION: 610 case POP_OPEN_CONNECTION:
611 /* Establish the connection. */ 611 /* Establish the connection. */
612 MAILBOX_DEBUG2 (mbox, MU_DEBUG_PROT, "open (%s:%d)\n", host, port); 612 MAILBOX_DEBUG2 (mbox, MU_DEBUG_PROT, "open (%s:%d)\n", host, port);
613 status = stream_open (mbox->stream, host, port, mbox->flags); 613 status = stream_open (mbox->stream);
614 CHECK_EAGAIN (mpd, status); 614 CHECK_EAGAIN (mpd, status);
615 /* Can't recover bailout. */ 615 /* Can't recover bailout. */
616 CHECK_ERROR_CLOSE (mbox, mpd, status); 616 CHECK_ERROR_CLOSE (mbox, mpd, status);
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
37 37
38 struct _memory_stream 38 struct _memory_stream
39 { 39 {
40 char *filename;
40 char *ptr; 41 char *ptr;
41 size_t size; 42 size_t size;
42 size_t capacity; 43 size_t capacity;
...@@ -48,6 +49,8 @@ _memory_destroy (stream_t stream) ...@@ -48,6 +49,8 @@ _memory_destroy (stream_t stream)
48 struct _memory_stream *mfs = stream_get_owner (stream); 49 struct _memory_stream *mfs = stream_get_owner (stream);
49 if (mfs && mfs->ptr != NULL) 50 if (mfs && mfs->ptr != NULL)
50 free (mfs->ptr); 51 free (mfs->ptr);
52 if(mfs->filename)
53 free (mfs->filename);
51 free (mfs); 54 free (mfs);
52 } 55 }
53 56
...@@ -160,26 +163,23 @@ _memory_close (stream_t stream) ...@@ -160,26 +163,23 @@ _memory_close (stream_t stream)
160 } 163 }
161 164
162 static int 165 static int
163 _memory_open (stream_t stream, const char *filename, int port, int flags) 166 _memory_open (stream_t stream)
164 { 167 {
165 struct _memory_stream *mfs = stream_get_owner (stream); 168 struct _memory_stream *mfs = stream_get_owner (stream);
166 int status = 0; 169 int status = 0;
167 170
168 (void)port; /* Ignored. */
169 (void)filename; /* Ignored. */
170 (void)flags; /* Ignored. */
171
172 /* Close any previous file. */ 171 /* Close any previous file. */
173 if (mfs->ptr) 172 if (mfs->ptr)
174 free (mfs->ptr); 173 free (mfs->ptr);
175 mfs->ptr = NULL; 174 mfs->ptr = NULL;
176 mfs->size = 0; 175 mfs->size = 0;
177 mfs->capacity = 0; 176 mfs->capacity = 0;
178 stream_set_flags (stream, flags |MU_STREAM_NO_CHECK); 177
179 if (filename) 178 /* Initialize the data with file contents, if a filename was provided. */
179 if (mfs->filename)
180 { 180 {
181 struct stat statbuf; 181 struct stat statbuf;
182 if (stat (filename, &statbuf) == 0) 182 if (stat (mfs->filename, &statbuf) == 0)
183 { 183 {
184 mfs->ptr = calloc (statbuf.st_size, 1); 184 mfs->ptr = calloc (statbuf.st_size, 1);
185 if (mfs->ptr) 185 if (mfs->ptr)
...@@ -187,7 +187,7 @@ _memory_open (stream_t stream, const char *filename, int port, int flags) ...@@ -187,7 +187,7 @@ _memory_open (stream_t stream, const char *filename, int port, int flags)
187 FILE *fp; 187 FILE *fp;
188 mfs->capacity = statbuf.st_size; 188 mfs->capacity = statbuf.st_size;
189 mfs->size = statbuf.st_size; 189 mfs->size = statbuf.st_size;
190 fp = fopen (filename, "r"); 190 fp = fopen (mfs->filename, "r");
191 if (fp) 191 if (fp)
192 { 192 {
193 size_t r = fread (mfs->ptr, mfs->size, 1, fp); 193 size_t r = fread (mfs->ptr, mfs->size, 1, fp);
...@@ -215,7 +215,7 @@ _memory_open (stream_t stream, const char *filename, int port, int flags) ...@@ -215,7 +215,7 @@ _memory_open (stream_t stream, const char *filename, int port, int flags)
215 } 215 }
216 216
217 int 217 int
218 memory_stream_create (stream_t *stream) 218 memory_stream_create (stream_t * stream, const char *filename, int flags)
219 { 219 {
220 struct _memory_stream *mfs; 220 struct _memory_stream *mfs;
221 int ret; 221 int ret;
...@@ -224,16 +224,29 @@ memory_stream_create (stream_t *stream) ...@@ -224,16 +224,29 @@ memory_stream_create (stream_t *stream)
224 return EINVAL; 224 return EINVAL;
225 225
226 mfs = calloc (1, sizeof (*mfs)); 226 mfs = calloc (1, sizeof (*mfs));
227
227 if (mfs == NULL) 228 if (mfs == NULL)
228 return ENOMEM; 229 return ENOMEM;
229 230
231 if (filename)
232 {
233 mfs->filename = strdup (filename);
234 if (!mfs->filename)
235 {
236 free (mfs);
237 return ENOMEM;
238 }
239 }
240
230 mfs->ptr = NULL; 241 mfs->ptr = NULL;
231 mfs->size = 0; 242 mfs->size = 0;
232 243
233 ret = stream_create (stream, MU_STREAM_NO_CHECK, mfs); 244 ret = stream_create (stream, flags | MU_STREAM_NO_CHECK, mfs);
234 if (ret != 0) 245 if (ret != 0)
235 { 246 {
247 free (mfs->filename);
236 free (mfs); 248 free (mfs);
249
237 return ret; 250 return ret;
238 } 251 }
239 252
...@@ -245,5 +258,6 @@ memory_stream_create (stream_t *stream) ...@@ -245,5 +258,6 @@ memory_stream_create (stream_t *stream)
245 stream_set_truncate (*stream, _memory_truncate, mfs); 258 stream_set_truncate (*stream, _memory_truncate, mfs);
246 stream_set_size (*stream, _memory_size, mfs); 259 stream_set_size (*stream, _memory_size, mfs);
247 stream_set_destroy (*stream, _memory_destroy, mfs); 260 stream_set_destroy (*stream, _memory_destroy, mfs);
261
248 return 0; 262 return 0;
249 } 263 }
......
...@@ -302,7 +302,7 @@ smtp_open (mailer_t mailer, int flags) ...@@ -302,7 +302,7 @@ smtp_open (mailer_t mailer, int flags)
302 /* Create a TCP stack if one is not given. */ 302 /* Create a TCP stack if one is not given. */
303 if (mailer->stream == NULL) 303 if (mailer->stream == NULL)
304 { 304 {
305 status = tcp_stream_create (&(mailer->stream)); 305 status = tcp_stream_create (&mailer->stream, smtp->mailhost, port, mailer->flags);
306 CHECK_ERROR (smtp, status); 306 CHECK_ERROR (smtp, status);
307 stream_setbufsiz (mailer->stream, BUFSIZ); 307 stream_setbufsiz (mailer->stream, BUFSIZ);
308 } 308 }
...@@ -312,8 +312,7 @@ smtp_open (mailer_t mailer, int flags) ...@@ -312,8 +312,7 @@ smtp_open (mailer_t mailer, int flags)
312 case SMTP_OPEN: 312 case SMTP_OPEN:
313 MAILER_DEBUG2 (mailer, MU_DEBUG_PROT, "smtp_open (%s:%d)\n", 313 MAILER_DEBUG2 (mailer, MU_DEBUG_PROT, "smtp_open (%s:%d)\n",
314 smtp->mailhost, port); 314 smtp->mailhost, port);
315 status = stream_open (mailer->stream, smtp->mailhost, port, 315 status = stream_open (mailer->stream);
316 mailer->flags);
317 CHECK_EAGAIN (smtp, status); 316 CHECK_EAGAIN (smtp, status);
318 smtp->state = SMTP_GREETINGS; 317 smtp->state = SMTP_GREETINGS;
319 318
......
...@@ -71,8 +71,7 @@ stream_destroy (stream_t *pstream, void *owner) ...@@ -71,8 +71,7 @@ stream_destroy (stream_t *pstream, void *owner)
71 stream_t stream = *pstream; 71 stream_t stream = *pstream;
72 if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner) 72 if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner)
73 { 73 {
74 if (stream->_destroy) 74 stream_close(stream);
75 stream->_destroy (stream);
76 if (stream->rbuffer.base) 75 if (stream->rbuffer.base)
77 free (stream->rbuffer.base); 76 free (stream->rbuffer.base);
78 free (stream); 77 free (stream);
...@@ -88,14 +87,14 @@ stream_get_owner (stream_t stream) ...@@ -88,14 +87,14 @@ stream_get_owner (stream_t stream)
88 } 87 }
89 88
90 int 89 int
91 stream_open (stream_t stream, const char *name, int port, int flags) 90 stream_open (stream_t stream)
92 { 91 {
93 if (stream == NULL) 92 if (stream == NULL)
94 return EINVAL; 93 return EINVAL;
95 stream->state = MU_STREAM_STATE_OPEN; 94 stream->state = MU_STREAM_STATE_OPEN;
96 stream->flags |= flags; 95
97 if (stream->_open) 96 if (stream->_open)
98 return stream->_open (stream, name, port, flags); 97 return stream->_open (stream);
99 return 0; 98 return 0;
100 } 99 }
101 100
...@@ -424,7 +423,7 @@ stream_get_fd (stream_t stream, int *pfd) ...@@ -424,7 +423,7 @@ stream_get_fd (stream_t stream, int *pfd)
424 int 423 int
425 stream_get_flags (stream_t stream, int *pfl) 424 stream_get_flags (stream_t stream, int *pfl)
426 { 425 {
427 if (stream == NULL && pfl == NULL ) 426 if (stream == NULL || pfl == NULL )
428 return EINVAL; 427 return EINVAL;
429 *pfl = stream->flags; 428 *pfl = stream->flags;
430 return 0; 429 return 0;
...@@ -509,7 +508,7 @@ stream_set_destroy (stream_t stream, void (*_destroy) (stream_t), void *owner) ...@@ -509,7 +508,7 @@ stream_set_destroy (stream_t stream, void (*_destroy) (stream_t), void *owner)
509 508
510 int 509 int
511 stream_set_open (stream_t stream, 510 stream_set_open (stream_t stream,
512 int (*_open) (stream_t, const char *, int, int), void *owner) 511 int (*_open) (stream_t), void *owner)
513 { 512 {
514 if (stream == NULL) 513 if (stream == NULL)
515 return EINVAL; 514 return EINVAL;
......
...@@ -32,7 +32,20 @@ ...@@ -32,7 +32,20 @@
32 #include <unistd.h> 32 #include <unistd.h>
33 33
34 #include <mailutils/stream.h> 34 #include <mailutils/stream.h>
35 #include <tcp0.h> 35
36 #define TCP_STATE_INIT 1
37 #define TCP_STATE_RESOLVE 2
38 #define TCP_STATE_RESOLVING 3
39 #define TCP_STATE_CONNECTING 4
40 #define TCP_STATE_CONNECTED 5
41
42 struct _tcp_instance {
43 int fd;
44 char *host;
45 int port;
46 int state;
47 unsigned long address;
48 };
36 49
37 /* On solaris inet_addr() return -1. */ 50 /* On solaris inet_addr() return -1. */
38 #ifndef INADDR_NONE 51 #ifndef INADDR_NONE
...@@ -52,7 +65,7 @@ _tcp_close (stream_t stream) ...@@ -52,7 +65,7 @@ _tcp_close (stream_t stream)
52 } 65 }
53 66
54 static int 67 static int
55 _tcp_open (stream_t stream, const char *host, int port, int flags) 68 _tcp_open (stream_t stream)
56 { 69 {
57 struct _tcp_instance *tcp = stream_get_owner (stream); 70 struct _tcp_instance *tcp = stream_get_owner (stream);
58 int flgs, ret; 71 int flgs, ret;
...@@ -60,6 +73,11 @@ _tcp_open (stream_t stream, const char *host, int port, int flags) ...@@ -60,6 +73,11 @@ _tcp_open (stream_t stream, const char *host, int port, int flags)
60 struct sockaddr_in peer_addr; 73 struct sockaddr_in peer_addr;
61 struct hostent *phe; 74 struct hostent *phe;
62 struct sockaddr_in soc_addr; 75 struct sockaddr_in soc_addr;
76 char* host = tcp->host;
77 int port = tcp->port;
78 int flags;
79
80 stream_get_flags(stream, &flags);
63 81
64 if (tcp->state == TCP_STATE_INIT) 82 if (tcp->state == TCP_STATE_INIT)
65 { 83 {
...@@ -197,11 +215,13 @@ _tcp_destroy (stream_t stream) ...@@ -197,11 +215,13 @@ _tcp_destroy (stream_t stream)
197 if (tcp->fd != -1) 215 if (tcp->fd != -1)
198 close (tcp->fd); 216 close (tcp->fd);
199 217
218 if(tcp->host)
219 free (tcp->host);
200 free (tcp); 220 free (tcp);
201 } 221 }
202 222
203 int 223 int
204 tcp_stream_create (stream_t * stream) 224 tcp_stream_create (stream_t * stream, const char* host, int port, int flags)
205 { 225 {
206 struct _tcp_instance *tcp; 226 struct _tcp_instance *tcp;
207 int ret; 227 int ret;
...@@ -209,17 +229,30 @@ tcp_stream_create (stream_t * stream) ...@@ -209,17 +229,30 @@ tcp_stream_create (stream_t * stream)
209 if ((tcp = malloc (sizeof (*tcp))) == NULL) 229 if ((tcp = malloc (sizeof (*tcp))) == NULL)
210 return ENOMEM; 230 return ENOMEM;
211 tcp->fd = -1; 231 tcp->fd = -1;
212 tcp->host = NULL; 232 tcp->host = strdup (host);
213 tcp->port = -1; 233 if(!tcp->host)
234 {
235 free (tcp);
236 return ENOMEM;
237 }
238 tcp->port = port;
214 tcp->state = TCP_STATE_INIT; 239 tcp->state = TCP_STATE_INIT;
215 if ((ret = 240
216 stream_create (stream, MU_STREAM_NO_CHECK | MU_STREAM_RDWR, tcp)) != 0) 241 if ((ret = stream_create (stream,
242 flags | MU_STREAM_NO_CHECK | MU_STREAM_RDWR, tcp)) != 0)
243 {
244 free (tcp->host);
245 free (tcp);
246
217 return ret; 247 return ret;
248 }
249
218 stream_set_open (*stream, _tcp_open, tcp); 250 stream_set_open (*stream, _tcp_open, tcp);
219 stream_set_close (*stream, _tcp_close, tcp); 251 stream_set_close (*stream, _tcp_close, tcp);
220 stream_set_read (*stream, _tcp_read, tcp); 252 stream_set_read (*stream, _tcp_read, tcp);
221 stream_set_write (*stream, _tcp_write, tcp); 253 stream_set_write (*stream, _tcp_write, tcp);
222 stream_set_fd (*stream, _tcp_get_fd, tcp); 254 stream_set_fd (*stream, _tcp_get_fd, tcp);
223 stream_set_destroy (*stream, _tcp_destroy, tcp); 255 stream_set_destroy (*stream, _tcp_destroy, tcp);
256
224 return 0; 257 return 0;
225 } 258 }
......