summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2010-10-02 21:31:50 +0200
committerLinus Nordberg <linus@nordu.net>2010-10-02 21:31:50 +0200
commitbe4b786700ad03de5226951c043fefb345c0df4c (patch)
treee048b100e34ddcfebadabb459f9af7c55d01b3ce /lib
parent7ec20f4101e5a1ef0b5f2537f25a4f8d74ad1104 (diff)
WIP
Diffstat (limited to 'lib')
-rw-r--r--lib/HACKING2
-rw-r--r--lib/Makefile4
-rw-r--r--lib/attr.c4
-rw-r--r--lib/debug.c4
-rw-r--r--lib/err.c4
-rw-r--r--lib/examples/Makefile11
-rw-r--r--lib/examples/client-blocking.c (renamed from lib/examples/client.c)13
-rw-r--r--lib/include/radsec/libradsec-impl.h (renamed from lib/libradsec-impl.h)0
-rw-r--r--lib/include/radsec/libradsec.h (renamed from lib/libradsec.h)18
-rw-r--r--lib/packet.c76
-rw-r--r--lib/radsec.c4
11 files changed, 79 insertions, 61 deletions
diff --git a/lib/HACKING b/lib/HACKING
index ed9717d..9cbab82 100644
--- a/lib/HACKING
+++ b/lib/HACKING
@@ -1,6 +1,6 @@
HACKING file for libradsec (in Emacs -*- org -*- mode).
* Design of the libraray
-* Features
+* Functionality
** Not implemented
- reading config file
- server fail over
diff --git a/lib/Makefile b/lib/Makefile
index 1b26f04..3575305 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,4 +1,4 @@
-CFLAGS = -Wall -g -DDEBUG
+CFLAGS = -Wall -g -I include -DDEBUG
OFILES = attr.o \
debug.o \
@@ -11,7 +11,7 @@ all: libradsec.a
libradsec.a: $(OFILES)
ar rc $@ $^
-$(OFILES): libradsec.h libradsec-impl.h
+$(OFILES): include/radsec/libradsec.h include/radsec/libradsec-impl.h
doc:
doxygen
diff --git a/lib/attr.c b/lib/attr.c
index 0b1542e..8dede27 100644
--- a/lib/attr.c
+++ b/lib/attr.c
@@ -1,8 +1,8 @@
/* See the file COPYING for licensing information. */
#include <freeradius/libradius.h>
-#include "libradsec.h"
-#include "libradsec-impl.h"
+#include <radsec/libradsec.h>
+#include <radsec/libradsec-impl.h>
int
rs_attr_create(struct rs_connection *conn, struct rs_attr **attr, const char *type, const char *val)
diff --git a/lib/debug.c b/lib/debug.c
index d2d8d9c..26e2403 100644
--- a/lib/debug.c
+++ b/lib/debug.c
@@ -2,8 +2,8 @@
#include <stdio.h>
#include <freeradius/libradius.h>
-#include "libradsec.h"
-#include "libradsec-impl.h"
+#include <radsec/libradsec.h>
+#include <radsec/libradsec-impl.h>
#include "debug.h"
/* From freeradius-server/src/lib/radius.c */
diff --git a/lib/err.c b/lib/err.c
index f81644a..ec72a29 100644
--- a/lib/err.c
+++ b/lib/err.c
@@ -3,8 +3,8 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
-#include "libradsec.h"
-#include "libradsec-impl.h"
+#include <radsec/libradsec.h>
+#include <radsec/libradsec-impl.h>
const char *_errtxt[] = {
"SUCCESS", /* 0 RSE_OK */
diff --git a/lib/examples/Makefile b/lib/examples/Makefile
index f9f2ee1..618afdb 100644
--- a/lib/examples/Makefile
+++ b/lib/examples/Makefile
@@ -1,12 +1,11 @@
-CFLAGS = -Wall -g
+CFLAGS = -Wall -g -I ../include
-all: client
+HFILES = ../include/radsec/libradsec.h ../include/radsec/libradsec-impl.h
-blocking.o: blocking.c blocking.h ../libradsec-base.h ../libradsec.h
- $(CC) $(CFLAGS) -c -I .. $^
+all: client-blocking
-client: client.c ../libradsec.a ../libradsec.h ../libradsec-impl.h
+client-blocking: client-blocking.c ../libradsec.a $(HFILES)
$(CC) $(CFLAGS) -o $@ $< -L /usr/lib/freeradius -lfreeradius-radius -L .. -lradsec -L /usr/local/lib -levent_core
clean:
- -rm *.o client
+ -rm *.o client-blocking *.gch
diff --git a/lib/examples/client.c b/lib/examples/client-blocking.c
index 1e9f209..0d2a58a 100644
--- a/lib/examples/client.c
+++ b/lib/examples/client-blocking.c
@@ -5,7 +5,8 @@
#include <unistd.h>
#include <stdlib.h>
#include <event2/event.h>
-#include "../libradsec.h"
+#include <freeradius/libradius.h>
+#include <radsec/libradsec.h>
#define SECRET "sikrit"
#define USER_NAME "bob"
@@ -18,6 +19,8 @@ rsx_client (const char *srvname, int srvport)
struct rs_connection *conn;
struct rs_peer *server;
struct rs_packet *req, *resp;
+ RADIUS_PACKET *fr_pkt;
+ VALUE_PAIR *fr_vp;
if (rs_context_create (&h, "/usr/share/freeradius/dictionary"))
return NULL;
@@ -33,7 +36,7 @@ rsx_client (const char *srvname, int srvport)
if (rs_packet_create_acc_request (conn, &req, USER_NAME, USER_PW))
return rs_conn_err_pop (conn);
- if (rs_packet_send (conn, req, NULL))
+ if (rs_packet_send (req, NULL))
return rs_conn_err_pop (conn);
req = NULL;
@@ -46,9 +49,11 @@ rsx_client (const char *srvname, int srvport)
req = NULL;
#endif
- if (rs_packet_receive (conn, &resp))
+ if (rs_conn_receive_packet (conn, &resp))
return rs_conn_err_pop (conn);
- /* TODO: do something interesting with the response */
+ fr_pkt = rs_packet_frpkt (resp);
+ fr_vp = fr_pkt->vps; /* FIXME: Is there an accessor? */
+ vp_printlist(stdout, fr_vp);
rs_packet_destroy (resp);
rs_conn_destroy (conn);
diff --git a/lib/libradsec-impl.h b/lib/include/radsec/libradsec-impl.h
index 3abb71e..3abb71e 100644
--- a/lib/libradsec-impl.h
+++ b/lib/include/radsec/libradsec-impl.h
diff --git a/lib/libradsec.h b/lib/include/radsec/libradsec.h
index e487064..ce6940e 100644
--- a/lib/libradsec.h
+++ b/lib/include/radsec/libradsec.h
@@ -37,7 +37,8 @@ struct rs_conn; /* radsec-impl.h */
struct rs_attr; /* radsec-impl.h */
struct rs_error; /* radsec-impl.h */
struct rs_peer; /* radsec-impl.h */
-struct event_base; /* <event.h> */
+struct radius_packet; /* <freeradius/libradius.h> */
+struct event_base; /* <event2/event-internal.h> */
typedef void * (*rs_calloc_fp)(size_t nmemb, size_t size);
typedef void * (*rs_malloc_fp)(size_t size);
@@ -74,6 +75,11 @@ void rs_context_destroy(struct rs_handle *ctx);
int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *scheme);
int rs_context_config_read(struct rs_handle *ctx, const char *config_file);
+/* Server and client configuration. */
+void rs_server_set_timeout(struct rs_peer *server, int timeout);
+void rs_server_set_tries(struct rs_peer *server, int tries);
+int rs_server_set_secret(struct rs_peer *server, const char *secret);
+
/* Connection. */
int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn);
int rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server, rs_conn_type_t type, const char *hostname, int port);
@@ -83,11 +89,7 @@ int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb);
int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb);
int rs_conn_select_server(struct rs_connection *conn, const char *name);
int rs_conn_get_current_server(struct rs_connection *conn, const char *name, size_t buflen);
-
-/* Server and client configuration. */
-void rs_server_set_timeout(struct rs_peer *server, int timeout);
-void rs_server_set_tries(struct rs_peer *server, int tries);
-int rs_server_set_secret(struct rs_peer *server, const char *secret);
+int rs_conn_receive_packet(struct rs_connection *conn, struct rs_packet **pkt_out);
/* Packet. */
int rs_packet_create_acc_request(struct rs_connection *conn, struct rs_packet **pkt, const char *user_name, const char *user_pw);
@@ -96,8 +98,8 @@ int rs_packet_create_acc_request(struct rs_connection *conn, struct rs_packet **
//int rs_packet_create_acc_challenge(struct rs_connection *conn, struct rs_packet **pkt);
void rs_packet_destroy(struct rs_packet *pkt);
void rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr);
-int rs_packet_send(struct rs_connection *conn, struct rs_packet *pkt, void *data);
-int rs_packet_receive(struct rs_connection *conn, struct rs_packet **pkt_out);
+int rs_packet_send(struct rs_packet *pkt, void *data);
+struct radius_packet *rs_packet_frpkt(struct rs_packet *pkt);
/* Attribute. */
int rs_attr_create(struct rs_connection *conn, struct rs_attr **attr, const char *type, const char *val);
diff --git a/lib/packet.c b/lib/packet.c
index 5f7293c..8d9be93 100644
--- a/lib/packet.c
+++ b/lib/packet.c
@@ -5,8 +5,8 @@
#include <freeradius/libradius.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
-#include "libradsec.h"
-#include "libradsec-impl.h"
+#include <radsec/libradsec.h>
+#include <radsec/libradsec-impl.h>
#if defined DEBUG
#include "debug.h"
#endif
@@ -130,40 +130,36 @@ _read_cb (struct bufferevent *bev, void *ctx)
assert (pkt->conn);
if (!pkt->hdr_read_flag)
{
- n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN;
+ n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
if (n == RS_HEADER_LEN)
{
- uint16_t len = (pkt->hdr[2] << 8) + pkt->hdr[3];
- uint8_t *buf = rs_malloc (pkt->conn->ctx, len);
-
pkt->hdr_read_flag = 1;
- if (!buf)
+ pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
+ if (pkt->rpkt->data_len < 20 /* || len > 4096 */)
+ abort (); /* TODO: Read and discard. */
+ pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
+ if (!pkt->rpkt->data)
{
- rs_conn_err_push_fl (pkt->conn, RSE_NOMEM, __FILE__,
- __LINE__, NULL);
+ rs_conn_err_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
+ NULL);
abort (); /* FIXME: recovering takes reading of packet */
}
- pkt->rpkt->data = buf;
- pkt->rpkt->data_len = len;
+ memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
bufferevent_setwatermark (pkt->conn->bev, EV_READ,
- len - RS_HEADER_LEN, 0);
+ pkt->rpkt->data_len - RS_HEADER_LEN, 0);
#if defined (DEBUG)
- fprintf (stderr, "%s: packet header read, pkt len=%d\n", __func__,
- len);
+ fprintf (stderr, "%s: packet header read, total pkt len=%d\n",
+ __func__, pkt->rpkt->data_len);
#endif
}
else if (n < 0)
- return; /* Buffer frozen, i suppose. Let's hope it thaws. */
+ return; /* Buffer frozen. */
else
- {
- assert (n < RS_HEADER_LEN);
- return; /* Need more to complete header. */
- }
+ assert (!"short header");
}
- printf ("%s: trying to read %d octets of packet data\n", __func__, pkt->rpkt->data_len - RS_HEADER_LEN;
- n = bufferevent_read (pkt->conn->bev, pkt->rpkt->data,
- pkt->rpkt->data_len - RS_HEADER_LEN);
+ printf ("%s: trying to read %d octets of packet data\n", __func__, pkt->rpkt->data_len - RS_HEADER_LEN);
+ n = bufferevent_read (pkt->conn->bev, pkt->rpkt->data + RS_HEADER_LEN, pkt->rpkt->data_len - RS_HEADER_LEN);
printf ("%s: read %d octets of packet data\n", __func__, n);
if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
{
@@ -173,9 +169,14 @@ _read_cb (struct bufferevent *bev, void *ctx)
#if defined (DEBUG)
fprintf (stderr, "%s: complete packet read\n", __func__);
#endif
+ rad_decode (pkt->rpkt, NULL, pkt->conn->active_peer->secret);
if (event_base_loopbreak (pkt->conn->evb) < 0)
abort (); /* FIXME */
}
+ else if (n < 0)
+ return; /* Buffer frozen. */
+ else
+ assert (!"short packet");
}
static int
@@ -268,10 +269,13 @@ _conn_open(struct rs_connection *conn, struct rs_packet *pkt)
}
int
-rs_packet_send (struct rs_connection *conn, struct rs_packet *pkt, void *data)
+rs_packet_send (struct rs_packet *pkt, void *data)
{
- assert (conn);
+ struct rs_connection *conn;
+ assert (pkt);
+ assert (pkt->conn);
assert (pkt->rpkt);
+ conn = pkt->conn;
if (_conn_open (conn, pkt))
return -1;
@@ -291,7 +295,7 @@ rs_packet_send (struct rs_connection *conn, struct rs_packet *pkt, void *data)
}
int
-rs_packet_receive(struct rs_connection *conn, struct rs_packet **pkt_out)
+rs_conn_receive_packet (struct rs_connection *conn, struct rs_packet **pkt_out)
{
struct rs_packet *pkt;
@@ -313,13 +317,14 @@ rs_packet_receive(struct rs_connection *conn, struct rs_packet **pkt_out)
bufferevent_enable (conn->bev, EV_READ);
event_base_dispatch (conn->evb);
#if defined (DEBUG)
- fprintf (stderr, "%s: event loop done\n", __func__);
- assert (event_base_got_break(conn->evb));
-#endif
-
-#if defined (DEBUG)
- fprintf (stderr, "%s: got this:\n", __func__);
- rs_dump_packet (pkt);
+ fprintf (stderr, "%s: event loop done", __func__);
+ if (event_base_got_break(conn->evb))
+ {
+ fprintf (stderr, ", got this:\n");
+ rs_dump_packet (pkt);
+ }
+ else
+ fprintf (stderr, ", no reply\n");
#endif
return RSE_OK;
@@ -331,3 +336,10 @@ rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr)
pairadd (&pkt->rpkt->vps, attr->vp);
attr->pkt = pkt;
}
+
+struct radius_packet *
+rs_packet_frpkt(struct rs_packet *pkt)
+{
+ assert (pkt);
+ return pkt->rpkt;
+}
diff --git a/lib/radsec.c b/lib/radsec.c
index 3d8c5c2..6d0308d 100644
--- a/lib/radsec.c
+++ b/lib/radsec.c
@@ -9,8 +9,8 @@
#include <freeradius/libradius.h>
#include <event2/event.h>
#include <event2/util.h>
-#include "libradsec.h"
-#include "libradsec-impl.h"
+#include <radsec/libradsec.h>
+#include <radsec/libradsec-impl.h>
int
rs_context_create(struct rs_handle **ctx, const char *dict)