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