SILC Runtime Toolkit 1.2 Beta 1
[runtime.git] / lib / silcutil / silcmime.h
index 62ebdefcef82efb8631863f21c06dd58b019c102..e2831e6a4620098fdf5b22f0bfc06821cabb65c7 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 Pekka Riikonen
+  Copyright (C) 2005 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 
 */
 
-/****h* silcutil/SILC MIME Interface
+/****h* silcutil/MIME Interface
  *
  * DESCRIPTION
  *
- * Simple implementation of MIME.  Supports creation and parsing of simple
+ * Simple MIME Interface.  This API supports creation and parsing of simple
  * MIME messages, multipart MIME messages, including nested multiparts, and
  * MIME fragmentation and defragmentation.
  *
+ * SILC Mime API is not thread-safe.  If the same SilcMime context must be
+ * used in multithreaded environment concurrency control must be employed.
+ *
  ***/
 
 #ifndef SILCMIME_H
 #define SILCMIME_H
 
-/****s* silcutil/SILCMIMEAPI/SilcMime
+/****s* silcutil/SilcMime
  *
  * NAME
  *
@@ -45,7 +48,7 @@
  ***/
 typedef struct SilcMimeStruct *SilcMime;
 
-/****s* silcutil/SILCMIMEAPI/SilcMimeAssembler
+/****s* silcutil/SilcMimeAssembler
  *
  * NAME
  *
@@ -60,7 +63,7 @@ typedef struct SilcMimeStruct *SilcMime;
  ***/
 typedef struct SilcMimeAssemblerStruct *SilcMimeAssembler;
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_alloc
+/****f* silcutil/silc_mime_alloc
  *
  * SYNOPSIS
  *
@@ -68,12 +71,13 @@ typedef struct SilcMimeAssemblerStruct *SilcMimeAssembler;
  *
  * DESCRIPTION
  *
- *    Allocates SILC Mime message context.
+ *    Allocates SILC Mime message context.  Returns NULL if system is out of
+ *    memory.
  *
  ***/
 SilcMime silc_mime_alloc(void);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_free
+/****f* silcutil/silc_mime_free
  *
  * SYNOPSIS
  *
@@ -86,7 +90,7 @@ SilcMime silc_mime_alloc(void);
  ***/
 void silc_mime_free(SilcMime mime);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_assembler_alloc
+/****f* silcutil/silc_mime_assembler_alloc
  *
  * SYNOPSIS
  *
@@ -94,12 +98,13 @@ void silc_mime_free(SilcMime mime);
  *
  * DESCRIPTION
  *
- *    Allocates MIME fragment assembler.
+ *    Allocates MIME fragment assembler.  Returns NULL if system is out of
+ *    memory.
  *
  ***/
 SilcMimeAssembler silc_mime_assembler_alloc(void);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_assembler_free
+/****f* silcutil/silc_mime_assembler_free
  *
  * SYNOPSIS
  *
@@ -112,34 +117,61 @@ SilcMimeAssembler silc_mime_assembler_alloc(void);
  ***/
 void silc_mime_assembler_free(SilcMimeAssembler assembler);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_decode
+/****f* silcutil/silc_mime_assembler_purge
+ *
+ * SYNOPSIS
+ *
+ *    void silc_mime_assembler_purge(SilcMimeAssembler assembler,
+ *                                   SilcUInt32 purge_minutes);
+ *
+ * DESCRIPTION
+ *
+ *    Purges the MIME fragment assembler from old fragments that have never
+ *    completed into a full MIME message.  This function may be called
+ *    periodically to purge MIME fragments.  The `purge_minutes' specify
+ *    how old fragments are purged.  If it is 0, fragments older than 5 minutes
+ *    are purged, by default.  The value is in minutes.
+ *
+ *    It is usefull to call this periodically to assure that memory is not
+ *    consumed needlessly by keeping old unfinished fragments in a long
+ *    running assembler.
+ *
+ ***/
+void silc_mime_assembler_purge(SilcMimeAssembler assembler,
+                              SilcUInt32 purge_minutes);
+
+/****f* silcutil/silc_mime_decode
  *
  * SYNOPSIS
  *
- *    SilcMime silc_mime_decode(const unsigned char *data,
+ *    SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
  *                              SilcUInt32 data_len);
  *
  * DESCRIPTION
  *
  *    Decodes a MIME message and returns the parsed message into newly
- *    allocated SilcMime context.
+ *    allocated SilcMime context and returns it.  If `mime' is non-NULL
+ *    then the MIME message will be encoded into the pre-allocated `mime'
+ *    context and same context is returned.  If it is NULL then newly
+ *    allocated SilcMime context is returned.  On error NULL is returned.
  *
  * EXAMPLE
  *
  *    // Parse MIME message and get its content type
- *    mime = silc_mime_decode(data, data_len);
+ *    mime = silc_mime_decode(NULL, data, data_len);
  *    type = silc_mime_get_field(mime, "Content-Type");
  *    ...
  *
  *    // Assemble received MIME fragment
- *    mime = silc_mime_decode(data, data_len);
+ *    mime = silc_mime_decode(NULL, data, data_len);
  *    if (silc_mime_is_partial(mime) == TRUE)
  *      silc_mime_assmeble(assembler, mime);
  *
  ***/
-SilcMime silc_mime_decode(const unsigned char *data, SilcUInt32 data_len);
+SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
+                         SilcUInt32 data_len);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_encode
+/****f* silcutil/silc_mime_encode
  *
  * SYNOPSIS
  *
@@ -158,7 +190,7 @@ SilcMime silc_mime_decode(const unsigned char *data, SilcUInt32 data_len);
  ***/
 unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_assemble
+/****f* silcutil/silc_mime_assemble
  *
  * SYNOPSIS
  *
@@ -169,7 +201,7 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len);
  *
  *    Processes and attempts to assemble the received MIME fragment `partial'.
  *    To check if a received MIME message is a fragment use the
- *    silc_mime_is_partial function.  Returns NULL if all fragments has not
+ *    silc_mime_is_partial function.  Returns NULL if all fragments have not
  *    yet been received, or the newly allocated completed MIME message if
  *    all fragments were received.  The caller must free the returned
  *    SilcMime context.  The caller must not free the `partial'.
@@ -188,7 +220,7 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len);
  ***/
 SilcMime silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_encode_partial
+/****f* silcutil/silc_mime_encode_partial
  *
  * SYNOPSIS
  *
@@ -208,7 +240,7 @@ SilcMime silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial);
  ***/
 SilcDList silc_mime_encode_partial(SilcMime mime, int max_size);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_partial_free
+/****f* silcutil/silc_mime_partial_free
  *
  * SYNOPSIS
  *
@@ -222,7 +254,7 @@ SilcDList silc_mime_encode_partial(SilcMime mime, int max_size);
  ***/
 void silc_mime_partial_free(SilcDList partials);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_add_field
+/****f* silcutil/silc_mime_add_field
  *
  * SYNOPSIS
  *
@@ -243,7 +275,7 @@ void silc_mime_partial_free(SilcDList partials);
  ***/
 void silc_mime_add_field(SilcMime mime, const char *field, const char *value);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_get_field
+/****f* silcutil/silc_mime_get_field
  *
  * SYNOPSIS
  *
@@ -257,7 +289,7 @@ void silc_mime_add_field(SilcMime mime, const char *field, const char *value);
  ***/
 const char *silc_mime_get_field(SilcMime mime, const char *field);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_add_data
+/****f* silcutil/silc_mime_add_data
  *
  * SYNOPSIS
  *
@@ -272,7 +304,7 @@ const char *silc_mime_get_field(SilcMime mime, const char *field);
 void silc_mime_add_data(SilcMime mime, const unsigned char *data,
                        SilcUInt32 data_len);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_get_data
+/****f* silcutil/silc_mime_get_data
  *
  * SYNOPSIS
  *
@@ -286,7 +318,23 @@ void silc_mime_add_data(SilcMime mime, const unsigned char *data,
  ***/
 const unsigned char *silc_mime_get_data(SilcMime mime, SilcUInt32 *data_len);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_is_partial
+/****f* silcutil/silc_mime_steal_data
+ *
+ * SYNOPSIS
+ *
+ *    unsigned char *
+ *    silc_mime_steal_data(SilcMime mime, SilcUInt32 *data_len);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the MIME data from the `mime' message.  The data will be
+ *    removed from the `mime' and the caller is responsible of freeing the
+ *    returned pointer.
+ *
+ ***/
+unsigned char *silc_mime_steal_data(SilcMime mime, SilcUInt32 *data_len);
+
+/****f* silcutil/silc_mime_is_partial
  *
  * SYNOPSIS
  *
@@ -299,7 +347,7 @@ const unsigned char *silc_mime_get_data(SilcMime mime, SilcUInt32 *data_len);
  ***/
 SilcBool silc_mime_is_partial(SilcMime mime);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_set_multipart
+/****f* silcutil/silc_mime_set_multipart
  *
  * SYNOPSIS
  *
@@ -316,7 +364,7 @@ SilcBool silc_mime_is_partial(SilcMime mime);
 void silc_mime_set_multipart(SilcMime mime, const char *type,
                             const char *boundary);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_add_multipart
+/****f* silcutil/silc_mime_add_multipart
  *
  * SYNOPSIS
  *
@@ -346,7 +394,7 @@ void silc_mime_set_multipart(SilcMime mime, const char *type,
  ***/
 SilcBool silc_mime_add_multipart(SilcMime mime, SilcMime part);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_is_multipart
+/****f* silcutil/silc_mime_is_multipart
  *
  * SYNOPSIS
  *
@@ -360,7 +408,7 @@ SilcBool silc_mime_add_multipart(SilcMime mime, SilcMime part);
  ***/
 SilcBool silc_mime_is_multipart(SilcMime mime);
 
-/****f* silcutil/SILCMIMEAPI/silc_mime_get_multiparts
+/****f* silcutil/silc_mime_get_multiparts
  *
  * SYNOPSIS
  *
@@ -377,4 +425,6 @@ SilcBool silc_mime_is_multipart(SilcMime mime);
  ***/
 SilcDList silc_mime_get_multiparts(SilcMime mime, const char **type);
 
+#include "silcmime_i.h"
+
 #endif /* SILCMIME_H */