diff options
| -rw-r--r-- | lib/libradsec-base.h | 92 | ||||
| -rw-r--r-- | lib/libradsec-libevent.h | 102 | ||||
| -rw-r--r-- | lib/libradsec.h | 106 | 
3 files changed, 201 insertions, 99 deletions
| diff --git a/lib/libradsec-base.h b/lib/libradsec-base.h new file mode 100644 index 0000000..5a80ab8 --- /dev/null +++ b/lib/libradsec-base.h @@ -0,0 +1,92 @@ +/** @file libradsec-minimal.h +    @brief Low level API for libradsec.  */ + +/* FIXME: License blurb goes here.  */ + +#include "libevent.h" + +/* Example usage.  */ +#if 0 +{ +  fd = rs_connect (address, psk); +  if (!fd) +    /* check errno */ ; +  n = read (fd, buf, buflen); +  struct rs_packet *p = rs_packet_new (buf, buflen, &count); +  if (!p) +    { +      if (count < 0) +	/* check errno */ ; +      else +	/* need another COUNT octets */ ; +    } +  else +    /* next unused octet is at buf+count */ + +  n = rs_packet_serialize (p, buf, buflen); +  if (n < 0) +    /* invalid packet */ ; +  else if (n == 0) +    /* out of buffer space */ ; +  else +    write (fd, buf, n); + +  if (p) +    rs_packet_free(p); +  if (fd) +    rs_disconnect(fd); +} +#endif + + +/* 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(enum rs_conn_type type, +	       const struct sockaddr_storage *addr, +	       const struct rs_credentials *cred); + +/** 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(int fd); + +/** Allocate and initialize a packet object from a buffer containing a +    packet as seen on the wire.  Free the packet using @a +    rs_packet_free(). + +    @param buf +    @param buflen +    @param count + +    @return Packet or NULL on error or not enough data in @a buf.  If +    return value is NULL and @a count is < 0, an error has occurred +    and errno is set appropriately.  If return value is NULL and @a +    count is > 0 it shows the number of bytes needed to complete the +    packet. +*/ +struct rs_packet *rs_packet_new(const uint8_t *buf, +				size_t buflen, +				ssize_t *count); + +/** Free a packet object that has been allocated by @a +    rs_packet_new(). + + +*/ +void rs_packet_free(struct rs_packet *packet); + +/** Serialize a packet.  */ +ssize_t rs_packet_serialize(const struct rs_packet *packet, +			    uint8_t *buf, +			    size_t buflen); diff --git a/lib/libradsec-libevent.h b/lib/libradsec-libevent.h new file mode 100644 index 0000000..d2fffcd --- /dev/null +++ b/lib/libradsec-libevent.h @@ -0,0 +1,102 @@ +/** @file libradsec-libevent.h +    @brief API for libradsec-libevent.  */ + +/* FIXME: License blurb goes here.  */ + +#include "libradsec.h" + +typedef void (*rs_conn_connected_cb)(void *user_data /* FIXME: peer? */); +typedef void (*rs_conn_disconnected_cb)(void *user_data /* FIXME: reason? */); +typedef void (*rs_conn_packet_received_cb)(const struct rs_packet *packet, +					   void *user_data); +typedef void (*rs_conn_packet_sent_cb)(void *user_data); + +/** Connection callbacks.  */ +struct rs_conn_callbacks { +    /** Callback invoked when the connection has been established.  */ +    rs_conn_connected_cb connected_cb; +    /** Callback invoked when the connection has been torn down.  */ +    rs_conn_disconnected_cb disconnected_cb; +    /** Callback invoked when a packet was received.  */ +    rs_conn_packet_received_cb received_cb; +    /** Callback invoked when a packet was successfully sent.  */ +    rs_conn_packet_sent_cb sent_cb; +}; + + +/* 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." diff --git a/lib/libradsec.h b/lib/libradsec.h index 5765282..89f9f2f 100644 --- a/lib/libradsec.h +++ b/lib/libradsec.h @@ -1,5 +1,5 @@ -/*! \file libradsec.h -  \brief Header file for libradsec.  */ +/** @file libradsec.h +    @brief Header file for libradsec.  */  /* FIXME: License blurb goes here.  */ @@ -7,9 +7,13 @@  #include <sys/socket.h>  #include "../list.h" -  /* Data types.  */ +struct rs_config { +    /* FIXME: What's in here that's not in struct rs_conn or +     * rs_credentials?  */; +}; +  enum rs_cred_type {      RS_CRED_NONE = 0,      RS_CRED_TLS_PSK_RSA,	/* RFC 4279.  */ @@ -48,102 +52,6 @@ struct rs_packet {      struct list *attrs;  }; - -typedef void (*rs_conn_connected_cb)(void *user_data /* FIXME: peer? */); -typedef void (*rs_conn_disconnected_cb)(void *user_data /* FIXME: reason? */); -typedef void (*rs_conn_packet_received_cb)(const struct rs_packet *packet, -					   void *user_data); -typedef void (*rs_conn_packet_sent_cb)(void *user_data); - -/*! Connection callbacks.  */ -struct rs_conn_callbacks { -    /*! Callback invoked when the connection has been established.  */ -    rs_conn_connected_cb connected_cb; -    /*! Callback invoked when the connection has been torn down.  */ -    rs_conn_disconnected_cb disconnected_cb; -    /*! Callback invoked when a packet was received.  */ -    rs_conn_packet_received_cb received_cb; -    /*! Callback invoked when a packet was successfully sent.  */ -    rs_conn_packet_sent_cb sent_cb; -}; - - -/* 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" - -  /* Local Variables: */  /* c-file-style: "stroustrup" */  /* End: */ | 
