diff options
-rw-r--r-- | lib/base.c | 238 | ||||
-rw-r--r-- | lib/libradsec-base.h | 83 | ||||
-rw-r--r-- | lib/libradsec-levent.h | 91 |
3 files changed, 0 insertions, 412 deletions
diff --git a/lib/base.c b/lib/base.c deleted file mode 100644 index 2081b1c..0000000 --- a/lib/base.c +++ /dev/null @@ -1,238 +0,0 @@ -/* See the file COPYING for licensing information. */ - -#include <sys/socket.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> -//#include <unistd.h> -#include <stdint.h> -#include "../tlv11.h" /* FIXME: .. */ -#include "libradsec-base.h" - -static int -_do_connect(int type, - const struct sockaddr *addr, - socklen_t addrlen) -{ - int s; - - s = socket(AF_INET, type, 0); /* FIXME: do AF_INET6 too */ - if (s >= 0) - if (connect(s, addr, addrlen)) { - close(s); - s = -1; - } - return s; -} - -static struct list * -_list_new(const struct rs_handle *ctx) -{ - struct list *list = rs_malloc(ctx, sizeof(struct list)); - if (list) - memset(list, 0, sizeof(struct list)); - return list; -} - -static int -_list_push(const struct rs_handle *ctx, /* FIXME: code duplicate, list.c */ - struct list *list, - void *data) -{ - struct list_node *node; - - node = rs_malloc(ctx, sizeof(struct list_node)); - if (!node) - return 0; - - node->next = NULL; - node->data = data; - - if (list->first) - list->last->next = node; - else - list->first = node; - list->last = node; - - list->count++; - return 1; -} - -static void -_list_destroy(const struct rs_handle *ctx, /* FIXME: code dup */ - struct list *list) -{ - struct list_node *node, *next; - - if (list) { - for (node = list->first; node; node = next) { - rs_free(ctx, node->data); - next = node->next; - rs_free(ctx, node); - } - free(list); - } -} - -/* ------------------------------------------------------- */ -int -rs_connect(const struct rs_handle *conf, - const struct sockaddr *addr, - socklen_t addrlen) -{ - switch (conf->conn_type) - { - case RS_CONN_TYPE_UDP: - return _do_connect(SOCK_DGRAM, addr, addrlen); - case RS_CONN_TYPE_TCP: - return _do_connect(SOCK_STREAM, addr, addrlen); - /* fall thru */ - case RS_CONN_TYPE_TLS: - /* fall thru */ - case RS_CONN_TYPE_DTLS: - /* fall thru */ - default: - errno = ENOSYS; - return -1; - } -} - -int -rs_disconnect( const struct rs_handle *conf, int fd) -{ - switch (conf->conn_type) - { - case RS_CONN_TYPE_UDP: - return close(fd); - case RS_CONN_TYPE_TCP: - shutdown(fd, SHUT_RDWR); - return close(fd); - case RS_CONN_TYPE_TLS: - /* fall thru */ - case RS_CONN_TYPE_DTLS: - /* fall thru */ - default: - errno = ENOSYS; - return -1; - } -} - -struct rs_packet * -rs_packet_new(const struct rs_handle *ctx, - const uint8_t buf[RS_HEADER_LEN], - size_t *count) -{ - struct rs_packet *p = rs_malloc(ctx, sizeof(struct rs_packet)); - if (p) { - p->attrs = _list_new(ctx); - if (p->attrs) { - p->code = buf[0]; - p->id = buf[1]; - if (count) - *count = (buf[2] << 8) + buf[3]; - } - else - rs_packet_free(ctx, &p); - } - return p; -} - -struct rs_packet * -rs_packet_parse(const struct rs_handle *ctx, - struct rs_packet **packet, - const uint8_t *buf, - size_t buflen) -{ - struct rs_packet *p = *packet; - struct tlv *tlv; - size_t i; - uint8_t atype, alen; - - if (buflen < 16) { - errno = EPROTO; - rs_packet_free(ctx, &p); - return NULL; - } - - i = 16; - while (i + 2 < buflen) { - atype = buf[i++]; - alen = buf[i++]; - if (alen < 2) { -#if DEBUG - fprintf(stderr, - "%s: DEBUG: attribute (type %d, len %d) has an invalid length\n", - __func__, atype, alen); -#endif - errno = EPROTO; - rs_packet_free(ctx, &p); - return NULL; - } - alen -= 2; - if (alen + i >= buflen) { -#if DEBUG - fprintf(stderr, - "%s: DEBUG: attribute (type %d, len %d) wouldn't fit packet\n", - __func__, atype, alen); -#endif - errno = EPROTO; - rs_packet_free(ctx, &p); - return NULL; - } - tlv = maketlv(atype, alen, (void *) (buf + i)); - if (tlv) - _list_push(ctx, p->attrs, tlv); - else { - errno = ENOMEM; - rs_packet_free(ctx, &p); - } - i += alen; - } - memcpy(p->auth, buf, 16); - return p; -} - -void -rs_packet_free(const struct rs_handle *ctx, - struct rs_packet **packet) -{ - _list_destroy(ctx, (*packet)->attrs); - rs_free(ctx, *packet); - *packet = NULL; -} - -ssize_t -rs_packet_serialize(const struct rs_packet *packet, - uint8_t *buf, - size_t buflen) -{ - struct list_node *ln; - size_t pktlen; - ssize_t i; - - for (ln = list_first(packet->attrs), pktlen = 20; ln; ln = list_next(ln)) - pktlen += ((struct rs_attribute *)(ln->data))->length; - if (pktlen > buflen) - return -(pktlen - buflen); - - buf[0] = packet->code; - buf[1] = packet->id; - buf[2] = (pktlen & 0xff00) >> 8; - buf[3] = pktlen & 0xff; - - memcpy(buf + 4, packet->auth, 16); - - for (ln = list_first(packet->attrs), i = 20; ln; ln = list_next(ln)) { - struct rs_attribute *a = (struct rs_attribute *)(ln->data); - buf[i++] = a->type; - buf[i++] = a->length; - memcpy(buf + i, a->value, a->length - 2); - i += a->length - 2; - } - - return i; -} - -/* Local Variables: */ -/* c-file-style: "stroustrup" */ -/* End: */ diff --git a/lib/libradsec-base.h b/lib/libradsec-base.h deleted file mode 100644 index 5d8dd11..0000000 --- a/lib/libradsec-base.h +++ /dev/null @@ -1,83 +0,0 @@ -/** @file libradsec-base.h - @brief Low level API for libradsec. */ - -/* See the file COPYING for licensing information. */ - -#include <unistd.h> -#include <stdint.h> -#include <sys/socket.h> -#include "libradsec.h" - -/* Function prototypes. */ - - - -/** Establish a connection. - @param type Connection type. - @param addr Network address to connect to. - @param cred Credentials, or NULL. - @return A file descriptor or -1 if an error occurred, in which - case errno is set appropriately. */ -int rs_connect(const struct rs_handle *conf, const struct sockaddr *addr, - socklen_t addrlen); - -/** Disconnect. - @param fd File descriptor to close. - @return 0 on success or -1 if an error occurred, in which case - errno is set appropriately. */ -int rs_disconnect(const struct rs_handle *conf, int fd); - -/** Allocate and initialize a packet from a buffer containing a RADIUS - message header. The packet should be freed using @a - rs_packet_free(). - @param ctx Context. - @param buf Buffer with on-the-wire data with RADIUS message - header. - @param count Optionally a pointer to a size_t where the number of - additional octets needed to complete the RADIUS message will be - written. Or NULL. - @return A pointer to a newly allocated packet or NULL on error. -*/ -struct rs_packet *rs_packet_new(const struct rs_handle *ctx, - const uint8_t buf[RS_HEADER_LEN], - size_t *count); - -/** Parse an on wire RADIUS packet and store it in @a packet. - @param ctx Context. - @param packet A pointer to the address of a struct rs_packet - allocated by @a rs_packet_new(). Will be freed if an error - occurs. - @param buf Buffer with on-the-wire data with RADIUS message, not - including the four octet RADIUS header. - @param buflen Number of octets in @a buf. - @return *packet or NULL on error. If NULL, the packet has been - freed and *packet is no longer valid. -*/ -struct rs_packet *rs_packet_parse(const struct rs_handle *ctx, - struct rs_packet **packet, - const uint8_t *buf, - size_t buflen); - -/** Free @a packet, previously allocated by @a rs_packet_new(). - @param ctx Context. - @param packet Packet to free. -*/ -void rs_packet_free(const struct rs_handle *ctx, struct rs_packet **packet); - -/** Serialize @a packet into @a buf. - @param packet Packet to serialize. - @param buf Buffer to store the serialized packet in. - @param buflen Length of buffer. - @return Number of bytes written to buf or 0 if the buffer wasn't - large enough to hold the packet or < 0 in case the packet couldn't - be serialized for some other reason (FIXME: elaborate) */ - -ssize_t rs_packet_serialize(const struct rs_packet *packet, - uint8_t *buf, size_t buflen); - - -/** Add an attribute to a packet. - @param packet The packet. - @param attribute Attribute to add to packet. */ -int rs_packet_add_attr(struct rs_packet *packet, - const struct rs_attribute *attribute); diff --git a/lib/libradsec-levent.h b/lib/libradsec-levent.h deleted file mode 100644 index 3caa4ad..0000000 --- a/lib/libradsec-levent.h +++ /dev/null @@ -1,91 +0,0 @@ -/** @file libradsec-levent.h - @brief API for libradsec-libevent. */ - -/* See the file COPYING for licensing information. */ - -#include <sys/socket.h> -#include "libradsec.h" - -struct rs_connection { - struct rs_handle *conf; - struct sockaddr_storage addr; - char open_flag; -}; - - -/* Function prototypes. */ - -/* - FIXME: Do we want alloc and free? Or perhaps init and free, - decoupling allocation from initialization? IMO we want _some_ init - function, f.ex. for setting open_flag = 1 when type == UDP. - -struct conn *conn_alloc (enum conn_type type, struct sockaddr_in6 address, ...); -void conn_free (struct conn *conn); -*/ - -/** Open connection and return 0 on success. - @param conn Connection object, obtained through a call to @a - conn_alloc. - @param cb Callbacks for events on the connection. If NULL, all I/O - will be blocking. - @param user_data A pointer passed to the callbacks when invoked. */ -int rs_conn_open(struct rs_conn *conn, - const struct rs_conn_callbacks *cb, - void *user_data); - -/** Close connection and return 0 on success. - @param conn Connection object, obtained through a call to @a - conn_alloc. - @param user_data A pointer passed to the callbacks when the @a - disconnected_cb in @a conn is invoked. */ -int rs_conn_close(struct rs_conn *conn, void *user_data); /* FIXME: return type? */ - -/** Allocate a packet object. Should be freed using @a rs_packet_free. */ -struct rs_packet *rs_packet_alloc(); - -/** Free a packet object previously allocated with @a rs_packet_alloc. */ -void rs_packet_free(); - -/** Add an attribute to a packet. - @param packet The packet. - @param attribute Attribute to add to packet. */ -int rs_packet_add_attribute(struct rs_packet *packet, - const struct rs_attribute *attribute); - -/** Send @a packet on @a conn and return 0 on success. - @param conn Connection object, obtained through a call to @a - conn_alloc and opened with @a rs_conn_open. - @param packet Packet to send. - @param user_data Pointer passed to @a rs_conn_packet_sent_cb, invoked - when packet has been sent. - */ -int rs_packet_send(const struct rs_conn *conn, - const struct rs_packet *packet, - void *user_data); - -/** Return the next packet received on @a conn, blocking while waiting. - The packet returned must be freed using @a rs_packet_free. */ -struct rs_packet *rs_packet_receive(const struct rs_conn *conn); - - -/* Thinking out loud here... - - We could let the user drive the underlying libevent event loop in - three different ways, from easiest to hairiest: - - i) Blocking i/o model: User passes NULL for the callbacks in - rs_conn_open(). The open, send and receive calls will block until - the desired event occurs. Other events occurring while waiting - will be either silently discarded or signaled as an error - (f.ex. broken connection while sending). - - ii) Simple event loop interface with a timeout: User calls - rs_event_loop(timeout) to process pending i/o. Should be a good - choice for most applications. - - iii) Full libevent interface: TODO. - */ - - -#error "Need an rs_event_loop(). And more." |