Commit 4ccdee87 4ccdee87a2ae063a7b87f13b9e5b23704931ac84 by Alain Magloire

attribute.c body.c header.c io.c mbx_unix.c message.c mime.c

 	net.c tcp.c transcode.c url_pop.c include/private/io0.h
 	include/private/url0.h include/public/attribute.h
 	include/public/io.h include/public/mailbox.h

all function *_init() rename to *_create().
1 parent bea16f22
......@@ -254,8 +254,7 @@ attribute_copy (attribute_t dest, attribute_t src)
}
int
string_to_attribute (const char *buffer, size_t len,
attribute_t *pattr, void *owner)
string_to_attribute (const char *buffer, attribute_t *pattr, void *owner)
{
char *sep;
int status;
......@@ -265,9 +264,11 @@ string_to_attribute (const char *buffer, size_t len,
return status;
/* Set the attribute */
if (len > 7 && strncasecmp (buffer, "Status:", 7) == 0)
if (strncasecmp (buffer, "Status:", 7) == 0)
{
sep = strchr(buffer, ':'); /* pass the ':' */
sep++;
while (*sep == ' ') sep++; /* glob spaces */
if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
attribute_set_read (*pattr);
if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
......@@ -280,3 +281,40 @@ string_to_attribute (const char *buffer, size_t len,
return 0;
}
int
attribute_to_string (attribute_t attr, char *buffer, size_t len, size_t *pn)
{
char status[32];
char a[8];
size_t i;
*status = *a = '\0';
if (attribute_is_seen (attr))
strcat (a, "R");
if (attribute_is_answered (attr))
strcat (a, "A");
if (attribute_is_flagged (attr))
strcat (a, "F");
if (attribute_is_read (attr))
strcat (a, "O");
if (*a != '\0')
{
strcpy (status, "Status: ");
strcat (status, a);
strcat (status, "\n");
}
i = strlen (status);
if (buffer && len != 0)
{
strncpy (buffer, status, len - 1);
buffer[len - 1] = '\0';
i = strlen (buffer);
}
if (pn)
*pn = i;
return 0;
}
......
......@@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream)
/* lazy floating body it is created when
* doing the first body_write call
*/
status = stream_create (&stream, body);
status = stream_create (&stream, 0, body);
if (status != 0)
return status;
stream_set_read (stream, body_read, body);
......
......@@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner)
header_parse (h, (char *)blurb, len);
status = stream_create (&(h->stream), h);
status = stream_create (&(h->stream), 0, h);
if (status != 0)
return status;
......
......@@ -35,6 +35,8 @@ extern "C" {
struct _stream
{
void *owner;
int flags;
void (*_destroy) __P ((void *));
int (*_get_fd) __P ((stream_t, int *));
int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *));
int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *));
......
......@@ -92,22 +92,9 @@ struct _url
/* MMDF */
/* POP3 */
struct _url_pop;
typedef struct _url_pop * url_pop_t;
struct _url_pop
{
/* we use the fields from url_t */
/* user, passwd, host, port */
char * auth;
int (*_get_auth) __P ((const url_pop_t, char *, size_t, size_t *));
};
#define MU_POP_PORT 110
/* pop*/
extern int url_pop_get_auth (const url_t, char *, size_t, size_t *);
/* UNIX MBOX */
#ifdef __cplusplus
......
......@@ -67,8 +67,10 @@ extern int attribute_is_equal __P ((attribute_t att1, attribute_t att2));
extern int attribute_copy __P ((attribute_t dst,
attribute_t src));
extern int string_to_attribute __P ((const char *buf, size_t len,
extern int string_to_attribute __P ((const char *buf,
attribute_t *pattr, void *owner));
extern int attribute_to_string __P ((attribute_t attr, char *buf,
size_t len, size_t *));
extern int attribute_get_owner __P ((attribute_t attr, void **owner));
#ifdef __cplusplus
}
......
......@@ -35,9 +35,16 @@ extern "C" { /*}*/
struct _stream;
typedef struct _stream *stream_t;
extern int stream_create __P ((stream_t *, void *owner));
/* stream will be destroy on stream_destroy */
#define MU_STREAM_NO_CHECK 1
extern int stream_create __P ((stream_t *, int flags, void *owner));
extern void stream_destroy __P ((stream_t *, void *owner));
extern int stream_set_destroy __P ((stream_t,
void (*_destroy) __P ((void *)),
void *owner));
extern int stream_set_fd __P ((stream_t,
int (*_get_fd)(stream_t, int *),
void *owner));
......
......@@ -81,7 +81,7 @@ extern int mailbox_is_updated __P ((mailbox_t));
extern int mailbox_scan __P ((mailbox_t, size_t msgno, size_t *count));
/* mailbox size ? */
extern int mailbox_size __P ((mailbox_t, off_t size));
extern int mailbox_size __P ((mailbox_t, off_t *size));
extern int mailbox_get_url __P ((mailbox_t, url_t *));
......
......@@ -22,7 +22,7 @@
#include <stdio.h>
int
stream_create (stream_t *pstream, void *owner)
stream_create (stream_t *pstream, int flags, void *owner)
{
stream_t stream;
if (pstream == NULL || owner == NULL)
......@@ -31,6 +31,7 @@ stream_create (stream_t *pstream, void *owner)
if (stream == NULL)
return ENOMEM;
stream->owner = owner;
stream->flags = flags;
*pstream = stream;
return 0;
}
......@@ -41,13 +42,30 @@ stream_destroy (stream_t *pstream, void *owner)
if (pstream && *pstream)
{
stream_t stream = *pstream;
if (stream->owner == owner)
if (stream->_destroy)
stream->_destroy (owner);
if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner)
free (stream);
*pstream = NULL;
}
}
int
stream_set_destroy (stream_t stream, void (*_destroy) (void *),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_destroy = _destroy;
return 0;
}
int
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
if (stream == NULL)
......
......@@ -908,7 +908,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
mailbox_unix_message_t mum;
size_t nread = 0;
if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL)
if (is == NULL || (mum = is->owner) == NULL)
return EINVAL;
if (buffer == NULL || buflen == 0)
......@@ -1167,7 +1167,9 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
message_destroy (&msg, mum);
return status;
}
status = stream_create (&stream, mum);
message_set_body (msg, body, mum);
status = stream_create (&stream, 0, mum);
if (status != 0)
{
message_destroy (&msg, mum);
......@@ -1177,9 +1179,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
stream_set_fd (stream, mailbox_unix_getfd, mum);
body_set_stream (body, stream, mum);
body_set_size (body, mailbox_unix_body_size, mum);
/* set the line */
body_set_lines (body, mailbox_unix_body_lines, mum);
message_set_body (msg, body, mum);
/* set the attribute */
status = message_set_attribute (msg, mum->new_attr, mum);
......@@ -1293,7 +1293,7 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
}
static int
mailbox_unix_size (mailbox_t mbox, off_t *size)
mailbox_unix_size (mailbox_t mbox, off_t *psize)
{
mailbox_unix_data_t mud;
struct stat st;
......@@ -1310,7 +1310,8 @@ mailbox_unix_size (mailbox_t mbox, off_t *size)
/* oops !! */
if (fstat (fd, &st) != 0)
return errno;
*size = st.st_size;
if (psize)
*psize = st.st_size;
return 0;
}
......
......@@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner)
msg = calloc (1, sizeof (*msg));
if (msg == NULL)
return ENOMEM;
status = stream_create (&stream, msg);
status = stream_create (&stream, 0, msg);
if (status != 0)
{
free (msg);
......@@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream)
{
stream_t stream;
int status;
status = stream_create (&stream, msg);
status = stream_create (&stream, 0, msg);
if (status != 0)
return status;
stream_set_read (stream, message_read, msg);
......
......@@ -42,7 +42,7 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo
{
struct _mime_part *mime_part, **part_arr;
int ret;
if ( ( mime_part = calloc(1, sizeof(*mime_part)) ) == NULL )
return ENOMEM;
......@@ -86,14 +86,14 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo
static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg)
{
int i;
for ( i = 0; i < mime->nmtp_parts; i++ ) {
if ( mime->mtp_parts[i] == msg->owner )
return mime->mtp_parts[i];
}
return NULL;
}
static char *_strltrim(char *str)
{
char *p;
......@@ -106,7 +106,7 @@ static char *_strltrim(char *str)
static char *_strttrim(char *str)
{
char *p;
for (p = str + strlen(str) - 1; isspace(*p) && p >= str; --p)
;
*++p = '\0';
......@@ -121,28 +121,28 @@ char *_strtrim(char *str);
|| ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \
|| ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \
|| ((c) == ']') )
static void _mime_munge_content_header(char *field_body )
{
char *p, *e, *str = field_body;
int quoted = 0;
_strtrim(field_body);
if ( ( e = p = strchr(str, ';') ) == NULL )
return;
e++;
return;
e++;
while ( *e && isspace(*e) ) /* remove space upto param */
e++;
memmove(p+1, e, strlen(e)+1);
e = p+1;
while ( *e && *e != '=' ) /* find end of value */
e++;
e = p = e+1;
while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) {
if ( *e == '\\' ) { /* escaped */
memmove(e, e+1, strlen(e)+2);
memmove(e, e+1, strlen(e)+2);
} else if ( *e == '\"' )
quoted = ~quoted;
e++;
......@@ -153,7 +153,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
{
char *str, *p, *v, *e;
int quoted = 0, was_quoted = 0;
if ( len == NULL || ( str = field_body ) == NULL )
return NULL;
......@@ -168,7 +168,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
if ( *e == '\"' )
quoted = ~quoted, was_quoted = 1;
else
(*len)++;
(*len)++;
e++;
}
if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */
......@@ -177,7 +177,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
}
else
return was_quoted ? v + 1 : v; /* return unquted value */
}
}
return NULL;
}
......@@ -211,11 +211,11 @@ static int _mime_parse_mpart_message(mime_t mime)
char *cp, *cp2;
int blength, body_length, body_offset, ret;
size_t nbytes;
if ( !(mime->flags & MIME_PARSER_ACTIVE) ) {
char *boundary;
int len;
if ( ( ret = _mime_setup_buffers(mime) ) != 0 )
return ret;
if ( ( boundary = _mime_get_param(mime->content_type, "boundary", &len) ) == NULL )
......@@ -223,7 +223,7 @@ static int _mime_parse_mpart_message(mime_t mime)
if ( ( mime->boundary = calloc(1, len + 1) ) == NULL )
return ENOMEM;
strncpy(mime->boundary, boundary, len );
mime->cur_offset = 0;
mime->line_ndx = 0;
mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
......@@ -231,7 +231,7 @@ static int _mime_parse_mpart_message(mime_t mime)
}
body_length = mime->body_length;
body_offset = mime->body_offset;
while ( ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) {
cp = mime->cur_buf;
while ( nbytes ) {
......@@ -247,14 +247,14 @@ static int _mime_parse_mpart_message(mime_t mime)
cp2 = mime->cur_line[0] == '\n' ? mime->cur_line + 1 : mime->cur_line;
blength = strlen(mime->boundary);
if ( mime->line_ndx >= blength ) {
if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) )
if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) )
|| !strncasecmp(cp2, mime->boundary, blength) ) {
mime->parser_state = MIME_STATE_HEADERS;
mime->flags &= ~MIME_PARSER_HAVE_CR;
body_length = mime->cur_offset - body_offset - mime->line_ndx + 1;
if ( mime->header_length ) /* this skips the preamble */
_mime_append_part(mime, NULL, body_offset, body_length, FALSE );
if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) ||
if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) ||
!strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */
break;
}
......@@ -262,7 +262,7 @@ static int _mime_parse_mpart_message(mime_t mime)
break;
}
}
mime->line_ndx = 0;
mime->line_ndx = 0;
mime->cur_line[0] = *cp; /* stay in this state but leave '\n' at begining */
break;
case MIME_STATE_HEADERS:
......@@ -271,7 +271,7 @@ static int _mime_parse_mpart_message(mime_t mime)
if ( mime->line_ndx == 1 || mime->cur_line[0] == '\r' ) {
mime->parser_state = MIME_STATE_BEGIN_LINE;
body_offset = mime->cur_offset + 1;
}
}
mime->line_ndx = -1;
break;
}
......@@ -284,12 +284,12 @@ static int _mime_parse_mpart_message(mime_t mime)
mime->cur_offset++;
nbytes--;
cp++;
}
}
if ( mime->flags & MIME_INCREAMENTAL_PARSER ) {
ret = EAGAIN;
break;
break;
}
}
}
mime->body_length = body_length;
mime->body_offset = body_offset;
if ( ret != EAGAIN ) { /* finished cleanup */
......@@ -303,7 +303,7 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o
{
struct _mime_part *mime_part = stream->owner;
size_t read_len;
if ( nbytes == NULL )
return(EINVAL);
......@@ -321,7 +321,7 @@ static int _mime_new_message_read(stream_t stream, char *buf, size_t buflen, off
(void)stream; (void)buf; (void)buflen; (void)off;
if ( nbytes == NULL )
return(EINVAL);
return 0;
}
......@@ -361,10 +361,10 @@ int mime_create(mime_t *pmime, message_t msg, int flags)
mime->msg = msg;
mime->buf_size = MIME_DFLT_BUF_SIZE;
message_get_stream(msg, &(mime->stream));
}
}
}
}
else {
}
else {
if ( ( ret = message_create( &msg, mime ) ) == 0 ) {
mime->flags |= MIME_NEW_MESSAGE;
}
......@@ -386,22 +386,22 @@ void mime_destroy(mime_t *pmime)
mime_t mime;
struct _mime_part *mime_part;
int i;
if (pmime && *pmime) {
mime = *pmime;
if ( mime->mtp_parts != NULL ) {
for ( i = 0; i < mime->nmtp_parts; i++ ) {
mime_part = mime->mtp_parts[i];
if ( mime_part->msg )
for ( i = 0; i < mime->nmtp_parts; i++ ) {
mime_part = mime->mtp_parts[i];
if ( mime_part->msg )
message_destroy(&mime_part->msg, mime_part);
else
header_destroy(&mime_part->hdr, mime_part);
}
}
if ( mime->cap_msgs != NULL ) {
for ( i = 0; i < mime->ncap_msgs; i++ ) {
mime_part = mime->cap_msgs[i];
if ( mime_part->msg )
for ( i = 0; i < mime->ncap_msgs; i++ ) {
mime_part = mime->cap_msgs[i];
if ( mime_part->msg )
message_destroy(&mime_part->msg, mime_part);
else
header_destroy(&mime_part->hdr, mime_part);
......@@ -413,7 +413,7 @@ void mime_destroy(mime_t *pmime)
free(mime->cur_buf);
if ( mime->cur_line )
free(mime->cur_line);
if ( mime->boundary )
if ( mime->boundary )
free(mime->boundary);
if ( mime->header_buf )
free(mime->header_buf);
......@@ -437,7 +437,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
stream_t stream;
body_t body;
struct _mime_part *mime_part;
if ( ( ret = mime_get_num_parts(mime, &nmtp_parts ) ) == 0 ) {
if ( part < 1 || part > nmtp_parts )
return EINVAL;
......@@ -449,7 +449,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
message_set_header(mime_part->msg, mime_part->hdr, mime_part);
header_size(mime_part->hdr, &hsize);
if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) {
body_set_size (body, _mime_body_size, mime_part);
stream_set_read(stream, _mime_message_read, mime_part);
body_set_stream(body, stream, mime_part);
......@@ -473,10 +473,10 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
stream_t stream;
body_t body;
struct _mime_part *mime_part;
if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE )
return EINVAL;
if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */
return EPERM;
......@@ -521,7 +521,7 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) {
message_set_header(mime_part->msg, mime_part->hdr, mime_part);
if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) {
stream_set_read(stream, _mime_message_read, mime_part);
body_set_size (body, _mime_body_size, mime_part);
body_set_stream( body, stream, mime_part);
......@@ -537,13 +537,13 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
}
}
}
return ret;
return ret;
}
int mime_get_num_parts(mime_t mime, int *nmtp_parts)
{
int ret = 0;
if ( mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE ) {
if ( mime_is_multi_part(mime) ) {
if ( ( ret = _mime_parse_mpart_message(mime) ) != 0 )
......@@ -553,14 +553,14 @@ int mime_get_num_parts(mime_t mime, int *nmtp_parts)
}
*nmtp_parts = mime->nmtp_parts;
return(ret);
}
int mime_add_part(mime_t mime, message_t msg)
{
if ( mime == NULL || msg == NULL || ( mime->flags & MIME_NEW_MESSAGE ) == 0 )
return EINVAL;
return _mime_append_part(mime, msg, 0, 0, FALSE);
return _mime_append_part(mime, msg, 0, 0, FALSE);
}
int mime_get_message(mime_t mime, message_t *msg)
......
......@@ -17,6 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net0.h>
......@@ -27,22 +28,22 @@ int net_api_create(net_t *net, net_t parent, const char *type)
{
net_t n;
int i, napis, ret = 0;
if ( net == NULL || type == NULL )
if ( net == NULL || type == NULL )
return EINVAL;
*net = NULL;
*net = NULL;
if ( ( n = calloc(1, sizeof(*n)) ) == NULL )
return ENOMEM;
napis = sizeof(_netreg) / sizeof(_netreg[0]);
napis = sizeof(_netreg) / sizeof(_netreg[0]);
for( i = 0; i < napis; i++ ) {
if ( strcasecmp(_netreg[i].type, type) == 0 )
break;
}
if ( i == napis )
return ENOTSUP;
if ( ret = ( _netreg[i].create(&(n->data), &(n->api)) ) != 0 )
if ( (ret = ( _netreg[i].create(&(n->data), &(n->api)) )) != 0 )
free(n);
n->parent = parent;
n->net_reg = &_netreg[i];
......@@ -52,35 +53,35 @@ int net_api_create(net_t *net, net_t parent, const char *type)
int net_api_set_option(net_t net, const char *name, const char *value)
{
if ( net && name && value )
if ( net && name && value )
return net->net_reg->set_option(net->data, name, value);
return EINVAL;
return EINVAL;
}
int net_api_destroy(net_t *net)
{
net_t n;
if ( net == NULL || *net == NULL )
if ( net == NULL || *net == NULL )
return EINVAL;
n = *net;
n->net_reg->destroy(&n->data);
n = *net;
n->net_reg->destroy(&n->data);
free(n);
*net = NULL;
return 0;
*net = NULL;
return 0;
}
int net_new(net_t net, netinstance_t *inst)
{
netinstance_t netinst;
int ret = 0;
if ( net == NULL || inst == NULL )
return EINVAL;
*inst = NULL;
if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL )
if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL )
return ENOMEM;
netinst->api = net->api;
if ( ( ret = net->api->new(net->data, net->parent, &(netinst->data)) ) != 0 ) {
......@@ -95,7 +96,7 @@ int net_connect(netinstance_t inst, const char *host, int port)
{
if ( inst == NULL || host == NULL )
return EINVAL;
return inst->api->connect(inst->data, host, port);
}
......@@ -103,7 +104,7 @@ int net_get_stream(netinstance_t inst, stream_t *iostr)
{
if ( inst == NULL || iostr == NULL )
return EINVAL;
return inst->api->get_stream(inst->data, iostr);
}
......@@ -111,7 +112,7 @@ int net_close(netinstance_t inst)
{
if ( inst == NULL )
return EINVAL;
return inst->api->close(inst->data);
}
......@@ -119,10 +120,10 @@ int net_free(netinstance_t *pinst)
{
int ret;
netinstance_t inst;
if ( pinst == NULL || *pinst == NULL )
return EINVAL;
inst = *pinst;
ret = inst->api->free(&(inst->data));
free(inst);
......
......@@ -39,7 +39,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp)
struct sockaddr_in peer_addr;
struct hostent *phe;
struct sockaddr_in soc_addr;
switch( tcp->state ) {
case TCP_STATE_INIT:
if ( tcp->fd == -1 ) {
......@@ -53,7 +53,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp)
}
tcp->state = TCP_STATE_RESOLVING;
case TCP_STATE_RESOLVING:
if ( tcp->host == NULL || tcp->port == -1 )
if ( tcp->host == NULL || tcp->port == -1 )
return EINVAL;
tcp->address = inet_addr(tcp->host);
if (tcp->address == INADDR_NONE) {
......@@ -68,9 +68,9 @@ static int _tcp_doconnect(struct _tcp_instance *tcp)
case TCP_STATE_RESOLVE:
memset (&soc_addr, 0, sizeof (soc_addr));
soc_addr.sin_family = AF_INET;
soc_addr.sin_port = htons(tcp->port);
soc_addr.sin_port = htons(tcp->port);
soc_addr.sin_addr.s_addr = tcp->address;
if ( ( connect(tcp->fd, (struct sockaddr *) &soc_addr, sizeof(soc_addr)) ) == -1 ) {
ret = errno;
if ( ret == EINPROGRESS || ret == EAGAIN ) {
......@@ -101,21 +101,21 @@ static int _tcp_get_fd(stream_t stream, int *fd)
if ( fd == NULL )
return EINVAL;
if ( tcp->fd == -1 ) {
if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
return errno;
}
*fd = tcp->fd;
return 0;
return 0;
}
static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br)
{
struct _tcp_instance *tcp = stream->owner;
offset;
if ( br == NULL )
if ( br == NULL )
return EINVAL;
*br = 0;
if ( ( *br = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) {
......@@ -128,9 +128,9 @@ static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset,
static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw)
{
struct _tcp_instance *tcp = stream->owner;
offset;
if ( bw == NULL )
if ( bw == NULL )
return EINVAL;
*bw = 0;
if ( ( *bw = send(tcp->fd, buf, buf_size, 0) ) == -1 ) {
......@@ -143,9 +143,9 @@ static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t o
static int _tcp_new(void *netdata, net_t parent, void **data)
{
struct _tcp_instance *tcp;
if ( parent ) /* tcp must be top level api */
return EINVAL;
return EINVAL;
if ( ( tcp = malloc(sizeof(*tcp)) ) == NULL )
return ENOMEM;
......@@ -154,7 +154,7 @@ static int _tcp_new(void *netdata, net_t parent, void **data)
tcp->host = NULL;
tcp->port = -1;
tcp->state = TCP_STATE_INIT;
stream_create(&tcp->stream, tcp);
stream_create(&tcp->stream, 0, tcp);
stream_set_read(tcp->stream, _tcp_read, tcp);
stream_set_write(tcp->stream, _tcp_write, tcp);
stream_set_fd(tcp->stream, _tcp_get_fd, tcp);
......@@ -165,9 +165,9 @@ static int _tcp_new(void *netdata, net_t parent, void **data)
static int _tcp_connect(void *data, const char *host, int port)
{
struct _tcp_instance *tcp = data;
if ( tcp->state == TCP_STATE_INIT ) {
tcp->port = port;
tcp->port = port;
if ( ( tcp->host = strdup(host) ) == NULL )
return ENOMEM;
}
......@@ -199,15 +199,15 @@ static int _tcp_free(void **data)
{
struct _tcp_instance *tcp;
if ( data == NULL || *data == NULL )
if ( data == NULL || *data == NULL )
return EINVAL;
tcp = *data;
if ( tcp->host )
if ( tcp->host )
free(tcp->host);
if ( tcp->fd != -1 )
close(tcp->fd);
free(*data);
*data = NULL;
return 0;
......@@ -221,10 +221,10 @@ static struct _net_api _tcp_net_api = {
_tcp_free
};
int _tcp_create(void **netdata, struct _net_api **netapi)
int _tcp_create(void **netdata, struct _net_api **netapi)
{
struct _tcp_options *options;
if ( ( options = malloc(sizeof(*options)) ) == NULL )
return ENOMEM;
......@@ -239,13 +239,13 @@ int _tcp_create(void **netdata, struct _net_api **netapi)
int _tcp_set_option(void *netdata, const char *name, const char *value)
{
struct _tcp_options *options = netdata;
if ( strcasecmp(name, "tcp_non_block") == 0 ) {
if ( value[0] == 't' || value[0] == 'T' || value[0] == '1' || value[0] == 'y' || value[0] == 'Y')
options->non_block = 1;
else
options->non_block = 0;
}
}
else if ( strcasecmp(name, "tcp_timeout") == 0 )
options->net_timeout = atoi(value);
else
......@@ -256,7 +256,7 @@ int _tcp_set_option(void *netdata, const char *name, const char *value)
int _tcp_destroy(void **netdata)
{
struct _tcp_options *options = *netdata;
free(options);
*netdata = NULL;
return 0;
......
......@@ -40,7 +40,7 @@ struct _tc_desc {
};
#define NUM_TRANSCODERS 5
struct _tc_desc tclist[NUM_TRANSCODERS] = { { "base64", _base64_decode, _base64_encode, _base64_destroy },
struct _tc_desc tclist[NUM_TRANSCODERS] = { { "base64", _base64_decode, _base64_encode, _base64_destroy },
{ "quoted-printable", _qp_decode, _qp_encode, NULL },
{ "7bit", _identity_decode, _identity_encode, NULL },
{ "8bit", _identity_decode, _identity_encode, NULL },
......@@ -51,7 +51,7 @@ int transcode_create(transcoder_t *ptc, char *encoding)
{
transcoder_t tc;
int i;
if ( ( tc = calloc(sizeof(struct _transcoder), 1) ) == NULL )
return ENOMEM;
for( i = 0; i < NUM_TRANSCODERS; i++ ) {
......@@ -61,7 +61,7 @@ int transcode_create(transcoder_t *ptc, char *encoding)
if ( i == NUM_TRANSCODERS )
return ENOTSUP;
stream_create(&tc->ustream, tc );
stream_create(&tc->ustream, 0, tc );
stream_set_read(tc->ustream, tclist[i].decode_read, tc );
stream_set_write(tc->ustream, tclist[i].encode_write, tc );
tc->destroy = tclist[i].destroy;
......@@ -73,8 +73,8 @@ void transcode_destroy(transcoder_t *ptc)
{
transcoder_t tc = *ptc;
if ( tc->destroy)
tc->destroy(tc);
if ( tc->destroy)
tc->destroy(tc);
stream_destroy(&tc->ustream, tc);
free(tc);
*ptc = NULL;
......@@ -85,7 +85,7 @@ int transcode_get_stream(transcoder_t tc, stream_t *pstream)
if (tc == NULL || pstream == NULL)
return EINVAL;
*pstream = tc->ustream;
return 0;
return 0;
}
int transcode_set_stream(transcoder_t tc, stream_t stream)
......@@ -93,7 +93,7 @@ int transcode_set_stream(transcoder_t tc, stream_t stream)
if (tc == NULL || stream == NULL)
return EINVAL;
tc->stream = stream;
return 0;
return 0;
}
/*------------------------------------------------------
......@@ -142,9 +142,9 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz
int i, tmp = 0, ret;
size_t isize, consumed = 0;
char *iptr;
transcoder_t tc = (transcoder_t)ustream->owner;
transcoder_t tc = (transcoder_t)ustream->owner;
struct _b64_decode *decode;
if ( nbytes == NULL || optr == NULL || osize == 0 )
return EINVAL;
......@@ -152,19 +152,19 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz
if ( ( decode = tc->tcdata ) == NULL ) {
if ( ( decode = calloc(1, sizeof(struct _b64_decode)) ) == NULL )
return ENOMEM;
tc->tcdata = decode;
tc->tcdata = decode;
}
if ( decode->offset != offset )
return ESPIPE;
if ( ( iptr = alloca(osize) ) == NULL )
return ENOMEM;
isize = osize;
if ( ( ret = stream_read(tc->stream, iptr, isize, decode->cur_offset, &isize) ) != 0 )
if ( ( ret = stream_read(tc->stream, iptr, isize, decode->cur_offset, &isize) ) != 0 )
return ret;
decode->cur_offset += isize;
decode->cur_offset += isize;
i = decode->data_len;
while ( consumed < isize ) {
while ( ( i < 4 ) && ( consumed < isize ) ) {
......@@ -234,7 +234,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t
size_t isize, consumed = 0;
struct _qp_decode *decode;
char *iptr;
if ( nbytes == NULL || optr == NULL )
return EINVAL;
......@@ -242,11 +242,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t
if ( ( decode = tc->tcdata ) == NULL ) {
if ( ( decode = calloc(1, sizeof(struct _qp_decode)) ) == NULL )
return ENOMEM;
tc->tcdata = decode;
tc->tcdata = decode;
}
if ( decode->offset != offset )
return ESPIPE;
if ( ( iptr = alloca(osize) ) == NULL )
return ENOMEM;
isize = osize;
......@@ -254,11 +254,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t
if ( decode->data_len )
memcpy( iptr, decode->data, decode->data_len);
if ( ( ret = stream_read(tc->stream, iptr + decode->data_len, isize - decode->data_len, decode->cur_offset, &isize) ) != 0 )
if ( ( ret = stream_read(tc->stream, iptr + decode->data_len, isize - decode->data_len, decode->cur_offset, &isize) ) != 0 )
return ret;
decode->data_len = 0;
decode->cur_offset += isize;
decode->cur_offset += isize;
while (consumed < isize) {
c = *iptr++;
if ( ((c >= 33) && (c <= 60)) || ((c >= 62) && (c <= 126)) || ((c == '=') && !_ishex(*iptr)) ) {
......@@ -271,7 +271,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t
if ((isize - consumed) < 3) {
memcpy( decode->data, iptr-1, decode->data_len = isize - consumed + 1);
return 0;
}
}
else {
// you get =XX where XX are hex characters
char chr[2];
......@@ -334,7 +334,7 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s
int consumed = 0, c, osize;
char *obuf;
struct _qp_encode *encode;
if ( nbytes == NULL || iptr == NULL )
return EINVAL;
......@@ -342,9 +342,9 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s
if ( isize == 0 )
return 0;
*nbytes = 0;
*nbytes = 0;
if ( ( encode = tc->tcdata ) == NULL ) {
}
if ( ( encode->obuf = malloc(isize) ) == NULL )
return ENOMEM;
......
......@@ -19,7 +19,6 @@
#include <registrar.h>
#include <errno.h>
#include <cpystr.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
......@@ -33,27 +32,6 @@ struct url_registrar _url_pop_registrar =
url_pop_create, url_pop_destroy
};
static int get_auth (const url_pop_t up, char *s, size_t len, size_t *);
static int
get_auth (const url_pop_t up, char *s, size_t len, size_t *n)
{
size_t i;
if (up)
return EINVAL;
i = _cpystr (s, up->auth, len);
if (n)
*n = i;
return 0;
}
int
(url_pop_get_auth) (const url_t url, char *auth, size_t len, size_t *n)
{
return (url) ?
((url_pop_t)(url->data))->_get_auth(url->data, auth, len, n) : EINVAL;
}
static void
url_pop_destroy (url_t *purl)
{
......@@ -64,12 +42,6 @@ url_pop_destroy (url_t *purl)
free (url->user);
free (url->passwd);
free (url->host);
if (url->data)
{
url_pop_t up = url->data;
free (up->auth);
}
free (url->data);
free (url);
*purl = NULL;
}
......@@ -77,28 +49,25 @@ url_pop_destroy (url_t *purl)
/*
POP URL
pop://<user>;AUTH=<auth>@<host>:<port>
pop://[<user>;AUTH=<auth>@]<host>[:<port>]
*/
static int
url_pop_create (url_t *purl, const char *name)
{
const char *host_port, *indexe;
struct url_registrar *ureg = &_url_pop_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
size_t scheme_len = strlen (ureg->scheme);
url_t url;
url_pop_t up;
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (host_port = strchr (name, '@')) == NULL
|| (len = strlen (name)) < 9 /* 6(scheme)+1(user)+1(@)+1(host)*/)
return ENOMEM;
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
url->data = up = calloc(1, sizeof(*up));
up->_get_auth = get_auth;
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_create = _url_pop_registrar._create;
......@@ -114,24 +83,22 @@ url_pop_create (url_t *purl, const char *name)
name += scheme_len; /* pass the scheme */
host_port = strchr (name, '@');
if (host_port == NULL)
host_port= name;
/* looking for "user;auth=auth-enc" */
for (indexe = name; indexe != host_port; indexe++)
{
/* Auth ? */
if (*indexe == ';')
{
/* make sure it the token */
/* make sure it's the token */
if (strncasecmp(indexe + 1, "auth=", 5) == 0)
break;
}
}
if (indexe == name)
{
url_pop_destroy (&url);
return -1;
}
/* USER */
url->user = malloc(indexe - name + 1);
if (url->user == NULL)
......@@ -142,29 +109,29 @@ url_pop_create (url_t *purl, const char *name)
((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
/* AUTH */
if ((host_port - indexe) <= 6 /*strlen(";AUTH=")*/)
if (indexe == host_port)
{
/* Use default AUTH '*' */
up->auth = malloc (1 + 1);
if (up->auth)
url->passwd = malloc (1 + 1);
if (url->passwd)
{
up->auth[0] = '*';
up->auth[1] = '\0';
url->passwd[0] = '*';
url->passwd[1] = '\0';
}
}
else
{
/* move pass AUTH= */
indexe += 6;
up->auth = malloc (host_port - indexe + 1);
if (up->auth)
url->passwd = malloc (host_port - indexe + 1);
if (url->passwd)
{
((char *)memcpy (up->auth, indexe, host_port - indexe))
((char *)memcpy (url->passwd, indexe, host_port - indexe))
[host_port - indexe] = '\0';
}
}
if (up->auth == NULL)
if (url->passwd == NULL)
{
url_pop_destroy (&url);
return -1;
......