#include "secnet.h"
#include <stdio.h>
#include <string.h>
+#include <limits.h>
#include <sys/socket.h>
#include <sys/mman.h>
#define SITE_SENTMSG5 7
#define SITE_WAIT 8
-static string_t state_name(uint32_t state)
+static cstring_t state_name(uint32_t state)
{
switch (state) {
case 0: return "STOP";
struct transform_inst_if *new_transform; /* For key setup/verify */
};
-static void slog(struct site *st, uint32_t event, string_t msg, ...)
+static void slog(struct site *st, uint32_t event, cstring_t msg, ...)
{
va_list ap;
uint8_t buf[240];
}
static void set_link_quality(struct site *st);
-static void delete_key(struct site *st, string_t reason, uint32_t loglevel);
-static bool_t initiate_key_setup(struct site *st, string_t reason);
+static void delete_key(struct site *st, cstring_t reason, uint32_t loglevel);
+static bool_t initiate_key_setup(struct site *st, cstring_t reason);
static void enter_state_run(struct site *st);
static bool_t enter_state_resolve(struct site *st);
static bool_t enter_new_state(struct site *st,uint32_t next);
/* Build any of msg1 to msg4. msg5 and msg6 are built from the inside
out using a transform of config data supplied by netlink */
-static bool_t generate_msg(struct site *st, uint32_t type, string_t what)
+static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what)
{
void *hst;
uint8_t *hash=alloca(st->hash->len);
}
static bool_t check_msg(struct site *st, uint32_t type, struct msg *m,
- string_t *error)
+ cstring_t *error)
{
if (type==LABEL_MSG1) return True;
struct sockaddr_in *src)
{
struct msg m;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG2,msg2,&m)) return False;
if (!check_msg(st,LABEL_MSG2,&m,&err)) {
struct msg m;
uint8_t *hash=alloca(st->hash->len);
void *hst;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG3,msg3,&m)) return False;
if (!check_msg(st,LABEL_MSG3,&m,&err)) {
struct msg m;
uint8_t *hash=alloca(st->hash->len);
void *hst;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG4,msg4,&m)) return False;
if (!check_msg(st,LABEL_MSG4,&m,&err)) {
static bool_t generate_msg5(struct site *st)
{
- string_t transform_err;
+ cstring_t transform_err;
BUF_ALLOC(&st->buffer,"site:MSG5");
/* We are going to add four words to the message */
struct sockaddr_in *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
if (!unpick_msg0(st,msg5,&m)) return False;
static bool_t generate_msg6(struct site *st)
{
- string_t transform_err;
+ cstring_t transform_err;
BUF_ALLOC(&st->buffer,"site:MSG6");
/* We are going to add four words to the message */
struct sockaddr_in *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
if (!unpick_msg0(st,msg6,&m)) return False;
struct sockaddr_in *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
uint32_t type;
if (!st->current_valid) {
}
}
-static bool_t initiate_key_setup(struct site *st,string_t reason)
+static bool_t initiate_key_setup(struct site *st, cstring_t reason)
{
if (st->state!=SITE_RUN) return False;
slog(st,LOG_SETUP_INIT,"initiating key exchange (%s)",reason);
enter_state_run(st);
}
-static void delete_key(struct site *st, string_t reason, uint32_t loglevel)
+static void delete_key(struct site *st, cstring_t reason, uint32_t loglevel)
{
if (st->current_valid) {
slog(st,loglevel,"session closed (%s)",reason);
}
/* msg7 tells our peer that we're about to forget our key */
-static bool_t send_msg7(struct site *st,string_t reason)
+static bool_t send_msg7(struct site *st, cstring_t reason)
{
- string_t transform_err;
+ cstring_t transform_err;
if (st->current_valid && st->peer_valid && st->buffer.free) {
BUF_ALLOC(&st->buffer,"site:MSG7");
/* XXX Erase keys etc. */
}
+static inline void site_settimeout(uint64_t timeout, uint64_t *now,
+ int *timeout_io)
+{
+ if (timeout) {
+ uint64_t offset=timeout-*now;
+ if (offset>INT_MAX) offset=INT_MAX;
+ if (*timeout_io<0 || offset<*timeout_io)
+ *timeout_io=offset;
+ }
+}
+
static int site_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
int *timeout_io, const struct timeval *tv_now,
uint64_t *now)
/* Work out when our next timeout is. The earlier of 'timeout' or
'current_key_timeout'. A stored value of '0' indicates no timeout
active. */
- if (st->timeout && st->timeout-*now < *timeout_io) {
- *timeout_io=st->timeout-*now;
- }
-
- if (st->current_key_timeout && st->current_key_timeout-*now < *timeout_io)
- *timeout_io=st->current_key_timeout-*now;
+ site_settimeout(st->timeout, now, timeout_io);
+ site_settimeout(st->current_key_timeout, now, timeout_io);
return 0; /* success */
}
static void site_outgoing(void *sst, struct buffer_if *buf)
{
struct site *st=sst;
- string_t transform_err;
+ cstring_t transform_err;
if (st->state==SITE_STOP) {
BUF_FREE(buf);