src/transport.c
author vb
Sat, 21 Feb 2015 16:55:43 +0100
changeset 62 ad5e484720e1
parent 48 9ff5d2dfa75a
child 63 f5b2641f4ae7
permissions -rw-r--r--
signal() and init()
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@40
    36
    if (id_list == NULL) {
vb@40
    37
        free(ident);
vb@29
    38
        return NULL;
vb@40
    39
    }
vb@29
    40
vb@39
    41
    id_list->ident = ident;
vb@29
    42
vb@29
    43
    return id_list;
vb@29
    44
}
vb@29
    45
vb@48
    46
DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
vb@37
    47
{
vb@37
    48
    assert(src);
vb@37
    49
vb@40
    50
    identity_list *id_list = new_identity_list(identity_dup(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@48
    66
DYNAMIC_API 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@48
    75
DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, 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@39
    83
        id_list->ident = ident;
vb@39
    84
        return id_list;
vb@29
    85
    }
vb@29
    86
    else if (id_list->next == NULL) {
vb@29
    87
        id_list->next = new_identity_list(ident);
vb@29
    88
        return id_list->next;
vb@29
    89
    }
vb@29
    90
    else {
vb@29
    91
        return identity_list_add(id_list->next, ident);
vb@29
    92
    }
vb@29
    93
}
vb@29
    94
vb@48
    95
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@39
    96
        const char *file_name)
vb@38
    97
{
vb@38
    98
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@38
    99
    if (bloblist == NULL)
vb@38
   100
        return NULL;
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@39
   108
    if (file_name) {
vb@39
   109
        bloblist->file_name = strdup(file_name);
vb@39
   110
        if (bloblist->file_name == NULL) {
vb@39
   111
            free(bloblist->mime_type);
vb@39
   112
            free(bloblist);
vb@39
   113
            return NULL;
vb@39
   114
        }
vb@39
   115
    }
vb@41
   116
    bloblist->data = blob;
vb@41
   117
    bloblist->size = size;
vb@38
   118
    return bloblist;
vb@38
   119
}
vb@38
   120
vb@48
   121
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@38
   122
{
vb@39
   123
    if (bloblist) {
vb@39
   124
        if (bloblist->next)
vb@39
   125
            free_bloblist(bloblist->next);
vb@41
   126
        free(bloblist->data);
vb@41
   127
        free(bloblist->mime_type);
vb@41
   128
        free(bloblist->file_name);
vb@39
   129
        free(bloblist);
vb@39
   130
    }
vb@38
   131
}
vb@38
   132
vb@48
   133
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@39
   134
        const char *mime_type, const char *file_name)
vb@38
   135
{
vb@38
   136
    assert(blob);
vb@38
   137
vb@38
   138
    if (bloblist == NULL)
vb@39
   139
        return new_bloblist(blob, size, mime_type, file_name);
vb@38
   140
vb@41
   141
    if (bloblist->data == NULL) {
vb@39
   142
        if (mime_type) {
vb@39
   143
            bloblist->mime_type = strdup(mime_type);
vb@39
   144
            if (bloblist->mime_type == NULL) {
vb@39
   145
                free(bloblist);
vb@39
   146
                return NULL;
vb@39
   147
            }
vb@39
   148
        }
vb@39
   149
        if (file_name) {
vb@39
   150
            bloblist->file_name = strdup(file_name);
vb@39
   151
            if (bloblist->file_name == NULL) {
vb@39
   152
                free(bloblist->mime_type);
vb@39
   153
                free(bloblist);
vb@39
   154
                return NULL;
vb@39
   155
            }
vb@39
   156
        }
vb@41
   157
        bloblist->data = blob;
vb@41
   158
        bloblist->size = size;
vb@38
   159
        return bloblist;
vb@38
   160
    }
vb@38
   161
vb@38
   162
    if (bloblist->next == NULL) {
vb@39
   163
        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
vb@38
   164
        return bloblist->next;
vb@38
   165
    }
vb@38
   166
vb@39
   167
    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
vb@38
   168
}
vb@38
   169
vb@48
   170
DYNAMIC_API message *new_message(
vb@38
   171
        PEP_msg_direction dir,
vb@39
   172
        pEp_identity *from,
vb@39
   173
        identity_list *to,
vb@29
   174
        const char *shortmsg
vb@29
   175
    )
vb@29
   176
{
vb@29
   177
    message *msg = calloc(1, sizeof(message));
vb@29
   178
    assert(msg);
vb@29
   179
    if (msg == NULL)
vb@29
   180
        return NULL;
vb@29
   181
vb@39
   182
    if (shortmsg) {
vb@37
   183
        msg->shortmsg = strdup(shortmsg);
vb@37
   184
        assert(msg->shortmsg);
vb@37
   185
        if (msg->shortmsg == NULL) {
vb@37
   186
            free(msg);
vb@37
   187
            return NULL;
vb@37
   188
        }
vb@29
   189
    }
vb@29
   190
vb@29
   191
    msg->dir = dir;
vb@39
   192
    msg->from = from;
vb@39
   193
    msg->to = to;
vb@29
   194
vb@29
   195
    return msg;
vb@29
   196
}
vb@29
   197
vb@48
   198
DYNAMIC_API void free_message(message *msg)
vb@29
   199
{
vb@29
   200
    free(msg->id);
vb@29
   201
    free(msg->shortmsg);
vb@29
   202
    free(msg->longmsg);
vb@29
   203
    free(msg->longmsg_formatted);
vb@38
   204
    free_bloblist(msg->attachments);
vb@40
   205
    free_identity(msg->from);
vb@29
   206
    free_identity_list(msg->to);
vb@40
   207
    free_identity(msg->recv_by);
vb@29
   208
    free_identity_list(msg->cc);
vb@29
   209
    free_identity_list(msg->bcc);
vb@29
   210
    free(msg->refering_id);
vb@29
   211
    free_message_ref_list(msg->refered_by);
vb@29
   212
    free(msg);
vb@29
   213
}
vb@29
   214
vb@48
   215
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@29
   216
{
vb@29
   217
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   218
    assert(msg_list);
vb@29
   219
    if (msg_list == NULL)
vb@29
   220
        return NULL;
vb@29
   221
vb@29
   222
    msg_list->msg_ref = msg;
vb@29
   223
vb@29
   224
    return msg_list;
vb@29
   225
}
vb@29
   226
vb@48
   227
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@29
   228
{
vb@29
   229
    if (msg_list) {
vb@29
   230
        free_message_ref_list(msg_list->next);
vb@29
   231
        free(msg_list);
vb@29
   232
    }
vb@29
   233
}
vb@29
   234
vb@48
   235
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   236
{
vb@29
   237
    assert(msg);
vb@29
   238
vb@38
   239
    if (msg_list == NULL)
vb@38
   240
        return new_message_ref_list(msg);
vb@38
   241
vb@29
   242
    if (msg_list->msg_ref == NULL) {
vb@29
   243
        msg_list->msg_ref = msg;
vb@29
   244
        return msg_list;
vb@29
   245
    }
vb@29
   246
    else if (msg_list->next == NULL) {
vb@29
   247
        msg_list->next = new_message_ref_list(msg);
vb@29
   248
        assert(msg_list->next);
vb@29
   249
        return msg_list->next;
vb@29
   250
    }
vb@29
   251
    else {
vb@29
   252
        return message_ref_list_add(msg_list->next, msg);
vb@29
   253
    }
vb@29
   254
}
vb@29
   255