attribute.c auth.c body.c header.c io.c mailbox.c mbx_pop.c
mbx_unix.c mbx_unixscan.c message.c mime.c tcp.c transcode.c include/private/attribute0.h include/private/io0.h include/private/mailbox0.h include/private/tcp.h include/public/attribute.h include/public/auth.h include/public/io.h include/public/mailbox.h include/public/message.h added the first basic support for pop. include/private/auth0.h move the definition of auth_t struct here. net.c include/private/net0.h include/public/net.h Overhaul change of the net_t by D. I.
Showing
24 changed files
with
302 additions
and
581 deletions
... | @@ -23,27 +23,25 @@ | ... | @@ -23,27 +23,25 @@ |
23 | #include <errno.h> | 23 | #include <errno.h> |
24 | 24 | ||
25 | int | 25 | int |
26 | attribute_create (attribute_t *pattr, void *owner) | 26 | attribute_create (attribute_t *pattr) |
27 | { | 27 | { |
28 | attribute_t attr; | 28 | attribute_t attr; |
29 | if (pattr == NULL || owner == NULL) | 29 | if (pattr == NULL) |
30 | return EINVAL; | 30 | return EINVAL; |
31 | attr = calloc (1, sizeof(*attr)); | 31 | attr = calloc (1, sizeof(*attr)); |
32 | if (attr == NULL) | 32 | if (attr == NULL) |
33 | return ENOMEM; | 33 | return ENOMEM; |
34 | attr->owner = owner; | ||
35 | *pattr = attr; | 34 | *pattr = attr; |
36 | return 0; | 35 | return 0; |
37 | } | 36 | } |
38 | 37 | ||
39 | void | 38 | void |
40 | attribute_destroy (attribute_t *pattr, void *owner) | 39 | attribute_destroy (attribute_t *pattr) |
41 | { | 40 | { |
42 | if (pattr && *pattr) | 41 | if (pattr && *pattr) |
43 | { | 42 | { |
44 | attribute_t attr = *pattr; | 43 | attribute_t attr = *pattr; |
45 | if (attr->owner == owner) | 44 | free (attr); |
46 | free (attr); | ||
47 | /* loose the link */ | 45 | /* loose the link */ |
48 | *pattr = NULL; | 46 | *pattr = NULL; |
49 | } | 47 | } |
... | @@ -254,12 +252,12 @@ attribute_copy (attribute_t dest, attribute_t src) | ... | @@ -254,12 +252,12 @@ attribute_copy (attribute_t dest, attribute_t src) |
254 | } | 252 | } |
255 | 253 | ||
256 | int | 254 | int |
257 | string_to_attribute (const char *buffer, attribute_t *pattr, void *owner) | 255 | string_to_attribute (const char *buffer, attribute_t *pattr) |
258 | { | 256 | { |
259 | char *sep; | 257 | const char *sep; |
260 | int status; | 258 | int status; |
261 | 259 | ||
262 | status = attribute_create (pattr, owner); | 260 | status = attribute_create (pattr); |
263 | if (status != 0) | 261 | if (status != 0) |
264 | return status; | 262 | return status; |
265 | 263 | ||
... | @@ -268,16 +266,19 @@ string_to_attribute (const char *buffer, attribute_t *pattr, void *owner) | ... | @@ -268,16 +266,19 @@ string_to_attribute (const char *buffer, attribute_t *pattr, void *owner) |
268 | { | 266 | { |
269 | sep = strchr(buffer, ':'); /* pass the ':' */ | 267 | sep = strchr(buffer, ':'); /* pass the ':' */ |
270 | sep++; | 268 | sep++; |
271 | while (*sep == ' ') sep++; /* glob spaces */ | ||
272 | if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL) | ||
273 | attribute_set_read (*pattr); | ||
274 | if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL) | ||
275 | attribute_set_seen (*pattr); | ||
276 | if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL) | ||
277 | attribute_set_answered (*pattr); | ||
278 | if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL) | ||
279 | attribute_set_flagged (*pattr); | ||
280 | } | 269 | } |
270 | else | ||
271 | sep = buffer; | ||
272 | |||
273 | while (*sep == ' ') sep++; /* glob spaces */ | ||
274 | if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL) | ||
275 | attribute_set_read (*pattr); | ||
276 | if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL) | ||
277 | attribute_set_seen (*pattr); | ||
278 | if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL) | ||
279 | attribute_set_answered (*pattr); | ||
280 | if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL) | ||
281 | attribute_set_flagged (*pattr); | ||
281 | return 0; | 282 | return 0; |
282 | } | 283 | } |
283 | 284 | ... | ... |
... | @@ -15,7 +15,7 @@ | ... | @@ -15,7 +15,7 @@ |
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
17 | 17 | ||
18 | #include <auth.h> | 18 | #include <auth0.h> |
19 | #include <cpystr.h> | 19 | #include <cpystr.h> |
20 | 20 | ||
21 | #include <errno.h> | 21 | #include <errno.h> |
... | @@ -23,14 +23,6 @@ | ... | @@ -23,14 +23,6 @@ |
23 | #include <string.h> | 23 | #include <string.h> |
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | 25 | ||
26 | struct _auth | ||
27 | { | ||
28 | void *owner; | ||
29 | int (*_prologue) (auth_t); | ||
30 | int (*_authenticate) (auth_t); | ||
31 | int (*_epilogue) (auth_t); | ||
32 | }; | ||
33 | |||
34 | int | 26 | int |
35 | auth_create (auth_t *pauth, void *owner) | 27 | auth_create (auth_t *pauth, void *owner) |
36 | { | 28 | { |
... | @@ -58,7 +50,8 @@ auth_destroy (auth_t *pauth, void *owner) | ... | @@ -58,7 +50,8 @@ auth_destroy (auth_t *pauth, void *owner) |
58 | } | 50 | } |
59 | 51 | ||
60 | int | 52 | int |
61 | auth_set_authenticate (auth_t auth, int (*_authenticate)(auth_t), | 53 | auth_set_authenticate (auth_t auth, |
54 | int (*_authenticate)(auth_t, char **, char **), | ||
62 | void *owner) | 55 | void *owner) |
63 | { | 56 | { |
64 | if (auth == NULL) | 57 | if (auth == NULL) |
... | @@ -70,11 +63,11 @@ auth_set_authenticate (auth_t auth, int (*_authenticate)(auth_t), | ... | @@ -70,11 +63,11 @@ auth_set_authenticate (auth_t auth, int (*_authenticate)(auth_t), |
70 | } | 63 | } |
71 | 64 | ||
72 | int | 65 | int |
73 | auth_authenticate (auth_t auth) | 66 | auth_authenticate (auth_t auth, char **user, char **passwd) |
74 | { | 67 | { |
75 | if (auth == NULL || auth->_authenticate == NULL) | 68 | if (auth == NULL || auth->_authenticate == NULL) |
76 | return EINVAL; | 69 | return EINVAL; |
77 | return auth->_authenticate (auth); | 70 | return auth->_authenticate (auth, user, passwd); |
78 | } | 71 | } |
79 | 72 | ||
80 | int | 73 | int |
... | @@ -91,7 +84,7 @@ auth_set_epilogue (auth_t auth, int (*_epilogue)(auth_t), void *owner) | ... | @@ -91,7 +84,7 @@ auth_set_epilogue (auth_t auth, int (*_epilogue)(auth_t), void *owner) |
91 | int | 84 | int |
92 | auth_epilogue (auth_t auth) | 85 | auth_epilogue (auth_t auth) |
93 | { | 86 | { |
94 | if (auth == NULL && auth->_epilogue == NULL) | 87 | if (auth == NULL || auth->_epilogue == NULL) |
95 | return EINVAL; | 88 | return EINVAL; |
96 | return auth->_epilogue (auth); | 89 | return auth->_epilogue (auth); |
97 | } | 90 | } |
... | @@ -110,7 +103,7 @@ auth_set_prologue (auth_t auth, int (*_prologue)(auth_t), void *owner) | ... | @@ -110,7 +103,7 @@ auth_set_prologue (auth_t auth, int (*_prologue)(auth_t), void *owner) |
110 | int | 103 | int |
111 | auth_prologue (auth_t auth) | 104 | auth_prologue (auth_t auth) |
112 | { | 105 | { |
113 | if (auth == NULL && auth->_prologue == NULL) | 106 | if (auth == NULL || auth->_prologue == NULL) |
114 | return EINVAL; | 107 | return EINVAL; |
115 | return auth->_prologue (auth); | 108 | return auth->_prologue (auth); |
116 | } | 109 | } | ... | ... |
... | @@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream) | ... | @@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream) |
82 | /* lazy floating body it is created when | 82 | /* lazy floating body it is created when |
83 | * doing the first body_write call | 83 | * doing the first body_write call |
84 | */ | 84 | */ |
85 | status = stream_create (&stream, 0, body); | 85 | status = stream_create (&stream, MU_STREAM_RDWR, body); |
86 | if (status != 0) | 86 | if (status != 0) |
87 | return status; | 87 | return status; |
88 | stream_set_read (stream, body_read, body); | 88 | stream_set_read (stream, body_read, body); | ... | ... |
... | @@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner) | ... | @@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner) |
68 | 68 | ||
69 | header_parse (h, (char *)blurb, len); | 69 | header_parse (h, (char *)blurb, len); |
70 | 70 | ||
71 | status = stream_create (&(h->stream), 0, h); | 71 | status = stream_create (&(h->stream), MU_STREAM_READ|MU_STREAM_WRITE, h); |
72 | if (status != 0) | 72 | if (status != 0) |
73 | return status; | 73 | return status; |
74 | 74 | ... | ... |
... | @@ -2,30 +2,25 @@ | ... | @@ -2,30 +2,25 @@ |
2 | Copyright (C) 1999, 2000 Free Software Foundation, Inc. | 2 | Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This program is free software; you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU General Library Public License as published by |
6 | the Free Software Foundation; either version 2, or (at your option) | 6 | the Free Software Foundation; either version 2, or (at your option) |
7 | any later version. | 7 | any later version. |
8 | 8 | ||
9 | This program is distributed in the hope that it will be useful, | 9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. | 12 | GNU Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 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 | 15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
17 | 17 | ||
18 | #ifndef _NET_H | 18 | #ifndef _AUTH0_H |
19 | #define _NET_H | 19 | #define _AUTH0_H |
20 | 20 | ||
21 | #include <auth.h> | ||
21 | #include <sys/types.h> | 22 | #include <sys/types.h> |
22 | 23 | ||
23 | #include <io.h> | ||
24 | |||
25 | #ifdef _cplusplus | ||
26 | extern "C" { | ||
27 | #endif | ||
28 | |||
29 | #ifndef __P | 24 | #ifndef __P |
30 | #ifdef __STDC__ | 25 | #ifdef __STDC__ |
31 | #define __P(args) args | 26 | #define __P(args) args |
... | @@ -34,24 +29,21 @@ extern "C" { | ... | @@ -34,24 +29,21 @@ extern "C" { |
34 | #endif | 29 | #endif |
35 | #endif /*__P */ | 30 | #endif /*__P */ |
36 | 31 | ||
37 | struct _net; | 32 | #ifdef _cplusplus |
38 | typedef struct _net *net_t; | 33 | extern "C" { |
39 | 34 | #endif | |
40 | extern int net_api_create __P((net_t *, net_t, const char *type)); | ||
41 | extern int net_api_set_option __P((net_t net, const char *name, const char *value)); | ||
42 | extern int net_api_destroy __P((net_t *)); | ||
43 | 35 | ||
44 | struct _netinstance; | 36 | struct _auth |
45 | typedef struct _netinstance *netinstance_t; | 37 | { |
38 | void *owner; | ||
39 | int (*_prologue) (auth_t); | ||
40 | int (*_authenticate) (auth_t, char **user, char **passwd); | ||
41 | int (*_epilogue) (auth_t); | ||
42 | }; | ||
46 | 43 | ||
47 | extern int net_new __P((net_t, netinstance_t *)); | ||
48 | extern int net_connect __P((netinstance_t, const char *host, int port)); | ||
49 | extern int net_get_stream __P((netinstance_t, stream_t *iostr)); | ||
50 | extern int net_close __P((netinstance_t)); | ||
51 | extern int net_free __P((netinstance_t *)); | ||
52 | 44 | ||
53 | #ifdef _cplusplus | 45 | #ifdef _cplusplus |
54 | } | 46 | } |
55 | #endif | 47 | #endif |
56 | 48 | ||
57 | #endif /* NET_H */ | 49 | #endif /* _AUTH0_H */ | ... | ... |
... | @@ -36,7 +36,9 @@ struct _stream | ... | @@ -36,7 +36,9 @@ struct _stream |
36 | { | 36 | { |
37 | void *owner; | 37 | void *owner; |
38 | int flags; | 38 | int flags; |
39 | void (*_destroy) __P ((void *)); | 39 | void (*_destroy) __P ((stream_t)); |
40 | int (*_open) __P ((stream_t, const char *, int port, int flags)); | ||
41 | int (*_close) __P ((stream_t)); | ||
40 | int (*_get_fd) __P ((stream_t, int *)); | 42 | int (*_get_fd) __P ((stream_t, int *)); |
41 | int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)); | 43 | int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)); |
42 | int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *)); | 44 | int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *)); | ... | ... |
... | @@ -42,7 +42,7 @@ struct _mailbox | ... | @@ -42,7 +42,7 @@ struct _mailbox |
42 | char *name; | 42 | char *name; |
43 | auth_t auth; | 43 | auth_t auth; |
44 | locker_t locker; | 44 | locker_t locker; |
45 | netinstance_t netinstance; | 45 | stream_t stream; |
46 | url_t url; | 46 | url_t url; |
47 | 47 | ||
48 | /* register events */ | 48 | /* register events */ | ... | ... |
mailbox/include/private/net0.h
deleted
100644 → 0
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 _NET0_H | ||
19 | #define _NET0_H | ||
20 | |||
21 | #include <sys/types.h> | ||
22 | |||
23 | #include <io.h> | ||
24 | #include <net.h> | ||
25 | |||
26 | #ifdef _cplusplus | ||
27 | extern "C" { | ||
28 | #endif | ||
29 | |||
30 | #ifndef __P | ||
31 | #ifdef __STDC__ | ||
32 | #define __P(args) args | ||
33 | #else | ||
34 | #define __P(args) () | ||
35 | #endif | ||
36 | #endif /*__P */ | ||
37 | |||
38 | |||
39 | struct _net_api { | ||
40 | int (*new)(void *netdata, net_t parent, void **data); | ||
41 | int (*connect)(void *data, const char *host, int port); | ||
42 | int (*get_stream)(void *data, stream_t *iostr); | ||
43 | int (*close)(void *data); | ||
44 | int (*free)(void **data); | ||
45 | }; | ||
46 | |||
47 | struct _netregistrar { | ||
48 | const char *type; | ||
49 | |||
50 | int (*create)(void **netdata, struct _net_api **api); | ||
51 | int (*set_option)(void *netdata, const char *name, const char *value); | ||
52 | int (*destroy)(void **netdata); | ||
53 | }; | ||
54 | |||
55 | struct _net { | ||
56 | struct _net_api *api; | ||
57 | void *data; | ||
58 | struct _net *parent; | ||
59 | |||
60 | struct _netregistrar *net_reg; | ||
61 | }; | ||
62 | |||
63 | struct _netinstance { | ||
64 | struct _net_api *api; | ||
65 | void *data; | ||
66 | }; | ||
67 | |||
68 | int _tcp_create(void **data, struct _net_api **api); | ||
69 | int _tcp_set_option(void *data, const char *name, const char *value); | ||
70 | int _tcp_destroy(void **data); | ||
71 | |||
72 | #ifdef _cplusplus | ||
73 | } | ||
74 | #endif | ||
75 | |||
76 | #endif /* NET0_H */ |
... | @@ -15,25 +15,16 @@ | ... | @@ -15,25 +15,16 @@ |
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
17 | 17 | ||
18 | #include <net0.h> | ||
19 | |||
20 | #define TCP_STATE_INIT 1 | 18 | #define TCP_STATE_INIT 1 |
21 | #define TCP_STATE_RESOLVE 2 | 19 | #define TCP_STATE_RESOLVE 2 |
22 | #define TCP_STATE_RESOLVING 3 | 20 | #define TCP_STATE_RESOLVING 3 |
23 | #define TCP_STATE_CONNECTING 4 | 21 | #define TCP_STATE_CONNECTING 4 |
24 | #define TCP_STATE_CONNECTED 5 | 22 | #define TCP_STATE_CONNECTED 5 |
25 | 23 | ||
26 | struct _tcp_options { | ||
27 | int non_block; | ||
28 | int net_timeout; | ||
29 | }; | ||
30 | |||
31 | struct _tcp_instance { | 24 | struct _tcp_instance { |
32 | struct _tcp_options *options; | 25 | int fd; |
33 | int fd; | 26 | char *host; |
34 | char *host; | 27 | int port; |
35 | int port; | 28 | int state; |
36 | int state; | ||
37 | stream_t stream; | ||
38 | unsigned long address; | 29 | unsigned long address; |
39 | }; | 30 | }; | ... | ... |
... | @@ -35,8 +35,8 @@ extern "C" { | ... | @@ -35,8 +35,8 @@ extern "C" { |
35 | struct _attribute; | 35 | struct _attribute; |
36 | typedef struct _attribute * attribute_t; | 36 | typedef struct _attribute * attribute_t; |
37 | 37 | ||
38 | extern int attribute_create __P ((attribute_t *, void *owner)); | 38 | extern int attribute_create __P ((attribute_t *)); |
39 | extern void attribute_destroy __P ((attribute_t *, void *owner)); | 39 | extern void attribute_destroy __P ((attribute_t *)); |
40 | 40 | ||
41 | extern int attribute_is_seen __P ((attribute_t)); | 41 | extern int attribute_is_seen __P ((attribute_t)); |
42 | extern int attribute_is_answered __P ((attribute_t)); | 42 | extern int attribute_is_answered __P ((attribute_t)); |
... | @@ -68,10 +68,10 @@ extern int attribute_copy __P ((attribute_t dst, | ... | @@ -68,10 +68,10 @@ extern int attribute_copy __P ((attribute_t dst, |
68 | attribute_t src)); | 68 | attribute_t src)); |
69 | 69 | ||
70 | extern int string_to_attribute __P ((const char *buf, | 70 | extern int string_to_attribute __P ((const char *buf, |
71 | attribute_t *pattr, void *owner)); | 71 | attribute_t *pattr)); |
72 | extern int attribute_to_string __P ((attribute_t attr, char *buf, | 72 | extern int attribute_to_string __P ((attribute_t attr, char *buf, |
73 | size_t len, size_t *)); | 73 | size_t len, size_t *)); |
74 | extern int attribute_get_owner __P ((attribute_t attr, void **owner)); | 74 | |
75 | #ifdef __cplusplus | 75 | #ifdef __cplusplus |
76 | } | 76 | } |
77 | #endif | 77 | #endif | ... | ... |
... | @@ -36,7 +36,7 @@ extern "C" { | ... | @@ -36,7 +36,7 @@ extern "C" { |
36 | struct _auth; | 36 | struct _auth; |
37 | typedef struct _auth *auth_t; | 37 | typedef struct _auth *auth_t; |
38 | 38 | ||
39 | extern int auth_create __P ((auth_t *, void *owner)); | 39 | extern int auth_create __P ((auth_t *, void *owner)); |
40 | extern void auth_destroy __P ((auth_t *, void *owner)); | 40 | extern void auth_destroy __P ((auth_t *, void *owner)); |
41 | 41 | ||
42 | extern int auth_prologue __P ((auth_t)); | 42 | extern int auth_prologue __P ((auth_t)); |
... | @@ -44,9 +44,10 @@ extern int auth_set_prologue __P ((auth_t auth, | ... | @@ -44,9 +44,10 @@ extern int auth_set_prologue __P ((auth_t auth, |
44 | int (*_prologue) __P ((auth_t)), | 44 | int (*_prologue) __P ((auth_t)), |
45 | void *owner)); | 45 | void *owner)); |
46 | 46 | ||
47 | extern int auth_authenticate __P ((auth_t)); | 47 | extern int auth_authenticate __P ((auth_t, char **, char **)); |
48 | extern int auth_set_authenticate __P ((auth_t auth, | 48 | extern int auth_set_authenticate __P ((auth_t auth, |
49 | int (*_authenticate) __P ((auth_t)), | 49 | int (*_authenticate) |
50 | __P ((auth_t, char **, char **)), | ||
50 | void *owner)); | 51 | void *owner)); |
51 | 52 | ||
52 | extern int auth_epilogue __P ((auth_t)); | 53 | extern int auth_epilogue __P ((auth_t)); | ... | ... |
... | @@ -36,32 +36,58 @@ struct _stream; | ... | @@ -36,32 +36,58 @@ struct _stream; |
36 | typedef struct _stream *stream_t; | 36 | typedef struct _stream *stream_t; |
37 | 37 | ||
38 | /* stream will be destroy on stream_destroy */ | 38 | /* stream will be destroy on stream_destroy */ |
39 | #define MU_STREAM_NO_CHECK 1 | 39 | #define MU_STREAM_READ 0x00000001 |
40 | #define MU_STREAM_WRITE 0x00000002 | ||
41 | #define MU_STREAM_RDWR 0x00000004 | ||
42 | #define MU_STREAM_APPEND 0x00000008 | ||
43 | #define MU_STREAM_CREAT 0x00000010 | ||
44 | #define MU_STREAM_NONBLOCK 0x00000020 | ||
45 | #define MU_STREAM_NO_CHECK 0x00000040 | ||
40 | 46 | ||
41 | extern int stream_create __P ((stream_t *, int flags, void *owner)); | 47 | extern int stream_create __P ((stream_t *, int flags, void *owner)); |
42 | extern void stream_destroy __P ((stream_t *, void *owner)); | ||
43 | 48 | ||
44 | extern int stream_set_destroy __P ((stream_t, | 49 | extern void stream_destroy __P ((stream_t *, void *owner)); |
45 | void (*_destroy) __P ((void *)), | 50 | extern int stream_set_destroy __P ((stream_t, void (*_destroy) __P ((stream_t)), |
46 | void *owner)); | 51 | void *owner)); |
47 | 52 | ||
53 | extern int stream_open __P ((stream_t, const char *, int, int)); | ||
54 | extern int stream_set_open __P ((stream_t, | ||
55 | int (*_open) __P ((stream_t, const char *, | ||
56 | int, int)), | ||
57 | void *owner)); | ||
58 | |||
59 | extern int stream_close __P ((stream_t)); | ||
60 | extern int stream_set_close __P ((stream_t, int (*_close) __P ((stream_t)), | ||
61 | void *owner)); | ||
62 | |||
63 | extern int stream_get_fd __P ((stream_t , int *)); | ||
48 | extern int stream_set_fd __P ((stream_t, | 64 | extern int stream_set_fd __P ((stream_t, |
49 | int (*_get_fd)(stream_t, int *), | 65 | int (*_get_fd)(stream_t, int *), |
50 | void *owner)); | 66 | void *owner)); |
67 | |||
68 | extern int stream_read __P ((stream_t, char *, size_t, off_t, size_t *)); | ||
51 | extern int stream_set_read __P ((stream_t, | 69 | extern int stream_set_read __P ((stream_t, |
52 | int (*_read) __P ((stream_t, char *, | 70 | int (*_read) __P ((stream_t, char *, |
53 | size_t, off_t, size_t *)), | 71 | size_t, off_t, size_t *)), |
54 | void *owner)); | 72 | void *owner)); |
73 | |||
74 | extern int stream_write __P ((stream_t, const char *, size_t, off_t, size_t *)); | ||
55 | extern int stream_set_write __P ((stream_t, | 75 | extern int stream_set_write __P ((stream_t, |
56 | int (*_write) __P ((stream_t, const char *, | 76 | int (*_write) __P ((stream_t, const char *, |
57 | size_t, off_t, | 77 | size_t, off_t, |
58 | size_t *)), | 78 | size_t *)), |
59 | void *owner)); | 79 | void *owner)); |
60 | extern int stream_get_fd __P ((stream_t , int *)); | ||
61 | extern int stream_read __P ((stream_t, char *, size_t, off_t, size_t *)); | ||
62 | extern int stream_write __P ((stream_t, const char *, size_t, | ||
63 | off_t, size_t *)); | ||
64 | 80 | ||
81 | extern int stream_get_flags __P ((stream_t , int *flags)); | ||
82 | |||
83 | /* misc */ | ||
84 | extern int file_stream_create __P ((stream_t *stream, const char *filename, | ||
85 | int flags)); | ||
86 | extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream, | ||
87 | const char *encoding)); | ||
88 | extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream, | ||
89 | const char *encoding)); | ||
90 | extern int tcp_stream_create __P ((stream_t *stream)); | ||
65 | 91 | ||
66 | #ifdef __cplusplus | 92 | #ifdef __cplusplus |
67 | } | 93 | } | ... | ... |
... | @@ -23,7 +23,7 @@ | ... | @@ -23,7 +23,7 @@ |
23 | #include <attribute.h> | 23 | #include <attribute.h> |
24 | #include <auth.h> | 24 | #include <auth.h> |
25 | #include <locker.h> | 25 | #include <locker.h> |
26 | #include <net.h> | 26 | #include <io.h> |
27 | 27 | ||
28 | #include <sys/types.h> | 28 | #include <sys/types.h> |
29 | 29 | ||
... | @@ -48,12 +48,12 @@ extern int mailbox_create __P ((mailbox_t *, const char *, int id)); | ... | @@ -48,12 +48,12 @@ extern int mailbox_create __P ((mailbox_t *, const char *, int id)); |
48 | extern void mailbox_destroy __P ((mailbox_t *)); | 48 | extern void mailbox_destroy __P ((mailbox_t *)); |
49 | 49 | ||
50 | /* flags for mailbox_open () */ | 50 | /* flags for mailbox_open () */ |
51 | #define MU_MAILBOX_RDONLY ((int)1) | 51 | #define MU_MAILBOX_RDONLY MU_STREAM_READ |
52 | #define MU_MAILBOX_WRONLY (MU_MAILBOX_RDONLY << 1) | 52 | #define MU_MAILBOX_WRONLY MU_STREAM_WRITE |
53 | #define MU_MAILBOX_RDWR (MU_MAILBOX_WRONLY << 1) | 53 | #define MU_MAILBOX_RDWR MU_STREAM_RDWR |
54 | #define MU_MAILBOX_APPEND (MU_MAILBOX_RDWR << 1) | 54 | #define MU_MAILBOX_APPEND MU_STREAM_APPEND |
55 | #define MU_MAILBOX_CREAT (MU_MAILBOX_APPEND << 1) | 55 | #define MU_MAILBOX_CREAT MU_STREAM_CREAT |
56 | #define MU_MAILBOX_NONBLOCK (MU_MAILBOX_CREAT << 1) | 56 | #define MU_MAILBOX_NONBLOCK MU_STREAM_NONBLOCK |
57 | 57 | ||
58 | extern int mailbox_open __P ((mailbox_t, int flag)); | 58 | extern int mailbox_open __P ((mailbox_t, int flag)); |
59 | extern int mailbox_close __P ((mailbox_t)); | 59 | extern int mailbox_close __P ((mailbox_t)); |
... | @@ -64,9 +64,9 @@ extern int mailbox_append_message __P ((mailbox_t, message_t msg)); | ... | @@ -64,9 +64,9 @@ extern int mailbox_append_message __P ((mailbox_t, message_t msg)); |
64 | extern int mailbox_messages_count __P ((mailbox_t, size_t *num)); | 64 | extern int mailbox_messages_count __P ((mailbox_t, size_t *num)); |
65 | extern int mailbox_expunge __P ((mailbox_t)); | 65 | extern int mailbox_expunge __P ((mailbox_t)); |
66 | 66 | ||
67 | /* netinstance settings */ | 67 | /* stream settings */ |
68 | extern int mailbox_get_netinstance __P ((mailbox_t, netinstance_t *net)); | 68 | extern int mailbox_get_stream __P ((mailbox_t, stream_t *pstream)); |
69 | extern int mailbox_set_netinstance __P ((mailbox_t, netinstance_t net)); | 69 | extern int mailbox_set_stream __P ((mailbox_t, stream_t stream)); |
70 | 70 | ||
71 | /* Lock settings */ | 71 | /* Lock settings */ |
72 | extern int mailbox_get_locker __P ((mailbox_t, locker_t *locker)); | 72 | extern int mailbox_get_locker __P ((mailbox_t, locker_t *locker)); | ... | ... |
... | @@ -80,14 +80,25 @@ extern int message_set_received __P ((message_t, | ... | @@ -80,14 +80,25 @@ extern int message_set_received __P ((message_t, |
80 | extern int message_get_attribute __P ((message_t, attribute_t *)); | 80 | extern int message_get_attribute __P ((message_t, attribute_t *)); |
81 | extern int message_set_attribute __P ((message_t, attribute_t, void *owner)); | 81 | extern int message_set_attribute __P ((message_t, attribute_t, void *owner)); |
82 | 82 | ||
83 | //extern int message_clone __P ((message_t)); | ||
84 | |||
85 | /* events */ | 83 | /* events */ |
86 | #define MU_EVT_MSG_DESTROY 32 | 84 | #define MU_EVT_MSG_DESTROY 32 |
87 | extern int message_register __P ((message_t msg, size_t type, | 85 | extern int message_register __P ((message_t msg, size_t type, |
88 | int (*action) (size_t typ, void *arg), | 86 | int (*action) (size_t typ, void *arg), |
89 | void *arg)); | 87 | void *arg)); |
90 | extern int message_deregister __P ((message_t msg, void *action)); | 88 | extern int message_deregister __P ((message_t msg, void *action)); |
89 | |||
90 | /* misc functions */ | ||
91 | extern int message_create_attachment __P ((const char *content_type, | ||
92 | const char *encoding, | ||
93 | const char *filename, | ||
94 | message_t *newmsg)); | ||
95 | extern int message_save_attachment __P ((message_t msg, | ||
96 | const char *filename, void **data)); | ||
97 | extern int message_encapsulate __P ((message_t msg, message_t *newmsg, | ||
98 | void **data)); | ||
99 | extern int message_unencapsulate __P ((message_t msg, message_t *newmsg, | ||
100 | void **data)); | ||
101 | |||
91 | #ifdef _cplusplus | 102 | #ifdef _cplusplus |
92 | } | 103 | } |
93 | #endif | 104 | #endif | ... | ... |
... | @@ -39,21 +39,20 @@ stream_create (stream_t *pstream, int flags, void *owner) | ... | @@ -39,21 +39,20 @@ stream_create (stream_t *pstream, int flags, void *owner) |
39 | void | 39 | void |
40 | stream_destroy (stream_t *pstream, void *owner) | 40 | stream_destroy (stream_t *pstream, void *owner) |
41 | { | 41 | { |
42 | if (pstream && *pstream) | 42 | if (pstream && *pstream) |
43 | { | 43 | { |
44 | stream_t stream = *pstream; | 44 | stream_t stream = *pstream; |
45 | 45 | if (!(stream->flags & MU_STREAM_NO_CHECK) && stream->owner != owner) | |
46 | return; | ||
46 | if (stream->_destroy) | 47 | if (stream->_destroy) |
47 | stream->_destroy (owner); | 48 | stream->_destroy (stream); |
48 | if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner) | ||
49 | free (stream); | 49 | free (stream); |
50 | *pstream = NULL; | 50 | *pstream = NULL; |
51 | } | 51 | } |
52 | } | 52 | } |
53 | 53 | ||
54 | int | 54 | int |
55 | stream_set_destroy (stream_t stream, void (*_destroy) (void *), | 55 | stream_set_destroy (stream_t stream, void (*_destroy) (stream_t), void *owner) |
56 | void *owner) | ||
57 | { | 56 | { |
58 | if (stream == NULL) | 57 | if (stream == NULL) |
59 | return EINVAL; | 58 | return EINVAL; |
... | @@ -66,6 +65,53 @@ stream_set_destroy (stream_t stream, void (*_destroy) (void *), | ... | @@ -66,6 +65,53 @@ stream_set_destroy (stream_t stream, void (*_destroy) (void *), |
66 | } | 65 | } |
67 | 66 | ||
68 | int | 67 | int |
68 | stream_open (stream_t stream, const char *name, int port, int flags) | ||
69 | { | ||
70 | if (stream == NULL) | ||
71 | return EINVAL; | ||
72 | if (stream->_open) | ||
73 | return stream->_open (stream, name, port, flags); | ||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | int | ||
78 | stream_set_open (stream_t stream, | ||
79 | int (*_open) (stream_t, const char *, int, int), void *owner) | ||
80 | { | ||
81 | if (stream == NULL) | ||
82 | return EINVAL; | ||
83 | if (owner == stream->owner) | ||
84 | { | ||
85 | stream->_open = _open; | ||
86 | return 0; | ||
87 | } | ||
88 | return EACCES; | ||
89 | } | ||
90 | |||
91 | int | ||
92 | stream_close (stream_t stream) | ||
93 | { | ||
94 | if (stream == NULL) | ||
95 | return EINVAL; | ||
96 | if (stream->_close) | ||
97 | return stream->_close (stream); | ||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | int | ||
102 | stream_set_close (stream_t stream, int (*_close) (stream_t), void *owner) | ||
103 | { | ||
104 | if (stream == NULL) | ||
105 | return EINVAL; | ||
106 | if (owner == stream->owner) | ||
107 | { | ||
108 | stream->_close = _close; | ||
109 | return 0; | ||
110 | } | ||
111 | return EACCES; | ||
112 | } | ||
113 | |||
114 | int | ||
69 | stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner) | 115 | stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner) |
70 | { | 116 | { |
71 | if (stream == NULL) | 117 | if (stream == NULL) |
... | @@ -85,7 +131,9 @@ stream_set_read (stream_t stream, int (*_read) | ... | @@ -85,7 +131,9 @@ stream_set_read (stream_t stream, int (*_read) |
85 | { | 131 | { |
86 | if (stream == NULL) | 132 | if (stream == NULL) |
87 | return EINVAL; | 133 | return EINVAL; |
88 | if (owner == stream->owner) | 134 | if (owner == stream->owner && |
135 | ((stream->flags & MU_STREAM_READ) || | ||
136 | (stream->flags & MU_STREAM_RDWR))) | ||
89 | { | 137 | { |
90 | stream->_read = _read; | 138 | stream->_read = _read; |
91 | return 0; | 139 | return 0; |
... | @@ -100,7 +148,10 @@ stream_set_write (stream_t stream, int (*_write) | ... | @@ -100,7 +148,10 @@ stream_set_write (stream_t stream, int (*_write) |
100 | { | 148 | { |
101 | if (stream == NULL) | 149 | if (stream == NULL) |
102 | return EINVAL; | 150 | return EINVAL; |
103 | if (stream->owner == owner) | 151 | if (stream->owner == owner && |
152 | ((stream->flags & MU_STREAM_WRITE) || | ||
153 | (stream->flags & MU_STREAM_RDWR) || | ||
154 | (stream->flags & MU_STREAM_APPEND))) | ||
104 | { | 155 | { |
105 | stream->_write = _write; | 156 | stream->_write = _write; |
106 | return 0; | 157 | return 0; |
... | @@ -133,3 +184,12 @@ stream_get_fd (stream_t stream, int *pfd) | ... | @@ -133,3 +184,12 @@ stream_get_fd (stream_t stream, int *pfd) |
133 | return EINVAL; | 184 | return EINVAL; |
134 | return stream->_get_fd (stream, pfd); | 185 | return stream->_get_fd (stream, pfd); |
135 | } | 186 | } |
187 | |||
188 | int | ||
189 | stream_get_flags (stream_t stream, int *pfl) | ||
190 | { | ||
191 | if (stream == NULL && pfl == NULL ) | ||
192 | return EINVAL; | ||
193 | *pfl = stream->flags; | ||
194 | return 0; | ||
195 | } | ... | ... |
... | @@ -23,7 +23,6 @@ | ... | @@ -23,7 +23,6 @@ |
23 | #include <message0.h> | 23 | #include <message0.h> |
24 | #include <registrar.h> | 24 | #include <registrar.h> |
25 | #include <locker.h> | 25 | #include <locker.h> |
26 | #include <net.h> | ||
27 | 26 | ||
28 | #include <stdlib.h> | 27 | #include <stdlib.h> |
29 | #include <string.h> | 28 | #include <string.h> |
... | @@ -163,11 +162,12 @@ mailbox_set_locker (mailbox_t mbox, locker_t locker) | ... | @@ -163,11 +162,12 @@ mailbox_set_locker (mailbox_t mbox, locker_t locker) |
163 | } | 162 | } |
164 | 163 | ||
165 | int | 164 | int |
166 | mailbox_get_locker (mailbox_t mbox, locker_t *locker) | 165 | mailbox_get_locker (mailbox_t mbox, locker_t *plocker) |
167 | { | 166 | { |
168 | if (mbox == NULL || locker == NULL) | 167 | if (mbox == NULL || plocker == NULL) |
169 | return EINVAL; | 168 | return EINVAL; |
170 | *locker = mbox->locker; | 169 | if (plocker) |
170 | *plocker = mbox->locker; | ||
171 | return 0; | 171 | return 0; |
172 | } | 172 | } |
173 | 173 | ||
... | @@ -185,25 +185,27 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth) | ... | @@ -185,25 +185,27 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth) |
185 | { | 185 | { |
186 | if (mbox == NULL || pauth == NULL) | 186 | if (mbox == NULL || pauth == NULL) |
187 | return EINVAL; | 187 | return EINVAL; |
188 | *pauth = mbox->auth; | 188 | if (pauth) |
189 | *pauth = mbox->auth; | ||
189 | return 0; | 190 | return 0; |
190 | } | 191 | } |
191 | 192 | ||
192 | int | 193 | int |
193 | mailbox_set_netinstance (mailbox_t mbox, netinstance_t netinstance) | 194 | mailbox_set_stream (mailbox_t mbox, stream_t stream) |
194 | { | 195 | { |
195 | if (mbox == NULL) | 196 | if (mbox == NULL) |
196 | return EINVAL; | 197 | return EINVAL; |
197 | mbox->netinstance = netinstance; | 198 | mbox->stream = stream; |
198 | return 0; | 199 | return 0; |
199 | } | 200 | } |
200 | 201 | ||
201 | int | 202 | int |
202 | mailbox_get_netinstance (mailbox_t mbox, netinstance_t *pnetinstance) | 203 | mailbox_get_stream (mailbox_t mbox, stream_t *pstream) |
203 | { | 204 | { |
204 | if (mbox == NULL || pnetinstance == NULL) | 205 | if (mbox == NULL || pstream == NULL) |
205 | return EINVAL; | 206 | return EINVAL; |
206 | *pnetinstance = mbox->netinstance; | 207 | if (pstream) |
208 | *pstream = mbox->stream; | ||
207 | return 0; | 209 | return 0; |
208 | } | 210 | } |
209 | 211 | ... | ... |
This diff is collapsed.
Click to expand it.
... | @@ -313,8 +313,8 @@ mailbox_unix_destroy (mailbox_t *pmbox) | ... | @@ -313,8 +313,8 @@ mailbox_unix_destroy (mailbox_t *pmbox) |
313 | continue; | 313 | continue; |
314 | /* Destroy the attach messages */ | 314 | /* Destroy the attach messages */ |
315 | message_destroy (&(mum->message), mum); | 315 | message_destroy (&(mum->message), mum); |
316 | attribute_destroy (&(mum->old_attr), mbox); | 316 | attribute_destroy (&(mum->old_attr)); |
317 | attribute_destroy (&(mum->new_attr), mbox); | 317 | attribute_destroy (&(mum->new_attr)); |
318 | free (mum); | 318 | free (mum); |
319 | } | 319 | } |
320 | free (mud->umessages); | 320 | free (mud->umessages); |
... | @@ -399,7 +399,11 @@ mailbox_unix_open (mailbox_t mbox, int flags) | ... | @@ -399,7 +399,11 @@ mailbox_unix_open (mailbox_t mbox, int flags) |
399 | /* Authentication */ | 399 | /* Authentication */ |
400 | if (mbox->auth) | 400 | if (mbox->auth) |
401 | { | 401 | { |
402 | int status = auth_authenticate (mbox->auth); | 402 | char *user = NULL; |
403 | char *passwd = NULL; | ||
404 | int status = auth_authenticate (mbox->auth, &user, &passwd); | ||
405 | free (user); | ||
406 | free (passwd); | ||
403 | if (status != 0) | 407 | if (status != 0) |
404 | return status; | 408 | return status; |
405 | } | 409 | } |
... | @@ -516,7 +520,7 @@ mailbox_unix_is_updated (mailbox_t mbox) | ... | @@ -516,7 +520,7 @@ mailbox_unix_is_updated (mailbox_t mbox) |
516 | } | 520 | } |
517 | 521 | ||
518 | static int | 522 | static int |
519 | mailbox_unix_num_deleted (mailbox_t mbox, size_t *num) | 523 | mailbox_unix_num_deleted (mailbox_t mbox, size_t *pnum) |
520 | { | 524 | { |
521 | mailbox_unix_data_t mud; | 525 | mailbox_unix_data_t mud; |
522 | mailbox_unix_message_t mum; | 526 | mailbox_unix_message_t mum; |
... | @@ -531,8 +535,8 @@ mailbox_unix_num_deleted (mailbox_t mbox, size_t *num) | ... | @@ -531,8 +535,8 @@ mailbox_unix_num_deleted (mailbox_t mbox, size_t *num) |
531 | total++; | 535 | total++; |
532 | } | 536 | } |
533 | 537 | ||
534 | if (num) | 538 | if (pnum) |
535 | *num = total; | 539 | *pnum = total; |
536 | return 0; | 540 | return 0; |
537 | } | 541 | } |
538 | 542 | ||
... | @@ -894,7 +898,7 @@ mailbox_unix_getfd (stream_t is, int *pfd) | ... | @@ -894,7 +898,7 @@ mailbox_unix_getfd (stream_t is, int *pfd) |
894 | { | 898 | { |
895 | mailbox_unix_message_t mum; | 899 | mailbox_unix_message_t mum; |
896 | 900 | ||
897 | if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL) | 901 | if (is == NULL || (mum = is->owner) == NULL) |
898 | return EINVAL; | 902 | return EINVAL; |
899 | if (pfd) | 903 | if (pfd) |
900 | *pfd = fileno (mum->file); | 904 | *pfd = fileno (mum->file); |
... | @@ -908,7 +912,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, | ... | @@ -908,7 +912,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, |
908 | mailbox_unix_message_t mum; | 912 | mailbox_unix_message_t mum; |
909 | size_t nread = 0; | 913 | size_t nread = 0; |
910 | 914 | ||
911 | if (is == NULL || (mum = is->owner) == NULL) | 915 | if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL) |
912 | return EINVAL; | 916 | return EINVAL; |
913 | 917 | ||
914 | if (buffer == NULL || buflen == 0) | 918 | if (buffer == NULL || buflen == 0) |
... | @@ -1169,7 +1173,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -1169,7 +1173,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
1169 | } | 1173 | } |
1170 | message_set_body (msg, body, mum); | 1174 | message_set_body (msg, body, mum); |
1171 | 1175 | ||
1172 | status = stream_create (&stream, 0, mum); | 1176 | status = stream_create (&stream, MU_STREAM_READ, mum); |
1173 | if (status != 0) | 1177 | if (status != 0) |
1174 | { | 1178 | { |
1175 | message_destroy (&msg, mum); | 1179 | message_destroy (&msg, mum); | ... | ... |
... | @@ -254,7 +254,7 @@ do \ | ... | @@ -254,7 +254,7 @@ do \ |
254 | } while (0) | 254 | } while (0) |
255 | 255 | ||
256 | /* skip a function call, ?? do we gain that much */ | 256 | /* skip a function call, ?? do we gain that much */ |
257 | #define ATTRIBUTE_CREATE(attr,own) \ | 257 | #define ATTRIBUTE_CREATE(attr,mbox) \ |
258 | do \ | 258 | do \ |
259 | { \ | 259 | { \ |
260 | attr = calloc (1, sizeof(*(attr))); \ | 260 | attr = calloc (1, sizeof(*(attr))); \ |
... | @@ -265,7 +265,6 @@ do \ | ... | @@ -265,7 +265,6 @@ do \ |
265 | mailbox_unix_unlock (mbox); \ | 265 | mailbox_unix_unlock (mbox); \ |
266 | return ENOMEM; \ | 266 | return ENOMEM; \ |
267 | } \ | 267 | } \ |
268 | (attr)->owner = own; \ | ||
269 | } while (0) | 268 | } while (0) |
270 | 269 | ||
271 | /* allocate slots for the new messages */ | 270 | /* allocate slots for the new messages */ | ... | ... |
... | @@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner) | ... | @@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner) |
47 | msg = calloc (1, sizeof (*msg)); | 47 | msg = calloc (1, sizeof (*msg)); |
48 | if (msg == NULL) | 48 | if (msg == NULL) |
49 | return ENOMEM; | 49 | return ENOMEM; |
50 | status = stream_create (&stream, 0, msg); | 50 | status = stream_create (&stream, MU_STREAM_RDWR, msg); |
51 | if (status != 0) | 51 | if (status != 0) |
52 | { | 52 | { |
53 | free (msg); | 53 | free (msg); |
... | @@ -92,7 +92,7 @@ message_destroy (message_t *pmsg, void *owner) | ... | @@ -92,7 +92,7 @@ message_destroy (message_t *pmsg, void *owner) |
92 | /* header */ | 92 | /* header */ |
93 | header_destroy (&header, owner); | 93 | header_destroy (&header, owner); |
94 | /* attribute */ | 94 | /* attribute */ |
95 | attribute_destroy (&attribute, owner); | 95 | attribute_destroy (&attribute); |
96 | /* stream */ | 96 | /* stream */ |
97 | stream_destroy (&stream, owner); | 97 | stream_destroy (&stream, owner); |
98 | 98 | ||
... | @@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream) | ... | @@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream) |
188 | { | 188 | { |
189 | stream_t stream; | 189 | stream_t stream; |
190 | int status; | 190 | int status; |
191 | status = stream_create (&stream, 0, msg); | 191 | status = stream_create (&stream, MU_STREAM_RDWR, msg); |
192 | if (status != 0) | 192 | if (status != 0) |
193 | return status; | 193 | return status; |
194 | stream_set_read (stream, message_read, msg); | 194 | stream_set_read (stream, message_read, msg); |
... | @@ -350,7 +350,7 @@ message_get_attribute (message_t msg, attribute_t *pattribute) | ... | @@ -350,7 +350,7 @@ message_get_attribute (message_t msg, attribute_t *pattribute) |
350 | if (msg->attribute == NULL && msg->owner == NULL) | 350 | if (msg->attribute == NULL && msg->owner == NULL) |
351 | { | 351 | { |
352 | attribute_t attribute; | 352 | attribute_t attribute; |
353 | int status = attribute_create (&attribute, msg); | 353 | int status = attribute_create (&attribute); |
354 | if (status != 0) | 354 | if (status != 0) |
355 | return status; | 355 | return status; |
356 | msg->attribute = attribute; | 356 | msg->attribute = attribute; |
... | @@ -366,7 +366,7 @@ message_set_attribute (message_t msg, attribute_t attribute, void *owner) | ... | @@ -366,7 +366,7 @@ message_set_attribute (message_t msg, attribute_t attribute, void *owner) |
366 | return EINVAL; | 366 | return EINVAL; |
367 | if (msg->owner != owner) | 367 | if (msg->owner != owner) |
368 | return EACCES; | 368 | return EACCES; |
369 | attribute_destroy (&(msg->attribute), msg); | 369 | attribute_destroy (&(msg->attribute)); |
370 | msg->attribute = attribute; | 370 | msg->attribute = attribute; |
371 | return 0; | 371 | return 0; |
372 | } | 372 | } | ... | ... |
... | @@ -83,17 +83,6 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo | ... | @@ -83,17 +83,6 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo |
83 | return 0; | 83 | return 0; |
84 | } | 84 | } |
85 | 85 | ||
86 | static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg) | ||
87 | { | ||
88 | int i; | ||
89 | |||
90 | for ( i = 0; i < mime->nmtp_parts; i++ ) { | ||
91 | if ( mime->mtp_parts[i] == msg->owner ) | ||
92 | return mime->mtp_parts[i]; | ||
93 | } | ||
94 | return NULL; | ||
95 | } | ||
96 | |||
97 | static char *_strltrim(char *str) | 86 | static char *_strltrim(char *str) |
98 | { | 87 | { |
99 | char *p; | 88 | char *p; |
... | @@ -119,7 +108,7 @@ char *_strtrim(char *str); | ... | @@ -119,7 +108,7 @@ char *_strtrim(char *str); |
119 | #define _ISSPECIAL(c) ( \ | 108 | #define _ISSPECIAL(c) ( \ |
120 | ((c) == '(') || ((c) == ')') || ((c) == '<') || ((c) == '>') \ | 109 | ((c) == '(') || ((c) == ')') || ((c) == '<') || ((c) == '>') \ |
121 | || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \ | 110 | || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \ |
122 | || ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \ | 111 | || ((c) == '\\') || ((c) == '.') || ((c) == '[') \ |
123 | || ((c) == ']') ) | 112 | || ((c) == ']') ) |
124 | 113 | ||
125 | static void _mime_munge_content_header(char *field_body ) | 114 | static void _mime_munge_content_header(char *field_body ) |
... | @@ -129,23 +118,26 @@ static void _mime_munge_content_header(char *field_body ) | ... | @@ -129,23 +118,26 @@ static void _mime_munge_content_header(char *field_body ) |
129 | 118 | ||
130 | _strtrim(field_body); | 119 | _strtrim(field_body); |
131 | 120 | ||
132 | if ( ( e = p = strchr(str, ';') ) == NULL ) | 121 | if ( ( e = strchr(str, ';') ) == NULL ) |
133 | return; | 122 | return; |
134 | e++; | 123 | while( *e == ';' ) { |
135 | while ( *e && isspace(*e) ) /* remove space upto param */ | 124 | p = e; |
136 | e++; | 125 | e++; |
137 | memmove(p+1, e, strlen(e)+1); | 126 | while ( *e && isspace(*e) ) /* remove space upto param */ |
138 | e = p+1; | 127 | e++; |
139 | 128 | memmove(p+1, e, strlen(e)+1); | |
140 | while ( *e && *e != '=' ) /* find end of value */ | 129 | e = p+1; |
141 | e++; | 130 | |
142 | e = p = e+1; | 131 | while ( *e && *e != '=' ) /* find end of value */ |
143 | while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) { | 132 | e++; |
144 | if ( *e == '\\' ) { /* escaped */ | 133 | e = p = e+1; |
145 | memmove(e, e+1, strlen(e)+2); | 134 | while ( *e && (quoted || ( !_ISSPECIAL(*e) && !isspace(*e) ) ) ) { |
146 | } else if ( *e == '\"' ) | 135 | if ( *e == '\\' ) { /* escaped */ |
147 | quoted = ~quoted; | 136 | memmove(e, e+1, strlen(e)+2); |
148 | e++; | 137 | } else if ( *e == '\"' ) |
138 | quoted = ~quoted; | ||
139 | e++; | ||
140 | } | ||
149 | } | 141 | } |
150 | } | 142 | } |
151 | 143 | ||
... | @@ -164,7 +156,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) | ... | @@ -164,7 +156,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) |
164 | break; | 156 | break; |
165 | *len = 0; | 157 | *len = 0; |
166 | v = e = v + 1; | 158 | v = e = v + 1; |
167 | while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) { /* skip pass value and calc len */ | 159 | while ( *e && (quoted || ( !_ISSPECIAL(*e) && !isspace(*e) ) ) ) { /* skip pass value and calc len */ |
168 | if ( *e == '\"' ) | 160 | if ( *e == '\"' ) |
169 | quoted = ~quoted, was_quoted = 1; | 161 | quoted = ~quoted, was_quoted = 1; |
170 | else | 162 | else |
... | @@ -256,6 +248,8 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -256,6 +248,8 @@ static int _mime_parse_mpart_message(mime_t mime) |
256 | _mime_append_part(mime, NULL, body_offset, body_length, FALSE ); | 248 | _mime_append_part(mime, NULL, body_offset, body_length, FALSE ); |
257 | if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) || | 249 | if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) || |
258 | !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */ | 250 | !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */ |
251 | mime->parser_state = MIME_STATE_BEGIN_LINE; | ||
252 | mime->header_length = 0; | ||
259 | break; | 253 | break; |
260 | } | 254 | } |
261 | mime->line_ndx = -1; /* headers parsing requires empty line */ | 255 | mime->line_ndx = -1; /* headers parsing requires empty line */ |
... | @@ -293,6 +287,8 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -293,6 +287,8 @@ static int _mime_parse_mpart_message(mime_t mime) |
293 | mime->body_length = body_length; | 287 | mime->body_length = body_length; |
294 | mime->body_offset = body_offset; | 288 | mime->body_offset = body_offset; |
295 | if ( ret != EAGAIN ) { /* finished cleanup */ | 289 | if ( ret != EAGAIN ) { /* finished cleanup */ |
290 | if ( mime->header_length ) /* this skips the preamble */ | ||
291 | _mime_append_part(mime, NULL, body_offset, body_length, FALSE ); | ||
296 | mime->flags &= ~MIME_PARSER_ACTIVE; | 292 | mime->flags &= ~MIME_PARSER_ACTIVE; |
297 | mime->body_offset = mime->body_length = mime->header_length = 0; | 293 | mime->body_offset = mime->body_length = mime->header_length = 0; |
298 | } | 294 | } |
... | @@ -365,7 +361,7 @@ int mime_create(mime_t *pmime, message_t msg, int flags) | ... | @@ -365,7 +361,7 @@ int mime_create(mime_t *pmime, message_t msg, int flags) |
365 | } | 361 | } |
366 | } | 362 | } |
367 | else { | 363 | else { |
368 | if ( ( ret = message_create( &msg, mime ) ) == 0 ) { | 364 | if ( ( ret = message_create( &(mime->msg), mime ) ) == 0 ) { |
369 | mime->flags |= MIME_NEW_MESSAGE; | 365 | mime->flags |= MIME_NEW_MESSAGE; |
370 | } | 366 | } |
371 | } | 367 | } |
... | @@ -449,7 +445,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) | ... | @@ -449,7 +445,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) |
449 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); | 445 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); |
450 | header_size(mime_part->hdr, &hsize); | 446 | header_size(mime_part->hdr, &hsize); |
451 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { | 447 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { |
452 | if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) { | 448 | if ( ( ret = stream_create(&stream, MU_STREAM_READ, mime_part) ) == 0 ) { |
453 | body_set_size (body, _mime_body_size, mime_part); | 449 | body_set_size (body, _mime_body_size, mime_part); |
454 | stream_set_read(stream, _mime_message_read, mime_part); | 450 | stream_set_read(stream, _mime_message_read, mime_part); |
455 | body_set_stream(body, stream, mime_part); | 451 | body_set_stream(body, stream, mime_part); |
... | @@ -464,82 +460,6 @@ int mime_get_part(mime_t mime, int part, message_t *msg) | ... | @@ -464,82 +460,6 @@ int mime_get_part(mime_t mime, int part, message_t *msg) |
464 | return ret; | 460 | return ret; |
465 | } | 461 | } |
466 | 462 | ||
467 | int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) | ||
468 | { | ||
469 | size_t size, nbytes; | ||
470 | int ret, body_offset = 0, body_length = 0, done = 0; | ||
471 | char *content_type, *cp; | ||
472 | header_t hdr; | ||
473 | stream_t stream; | ||
474 | body_t body; | ||
475 | struct _mime_part *mime_part; | ||
476 | |||
477 | if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE ) | ||
478 | return EINVAL; | ||
479 | |||
480 | if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */ | ||
481 | return EPERM; | ||
482 | |||
483 | if ( ( ret = message_get_header(msg, &hdr) ) == 0 ) { | ||
484 | if ( ( ret = header_get_value(hdr, "Content-Type", NULL, 0, &size) ) == 0 && size ) { | ||
485 | if ( ( content_type = malloc(size+1) ) == NULL ) | ||
486 | ret = ENOMEM; | ||
487 | else if ( ( ret = header_get_value(hdr, "Content-Type", content_type, size+1, 0) ) == 0 ) { | ||
488 | _mime_munge_content_header(content_type); | ||
489 | if ( strncasecmp(content_type, "message/rfc822", strlen(content_type)) != 0 ) | ||
490 | ret = EINVAL; | ||
491 | else { | ||
492 | if ( mime_part ) { | ||
493 | body_offset = mime_part->body_offset; | ||
494 | body_length = mime_part->body_len; | ||
495 | } | ||
496 | if ( ( ret = _mime_setup_buffers(mime) ) == 0 ) { | ||
497 | mime->line_ndx = 0; | ||
498 | mime->cur_offset = body_offset; | ||
499 | while ( !done && ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) { | ||
500 | cp = mime->cur_buf; | ||
501 | while ( nbytes ) { | ||
502 | mime->cur_line[mime->line_ndx] = *cp; | ||
503 | mime->line_ndx++; | ||
504 | if ( *cp == '\n' ) { | ||
505 | _mime_append_header_line(mime); | ||
506 | if ( mime->line_ndx == 1 ) { | ||
507 | done = 1; | ||
508 | break; | ||
509 | } | ||
510 | mime->line_ndx = 0; | ||
511 | } | ||
512 | mime->cur_offset++; | ||
513 | nbytes--; | ||
514 | cp++; | ||
515 | } | ||
516 | } | ||
517 | body_length -= mime->cur_offset - body_offset; | ||
518 | body_offset = mime->cur_offset + 1; | ||
519 | if ( ( ret = _mime_append_part( mime, NULL, body_offset, body_length, TRUE ) ) == 0 ) { | ||
520 | mime_part = mime->cap_msgs[mime->ncap_msgs - 1]; | ||
521 | if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) { | ||
522 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); | ||
523 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { | ||
524 | if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) { | ||
525 | stream_set_read(stream, _mime_message_read, mime_part); | ||
526 | body_set_size (body, _mime_body_size, mime_part); | ||
527 | body_set_stream( body, stream, mime_part); | ||
528 | *newmsg = mime_part->msg; | ||
529 | return 0; | ||
530 | } | ||
531 | message_destroy(&mime_part->msg, mime_part); | ||
532 | } | ||
533 | } | ||
534 | } | ||
535 | } | ||
536 | } | ||
537 | } | ||
538 | } | ||
539 | } | ||
540 | return ret; | ||
541 | } | ||
542 | |||
543 | int mime_get_num_parts(mime_t mime, int *nmtp_parts) | 463 | int mime_get_num_parts(mime_t mime, int *nmtp_parts) |
544 | { | 464 | { |
545 | int ret = 0; | 465 | int ret = 0; | ... | ... |
mailbox/net.c
deleted
100644 → 0
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 | |||
19 | #include <stdlib.h> | ||
20 | #include <string.h> | ||
21 | #include <errno.h> | ||
22 | |||
23 | #include <net0.h> | ||
24 | |||
25 | static struct _netregistrar _netreg[1] = { "tcp", _tcp_create, _tcp_set_option, _tcp_destroy }; | ||
26 | |||
27 | int net_api_create(net_t *net, net_t parent, const char *type) | ||
28 | { | ||
29 | net_t n; | ||
30 | int i, napis, ret = 0; | ||
31 | |||
32 | if ( net == NULL || type == NULL ) | ||
33 | return EINVAL; | ||
34 | |||
35 | *net = NULL; | ||
36 | |||
37 | if ( ( n = calloc(1, sizeof(*n)) ) == NULL ) | ||
38 | return ENOMEM; | ||
39 | napis = sizeof(_netreg) / sizeof(_netreg[0]); | ||
40 | for( i = 0; i < napis; i++ ) { | ||
41 | if ( strcasecmp(_netreg[i].type, type) == 0 ) | ||
42 | break; | ||
43 | } | ||
44 | if ( i == napis ) | ||
45 | return ENOTSUP; | ||
46 | if ( (ret = ( _netreg[i].create(&(n->data), &(n->api)) )) != 0 ) | ||
47 | free(n); | ||
48 | n->parent = parent; | ||
49 | n->net_reg = &_netreg[i]; | ||
50 | *net = n; | ||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | int net_api_set_option(net_t net, const char *name, const char *value) | ||
55 | { | ||
56 | if ( net && name && value ) | ||
57 | return net->net_reg->set_option(net->data, name, value); | ||
58 | return EINVAL; | ||
59 | } | ||
60 | |||
61 | int net_api_destroy(net_t *net) | ||
62 | { | ||
63 | net_t n; | ||
64 | if ( net == NULL || *net == NULL ) | ||
65 | return EINVAL; | ||
66 | |||
67 | n = *net; | ||
68 | n->net_reg->destroy(&n->data); | ||
69 | free(n); | ||
70 | *net = NULL; | ||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | int net_new(net_t net, netinstance_t *inst) | ||
75 | { | ||
76 | netinstance_t netinst; | ||
77 | int ret = 0; | ||
78 | |||
79 | if ( net == NULL || inst == NULL ) | ||
80 | return EINVAL; | ||
81 | |||
82 | *inst = NULL; | ||
83 | |||
84 | if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL ) | ||
85 | return ENOMEM; | ||
86 | netinst->api = net->api; | ||
87 | if ( ( ret = net->api->new(net->data, net->parent, &(netinst->data)) ) != 0 ) { | ||
88 | free(netinst); | ||
89 | return ret; | ||
90 | } | ||
91 | *inst = netinst; | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | int net_connect(netinstance_t inst, const char *host, int port) | ||
96 | { | ||
97 | if ( inst == NULL || host == NULL ) | ||
98 | return EINVAL; | ||
99 | |||
100 | return inst->api->connect(inst->data, host, port); | ||
101 | } | ||
102 | |||
103 | int net_get_stream(netinstance_t inst, stream_t *iostr) | ||
104 | { | ||
105 | if ( inst == NULL || iostr == NULL ) | ||
106 | return EINVAL; | ||
107 | |||
108 | return inst->api->get_stream(inst->data, iostr); | ||
109 | } | ||
110 | |||
111 | int net_close(netinstance_t inst) | ||
112 | { | ||
113 | if ( inst == NULL ) | ||
114 | return EINVAL; | ||
115 | |||
116 | return inst->api->close(inst->data); | ||
117 | } | ||
118 | |||
119 | int net_free(netinstance_t *pinst) | ||
120 | { | ||
121 | int ret; | ||
122 | netinstance_t inst; | ||
123 | |||
124 | if ( pinst == NULL || *pinst == NULL ) | ||
125 | return EINVAL; | ||
126 | |||
127 | inst = *pinst; | ||
128 | ret = inst->api->free(&(inst->data)); | ||
129 | free(inst); | ||
130 | *pinst = NULL; | ||
131 | return ret; | ||
132 | } |
... | @@ -26,30 +26,46 @@ | ... | @@ -26,30 +26,46 @@ |
26 | #include <netinet/in.h> | 26 | #include <netinet/in.h> |
27 | #include <arpa/inet.h> | 27 | #include <arpa/inet.h> |
28 | 28 | ||
29 | #include <net0.h> | ||
30 | #include <io0.h> | 29 | #include <io0.h> |
31 | #include <tcp.h> | 30 | #include <tcp.h> |
32 | 31 | ||
33 | static int _tcp_close(void *data); | 32 | static int _tcp_close(stream_t stream) |
33 | { | ||
34 | struct _tcp_instance *tcp = stream->owner; | ||
34 | 35 | ||
35 | static int _tcp_doconnect(struct _tcp_instance *tcp) | 36 | if ( tcp->fd != -1 ) |
37 | close(tcp->fd); | ||
38 | tcp->fd = -1; | ||
39 | tcp->state = TCP_STATE_INIT; | ||
40 | return 0; | ||
41 | } | ||
42 | |||
43 | static int _tcp_open(stream_t stream, const char *host, int port, int flags) | ||
36 | { | 44 | { |
45 | struct _tcp_instance *tcp = stream->owner; | ||
37 | int flgs, ret; | 46 | int flgs, ret; |
38 | size_t namelen; | 47 | size_t namelen; |
39 | struct sockaddr_in peer_addr; | 48 | struct sockaddr_in peer_addr; |
40 | struct hostent *phe; | 49 | struct hostent *phe; |
41 | struct sockaddr_in soc_addr; | 50 | struct sockaddr_in soc_addr; |
42 | 51 | ||
52 | if ( tcp->state == TCP_STATE_INIT ) { | ||
53 | tcp->port = port; | ||
54 | if ( ( tcp->host = strdup(host) ) == NULL ) | ||
55 | return ENOMEM; | ||
56 | } | ||
57 | |||
43 | switch( tcp->state ) { | 58 | switch( tcp->state ) { |
44 | case TCP_STATE_INIT: | 59 | case TCP_STATE_INIT: |
45 | if ( tcp->fd == -1 ) { | 60 | if ( tcp->fd == -1 ) { |
46 | if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) | 61 | if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) |
47 | return errno; | 62 | return errno; |
48 | } | 63 | } |
49 | if ( tcp->options->non_block ) { | 64 | if ( flags & MU_STREAM_NONBLOCK ) { |
50 | flgs = fcntl(tcp->fd, F_GETFL); | 65 | flgs = fcntl(tcp->fd, F_GETFL); |
51 | flgs |= O_NONBLOCK; | 66 | flgs |= O_NONBLOCK; |
52 | fcntl(tcp->fd, F_SETFL, flgs); | 67 | fcntl(tcp->fd, F_SETFL, flgs); |
68 | stream->flags |= MU_STREAM_NONBLOCK; | ||
53 | } | 69 | } |
54 | tcp->state = TCP_STATE_RESOLVING; | 70 | tcp->state = TCP_STATE_RESOLVING; |
55 | case TCP_STATE_RESOLVING: | 71 | case TCP_STATE_RESOLVING: |
... | @@ -59,7 +75,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -59,7 +75,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
59 | if (tcp->address == INADDR_NONE) { | 75 | if (tcp->address == INADDR_NONE) { |
60 | phe = gethostbyname(tcp->host); | 76 | phe = gethostbyname(tcp->host); |
61 | if ( !phe ) { | 77 | if ( !phe ) { |
62 | _tcp_close(tcp); | 78 | _tcp_close(stream); |
63 | return EINVAL; | 79 | return EINVAL; |
64 | } | 80 | } |
65 | tcp->address = *(((unsigned long **)phe->h_addr_list)[0]); | 81 | tcp->address = *(((unsigned long **)phe->h_addr_list)[0]); |
... | @@ -77,7 +93,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -77,7 +93,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
77 | tcp->state = TCP_STATE_CONNECTING; | 93 | tcp->state = TCP_STATE_CONNECTING; |
78 | ret = EAGAIN; | 94 | ret = EAGAIN; |
79 | } else | 95 | } else |
80 | _tcp_close(tcp); | 96 | _tcp_close(stream); |
81 | return ret; | 97 | return ret; |
82 | } | 98 | } |
83 | tcp->state = TCP_STATE_CONNECTING; | 99 | tcp->state = TCP_STATE_CONNECTING; |
... | @@ -87,7 +103,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -87,7 +103,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
87 | tcp->state = TCP_STATE_CONNECTED; | 103 | tcp->state = TCP_STATE_CONNECTED; |
88 | else { | 104 | else { |
89 | ret = errno; | 105 | ret = errno; |
90 | _tcp_close(tcp); | 106 | _tcp_close(stream); |
91 | return ret; | 107 | return ret; |
92 | } | 108 | } |
93 | break; | 109 | break; |
... | @@ -95,17 +111,14 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -95,17 +111,14 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
95 | return 0; | 111 | return 0; |
96 | } | 112 | } |
97 | 113 | ||
114 | |||
98 | static int _tcp_get_fd(stream_t stream, int *fd) | 115 | static int _tcp_get_fd(stream_t stream, int *fd) |
99 | { | 116 | { |
100 | struct _tcp_instance *tcp = stream->owner; | 117 | struct _tcp_instance *tcp = stream->owner; |
101 | 118 | ||
102 | if ( fd == NULL ) | 119 | if ( fd == NULL || tcp->fd == EINVAL ) |
103 | return EINVAL; | 120 | return EINVAL; |
104 | 121 | ||
105 | if ( tcp->fd == -1 ) { | ||
106 | if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) | ||
107 | return errno; | ||
108 | } | ||
109 | *fd = tcp->fd; | 122 | *fd = tcp->fd; |
110 | return 0; | 123 | return 0; |
111 | } | 124 | } |
... | @@ -113,152 +126,67 @@ static int _tcp_get_fd(stream_t stream, int *fd) | ... | @@ -113,152 +126,67 @@ static int _tcp_get_fd(stream_t stream, int *fd) |
113 | static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br) | 126 | static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br) |
114 | { | 127 | { |
115 | struct _tcp_instance *tcp = stream->owner; | 128 | struct _tcp_instance *tcp = stream->owner; |
116 | 129 | int bytes; | |
117 | offset; | 130 | |
118 | if ( br == NULL ) | 131 | offset = offset; |
132 | if ( br == NULL ) | ||
119 | return EINVAL; | 133 | return EINVAL; |
120 | *br = 0; | 134 | *br = 0; |
121 | if ( ( *br = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) { | 135 | if ( ( bytes = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) { |
122 | *br = 0; | 136 | *br = 0; |
123 | return errno; | 137 | return errno; |
124 | } | 138 | } |
139 | *br = bytes; | ||
125 | return 0; | 140 | return 0; |
126 | } | 141 | } |
127 | 142 | ||
128 | static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw) | 143 | static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw) |
129 | { | 144 | { |
130 | struct _tcp_instance *tcp = stream->owner; | 145 | struct _tcp_instance *tcp = stream->owner; |
131 | 146 | int bytes; | |
132 | offset; | 147 | |
133 | if ( bw == NULL ) | 148 | offset = offset; |
149 | if ( bw == NULL ) | ||
134 | return EINVAL; | 150 | return EINVAL; |
135 | *bw = 0; | 151 | *bw = 0; |
136 | if ( ( *bw = send(tcp->fd, buf, buf_size, 0) ) == -1 ) { | 152 | if ( ( bytes = send(tcp->fd, buf, buf_size, 0) ) == -1 ) { |
137 | *bw = 0; | 153 | *bw = 0; |
138 | return errno; | 154 | return errno; |
139 | } | 155 | } |
156 | *bw = bytes; | ||
140 | return 0; | 157 | return 0; |
141 | } | 158 | } |
142 | 159 | ||
143 | static int _tcp_new(void *netdata, net_t parent, void **data) | 160 | static void _tcp_destroy(stream_t stream) |
144 | { | ||
145 | struct _tcp_instance *tcp; | ||
146 | |||
147 | if ( parent ) /* tcp must be top level api */ | ||
148 | return EINVAL; | ||
149 | |||
150 | if ( ( tcp = malloc(sizeof(*tcp)) ) == NULL ) | ||
151 | return ENOMEM; | ||
152 | tcp->options = (struct _tcp_options *)netdata; | ||
153 | tcp->fd = -1; | ||
154 | tcp->host = NULL; | ||
155 | tcp->port = -1; | ||
156 | tcp->state = TCP_STATE_INIT; | ||
157 | stream_create(&tcp->stream, 0, tcp); | ||
158 | stream_set_read(tcp->stream, _tcp_read, tcp); | ||
159 | stream_set_write(tcp->stream, _tcp_write, tcp); | ||
160 | stream_set_fd(tcp->stream, _tcp_get_fd, tcp); | ||
161 | *data = tcp; | ||
162 | return 0; | ||
163 | } | ||
164 | |||
165 | static int _tcp_connect(void *data, const char *host, int port) | ||
166 | { | ||
167 | struct _tcp_instance *tcp = data; | ||
168 | |||
169 | if ( tcp->state == TCP_STATE_INIT ) { | ||
170 | tcp->port = port; | ||
171 | if ( ( tcp->host = strdup(host) ) == NULL ) | ||
172 | return ENOMEM; | ||
173 | } | ||
174 | if ( tcp->state < TCP_STATE_CONNECTED ) | ||
175 | return _tcp_doconnect(tcp); | ||
176 | return 0; | ||
177 | } | ||
178 | |||
179 | static int _tcp_get_stream(void *data, stream_t *stream) | ||
180 | { | ||
181 | struct _tcp_instance *tcp = data; | ||
182 | |||
183 | *stream = tcp->stream; | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | static int _tcp_close(void *data) | ||
188 | { | 161 | { |
189 | struct _tcp_instance *tcp = data; | 162 | struct _tcp_instance *tcp = stream->owner; |
190 | |||
191 | if ( tcp->fd != -1 ) | ||
192 | close(tcp->fd); | ||
193 | tcp->fd = -1; | ||
194 | tcp->state = TCP_STATE_INIT; | ||
195 | return 0; | ||
196 | } | ||
197 | |||
198 | static int _tcp_free(void **data) | ||
199 | { | ||
200 | struct _tcp_instance *tcp; | ||
201 | |||
202 | if ( data == NULL || *data == NULL ) | ||
203 | return EINVAL; | ||
204 | tcp = *data; | ||
205 | 163 | ||
206 | if ( tcp->host ) | 164 | if ( tcp->host ) |
207 | free(tcp->host); | 165 | free(tcp->host); |
208 | if ( tcp->fd != -1 ) | 166 | if ( tcp->fd != -1 ) |
209 | close(tcp->fd); | 167 | close(tcp->fd); |
210 | 168 | ||
211 | free(*data); | 169 | free(tcp); |
212 | *data = NULL; | ||
213 | return 0; | ||
214 | } | 170 | } |
215 | 171 | ||
216 | static struct _net_api _tcp_net_api = { | 172 | int tcp_stream_create(stream_t *stream) |
217 | _tcp_new, | ||
218 | _tcp_connect, | ||
219 | _tcp_get_stream, | ||
220 | _tcp_close, | ||
221 | _tcp_free | ||
222 | }; | ||
223 | |||
224 | int _tcp_create(void **netdata, struct _net_api **netapi) | ||
225 | { | 173 | { |
226 | struct _tcp_options *options; | 174 | struct _tcp_instance *tcp; |
175 | int ret; | ||
227 | 176 | ||
228 | if ( ( options = malloc(sizeof(*options)) ) == NULL ) | 177 | if ( ( tcp = malloc(sizeof(*tcp)) ) == NULL ) |
229 | return ENOMEM; | 178 | return ENOMEM; |
230 | 179 | tcp->fd = -1; | |
231 | options->non_block = 0; | 180 | tcp->host = NULL; |
232 | options->net_timeout = -1; /* system default */ | 181 | tcp->port = -1; |
233 | 182 | tcp->state = TCP_STATE_INIT; | |
234 | *netdata = options; | 183 | if ( ( ret = stream_create(stream, MU_STREAM_NO_CHECK|MU_STREAM_RDWR, tcp) ) != 0 ) |
235 | *netapi = &_tcp_net_api; | 184 | return ret; |
236 | return 0; | 185 | stream_set_open(*stream, _tcp_open, tcp); |
237 | } | 186 | stream_set_close(*stream, _tcp_close, tcp); |
238 | 187 | stream_set_read(*stream, _tcp_read, tcp); | |
239 | int _tcp_set_option(void *netdata, const char *name, const char *value) | 188 | stream_set_write(*stream, _tcp_write, tcp); |
240 | { | 189 | stream_set_fd(*stream, _tcp_get_fd, tcp); |
241 | struct _tcp_options *options = netdata; | 190 | stream_set_destroy(*stream, _tcp_destroy, tcp); |
242 | |||
243 | if ( strcasecmp(name, "tcp_non_block") == 0 ) { | ||
244 | if ( value[0] == 't' || value[0] == 'T' || value[0] == '1' || value[0] == 'y' || value[0] == 'Y') | ||
245 | options->non_block = 1; | ||
246 | else | ||
247 | options->non_block = 0; | ||
248 | } | ||
249 | else if ( strcasecmp(name, "tcp_timeout") == 0 ) | ||
250 | options->net_timeout = atoi(value); | ||
251 | else | ||
252 | return EINVAL; | ||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | int _tcp_destroy(void **netdata) | ||
257 | { | ||
258 | struct _tcp_options *options = *netdata; | ||
259 | |||
260 | free(options); | ||
261 | *netdata = NULL; | ||
262 | return 0; | 191 | return 0; |
263 | } | 192 | } |
264 | ... | ... |
-
Please register or sign in to post a comment