aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libicalss/icalcstp.c
blob: 53ee7bdeefc80531c22ca760c782d87f4f001eb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- Mode: C -*-
    ======================================================================
    FILE: icalcstps.c
    CREATOR: ebusboom 23 Jun 2000
  
    $Id$
    $Locker$
    
    (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of either: 
    
    The LGPL as published by the Free Software Foundation, version
    2.1, available at: http://www.fsf.org/copyleft/lesser.html
    
    Or:
    
    The Mozilla Public License Version 1.0. You may obtain a copy of
    the License at http://www.mozilla.org/MPL/


    ======================================================================*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ical.h"
#include "icalcstp.h"
#include "pvl.h" 

#include <sys/types.h> /* For send(), others */
#include <sys/socket.h>  /* For send(), others. */
#include <unistd.h> /* For alarm */
#include <errno.h>
#include <stdlib.h> /* for malloc */
#include <string.h>

enum cstps_state {
    NO_STATE,
    CONNECTED,
    AUTHENTICATED,
    IDENTIFIED,
    DISCONNECTED,
    RECEIVE,
};

struct icalcstps_impl {
    int timeout;
    icalparser *parser;
    enum cstps_state major_state;
    struct icalcstps_stubs stubs;
};


enum cstp_command {
    ABORT,
    AUTHENTICATE,
    CAPABILITY,
    CONTINUE,
    EXPANDCALID,
    IDENTIFY,
    DISCONNECT,
    SENDDATA,
    STARTTLS,
    EXPANDUPN,
    COMMAND_COMPLETE,
    UNKNOWN
};

struct command_map {
    enum cstp_command command;
    char *str;
} command_map[] = 
{
    {ABORT,"ABORT"},
    {AUTHENTICATE,"AUTHENTICATE"},
    {CAPABILITY,"CAPABILITY"},
    {CONTINUE,"CONTINUE"},
    {EXPANDCALID,"EXPANDCALID"},
    {IDENTIFY,"IDENTIFY"},
    {DISCONNECT,"DISCONNECT"},
    {SENDDATA,"SENDDATA"},
    {STARTTLS,"STARTTLS"},
    {EXPANDUPN,"EXPANDUPN"},
    {UNKNOWN,"UNKNOWN"}
};



/* This state machine is a Mealy-type: actions occur on the
   transitions, not in the states.
   
   Here is the state machine diagram from the CAP draft:


        STARTTLS /
        CAPABILITY
       +-------+
       |       |                       +---------------+
       |   +-----------+ AUTHENTICATE  |               |
       +-->| Connected |-------------->| Authenticated |
           +-----------+               |               |
             |                         +---------------+
             |                              |
             |                              |
             |                              |
             |                              |       +-----+ STARTTLS /
             |                              V       |     | CAPABILITY /
             |                         +---------------+  | IDENTIFY
             |                         |               |<-+
             |                         |   Identified  |<----+
             |                +--------|               |     |
             |                |        +---------------+     | command
             |                |             |                | completes
             V                |DISCONNECT   |                |
           +--------------+   |             |SENDDATA        |
           | Disconnected |<--+             |                |
           +--------------+                 |                | ABORT
                     A                      |                |
                     |                      V                |
                     |     DISCONNECT     +---------------+  |
                     +--------------------|    Receive    |--+
                                          |               |<--+
                                          +---------------+   |
                                                         |    | CONTINUTE
                                                         +----+

   In this implmenetation, the transition from CONNECTED to IDENTIFIED
   is non-standard. The spec specifies that on the ATHENTICATE
   command, the machine transitions from CONNECTED to AUTHENTICATED,
   and then immediately goes to IDENTIFIED. This makes AUTHENTICATED a
   useless state, so I removed it */

struct state_table {
    enum cstps_state major_state;
    enum cstp_command command;
    void (*action)();
    enum cstps_state next_state;

} server_state_table[] = 
{
    { CONNECTED, CAPABILITY , 0, CONNECTED},
    { CONNECTED, AUTHENTICATE , 0,  IDENTIFIED}, /* Non-standard */
    { IDENTIFIED, STARTTLS, 0, IDENTIFIED},
    { IDENTIFIED, IDENTIFY, 0, IDENTIFIED},
    { IDENTIFIED, CAPABILITY, 0, IDENTIFIED},
    { IDENTIFIED, SENDDATA, 0, RECEIVE},
    { IDENTIFIED, DISCONNECT, 0, DISCONNECTED},
    { DISCONNECTED, 0, 0, 0},
    { RECEIVE,  DISCONNECT, 0, DISCONNECTED},
    { RECEIVE,  CONTINUE, 0, RECEIVE},
    { RECEIVE,  ABORT , 0, IDENTIFIED},
    { RECEIVE,  COMMAND_COMPLETE , 0, IDENTIFIED}
};


/**********************************************************************/



icalcstps* icalcstps_new(struct icalcstps_stubs stubs)
{
    struct icalcstps_impl* impl;

    if ( ( impl = (struct icalcstps_impl*)
       malloc(sizeof(struct icalcstps_impl))) == 0) {
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    return 0;
    }

    impl->stubs = stubs;
    impl->timeout = 10;

    return (icalcstps*)impl;

}

void icalcstps_free(icalcstps* cstp);

int icalcstps_set_timeout(icalcstps* cstp, int sec) 
{
    struct icalcstps_impl *impl = (struct icalcstps_impl *) cstp;

    icalerror_check_arg_rz( (cstp!=0), "cstp");

    impl->timeout = sec;

    return sec;
}

typedef struct icalcstps_response { 
    icalrequeststatus code;
    char caluid[1024];
    void* result;
} icalcstps_response;

int line_is_command(char* line);
int line_is_response(char* line);
int line_is_endofdata(char* line);
int line_is_mime(char* line);

icalerrorenum prep_abort(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_authenticate(struct icalcstps_impl* impl, char* data)
{    return ICAL_NO_ERROR;
}
icalerrorenum prep_capability(struct icalcstps_impl* impl, char* data)
{    return ICAL_NO_ERROR;
}
icalerrorenum prep_calidexpand(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_continue(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_disconnect(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_identify(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_starttls(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_upnexpand(struct icalcstps_impl* impl, char* data)
{
    return ICAL_NO_ERROR;
}
icalerrorenum prep_sendata(struct icalcstps_impl* impl, char* data)
{    return ICAL_NO_ERROR;
}

char* icalcstps_process_incoming(icalcstps* cstp, char* input)
{
    struct icalcstps_impl *impl = (struct icalcstps_impl *) cstp;
    char *i;
    char *cmd_or_resp;
    char *data;
    char *input_cpy;
    icalerrorenum error;

    icalerror_check_arg_rz(cstp !=0,"cstp");
    icalerror_check_arg_rz(input !=0,"input");

    if ((input_cpy = (char*)strdup(input)) == 0){
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    return 0;
    }

    i = (char*)strstr(" ",input_cpy);

    cmd_or_resp = input_cpy;

    if (i != 0){
    *i = '\0';
    data = ++i;
    } else {
    data = 0;
    }

    printf("cmd: %s\n",cmd_or_resp);
    printf("data: %s\n",data);
    
    /* extract the command, look up in the state table, and dispatch
       to the proper handler */    

    if(strcmp(cmd_or_resp,"ABORT") == 0){
    error = prep_abort(impl,data);  
    } else if(strcmp(cmd_or_resp,"AUTHENTICATE") == 0){
    error = prep_authenticate(impl,data);
    } else if(strcmp(cmd_or_resp,"CAPABILITY") == 0){
    error = prep_capability(impl,data);
    } else if(strcmp(cmd_or_resp,"CALIDEXPAND") == 0){
    error = prep_calidexpand(impl,data);
    } else if(strcmp(cmd_or_resp,"CONTINUE") == 0){
    error = prep_continue(impl,data);
    } else if(strcmp(cmd_or_resp,"DISCONNECT") == 0){
    error = prep_disconnect(impl,data);
    } else if(strcmp(cmd_or_resp,"IDENTIFY") == 0){
    error = prep_identify(impl,data);
    } else if(strcmp(cmd_or_resp,"STARTTLS") == 0){
    error = prep_starttls(impl,data);
    } else if(strcmp(cmd_or_resp,"UPNEXPAND") == 0){
    error = prep_upnexpand(impl,data);
    } else if(strcmp(cmd_or_resp,"SENDDATA") == 0){
    error = prep_sendata(impl,data);
    }
    
    return 0;
}

    /* Read data until we get a end of data marker */



struct icalcstps_server_stubs {
  icalerrorenum (*abort)(icalcstps* cstp);
  icalerrorenum (*authenticate)(icalcstps* cstp, char* mechanism, 
                                    char* data);
  icalerrorenum (*calidexpand)(icalcstps* cstp, char* calid);
  icalerrorenum (*capability)(icalcstps* cstp);
  icalerrorenum (*cont)(icalcstps* cstp, unsigned int time);
  icalerrorenum (*identify)(icalcstps* cstp, char* id);
  icalerrorenum (*disconnect)(icalcstps* cstp);
  icalerrorenum (*sendata)(icalcstps* cstp, unsigned int time, 
                               icalcomponent *comp);
  icalerrorenum (*starttls)(icalcstps* cstp, char* command, 
                                char* data);
  icalerrorenum (*upnexpand)(icalcstps* cstp, char* upn);
  icalerrorenum (*unknown)(icalcstps* cstp, char* command, char* data);
};


/********************** Client (Sender) Interfaces **************************/

struct icalcstpc_impl {
    int timeout;
    icalparser *parser;
    enum cstp_command command;
    char* next_output;
    char* next_input;   
};

icalcstps* icalcstpc_new();

void* icalcstpc_free(icalcstpc* cstpc);

/* Get the next string to send to the server */
char* icalcstpc_next_output(icalcstpc* cstp)
{
    return 0;
}

/* process the next string to send to the server */ 
int icalcstpc_next_input(icalcstpc* cstp)
{
    return 0;
}

/* After icalcstpc_next_input returns a 0, there are responses
   ready. use these to get them */
icalcstpc_response icalcstpc_first_response(icalcstpc* cstp);
icalcstpc_response icalcstpc_next_response(icalcstpc* cstp);

int icalcstpc_set_timeout(icalcstpc* cstp, int sec);

icalerrorenum icalcstpc_abort(icalcstpc* cstp)
{
    struct icalcstpc_impl* impl = (struct icalcstpc_impl*)cstp;

    icalerror_check_arg_re(cstp!=0,"cstp",ICAL_BADARG_ERROR);

    impl->next_output = "ABORT";

    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_authenticate(icalcstpc* cstp, char* mechanism, 
                                        char* data, char* f(char*))
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_capability(icalcstpc* cstp)
{
    return ICAL_NO_ERROR;}

icalerrorenum icalcstpc_calidexpand(icalcstpc* cstp,char* calid)
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_continue(icalcstpc* cstp, unsigned int time)
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_disconnect(icalcstpc* cstp)
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_identify(icalcstpc* cstp, char* id)
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_starttls(icalcstpc* cstp, char* command, 
                                    char* data, char * f(char*))
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_upnexpand(icalcstpc* cstp,char* calid)
{
    return ICAL_NO_ERROR;
}

icalerrorenum icalcstpc_sendata(icalcstpc* cstp, unsigned int time,
                                   icalcomponent *comp)
{
    return ICAL_NO_ERROR;
}