src/transport.c
author vb
Sun, 01 Mar 2015 15:32:16 +0100
changeset 81 044ce1c7fc9c
parent 76 6da29caa5dae
child 89 aef5a4bc78f3
permissions -rw-r--r--
...
vb@28
     1
#include "pEp_internal.h"
vb@46
     2
#include "trans_auto.h"
vb@23
     3
vb@23
     4
#include <memory.h>
vb@23
     5
#include <assert.h>
vb@23
     6
vb@62
     7
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first)
vb@23
     8
{
vb@62
     9
    static PEP_transport_t transports[PEP_trans__count];
vb@62
    10
    
vb@30
    11
    assert(session);
vb@62
    12
    session->transports = transports;
vb@30
    13
vb@62
    14
    if (in_first) {
vb@62
    15
        assert(PEP_trans__count == 1);
vb@62
    16
        memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
vb@28
    17
vb@62
    18
        transports[PEP_trans_auto].id = PEP_trans_auto;
vb@62
    19
        transports[PEP_trans_auto].sendto = auto_sendto;
vb@62
    20
        transports[PEP_trans_auto].readnext = auto_readnext;
vb@62
    21
    }
vb@23
    22
vb@23
    23
    return PEP_STATUS_OK;
vb@23
    24
}
vb@28
    25
vb@62
    26
void release_transport_system(PEP_SESSION session, bool out_last)
vb@28
    27
{
vb@30
    28
    assert(session);
vb@28
    29
    // nothing yet
vb@28
    30
}
vb@29
    31
vb@48
    32
DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
vb@29
    33
{
vb@29
    34
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@29
    35
    assert(id_list);
vb@76
    36
    if (id_list == NULL)
vb@29
    37
        return NULL;
vb@29
    38
vb@39
    39
    id_list->ident = ident;
vb@29
    40
vb@29
    41
    return id_list;
vb@29
    42
}
vb@29
    43
vb@48
    44
DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
vb@37
    45
{
vb@37
    46
    assert(src);
vb@37
    47
vb@40
    48
    identity_list *id_list = new_identity_list(identity_dup(src->ident));
vb@37
    49
    assert(id_list);
vb@37
    50
    if (id_list == NULL)
vb@37
    51
        return NULL;
vb@37
    52
vb@37
    53
    if (src->next) {
vb@37
    54
        id_list->next = identity_list_dup(src->next);
vb@37
    55
        if (id_list->next == NULL) {
vb@37
    56
            free_identity_list(id_list);
vb@37
    57
            return NULL;
vb@37
    58
        }
vb@37
    59
    }
vb@37
    60
vb@37
    61
    return id_list;
vb@37
    62
}
vb@37
    63
vb@48
    64
DYNAMIC_API void free_identity_list(identity_list *id_list)
vb@29
    65
{
vb@29
    66
    if (id_list) {
vb@29
    67
        free_identity_list(id_list->next);
vb@29
    68
        free_identity(id_list->ident);
vb@29
    69
        free(id_list);
vb@29
    70
    }
vb@29
    71
}
vb@29
    72
vb@48
    73
DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
vb@29
    74
{
vb@29
    75
    assert(ident);
vb@29
    76
vb@38
    77
    if (id_list == NULL)
vb@38
    78
        return new_identity_list(ident);
vb@38
    79
vb@29
    80
    if (id_list->ident == NULL) {
vb@39
    81
        id_list->ident = ident;
vb@39
    82
        return id_list;
vb@29
    83
    }
vb@29
    84
    else if (id_list->next == NULL) {
vb@29
    85
        id_list->next = new_identity_list(ident);
vb@29
    86
        return id_list->next;
vb@29
    87
    }
vb@29
    88
    else {
vb@29
    89
        return identity_list_add(id_list->next, ident);
vb@29
    90
    }
vb@29
    91
}
vb@29
    92
vb@48
    93
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@39
    94
        const char *file_name)
vb@38
    95
{
vb@38
    96
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@81
    97
    assert(bloblist);
vb@38
    98
    if (bloblist == NULL)
vb@38
    99
        return NULL;
vb@81
   100
vb@39
   101
    if (mime_type) {
vb@39
   102
        bloblist->mime_type = strdup(mime_type);
vb@39
   103
        if (bloblist->mime_type == NULL) {
vb@39
   104
            free(bloblist);
vb@39
   105
            return NULL;
vb@39
   106
        }
vb@39
   107
    }
vb@81
   108
vb@39
   109
    if (file_name) {
vb@39
   110
        bloblist->file_name = strdup(file_name);
vb@39
   111
        if (bloblist->file_name == NULL) {
vb@39
   112
            free(bloblist->mime_type);
vb@39
   113
            free(bloblist);
vb@39
   114
            return NULL;
vb@39
   115
        }
vb@39
   116
    }
vb@81
   117
vb@41
   118
    bloblist->data = blob;
vb@41
   119
    bloblist->size = size;
vb@81
   120
vb@38
   121
    return bloblist;
vb@38
   122
}
vb@38
   123
vb@48
   124
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@38
   125
{
vb@39
   126
    if (bloblist) {
vb@39
   127
        if (bloblist->next)
vb@39
   128
            free_bloblist(bloblist->next);
vb@41
   129
        free(bloblist->data);
vb@41
   130
        free(bloblist->mime_type);
vb@41
   131
        free(bloblist->file_name);
vb@39
   132
        free(bloblist);
vb@39
   133
    }
vb@38
   134
}
vb@38
   135
vb@81
   136
DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
vb@81
   137
{
vb@81
   138
    bloblist_t *bloblist = NULL;
vb@81
   139
vb@81
   140
    assert(src);
vb@81
   141
vb@81
   142
    bloblist = new_bloblist(src->data, src->size, src->mime_type, src->file_name);
vb@81
   143
    if (bloblist == NULL)
vb@81
   144
        goto enomem;
vb@81
   145
vb@81
   146
    if (src->next) {
vb@81
   147
        bloblist->next = bloblist_dup(src->next);
vb@81
   148
        if (bloblist->next == NULL)
vb@81
   149
            goto enomem;
vb@81
   150
    }
vb@81
   151
vb@81
   152
    return bloblist;
vb@81
   153
vb@81
   154
enomem:
vb@81
   155
    free_bloblist(bloblist);
vb@81
   156
    return NULL;
vb@81
   157
}
vb@81
   158
vb@48
   159
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@39
   160
        const char *mime_type, const char *file_name)
vb@38
   161
{
vb@38
   162
    assert(blob);
vb@38
   163
vb@38
   164
    if (bloblist == NULL)
vb@39
   165
        return new_bloblist(blob, size, mime_type, file_name);
vb@38
   166
vb@41
   167
    if (bloblist->data == NULL) {
vb@39
   168
        if (mime_type) {
vb@39
   169
            bloblist->mime_type = strdup(mime_type);
vb@39
   170
            if (bloblist->mime_type == NULL) {
vb@39
   171
                free(bloblist);
vb@39
   172
                return NULL;
vb@39
   173
            }
vb@39
   174
        }
vb@39
   175
        if (file_name) {
vb@39
   176
            bloblist->file_name = strdup(file_name);
vb@39
   177
            if (bloblist->file_name == NULL) {
vb@39
   178
                free(bloblist->mime_type);
vb@39
   179
                free(bloblist);
vb@39
   180
                return NULL;
vb@39
   181
            }
vb@39
   182
        }
vb@41
   183
        bloblist->data = blob;
vb@41
   184
        bloblist->size = size;
vb@38
   185
        return bloblist;
vb@38
   186
    }
vb@38
   187
vb@38
   188
    if (bloblist->next == NULL) {
vb@39
   189
        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
vb@38
   190
        return bloblist->next;
vb@38
   191
    }
vb@38
   192
vb@39
   193
    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
vb@38
   194
}
vb@38
   195
vb@48
   196
DYNAMIC_API message *new_message(
vb@38
   197
        PEP_msg_direction dir,
vb@39
   198
        pEp_identity *from,
vb@39
   199
        identity_list *to,
vb@29
   200
        const char *shortmsg
vb@29
   201
    )
vb@29
   202
{
vb@29
   203
    message *msg = calloc(1, sizeof(message));
vb@29
   204
    assert(msg);
vb@29
   205
    if (msg == NULL)
vb@29
   206
        return NULL;
vb@29
   207
vb@39
   208
    if (shortmsg) {
vb@37
   209
        msg->shortmsg = strdup(shortmsg);
vb@37
   210
        assert(msg->shortmsg);
vb@37
   211
        if (msg->shortmsg == NULL) {
vb@37
   212
            free(msg);
vb@37
   213
            return NULL;
vb@37
   214
        }
vb@29
   215
    }
vb@29
   216
vb@29
   217
    msg->dir = dir;
vb@39
   218
    msg->from = from;
vb@39
   219
    msg->to = to;
vb@29
   220
vb@29
   221
    return msg;
vb@29
   222
}
vb@29
   223
vb@48
   224
DYNAMIC_API void free_message(message *msg)
vb@29
   225
{
vb@63
   226
    if (msg) {
vb@63
   227
        free(msg->id);
vb@63
   228
        free(msg->shortmsg);
vb@63
   229
        free(msg->longmsg);
vb@63
   230
        free(msg->longmsg_formatted);
vb@63
   231
        free_bloblist(msg->attachments);
vb@63
   232
        free_identity(msg->from);
vb@63
   233
        free_identity_list(msg->to);
vb@63
   234
        free_identity(msg->recv_by);
vb@63
   235
        free_identity_list(msg->cc);
vb@63
   236
        free_identity_list(msg->bcc);
vb@81
   237
        free_identity(msg->reply_to);
vb@63
   238
        free(msg->refering_id);
vb@63
   239
        free(msg);
vb@63
   240
    }
vb@29
   241
}
vb@29
   242
vb@81
   243
DYNAMIC_API message * message_dup(const message *src)
vb@81
   244
{
vb@81
   245
    message * msg = NULL;
vb@81
   246
    pEp_identity * from = NULL;
vb@81
   247
    identity_list * to = NULL;
vb@81
   248
vb@81
   249
    assert(src);
vb@81
   250
vb@81
   251
    from = identity_dup(src->from);
vb@81
   252
    if (from == NULL)
vb@81
   253
        goto enomem;
vb@81
   254
vb@81
   255
    to = identity_list_dup(src->to);
vb@81
   256
    if (to == NULL)
vb@81
   257
        goto enomem;
vb@81
   258
vb@81
   259
    msg = new_message(src->dir, from, to, src->shortmsg);
vb@81
   260
    if (msg == NULL)
vb@81
   261
        goto enomem;
vb@81
   262
vb@81
   263
    if (src->id) {
vb@81
   264
        msg->id = strdup(src->id);
vb@81
   265
        assert(msg->id);
vb@81
   266
        if (msg->id == NULL)
vb@81
   267
            goto enomem;
vb@81
   268
    }
vb@81
   269
vb@81
   270
    if (src->longmsg) {
vb@81
   271
        msg->longmsg = strdup(src->longmsg);
vb@81
   272
        assert(msg->longmsg);
vb@81
   273
        if (msg->longmsg == NULL)
vb@81
   274
            goto enomem;
vb@81
   275
    }
vb@81
   276
    
vb@81
   277
    if (src->longmsg_formatted) {
vb@81
   278
        msg->longmsg_formatted = strdup(src->longmsg_formatted);
vb@81
   279
        assert(msg->longmsg_formatted);
vb@81
   280
        if (msg->longmsg_formatted == NULL)
vb@81
   281
            goto enomem;
vb@81
   282
    }
vb@81
   283
vb@81
   284
    if (src->attachments) {
vb@81
   285
        msg->attachments = bloblist_dup(src->attachments);
vb@81
   286
        if (msg->attachments == NULL)
vb@81
   287
            goto enomem;
vb@81
   288
    }
vb@81
   289
vb@81
   290
    msg->rawmsg_ref = src->rawmsg_ref;
vb@81
   291
    msg->rawmsg_size = src->rawmsg_size;
vb@81
   292
    msg->sent = src->sent;
vb@81
   293
    msg->recv = src->recv;
vb@81
   294
vb@81
   295
    if (src->recv_by) {
vb@81
   296
        msg->recv_by = identity_dup(src->recv_by);
vb@81
   297
        if (msg->recv_by == NULL)
vb@81
   298
            goto enomem;
vb@81
   299
    }
vb@81
   300
vb@81
   301
    if (src->cc) {
vb@81
   302
        msg->cc = identity_list_dup(src->cc);
vb@81
   303
        if (msg->cc == NULL)
vb@81
   304
            goto enomem;
vb@81
   305
    }
vb@81
   306
vb@81
   307
    if (src->bcc) {
vb@81
   308
        msg->bcc = identity_list_dup(src->bcc);
vb@81
   309
        if (msg->bcc == NULL)
vb@81
   310
            goto enomem;
vb@81
   311
    }
vb@81
   312
vb@81
   313
    if (src->reply_to) {
vb@81
   314
        msg->reply_to = identity_dup(src->reply_to);
vb@81
   315
        if (msg->reply_to == NULL)
vb@81
   316
            goto enomem;
vb@81
   317
    }
vb@81
   318
vb@81
   319
    if (src->refering_id) {
vb@81
   320
        msg->refering_id = strdup(src->refering_id);
vb@81
   321
        assert(msg->refering_id);
vb@81
   322
        if (msg->refering_id == NULL)
vb@81
   323
            goto enomem;
vb@81
   324
    }
vb@81
   325
vb@81
   326
    msg->refering_msg_ref = src->refering_msg_ref;
vb@81
   327
    
vb@81
   328
    if (src->refered_by) {
vb@81
   329
        msg->refered_by = message_ref_list_dup(src->refered_by);
vb@81
   330
        if (msg->refered_by == NULL)
vb@81
   331
            goto enomem;
vb@81
   332
    }
vb@81
   333
vb@81
   334
    msg->enc_format = src->enc_format;
vb@81
   335
vb@81
   336
    return msg;
vb@81
   337
vb@81
   338
enomem:
vb@81
   339
    if (msg) {
vb@81
   340
        free_message(msg);
vb@81
   341
    }
vb@81
   342
    else {
vb@81
   343
        free_identity(from);
vb@81
   344
        free_identity_list(to);
vb@81
   345
    }
vb@81
   346
vb@81
   347
    return NULL;
vb@81
   348
}
vb@81
   349
vb@48
   350
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@29
   351
{
vb@29
   352
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   353
    assert(msg_list);
vb@29
   354
    if (msg_list == NULL)
vb@29
   355
        return NULL;
vb@29
   356
vb@29
   357
    msg_list->msg_ref = msg;
vb@29
   358
vb@29
   359
    return msg_list;
vb@29
   360
}
vb@29
   361
vb@48
   362
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@29
   363
{
vb@29
   364
    if (msg_list) {
vb@29
   365
        free_message_ref_list(msg_list->next);
vb@29
   366
        free(msg_list);
vb@29
   367
    }
vb@29
   368
}
vb@29
   369
vb@81
   370
DYNAMIC_API message_ref_list *message_ref_list_dup(
vb@81
   371
        const message_ref_list *src
vb@81
   372
    )
vb@81
   373
{
vb@81
   374
    message_ref_list * msg_list = NULL;
vb@81
   375
vb@81
   376
    assert(src);
vb@81
   377
vb@81
   378
    msg_list = new_message_ref_list(src->msg_ref);
vb@81
   379
    if (msg_list == NULL)
vb@81
   380
        goto enomem;
vb@81
   381
vb@81
   382
    if (src->next) {
vb@81
   383
        msg_list->next = message_ref_list_dup(src->next);
vb@81
   384
        if (msg_list->next == NULL)
vb@81
   385
            goto enomem;
vb@81
   386
    }
vb@81
   387
vb@81
   388
    return msg_list;
vb@81
   389
vb@81
   390
enomem:
vb@81
   391
    free_message_ref_list(msg_list);
vb@81
   392
    return NULL;
vb@81
   393
}
vb@81
   394
vb@48
   395
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   396
{
vb@29
   397
    assert(msg);
vb@29
   398
vb@38
   399
    if (msg_list == NULL)
vb@38
   400
        return new_message_ref_list(msg);
vb@38
   401
vb@29
   402
    if (msg_list->msg_ref == NULL) {
vb@29
   403
        msg_list->msg_ref = msg;
vb@29
   404
        return msg_list;
vb@29
   405
    }
vb@29
   406
    else if (msg_list->next == NULL) {
vb@29
   407
        msg_list->next = new_message_ref_list(msg);
vb@29
   408
        assert(msg_list->next);
vb@29
   409
        return msg_list->next;
vb@29
   410
    }
vb@29
   411
    else {
vb@29
   412
        return message_ref_list_add(msg_list->next, msg);
vb@29
   413
    }
vb@29
   414
}
vb@29
   415