sxt: few functions added, base struct also; tools: minor includes fix;

master
Alexander Vdolainen 8 years ago
parent 82e97bce60
commit d6629f43bd

@ -27,6 +27,72 @@
#ifndef __SXT_SXT_H__
#define __SXT_SXT_H__
#define SXT_AUTH_PASSWORD (1 << 1)
#define SXT_AUTH_PEERKEY (1 << 2)
#define SXT_AUTH_PEERHASH (1 << 3)
#define SXTPROTO_VERSION 1
#define SXTBEER_TYPE "lager"
typedef struct __sxtlink_type {
uint64_t unuuid;
uint8_t unique[16];
uint8_t state;
/* sets keys */
sxtkey_t *pubkey;
sxtkey_t *privkey;
/* peer stuff */
sxtrdb_t *loginpass_pair;
sxtkey_t *peer_pubkey;
/* socket */
sxtsocket_t *socket;
void *priv;
} sxtlink_t;
typedef struct __sxtset_type {
/* flags for authentification */
uint8_t auth_types;
/* timeouts in secs */
uint32_t session_tm; /* timeout for session i.e. secs to forget about session while socket io error */
uint32_t session_lt; /* session life time i.e. when session will be closed, 0 if infinite */
sxtkey_t *pubkey;
sxtkey_t *privkey;
/* authentification ops */
struct {
int (*auth_loginpass_pair)(sxtlink_t *, void *);
int (*auth_key_peer)(sxtlink_t *, void *);
} auth_ops;
/* options */
uint64_t hash;
const char *strctx;
void *priv;
} sxtset_t;
typedef struct __sxtsession_type {
uint8_t unique[16];
uint8_t state;
sxtlink_t *link;
sxtset_t *linkset;
int sxt_version;
uint64_t peer_swhash;
sxtsafebuffer_t *peerbeer;
/* stats */
uint64_t rcv_pck;
uint64_t snd_pck;
} sxtsession_t;
int sxt_init(void);
int sxt_finish(void);

@ -52,8 +52,14 @@
#include <tdata/list.h>
#include <sxt/errno.h>
#include <sxt/ciphers.h>
#include <sxt/lcrypt.h>
#include <sxt/safebuf.h>
#include <sxt/rdb.h>
#include <sxt/sxtkey.h>
#include <sxt/socket.h>
#include <sxt/sxt.h>
int sxt_init(void)
{
@ -91,6 +97,119 @@ int sxt_get_random(void *data, int len, int pseudo)
return 1;
}
int sxt_genbeer(sxtsession_t *session, sxtsafebuffer_t *o)
{
char *beer;
if(!o || !session) return SXT_EINVAL;
if(!session->linkset) return SXT_EINVAL;
if(!(beer = malloc(128))) return SXT_ENOMEM;
else memset(beer, 0, 128);
snprintf(beer, 128, "sxtbeer-%s-%d-%lu-%s", SXTBEER_TYPE, SXTPROTO_VERSION,
session->linkset->hash,
session->linkset->strctx ? session->linkset->strctx : "nil");
sxtsafebuffer_setdata(o, beer, strlen(beer));
return SXT_SUCCESS;
}
int sxt_scanbeer(sxtsession_t *session, const char *rbuf, size_t rlen)
{
char *buf = (char *)rbuf, *tbuf;
char tuple[32];
uint64_t hash = 0;
int i, st, version, len = 0;
if(!session || !rbuf) return SXT_EINVAL;
if(!rlen || !session->peerbeer) return SXT_EINVAL;
memset(tuple, 0, 32);
for(i = 0, tbuf = NULL, st = 0; i < rlen; i++, buf++) {
switch(st) {
case 0: /* first tuple */
if(!tbuf) tbuf = buf;
if(*buf == '-') {
if(i != strlen("sxtbeer")) return SXT_EPROTO;
if(strncmp(tbuf, "sxtbeer", i)) return SXT_EPROTO; /* that's wine! */
st++;
tbuf = NULL;
}
break;
case 1:
if(!tbuf) tbuf = buf;
if(*buf == '-') {
len = strlen(tbuf) - strlen(buf);
if(len < 1 || len >= 32) return SXT_EPROTO;
memcpy(tuple, tbuf, len);
/* currently one type supported */
if(strcmp(tuple, SXTBEER_TYPE)) return SXT_EPROTO;
memset(tuple, 0, 32);
tbuf = NULL; st++;
}
break;
case 2:
if(!tbuf) tbuf = buf;
if(*buf == '-') {
len = strlen(tbuf) - strlen(buf);
if(len < 1 || len >= 32) return SXT_EPROTO;
memcpy(tuple, tbuf, len);
version = atoi(tuple);
/* first version supported only */
if(version != SXTPROTO_VERSION) return SXT_EPROTO;
memset(tuple, 0, 32);
tbuf = NULL; st++;
}
break;
case 3: /* hash value */
if(!tbuf) tbuf = buf;
if(*buf == '-') {
len = strlen(tbuf) - strlen(buf);
if(len < 1 || len >= 32) return SXT_EPROTO;
memcpy(tuple, tbuf, len);
hash = strtoul(tuple, NULL, 0);
memset(tuple, 0, 32);
tbuf = NULL; st++;
}
break;
case 4: /* the rest of buffer is some custom plate */
if(!tbuf) {
tbuf = buf;
len = i;
memset(tuple, 0, 32);
}
break;
}
}
/* ok check for validity */
if(st < 3) return SXT_EPROTO;
if(!tbuf) return SXT_EPROTO;
else len = rlen - len;
if(len >= 32 || len < 1) return SXT_EPROTO;
else memcpy(tuple, tbuf, len);
session->sxt_version = version;
session->peer_swhash = hash;
if(sxtsafebuffer_length(session->peerbeer) < 128)
return SXT_ENOMEM; /* no room for peer beer */
snprintf((char *)sxtsafebuffer_getdata(session->peerbeer), 128,
"sxtbeer-%s-%d-%lu-%s", SXTBEER_TYPE, session->sxt_version,
session->peer_swhash, tuple);
return SXT_SUCCESS;
}
/* wrappers */
SHA512CTX sha512_init(void){
SHA512CTX c = malloc(sizeof(*c));

@ -36,8 +36,10 @@
#include <termios.h>
#include <sxt/errno.h>
#include <sxt/safebuf.h>
#include <sxt/rdb.h>
#include <sxt/sxtkey.h>
#include <sxt/socket.h>
#include <sxt/sxt.h>
int passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv)

@ -36,8 +36,10 @@
#include <termios.h>
#include <sxt/errno.h>
#include <sxt/safebuf.h>
#include <sxt/rdb.h>
#include <sxt/sxtkey.h>
#include <sxt/socket.h>
#include <sxt/sxt.h>
#define MAX_PATHNAME 4096

@ -36,8 +36,10 @@
#include <termios.h>
#include <sxt/errno.h>
#include <sxt/safebuf.h>
#include <sxt/rdb.h>
#include <sxt/sxtkey.h>
#include <sxt/socket.h>
#include <sxt/sxt.h>
#define MAX_PATHNAME 4096

Loading…
Cancel
Save