From 5d4e4f7a570021e3dec473c45343b83c0dfecfb3 Mon Sep 17 00:00:00 2001 From: Baptiste Assmann Date: Mon, 13 Apr 2015 23:08:16 +0200 Subject: [PATCH] MEDIUM: protocol: add minimalist UDP protocol client Basic introduction of a UDP layer in HAProxy. It can be used as a client only and manages UDP exchanges with servers. It can't be used to load-balance UDP protocols, but only used by internal features such as DNS resolution. --- Makefile | 2 +- include/proto/proto_udp.h | 27 ++++++++++++++++++++ include/types/proto_udp.h | 52 +++++++++++++++++++++++++++++++++++++++ src/proto_udp.c | 33 +++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 include/proto/proto_udp.h create mode 100644 include/types/proto_udp.h create mode 100644 src/proto_udp.c diff --git a/Makefile b/Makefile index 10de1deae..2059da93e 100644 --- a/Makefile +++ b/Makefile @@ -731,7 +731,7 @@ OBJS = src/haproxy.o src/sessionhash.o src/base64.o src/protocol.o \ src/lb_chash.o src/lb_fwlc.o src/lb_fwrr.o src/lb_map.o src/lb_fas.o \ src/stream_interface.o src/dumpstats.o src/proto_tcp.o src/applet.o \ src/session.o src/stream.o src/hdr_idx.o src/ev_select.o src/signal.o \ - src/acl.o src/sample.o src/memory.o src/freq_ctr.o src/auth.o \ + src/acl.o src/sample.o src/memory.o src/freq_ctr.o src/auth.o src/proto_udp.o \ src/compression.o src/payload.o src/hash.o src/pattern.o src/map.o \ src/namespace.o src/mailers.o diff --git a/include/proto/proto_udp.h b/include/proto/proto_udp.h new file mode 100644 index 000000000..c452a10b9 --- /dev/null +++ b/include/proto/proto_udp.h @@ -0,0 +1,27 @@ +/* + * include/proto/proto_udp.h + * This file provides functions related to UDP protocol. + * + * Copyright (C) 2014 Baptiste Assmann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, version 2.1 + * exclusively. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _PROTO_PROTO_UDP_H +#define _PROTO_PROTO_UDP_H + +int dgram_fd_handler(int); + +#endif // _PROTO_PROTO_UDP_H diff --git a/include/types/proto_udp.h b/include/types/proto_udp.h new file mode 100644 index 000000000..260441859 --- /dev/null +++ b/include/types/proto_udp.h @@ -0,0 +1,52 @@ +/* + * include/types/proto_udp.h + * This file provides structures and types for UDP protocol. + * + * Copyright (C) 2014 Baptiste Assmann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, version 2.1 + * exclusively. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _TYPES_PROTO_UDP_H +#define _TYPES_PROTO_UDP_H + +#include + +/* + * datagram related structure + */ +struct dgram_conn { + const struct dgram_data_cb *data; /* data layer callbacks. Must be set before */ + void *owner; /* pointer to upper layer's entity */ + union { /* definitions which depend on connection type */ + struct { /*** information used by socket-based dgram ***/ + int fd; /* file descriptor */ + } sock; + } t; + struct { + struct sockaddr_storage from; /* client address, or address to spoof when connecting to the server */ + struct sockaddr_storage to; /* address reached by the client, or address to connect to */ + } addr; /* addresses of the remote side, client for producer and server for consumer */ +}; + +/* + * datagram callback structure + */ +struct dgram_data_cb { + void (*recv)(struct dgram_conn *dgram); /* recv callback */ + void (*send)(struct dgram_conn *dgram); /* send callback */ +}; + +#endif /* _TYPES_PROTO_UDP_H */ diff --git a/src/proto_udp.c b/src/proto_udp.c new file mode 100644 index 000000000..0e361d928 --- /dev/null +++ b/src/proto_udp.c @@ -0,0 +1,33 @@ +/* + * UDP protocol related functions + * + * Copyright 2014 Baptiste Assmann + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ + +#include +#include +#include + +#include + +/* datagram handler callback */ +int dgram_fd_handler(int fd) +{ + struct dgram_conn *dgram = fdtab[fd].owner; + + if (unlikely(!dgram)) + return 0; + + if (fd_recv_ready(fd)) + dgram->data->recv(dgram); + else if (fd_send_ready(fd)) + dgram->data->send(dgram); + + return 0; +}