d8c9a2b0267f6126ed79733c1ab5c1f977aa2e4d
[radsecproxy.git] / lib / libradsec-base.h
1 /** @file libradsec-base.h
2     @brief Low level API for libradsec.  */
3
4 /* FIXME: License blurb goes here.  */
5
6 #include <unistd.h>
7 #include <stdint.h>
8 #include <sys/socket.h>
9 #include "libradsec.h"
10
11 /* Function prototypes.  */
12
13
14
15 /** Establish a connection.
16     @param type Connection type.
17     @param addr Network address to connect to.
18     @param cred Credentials, or NULL.
19     @return A file descriptor or -1 if an error occurred, in which
20     case errno is set appropriately.  */
21 int rs_connect(const struct rs_handle *conf, const struct sockaddr *addr,
22                socklen_t addrlen);
23
24 /** Disconnect.
25     @param fd File descriptor to close.
26     @return 0 on success or -1 if an error occurred, in which case
27     errno is set appropriately.  */
28 int rs_disconnect(const struct rs_handle *conf, int fd);
29
30 /** Allocate and initialize a packet from a buffer containing a RADIUS
31     message header.  The packet should be freed using @a
32     rs_packet_free().
33     @param ctx Context.
34     @param buf Buffer with on-the-wire data with RADIUS message
35     header.
36     @param count Optionally a pointer to a size_t where the number of
37     additional octets needed to complete the RADIUS message will be
38     written.  Or NULL.
39     @return A pointer to a newly allocated packet or NULL on error.
40 */
41 struct rs_packet *rs_packet_new(const struct rs_handle *ctx,
42                                 const uint8_t buf[RS_HEADER_LEN],
43                                 size_t *count);
44
45 /** Parse an on wire RADIUS packet and store it in @a packet.
46     @param ctx Context.
47     @param packet A pointer to the address of a struct rs_packet
48     allocated by @a rs_packet_new().  Will be freed if an error
49     occurs.
50     @param buf Buffer with on-the-wire data with RADIUS message, not
51     including the four octet RADIUS header.
52     @param buflen Number of octets in @a buf.
53     @return *packet or NULL on error.  If NULL, the packet has been
54     freed and *packet is no longer valid.
55 */
56 struct rs_packet *rs_packet_parse(const struct rs_handle *ctx,
57                                   struct rs_packet **packet,
58                                   const uint8_t *buf,
59                                   size_t buflen);
60
61 /** Free @a packet, previously allocated by @a rs_packet_new().
62     @param ctx Context.
63     @param packet Packet to free.
64 */
65 void rs_packet_free(const struct rs_handle *ctx, struct rs_packet **packet);
66
67 /** Serialize @a packet into @a buf.
68     @param packet Packet to serialize.
69     @param buf Buffer to store the serialized packet in.
70     @param buflen Length of buffer.
71     @return Number of bytes written to buf or 0 if the buffer wasn't
72     large enough to hold the packet or < 0 in case the packet couldn't
73     be serialized for some other reason (FIXME: elaborate) */
74
75 ssize_t rs_packet_serialize(const struct rs_packet *packet,
76                             uint8_t *buf, size_t buflen);
77
78
79 /** Add an attribute to a packet.
80     @param packet The packet.
81     @param attribute Attribute to add to packet.  */
82 int rs_packet_add_attr(struct rs_packet *packet,
83                        const struct rs_attribute *attribute);