Add starttls
[apps/madmutt.git] / lib-sys / evtloop.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  */
17 /*
18  *  Copyright © 2006 Pierre Habouzit
19  */
20
21 #ifndef MUTT_LIB_SYS_EVTLOOP_H
22 #define MUTT_LIB_SYS_EVTLOOP_H
23
24 #include <lib-lib/lib-lib.h>
25 #include <gnutls/gnutls.h>
26
27 typedef enum el_state {
28     EL_LLP_INIT,
29     EL_LLP_READY,
30     EL_LLP_FINI,
31 } el_state;
32
33 typedef enum el_mode {
34     EL_NEW     = 0,
35     EL_READING = 1,
36     EL_WRITING = 2,
37     EL_RDWR    = 3,
38     EL_IDLE    = 4,
39 } el_mode;
40
41 typedef enum el_status {
42     EL_SUCCESS,
43     EL_ERROR,
44     EL_KILLED,
45     EL_RDHUP,
46 } el_status;
47
48 typedef enum el_event {
49     EL_EVT_IN      = EL_READING,
50     EL_EVT_OUT     = EL_WRITING,
51     EL_EVT_INOUT   = EL_RDWR,
52     EL_EVT_RUNNING = 4,
53     EL_EVT_WAKEUP  = 5,
54 } el_event;
55
56 typedef struct job_t {
57     int fd;
58     int ssf;
59
60     gnutls_session_t session;
61     gnutls_certificate_credentials_t xcred;
62
63     el_state state : 2;
64     el_mode  mode  : 3;
65     el_mode  emode : 3;
66
67     int (*llp)(struct job_t *);
68     const struct machine_t *m;
69     void *ptr;
70 } job_t;
71 DO_INIT(job_t, job);
72 void job_wipe(job_t *w);
73 DO_NEW(job_t, job);
74 DO_DELETE(job_t, job);
75
76 typedef struct machine_t {
77     const char *name;
78     __must_check__ int (*setup)(job_t *w, void *);
79     __must_check__ int (*on_event)(job_t *w, el_event);
80     void (*finalize)(job_t *w, el_status);
81 } machine_t;
82
83 #define EL_JOB_CHECK(expr) \
84     do { if ((expr) < 0) return -1; } while (0)
85
86 __must_check__ int el_job_setmode(job_t *w, el_mode);
87 __must_check__ int el_job_release(job_t *j, el_status);
88 __must_check__ int el_job_connect(job_t *w, struct sockaddr *, socklen_t len,
89                                   int type, int proto, int ssl);
90 __must_check__ int el_job_starttls(job_t *w);
91 __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf);
92 __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
93
94 static inline job_t *el_job_start(const machine_t *m, void *cfg) {
95     job_t *w = job_new();
96     w->m = m;
97     return m->setup(w, cfg) < 0 ? NULL : w;
98 }
99
100 int el_dispatch(int timeout);
101
102 #endif