5 void state_machine_init(state_machine_t * machine)
8 machine->bio_intossl = machine->bio_fromssl = NULL;
9 buffer_init(&machine->clean_in);
10 buffer_init(&machine->clean_out);
11 buffer_init(&machine->dirty_in);
12 buffer_init(&machine->dirty_out);
15 void state_machine_close(state_machine_t * machine)
18 SSL_free(machine->ssl);
20 * SSL_free seems to decrement the reference counts already so doing this
24 if (machine->bio_intossl)
25 BIO_free(machine->bio_intossl);
26 if (machine->bio_fromssl)
27 BIO_free(machine->bio_fromssl);
29 buffer_close(&machine->clean_in);
30 buffer_close(&machine->clean_out);
31 buffer_close(&machine->dirty_in);
32 buffer_close(&machine->dirty_out);
33 state_machine_init(machine);
36 buffer_t *state_machine_get_buffer(state_machine_t * machine,
41 return &machine->clean_in;
43 return &machine->clean_out;
45 return &machine->dirty_in;
47 return &machine->dirty_out;
51 /* Should never get here */
56 SSL *state_machine_get_SSL(state_machine_t * machine)
61 int state_machine_set_SSL(state_machine_t * machine, SSL *ssl, int is_server)
64 /* Shouldn't ever be set twice */
67 /* Create the BIOs to handle the dirty side of the SSL */
68 if ((machine->bio_intossl = BIO_new(BIO_s_mem())) == NULL)
70 if ((machine->bio_fromssl = BIO_new(BIO_s_mem())) == NULL)
72 /* Hook up the BIOs on the dirty side of the SSL */
73 SSL_set_bio(machine->ssl, machine->bio_intossl, machine->bio_fromssl);
75 SSL_set_accept_state(machine->ssl);
77 SSL_set_connect_state(machine->ssl);
79 * If we're the first one to generate traffic - do it now otherwise we go
80 * into the next select empty-handed and our peer will not send data but
81 * will similarly wait for us.
83 return state_machine_churn(machine);
86 /* Performs the data-IO loop and returns zero if the machine should close */
87 int state_machine_churn(state_machine_t * machine)
90 if (machine->ssl == NULL) {
91 if (buffer_empty(&machine->clean_out))
92 /* Time to close this state-machine altogether */
95 /* Still buffered data on the clean side to go out */
99 * Do this loop twice to cover any dependencies about which precise order
100 * of reads and writes is required.
102 for (loop = 0; loop < 2; loop++) {
103 buffer_to_SSL(&machine->clean_in, machine->ssl);
104 buffer_to_BIO(&machine->dirty_in, machine->bio_intossl);
105 buffer_from_SSL(&machine->clean_out, machine->ssl);
106 buffer_from_BIO(&machine->dirty_out, machine->bio_fromssl);
109 * We close on the SSL side if the info callback noticed some problems or
110 * an SSL shutdown was underway and shutdown traffic had all been sent.
112 if (SSL_get_app_data(machine->ssl) || (SSL_get_shutdown(machine->ssl) &&
113 buffer_empty(&machine->dirty_out)))
115 /* Great, we can seal off the dirty side completely */
116 if (!state_machine_close_dirty(machine))
120 * Either the SSL is alive and well, or the closing process still has
121 * outgoing data waiting to be sent
126 /* Called when the clean side of the SSL has lost its connection */
127 int state_machine_close_clean(state_machine_t * machine)
130 * Well, first thing to do is null out the clean-side buffers - they're
133 buffer_close(&machine->clean_in);
134 buffer_close(&machine->clean_out);
135 /* And start an SSL shutdown */
137 SSL_shutdown(machine->ssl);
138 /* This is an "event", so flush the SSL of any generated traffic */
139 state_machine_churn(machine);
140 if (buffer_empty(&machine->dirty_in) && buffer_empty(&machine->dirty_out))
146 * Called when the dirty side of the SSL has lost its connection. This is
147 * pretty terminal as all that can be left to do is send any buffered output
148 * on the clean side - after that, we're done.
150 int state_machine_close_dirty(state_machine_t * machine)
152 buffer_close(&machine->dirty_in);
153 buffer_close(&machine->dirty_out);
154 buffer_close(&machine->clean_in);
156 SSL_free(machine->ssl);
158 machine->bio_intossl = machine->bio_fromssl = NULL;
159 if (buffer_empty(&machine->clean_out))
164 #endif /* !defined(NO_TUNALA) */