Commit 3a4c8ebd authored by yids's avatar yids

- started on cli testing tool for sending messages

- added function to pl to load pubkeys from a folder
- add alias when loading key in list
- added uart read and write functions
parent b1f17f08
......@@ -2,7 +2,7 @@
CC=colorgcc
CFLAGS="-I ../libs -DDEBUG"
all: tx-test rx-test keygen test.c
all: pager-cli tx-test rx-test keygen
base-tx: base-tx.c
$(CC) -o base-tx base-tx.c $(LIBS) $(CFLAGS)
......@@ -25,9 +25,12 @@ keygen: keygen.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c
test2: test2.c
$(CC) -std=c99 -o test2 test2.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
pager-cli: pager-cli.c
$(CC) -std=c99 -g -o pager-cli pager-cli.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c -lsodium -lreadline $(LIBS) $(CFLAGS)
clean :
rm -r test tx-test rx-test keygen
rm -r test tx-test rx-test keygen pager-cli
debugger: test
ddd -x gdb.conf ./test
......@@ -13,18 +13,18 @@ void load_a_lot_of_keys(struct pl_ctx *ctx)
sprintf(filename, "%d.pub",i);
printf("loading pubkey: %s\n", filename);
pubkey = pl_load_public_key(filename);
pl_load_public_key_in_list(ctx, pubkey);
pl_load_public_key_in_list(ctx, pubkey, NULL);
}
}
int main()
{
struct pl_keypair *tx_kp;
// struct pl_keypair *tx_kp;
struct pl_ctx *tx_ctx;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
char *rx_pubkey;
//char *rx_pubkey;
/* init */
tx_kp = NULL;
//tx_kp = NULL;
tx_ctx = pl_init();
/* load keypair */
......@@ -33,6 +33,7 @@ int main()
/*load public key of receiver*/
char *pubkey;
pubkey = pl_load_public_key("rx.pub");
pl_load_public_key_in_list(tx_ctx, pubkey,"test");
pl_print_public_keylist(tx_ctx);
pl_set_receiver(tx_ctx, pubkey);
......@@ -56,9 +57,9 @@ int main()
printf("SEND: unknown error \n");
break;
}
write(STDOUT_FILENO,tx_ctx->msg, sizeof(struct pl_pagermessage)); // dump to stdout
pl_cleanup(tx_ctx);
free(pubkey);
// write(STDOUT_FILENO,tx_ctx->msg, sizeof(struct pl_pagermessage)); // dump to stdout
// pl_cleanup(tx_ctx);
// free(pubkey);
////// RRRRXXXX /////////
return 0;
}
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "packets.h"
#include <stdlib.h>
#include "things.h"
#include "pagerlib.h"
#include <sys/stat.h>
#include <sys/types.h>
#ifndef ARDUINO
//#include "nacl/crypto_box.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#else
//#include "avrnacl.h"
#endif
......@@ -290,6 +291,41 @@ char * pl_load_public_key(char *filename)
#endif
}
int pl_load_public_keys_from_folder(struct pl_ctx *ctx, char const *folder)
{
printf("opening pubkey folder...\n");
struct dirent *dp;
DIR *dfd;
char *dir ;
if ((dfd = opendir(folder)) == NULL)
{
fprintf(stderr, "Can't open %s\n", dir);
return 0;
}
char filename_qfd[100] ;
char new_name_qfd[100] ;
while ((dp = readdir(dfd)) != NULL)
{
struct stat stbuf ;
sprintf( filename_qfd , "%s/%s",folder,dp->d_name) ;
stat(filename_qfd, &stbuf);
if(S_ISREG(stbuf.st_mode)){
printf("loading key: %s\n", filename_qfd);
pl_load_public_key_in_list(ctx, pl_load_public_key(filename_qfd), filename_qfd);
}
if( stat(filename_qfd,&stbuf ) == -1 )
{
printf("Unable to stat file: %s\n",filename_qfd) ;
continue ;
}
}
}
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key)
{
struct list_kp *list, *ni, *last;
......@@ -331,7 +367,7 @@ int pl_print_keylist(struct pl_ctx *ctx)
}
}
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key)
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key, char *alias)
{
struct list_public_key *list, *ni, *last, *listprev;
list = ctx->public_keys;
......@@ -339,6 +375,7 @@ int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key)
ni->id = compressed_point_to_addr(public_key);
ni->next = NULL;
memcpy(ni->public_key, public_key, crypto_box_PUBLICKEYBYTES);
memcpy(ni->alias, alias, 32);
if (list == NULL) {
ctx->public_keys = ni;
}
......@@ -365,11 +402,11 @@ int pl_print_public_keylist(struct pl_ctx *ctx)
{
struct list_public_key * list;
#ifndef ARDUINO
printf("the list of loaded public keys: \n ");
printf("currently loaded public keys: \n");
#endif
for(list = ctx->public_keys; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("address in list: %u \n", list->id );
printf("address:%u - alias: %s \n", list->id, list->alias);
#endif
}
return 0;
......@@ -522,7 +559,7 @@ int pl_inbox_display_all(struct pl_ctx *ctx)
}
}
pl_cleanup(struct pl_ctx *ctx)
int pl_cleanup(struct pl_ctx *ctx)
{
#ifndef ARDUINO
struct list_kp * list_kp;
......@@ -542,6 +579,6 @@ pl_cleanup(struct pl_ctx *ctx)
free(ctx->public_keys);
free(ctx->inbox);
free(ctx);
return 0;
#endif
}
......@@ -60,6 +60,7 @@ typedef struct list_public_key {
uint32_t id;
char public_key[crypto_box_PUBLICKEYBYTES];
bool is_group;
char alias[32];
struct list_public_key *prev;
struct list_public_key *next;
} list_public_key;
......@@ -110,8 +111,9 @@ int pl_save_public_key(char pubkey[crypto_box_PUBLICKEYBYTES], char *filename);
char *pl_load_public_key(char *filename);
int pl_print_keylist(struct pl_ctx *ctx);
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key);
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key);
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key, char *alias);
int pl_print_public_keylist(struct pl_ctx *ctx);
int pl_load_public_keys_from_folder(struct pl_ctx *ctx, char const *folder);
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg);
uint8_t compressed_point_to_addr( uint8_t input[]);
......@@ -119,7 +121,7 @@ int pl_inbox_display(struct pl_ctx *ctx);
int pl_inbox_next(struct pl_ctx *ctx);
int pl_inbox_prev(struct pl_ctx *ctx);
int pl_inbox_delete(struct pl_ctx *ctx);
pl_cleanup(struct pl_ctx *ctx);
int pl_cleanup(struct pl_ctx *ctx);
int is_zero( const char *data, int len );
#ifdef __cplusplus
......
......@@ -53,14 +53,6 @@ char* to_hex( char hex[], const char bin[], size_t length )
#ifdef ARDUINO
void uart_print(char* string)
{
for (int i = 0; i < strlen(string); i++){
while (( UCSR0A & (1<<UDRE0)) == 0){};
UDR0 = string[i];
}
}
void uart_init(void)
{
UBRR0H = UBRRH_VALUE;
......@@ -74,6 +66,22 @@ void uart_init(void)
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
void uart_write(char *string)
{
for (int i = 0; i < strlen(string); i++){
while (( UCSR0A & (1<<UDRE0)) == 0){};
UDR0 = string[i];
}
}
char uart_read(void)
{
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
void dump_buffer(unsigned int n, const unsigned char* buf)
{
// not implemented
......
......@@ -86,5 +86,5 @@ void uart_init(void);
/* uart_print:
* send a string to serial
*/
void uart_print(char* string);
void uart_write(char* string);
char uart_read(void);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment