src/transport.c
author vb
Sat, 07 Mar 2015 20:29:13 +0100
changeset 89 aef5a4bc78f3
parent 81 044ce1c7fc9c
child 94 9ec29aa10b58
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@28
    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@23
    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@89
   232
        free(msg->sent);
vb@89
   233
        free(msg->recv);
vb@63
   234
        free_identity(msg->from);
vb@63
   235
        free_identity_list(msg->to);
vb@63
   236
        free_identity(msg->recv_by);
vb@63
   237
        free_identity_list(msg->cc);
vb@63
   238
        free_identity_list(msg->bcc);
vb@81
   239
        free_identity(msg->reply_to);
vb@89
   240
        free(msg->in_reply_to);
vb@89
   241
        free_stringlist(msg->references);
vb@89
   242
        free_stringlist(msg->keywords);
vb@89
   243
        free(msg->comments);
vb@63
   244
        free(msg);
vb@63
   245
    }
vb@29
   246
}
vb@29
   247
vb@81
   248
DYNAMIC_API message * message_dup(const message *src)
vb@81
   249
{
vb@81
   250
    message * msg = NULL;
vb@81
   251
    pEp_identity * from = NULL;
vb@81
   252
    identity_list * to = NULL;
vb@81
   253
vb@81
   254
    assert(src);
vb@81
   255
vb@81
   256
    from = identity_dup(src->from);
vb@81
   257
    if (from == NULL)
vb@81
   258
        goto enomem;
vb@81
   259
vb@81
   260
    to = identity_list_dup(src->to);
vb@81
   261
    if (to == NULL)
vb@81
   262
        goto enomem;
vb@81
   263
vb@81
   264
    msg = new_message(src->dir, from, to, src->shortmsg);
vb@81
   265
    if (msg == NULL)
vb@81
   266
        goto enomem;
vb@81
   267
vb@81
   268
    if (src->id) {
vb@81
   269
        msg->id = strdup(src->id);
vb@81
   270
        assert(msg->id);
vb@81
   271
        if (msg->id == NULL)
vb@81
   272
            goto enomem;
vb@81
   273
    }
vb@81
   274
vb@81
   275
    if (src->longmsg) {
vb@81
   276
        msg->longmsg = strdup(src->longmsg);
vb@81
   277
        assert(msg->longmsg);
vb@81
   278
        if (msg->longmsg == NULL)
vb@81
   279
            goto enomem;
vb@81
   280
    }
vb@81
   281
    
vb@81
   282
    if (src->longmsg_formatted) {
vb@81
   283
        msg->longmsg_formatted = strdup(src->longmsg_formatted);
vb@81
   284
        assert(msg->longmsg_formatted);
vb@81
   285
        if (msg->longmsg_formatted == NULL)
vb@81
   286
            goto enomem;
vb@81
   287
    }
vb@81
   288
vb@81
   289
    if (src->attachments) {
vb@81
   290
        msg->attachments = bloblist_dup(src->attachments);
vb@81
   291
        if (msg->attachments == NULL)
vb@81
   292
            goto enomem;
vb@81
   293
    }
vb@81
   294
vb@81
   295
    msg->rawmsg_ref = src->rawmsg_ref;
vb@81
   296
    msg->rawmsg_size = src->rawmsg_size;
vb@89
   297
vb@89
   298
    if (src->sent) {
vb@89
   299
        msg->sent = malloc(sizeof(timestamp));
vb@89
   300
        if (msg->sent == NULL)
vb@89
   301
            goto enomem;
vb@89
   302
        memcpy(msg->sent, src->sent, sizeof(timestamp));
vb@89
   303
    }
vb@89
   304
vb@89
   305
    if (src->recv) {
vb@89
   306
        msg->recv = malloc(sizeof(timestamp));
vb@89
   307
        if (msg->recv == NULL)
vb@89
   308
            goto enomem;
vb@89
   309
        memcpy(msg->recv, src->recv, sizeof(timestamp));
vb@89
   310
    }
vb@81
   311
vb@81
   312
    if (src->recv_by) {
vb@81
   313
        msg->recv_by = identity_dup(src->recv_by);
vb@81
   314
        if (msg->recv_by == NULL)
vb@81
   315
            goto enomem;
vb@81
   316
    }
vb@81
   317
vb@81
   318
    if (src->cc) {
vb@81
   319
        msg->cc = identity_list_dup(src->cc);
vb@81
   320
        if (msg->cc == NULL)
vb@81
   321
            goto enomem;
vb@81
   322
    }
vb@81
   323
vb@81
   324
    if (src->bcc) {
vb@81
   325
        msg->bcc = identity_list_dup(src->bcc);
vb@81
   326
        if (msg->bcc == NULL)
vb@81
   327
            goto enomem;
vb@81
   328
    }
vb@81
   329
vb@81
   330
    if (src->reply_to) {
vb@81
   331
        msg->reply_to = identity_dup(src->reply_to);
vb@81
   332
        if (msg->reply_to == NULL)
vb@81
   333
            goto enomem;
vb@81
   334
    }
vb@81
   335
vb@89
   336
    if (src->in_reply_to) {
vb@89
   337
        msg->in_reply_to = strdup(src->in_reply_to);
vb@89
   338
        assert(msg->in_reply_to);
vb@89
   339
        if (msg->in_reply_to == NULL)
vb@81
   340
            goto enomem;
vb@81
   341
    }
vb@81
   342
vb@81
   343
    msg->refering_msg_ref = src->refering_msg_ref;
vb@81
   344
    
vb@89
   345
    if (src->references) {
vb@89
   346
        msg->references = stringlist_dup(src->references);
vb@89
   347
        if (msg->references == NULL)
vb@89
   348
            goto enomem;
vb@89
   349
    }
vb@89
   350
vb@81
   351
    if (src->refered_by) {
vb@81
   352
        msg->refered_by = message_ref_list_dup(src->refered_by);
vb@81
   353
        if (msg->refered_by == NULL)
vb@81
   354
            goto enomem;
vb@81
   355
    }
vb@81
   356
vb@89
   357
    if (src->keywords) {
vb@89
   358
        msg->keywords = stringlist_dup(src->keywords);
vb@89
   359
        if (msg->keywords == NULL)
vb@89
   360
            goto enomem;
vb@89
   361
    }
vb@89
   362
vb@89
   363
    if (src->comments) {
vb@89
   364
        msg->comments = strdup(src->comments);
vb@89
   365
        assert(msg->comments);
vb@89
   366
        if (msg->comments == NULL)
vb@89
   367
            goto enomem;
vb@89
   368
    }
vb@89
   369
vb@81
   370
    msg->enc_format = src->enc_format;
vb@81
   371
vb@81
   372
    return msg;
vb@81
   373
vb@81
   374
enomem:
vb@81
   375
    if (msg) {
vb@81
   376
        free_message(msg);
vb@81
   377
    }
vb@81
   378
    else {
vb@81
   379
        free_identity(from);
vb@81
   380
        free_identity_list(to);
vb@81
   381
    }
vb@81
   382
vb@81
   383
    return NULL;
vb@81
   384
}
vb@81
   385
vb@48
   386
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@29
   387
{
vb@29
   388
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   389
    assert(msg_list);
vb@29
   390
    if (msg_list == NULL)
vb@29
   391
        return NULL;
vb@29
   392
vb@29
   393
    msg_list->msg_ref = msg;
vb@29
   394
vb@29
   395
    return msg_list;
vb@29
   396
}
vb@29
   397
vb@48
   398
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@29
   399
{
vb@29
   400
    if (msg_list) {
vb@29
   401
        free_message_ref_list(msg_list->next);
vb@29
   402
        free(msg_list);
vb@29
   403
    }
vb@29
   404
}
vb@29
   405
vb@81
   406
DYNAMIC_API message_ref_list *message_ref_list_dup(
vb@81
   407
        const message_ref_list *src
vb@81
   408
    )
vb@81
   409
{
vb@81
   410
    message_ref_list * msg_list = NULL;
vb@81
   411
vb@81
   412
    assert(src);
vb@81
   413
vb@81
   414
    msg_list = new_message_ref_list(src->msg_ref);
vb@81
   415
    if (msg_list == NULL)
vb@81
   416
        goto enomem;
vb@81
   417
vb@81
   418
    if (src->next) {
vb@81
   419
        msg_list->next = message_ref_list_dup(src->next);
vb@81
   420
        if (msg_list->next == NULL)
vb@81
   421
            goto enomem;
vb@81
   422
    }
vb@81
   423
vb@81
   424
    return msg_list;
vb@81
   425
vb@81
   426
enomem:
vb@81
   427
    free_message_ref_list(msg_list);
vb@81
   428
    return NULL;
vb@81
   429
}
vb@81
   430
vb@48
   431
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   432
{
vb@29
   433
    assert(msg);
vb@29
   434
vb@38
   435
    if (msg_list == NULL)
vb@38
   436
        return new_message_ref_list(msg);
vb@38
   437
vb@29
   438
    if (msg_list->msg_ref == NULL) {
vb@29
   439
        msg_list->msg_ref = msg;
vb@29
   440
        return msg_list;
vb@29
   441
    }
vb@29
   442
    else if (msg_list->next == NULL) {
vb@29
   443
        msg_list->next = new_message_ref_list(msg);
vb@29
   444
        assert(msg_list->next);
vb@29
   445
        return msg_list->next;
vb@29
   446
    }
vb@29
   447
    else {
vb@29
   448
        return message_ref_list_add(msg_list->next, msg);
vb@29
   449
    }
vb@29
   450
}
vb@29
   451