mirror of
https://gitlab.com/openconnect/ocserv.git
synced 2026-02-10 08:46:58 +08:00
simplified DTLS fd handling and dtls_pull()
This commit is contained in:
@@ -1145,7 +1145,7 @@ int main(int argc, char** argv)
|
||||
ws->config = s->config;
|
||||
ws->cmd_fd = cmd_fd[1];
|
||||
ws->tun_fd = -1;
|
||||
ws->udp_fd = -1;
|
||||
ws->dtls_tptr.fd = -1;
|
||||
ws->conn_fd = fd;
|
||||
ws->conn_type = stype;
|
||||
ws->creds = &creds;
|
||||
|
||||
@@ -138,19 +138,18 @@ int handle_worker_commands(struct worker_st *ws)
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
if (ws->dtls_session != NULL) {
|
||||
ws->dtls_tptr.fd = fd;
|
||||
}
|
||||
} else { /* received client hello */
|
||||
ws->udp_state = UP_SETUP;
|
||||
}
|
||||
|
||||
if (ws->udp_fd != -1) {
|
||||
close(ws->udp_fd);
|
||||
if (ws->dtls_tptr.fd != -1) {
|
||||
close(ws->dtls_tptr.fd);
|
||||
}
|
||||
|
||||
ws->dtls_tptr.msg = tmsg;
|
||||
ws->udp_fd = fd;
|
||||
ws->dtls_tptr.consumed = 0;
|
||||
|
||||
ws->dtls_tptr.fd = fd;
|
||||
set_non_block(fd);
|
||||
|
||||
oclog(ws, LOG_DEBUG, "received new UDP fd and connected to peer");
|
||||
@@ -171,7 +170,7 @@ int handle_worker_commands(struct worker_st *ws)
|
||||
|
||||
udp_fd_fail:
|
||||
udp_fd_msg__free_unpacked(tmsg, NULL);
|
||||
if (ws->udp_fd == -1)
|
||||
if (ws->dtls_tptr.fd == -1)
|
||||
ws->udp_state = UP_DISABLED;
|
||||
|
||||
return -1;
|
||||
|
||||
@@ -550,7 +550,7 @@ int body_cb(http_parser * parser, const char *at, size_t length)
|
||||
inline static ssize_t dtls_pull_buffer_size(gnutls_transport_ptr_t ptr)
|
||||
{
|
||||
dtls_transport_ptr *p = ptr;
|
||||
if (p->msg && p->consumed < p->msg->data.len)
|
||||
if (p->msg && p->consumed != 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -560,20 +560,17 @@ ssize_t dtls_pull(gnutls_transport_ptr_t ptr, void *data, size_t size)
|
||||
{
|
||||
dtls_transport_ptr *p = ptr;
|
||||
|
||||
if (p->msg) {
|
||||
if (p->consumed < p->msg->data.len) {
|
||||
ssize_t need = p->msg->data.len - p->consumed;
|
||||
|
||||
if (need > size) {
|
||||
need = size;
|
||||
}
|
||||
memcpy(data, &p->msg->data.data[p->consumed], need);
|
||||
p->consumed += need;
|
||||
return need;
|
||||
} else {
|
||||
udp_fd_msg__free_unpacked(p->msg, NULL);
|
||||
p->msg = NULL;
|
||||
if (p->msg && p->consumed == 0) {
|
||||
ssize_t need = p->msg->data.len;
|
||||
if (need > size) {
|
||||
need = size;
|
||||
}
|
||||
memcpy(data, p->msg->data.data, need);
|
||||
p->consumed = 1;
|
||||
|
||||
udp_fd_msg__free_unpacked(p->msg, NULL);
|
||||
p->msg = NULL;
|
||||
return need;
|
||||
}
|
||||
return recv(p->fd, data, size, 0);
|
||||
}
|
||||
@@ -675,7 +672,6 @@ static int setup_dtls_connection(struct worker_st *ws)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ws->dtls_tptr.fd = ws->udp_fd;
|
||||
gnutls_transport_set_push_function(session, dtls_push);
|
||||
gnutls_transport_set_pull_function(session, dtls_pull);
|
||||
gnutls_transport_set_pull_timeout_function(session, dtls_pull_timeout);
|
||||
@@ -2034,7 +2030,7 @@ static int connect_handler(worker_st * ws)
|
||||
|
||||
if (ws->config->output_buffer > 0) {
|
||||
t = MIN(2048, ws->conn_mtu * ws->config->output_buffer);
|
||||
setsockopt(ws->udp_fd, SOL_SOCKET, SO_SNDBUF, &t,
|
||||
setsockopt(ws->dtls_tptr.fd, SOL_SOCKET, SO_SNDBUF, &t,
|
||||
sizeof(t));
|
||||
if (ret == -1)
|
||||
oclog(ws, LOG_DEBUG,
|
||||
@@ -2042,7 +2038,7 @@ static int connect_handler(worker_st * ws)
|
||||
t);
|
||||
}
|
||||
|
||||
set_net_priority(ws, ws->udp_fd, ws->config->net_priority);
|
||||
set_net_priority(ws, ws->dtls_tptr.fd, ws->config->net_priority);
|
||||
}
|
||||
|
||||
/* hack for openconnect. It uses only a single MTU value */
|
||||
@@ -2125,8 +2121,8 @@ static int connect_handler(worker_st * ws)
|
||||
max = MAX(max, ws->tun_fd);
|
||||
|
||||
if (ws->udp_state > UP_WAIT_FD) {
|
||||
FD_SET(ws->udp_fd, &rfds);
|
||||
max = MAX(max, ws->udp_fd);
|
||||
FD_SET(ws->dtls_tptr.fd, &rfds);
|
||||
max = MAX(max, ws->dtls_tptr.fd);
|
||||
}
|
||||
|
||||
#ifdef HAVE_PSELECT
|
||||
@@ -2173,7 +2169,7 @@ static int connect_handler(worker_st * ws)
|
||||
|
||||
/* read data from UDP channel */
|
||||
if (ws->udp_state > UP_WAIT_FD &&
|
||||
(FD_ISSET(ws->udp_fd, &rfds) || dtls_pending != 0)) {
|
||||
(FD_ISSET(ws->dtls_tptr.fd, &rfds) || dtls_pending != 0)) {
|
||||
|
||||
ret = dtls_mainloop(ws, &tnow);
|
||||
if (ret < 0)
|
||||
|
||||
@@ -139,7 +139,6 @@ typedef struct worker_st {
|
||||
struct tls_st *creds;
|
||||
gnutls_session_t session;
|
||||
gnutls_session_t dtls_session;
|
||||
dtls_transport_ptr dtls_tptr;
|
||||
|
||||
struct http_req_st req;
|
||||
|
||||
@@ -173,7 +172,7 @@ typedef struct worker_st {
|
||||
time_t last_periodic_check;
|
||||
|
||||
/* set after authentication */
|
||||
int udp_fd;
|
||||
dtls_transport_ptr dtls_tptr;
|
||||
udp_port_state_t udp_state;
|
||||
time_t udp_recv_time; /* time last udp packet was received */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user