src/transport.c
author vb
Mon, 29 Dec 2014 20:48:30 +0100
changeset 38 271bea5552dc
parent 37 7928e0e6eee9
child 39 66b5cc6cb987
permissions -rw-r--r--
encrypt in pieces
     1 #include "pEp_internal.h"
     2 
     3 #include <memory.h>
     4 #include <assert.h>
     5 
     6 PEP_STATUS init_transport_system(PEP_SESSION session)
     7 {
     8     assert(session);
     9 
    10     pEpSession *_session = (pEpSession *) session;
    11     PEP_transport_t* transports = _session->transports;
    12 
    13     assert(PEP_trans__count == 1);
    14     memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
    15 
    16     transports[0].id = PEP_trans_auto;
    17 
    18     return PEP_STATUS_OK;
    19 }
    20 
    21 void release_transport_system(PEP_SESSION session)
    22 {
    23     assert(session);
    24     // nothing yet
    25 }
    26 
    27 identity_list *new_identity_list(const pEp_identity *ident)
    28 {
    29     identity_list *id_list = calloc(1, sizeof(identity_list));
    30     assert(id_list);
    31     if (id_list == NULL)
    32         return NULL;
    33 
    34     if (ident) {
    35         id_list->ident = identity_dup(ident);
    36         assert(id_list->ident);
    37         if (id_list->ident == NULL) {
    38             free(id_list);
    39             return NULL;
    40         }
    41     }
    42 
    43     return id_list;
    44 }
    45 
    46 identity_list *identity_list_dup(const identity_list *src)
    47 {
    48     assert(src);
    49 
    50     identity_list *id_list = new_identity_list(src->ident);
    51     assert(id_list);
    52     if (id_list == NULL)
    53         return NULL;
    54 
    55     if (src->next) {
    56         id_list->next = identity_list_dup(src->next);
    57         if (id_list->next == NULL) {
    58             free_identity_list(id_list);
    59             return NULL;
    60         }
    61     }
    62 
    63     return id_list;
    64 }
    65 
    66 void free_identity_list(identity_list *id_list)
    67 {
    68     if (id_list) {
    69         free_identity_list(id_list->next);
    70         free_identity(id_list->ident);
    71         free(id_list);
    72     }
    73 }
    74 
    75 identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
    76 {
    77     assert(ident);
    78 
    79     if (id_list == NULL)
    80         return new_identity_list(ident);
    81 
    82     if (id_list->ident == NULL) {
    83         id_list->ident = identity_dup(ident);
    84         assert(id_list->ident);
    85         if (id_list->ident == NULL)
    86             return NULL;
    87         else
    88             return id_list;
    89     }
    90     else if (id_list->next == NULL) {
    91         id_list->next = new_identity_list(ident);
    92         assert(id_list->next);
    93         return id_list->next;
    94     }
    95     else {
    96         return identity_list_add(id_list->next, ident);
    97     }
    98 }
    99 
   100 bloblist_t *new_bloblist(char *blob, size_t size)
   101 {
   102     bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
   103     if (bloblist == NULL)
   104         return NULL;
   105     bloblist->data_ref = blob;
   106     bloblist->size = size;
   107     return bloblist;
   108 }
   109 
   110 bloblist_t *bloblist_dup(const bloblist_t *src)
   111 {
   112     assert(src);
   113 
   114     if (src) {
   115         bloblist_t * dst = new_bloblist(src->data_ref, src->size);
   116         if (dst == NULL)
   117             return NULL;
   118         dst->next = bloblist_dup(src->next);
   119         return dst;
   120     }
   121     else
   122         return NULL;
   123 }
   124 
   125 void free_bloblist(bloblist_t *bloblist)
   126 {
   127     if (bloblist && bloblist->next)
   128         free_bloblist(bloblist->next);
   129     free(bloblist);
   130 }
   131 
   132 bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size)
   133 {
   134     assert(blob);
   135 
   136     if (bloblist == NULL)
   137         return new_bloblist(blob, size);
   138 
   139     if (bloblist->data_ref == NULL) {
   140         bloblist->data_ref = blob;
   141         bloblist->size = size;
   142         return bloblist;
   143     }
   144 
   145     if (bloblist->next == NULL) {
   146         bloblist->next = new_bloblist(blob, size);
   147         return bloblist->next;
   148     }
   149 
   150     return bloblist_add(bloblist->next, blob, size);
   151 }
   152 
   153 message *new_message(
   154         PEP_msg_direction dir,
   155         const pEp_identity *from,
   156         const identity_list *to,
   157         const char *shortmsg
   158     )
   159 {
   160     message *msg = calloc(1, sizeof(message));
   161     assert(msg);
   162     if (msg == NULL)
   163         return NULL;
   164 
   165     if (msg->shortmsg) {
   166         msg->shortmsg = strdup(shortmsg);
   167         assert(msg->shortmsg);
   168         if (msg->shortmsg == NULL) {
   169             free(msg);
   170             return NULL;
   171         }
   172         msg->shortmsg_size = strlen(msg->shortmsg);
   173     }
   174 
   175     msg->dir = dir;
   176 
   177     msg->from = identity_dup(from);
   178     assert(msg->from);
   179     if (msg->from == NULL) {
   180         free_message(msg);
   181         return NULL;
   182     }
   183 
   184     msg->to = identity_list_dup(to);
   185     assert(msg->to);
   186     if (msg->to == NULL) {
   187         free_message(msg);
   188         return NULL;
   189     }
   190 
   191     return msg;
   192 }
   193 
   194 void free_message(message *msg)
   195 {
   196     free(msg->id);
   197     free(msg->shortmsg);
   198     free(msg->longmsg);
   199     free(msg->longmsg_formatted);
   200     free_bloblist(msg->attachments);
   201     free(msg->rawmsg);
   202     free_identity_list(msg->to);
   203     free_identity_list(msg->cc);
   204     free_identity_list(msg->bcc);
   205     free(msg->refering_id);
   206     free_message_ref_list(msg->refered_by);
   207     free(msg);
   208 }
   209 
   210 message_ref_list *new_message_ref_list(message *msg)
   211 {
   212     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   213     assert(msg_list);
   214     if (msg_list == NULL)
   215         return NULL;
   216 
   217     msg_list->msg_ref = msg;
   218 
   219     return msg_list;
   220 }
   221 
   222 void free_message_ref_list(message_ref_list *msg_list)
   223 {
   224     if (msg_list) {
   225         free_message_ref_list(msg_list->next);
   226         free(msg_list);
   227     }
   228 }
   229 
   230 message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   231 {
   232     assert(msg);
   233 
   234     if (msg_list == NULL)
   235         return new_message_ref_list(msg);
   236 
   237     if (msg_list->msg_ref == NULL) {
   238         msg_list->msg_ref = msg;
   239         return msg_list;
   240     }
   241     else if (msg_list->next == NULL) {
   242         msg_list->next = new_message_ref_list(msg);
   243         assert(msg_list->next);
   244         return msg_list->next;
   245     }
   246     else {
   247         return message_ref_list_add(msg_list->next, msg);
   248     }
   249 }
   250