summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/err.c4
-rw-r--r--lib/examples/Makefile4
-rw-r--r--lib/examples/client.c6
-rw-r--r--lib/libradsec-impl.h6
-rw-r--r--lib/libradsec.h21
-rw-r--r--lib/packet.c13
-rw-r--r--lib/radsec.c102
7 files changed, 116 insertions, 40 deletions
diff --git a/lib/err.c b/lib/err.c
index 8ee76a6..b0f05c6 100644
--- a/lib/err.c
+++ b/lib/err.c
@@ -11,8 +11,8 @@ const char *_errtxt[] = {
"invalid connection" /* 4 RSE_INVALID_CONN */
"connection type mismatch" /* 5 RSE_CONN_TYPE_MISMATCH */
"FreeRadius error" /* 6 RSE_FR */
- "ERR 7" /* RSE_ */
- "ERR 8" /* RSE_ */
+ "bad hostname or port" /* 7 RSE_BADADDR */
+ "no peer configured" /* 8 RSE_NOPEER */
"ERR 9" /* RSE_ */
"ERR 10" /* RSE_ */
"ERR 11" /* RSE_ */
diff --git a/lib/examples/Makefile b/lib/examples/Makefile
index ceb97f1..a9876da 100644
--- a/lib/examples/Makefile
+++ b/lib/examples/Makefile
@@ -6,7 +6,7 @@ blocking.o: blocking.c blocking.h ../libradsec-base.h ../libradsec.h
$(CC) $(CFLAGS) -c -I .. $^
client: client.c ../libradsec.a ../libradsec.h ../libradsec-impl.h
- $(CC) $(CFLAGS) -o $@ $< -L /usr/lib/freeradius -lfreeradius-radius -L .. -lradsec
+ $(CC) $(CFLAGS) -o $@ $< -L /usr/lib/freeradius -lfreeradius-radius -L .. -lradsec -L /usr/local/lib -levent
clean:
- -rm *.o
+ -rm *.o client
diff --git a/lib/examples/client.c b/lib/examples/client.c
index eab4390..a6b7999 100644
--- a/lib/examples/client.c
+++ b/lib/examples/client.c
@@ -55,8 +55,12 @@ int
main (int argc, char *argv[])
{
struct rs_error *err;
+ char *host;
+ int port;
- err = rsx_client (strsep (argv + 1, ":"), atoi (argv[1]));
+ host = strsep (argv + 1, ":");
+ port = atoi (argv[1]);
+ err = rsx_client (host, port);
if (!err)
return -1;
fprintf (stderr, "%s\n", rs_err_msg (err, 0));
diff --git a/lib/libradsec-impl.h b/lib/libradsec-impl.h
index 170e90c..939cdd9 100644
--- a/lib/libradsec-impl.h
+++ b/lib/libradsec-impl.h
@@ -4,6 +4,7 @@
/* See the file COPYING for licensing information. */
#include <freeradius/libradius.h>
+#include <event2/util.h>
/* Constants. */
#define RS_HEADER_LEN 4
@@ -68,7 +69,8 @@ struct rs_handle {
struct rs_peer {
struct rs_connection *conn;
- struct addrinfo *addr;
+ struct evutil_addrinfo *addr;
+ int s; /* Socket. */
char *secret;
int timeout; /* client only */
int tries; /* client only */
@@ -80,7 +82,7 @@ struct rs_connection {
enum rs_conn_type type;
struct rs_credentials transport_credentials;
struct rs_conn_callbacks callbacks;
- struct rs_peer peers;
+ struct rs_peer *peers;
struct rs_peer *active_peer;
struct rs_error *err;
};
diff --git a/lib/libradsec.h b/lib/libradsec.h
index a2560c7..255c0f5 100644
--- a/lib/libradsec.h
+++ b/lib/libradsec.h
@@ -12,6 +12,8 @@ enum rs_err_code {
RSE_INVALID_CONN = 4,
RSE_CONN_TYPE_MISMATCH = 5,
RSE_FR = 6,
+ RSE_BADADDR = 7,
+ RSE_NOPEER = 8,
RSE_SOME_ERROR = 21,
};
@@ -44,9 +46,9 @@ int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *s
int rs_context_config_read(struct rs_handle *ctx, const char *config_file);
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 *host, int port);
-int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *host, int port);
-int rs_conn_destroy(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);
+int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *hostname, int port);
+void rs_conn_destroy(struct rs_connection *conn);
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);
@@ -70,22 +72,15 @@ void rs_attr_destroy(struct rs_attr *attr);
int rs_packet_send(struct rs_connection *conn, const struct rs_packet *pkt, void *user_data);
int rs_packet_recv(struct rs_connection *conn, struct rs_packet **pkt);
-
int rs_ctx_err_push(struct rs_handle *ctx, int code, const char *fmt, ...);
int rs_ctx_err_push_fl(struct rs_handle *ctx, int code, const char *file, int line, const char *fmt, ...);
struct rs_error *rs_ctx_err_pop (struct rs_handle *ctx);
int rs_conn_err_push(struct rs_connection *conn, int code, const char *fmt, ...);
int rs_conn_err_push_fl(struct rs_connection *conn, int code, const char *file, int line, const char *fmt, ...);
struct rs_error *rs_conn_err_pop (struct rs_connection *conn);
-void rs_err_free (struct rs_error *err);
-char *rs_err_msg (struct rs_error *err, int dofree_flag);
-int rs_err_code (struct rs_error *err, int dofree_flag);
-
-/* Local Variables: */
-/* c-file-style: "stroustrup" */
-/* End: */
-
-
+void rs_err_free(struct rs_error *err);
+char *rs_err_msg(struct rs_error *err, int dofree_flag);
+int rs_err_code(struct rs_error *err, int dofree_flag);
/* Local Variables: */
/* c-file-style: "stroustrup" */
diff --git a/lib/packet.c b/lib/packet.c
index 74ca391..eefb5eb 100644
--- a/lib/packet.c
+++ b/lib/packet.c
@@ -23,10 +23,11 @@ _packet_create (struct rs_connection *conn, struct rs_packet **pkt_out,
rpkt->code = code;
p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
- if (!p) {
- rad_free (&rpkt);
- return rs_conn_err_push (conn, RSE_NOMEM, __func__);
- }
+ if (!p)
+ {
+ rad_free (&rpkt);
+ return rs_conn_err_push (conn, RSE_NOMEM, __func__);
+ }
memset (p, 0, sizeof (struct rs_packet));
p->rpkt = rpkt;
@@ -50,7 +51,7 @@ rs_packet_create_acc_request (struct rs_connection *conn,
return -1;
rs_packet_add_attr (pkt, attr);
- if (rs_attr_create (conn, &attr, "User-Password", user_name))
+ if (rs_attr_create (conn, &attr, "User-Password", user_pw))
return -1;
/* FIXME: need this too? rad_pwencode(user_pw, &pwlen, SECRET, reqauth) */
rs_packet_add_attr (pkt, attr);
@@ -72,7 +73,7 @@ rs_packet_send (struct rs_connection *conn, const struct rs_packet *pkt,
}
rad_encode (pkt->rpkt, NULL, conn->active_peer->secret);
#if defined (DEBUG)
- fprintf (stderr, "%s: about to send this to %s", __func__, "fixme");
+ fprintf (stderr, "%s: about to send this to %s:\n", __func__, "<fixme>");
rs_dump_packet (pkt);
#endif
diff --git a/lib/radsec.c b/lib/radsec.c
index 309c305..97f62e6 100644
--- a/lib/radsec.c
+++ b/lib/radsec.c
@@ -5,6 +5,7 @@
#include <libgen.h>
#include <freeradius/libradius.h>
+#include <event2/util.h>
#include "libradsec.h"
#include "libradsec-impl.h"
@@ -84,7 +85,6 @@ int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
{
memset (c, 0, sizeof(struct rs_connection));
c->ctx = ctx;
- c->peers.next = &c->peers;
}
if (conn)
*conn = c;
@@ -92,30 +92,70 @@ int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
}
struct addrinfo *
-_resolv (const char *host, int port)
+_resolv (struct rs_connection *conn, const char *hostname, int port)
{
- return NULL;
+ int err;
+ char portstr[6];
+ struct evutil_addrinfo hints, *res = NULL;
+
+ snprintf (portstr, sizeof(portstr), "%d", port);
+ memset (&hints, 0, sizeof(struct evutil_addrinfo));
+ //hints.ai_family = AF_UNSPEC; /* v4 or v6. */
+ hints.ai_family = AF_INET; /* FIXME: v4 only, while debuging */
+ hints.ai_flags = AI_ADDRCONFIG;
+ switch (conn->type)
+ {
+ case RS_CONN_TYPE_NONE:
+ rs_conn_err_push_fl (conn, RSE_INVALID_CONN, __FILE__, __LINE__, NULL);
+ return NULL;
+ case RS_CONN_TYPE_TCP:
+ case RS_CONN_TYPE_TLS:
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ break;
+ case RS_CONN_TYPE_UDP:
+ case RS_CONN_TYPE_DTLS:
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_protocol = IPPROTO_UDP;
+ break;
+ }
+ err = evutil_getaddrinfo (hostname, portstr, &hints, &res);
+ if (err)
+ rs_conn_err_push_fl (conn, RSE_BADADDR, __FILE__, __LINE__,
+ " %s:%d: bad host name or port (%s)",
+ hostname, port, evutil_gai_strerror(err));
+ return res; /* Simply use first result. */
}
-int rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server, rs_conn_type_t type, const char *host, int port)
+int
+rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server,
+ rs_conn_type_t type, const char *hostname, int port)
{
struct rs_peer *srv;
+ struct evutil_addrinfo *addr;
if (conn->type == RS_CONN_TYPE_NONE)
conn->type = type;
else if (conn->type != type)
return rs_conn_err_push (conn, RSE_CONN_TYPE_MISMATCH, NULL);
+ addr = _resolv (conn, hostname, port);
+ if (!addr)
+ return -1;
+
srv = (struct rs_peer *) malloc (sizeof(struct rs_peer));
if (srv)
{
memset (srv, 0, sizeof(struct rs_peer));
srv->conn = conn;
- srv->addr = _resolv (host, port);
+ srv->addr = addr;
srv->timeout = 10;
srv->tries = 3;
- srv->next = conn->peers.next;
- conn->peers.next = srv;
+ srv->next = conn->peers;
+ if (conn->peers)
+ conn->peers->next = srv;
+ else
+ conn->peers = srv;
}
if (*server)
*server = srv;
@@ -141,16 +181,27 @@ int rs_server_set_secret(struct rs_peer *server, const char *secret)
return RSE_OK;
}
-int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *host, int port)
+int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *hostname, int port)
{
return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
"%s: NYI", __func__);
}
-int rs_conn_destroy(struct rs_connection *conn)
+
+void
+rs_conn_destroy(struct rs_connection *conn)
{
- return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
- "%s: NYI", __func__);
+ struct rs_peer *p;
+
+#warning "TODO: Disconnect active_peer."
+
+ for (p = conn->peers; p; p = p->next)
+ {
+ if (p->addr)
+ evutil_freeaddrinfo (p->addr);
+ if (p->secret)
+ rs_free (conn->ctx, p->secret);
+ }
}
int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
@@ -177,8 +228,31 @@ int rs_conn_get_current_server(struct rs_connection *conn, const char *name, siz
"%s: NYI", __func__);
}
-int rs_conn_open(struct rs_connection *conn)
+/* Non-public. */
+int
+rs_conn_open(struct rs_connection *conn)
{
- return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
- "%s: NYI", __func__);
+ int s;
+ struct rs_peer *p;
+
+ if (conn->active_peer)
+ return RSE_OK;
+ p = conn->peers;
+ if (!p)
+ return rs_conn_err_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
+
+ s = socket (p->addr->ai_family, p->addr->ai_socktype, p->addr->ai_protocol);
+ if (s < 0)
+ return rs_conn_err_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
+ strerror (errno));
+ if (connect (s, p->addr->ai_addr, p->addr->ai_addrlen))
+ {
+ /* TODO: handle nonblocking sockets (EINTR, EAGAIN). */
+ EVUTIL_CLOSESOCKET (s);
+ return rs_conn_err_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
+ strerror (errno));
+ }
+ p->s = s;
+ conn->active_peer = p;
+ return RSE_OK;
}