split out queue_packet()
[platform/upstream/openconnect.git] / mainloop.c
1 /*
2  * Open AnyConnect (SSL + DTLS) client
3  *
4  * © 2008 David Woodhouse <dwmw2@infradead.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software
7  * for any purpose with or without fee is hereby granted, provided
8  * that the above copyright notice and this permission notice appear
9  * in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
14  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
17  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 #include <errno.h>
22 #include <poll.h>
23 #include <limits.h>
24 #include <sys/select.h>
25 #include <signal.h>
26 #include <arpa/inet.h>
27
28 #include "anyconnect.h"
29
30 void queue_packet(struct pkt **q, struct pkt *new)
31 {
32         while (*q)
33                 q = &(*q)->next;
34
35         new->next = NULL;
36         *q = new;
37 }
38
39 int inflate_and_queue_packet(struct anyconnect_info *vpninfo, int type, void *buf, int len)
40 {
41         struct pkt *new = malloc(sizeof(struct pkt) + vpninfo->mtu);
42
43         if (!new)
44                 return -ENOMEM;
45
46         new->type = type;
47         new->next = NULL;
48
49         vpninfo->inflate_strm.next_in = buf;
50         vpninfo->inflate_strm.avail_in = len - 4;
51
52         vpninfo->inflate_strm.next_out = new->data;
53         vpninfo->inflate_strm.avail_out = vpninfo->mtu;
54         vpninfo->inflate_strm.total_out = 0;
55
56         if (inflate(&vpninfo->inflate_strm, Z_SYNC_FLUSH)) {
57                 fprintf(stderr, "inflate failed\n");
58                 free(new);
59                 return -EINVAL;
60         }
61
62         new->len = vpninfo->inflate_strm.total_out;
63
64         vpninfo->inflate_adler32 = adler32(vpninfo->inflate_adler32,
65                                            new->data, new->len);
66
67         if (vpninfo->inflate_adler32 != ntohl( *(uint32_t *)(buf + len - 4))) {
68                 vpninfo->quit_reason = "Compression (inflate) adler32 failure";
69         }
70
71         queue_packet(&vpninfo->incoming_queue, new);
72         return 0;
73 }
74
75 int queue_new_packet(struct pkt **q, int type, void *buf, int len)
76 {
77         struct pkt *new = malloc(sizeof(struct pkt) + len);
78         if (!new)
79                 return -ENOMEM;
80
81         new->type = type;
82         new->len = len;
83         new->next = NULL;
84         memcpy(new->data, buf, len);
85         queue_packet(q, new);
86         return 0;
87 }
88
89 int vpn_add_pollfd(struct anyconnect_info *vpninfo, int fd, short events)
90 {
91         vpninfo->nfds++;
92         vpninfo->pfds = realloc(vpninfo->pfds, sizeof(struct pollfd) * vpninfo->nfds);
93         if (!vpninfo->pfds) {
94                 fprintf(stderr, "Failed to reallocate pfds\n");
95                 exit(1);
96         }
97         vpninfo->pfds[vpninfo->nfds - 1].fd = fd;
98         vpninfo->pfds[vpninfo->nfds - 1].events = events;
99
100         return vpninfo->nfds - 1;
101 }
102
103 static int killed;
104
105 static void handle_sigint(int sig)
106 {
107         killed = 1;
108 }
109
110 int vpn_mainloop(struct anyconnect_info *vpninfo)
111 {
112         struct sigaction sa;
113         memset(&sa, 0, sizeof(sa));
114         sa.sa_handler = handle_sigint;
115         
116         sigaction(SIGINT, &sa, NULL);
117         while (!killed && !vpninfo->quit_reason) {
118                 int did_work = 0;
119                 int timeout = INT_MAX;
120
121                 if (vpninfo->dtls_fd != -1)
122                         did_work += dtls_mainloop(vpninfo, &timeout);
123
124                 did_work += ssl_mainloop(vpninfo, &timeout);
125                 did_work += tun_mainloop(vpninfo, &timeout);
126                 
127                 if (did_work)
128                         continue;
129                 
130                 if (verbose)
131                         printf("Did no work; sleeping for %d ms...\n", timeout);
132
133                 poll(vpninfo->pfds, vpninfo->nfds, timeout);
134                 if (vpninfo->pfds[vpninfo->ssl_pfd].revents & POLL_HUP) {
135                         fprintf(stderr, "Server closed connection!\n");
136                         /* OpenSSL doesn't seem to cope properly with this... */
137                         exit(1);
138                 }
139         }
140         if (!vpninfo->quit_reason)
141                 vpninfo->quit_reason = "Client received SIGINT";
142
143         ssl_bye(vpninfo, vpninfo->quit_reason);
144
145         return 0;
146 }