Okay, current state of the addrbook. And it is... Segmentation fault! xD
This commit is contained in:
parent
c8d69f23bc
commit
c468363efb
2
Makefile
2
Makefile
@ -12,7 +12,7 @@ OBJ_DIR := obj
|
||||
SRCs := $(wildcard $(SRC_DIR)/*.c)
|
||||
OBJs := $(SRCs:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
|
||||
|
||||
CFLAGS = --std=c99 -D_POSIX_C_SOURCE=200112L -O3 -Wall -Werror -Wextra -pedantic
|
||||
CFLAGS = --std=c99 -D_POSIX_C_SOURCE=200809L -O3 -Wall -Werror -Wextra -pedantic
|
||||
CFLAGS += -flto
|
||||
LDFLAGS += -lportaudio -lopus -lsodium
|
||||
|
||||
|
112
src/addrbook.c
Normal file
112
src/addrbook.c
Normal file
@ -0,0 +1,112 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "addrbook.h"
|
||||
#include "crypt.h"
|
||||
|
||||
#define ADDRBOOK_DELIM_COUNT 4
|
||||
|
||||
char *addrbook_entry_to_string(const addrbook_entry_t *const ent) {
|
||||
size_t str_len = CRYPT_PKEY_HEXLEN + ent->name_len + ent->last_addr_len
|
||||
+ ent->last_port_len + ADDRBOOK_DELIM_COUNT;
|
||||
char *str = (char *)malloc(str_len * sizeof(char));
|
||||
sprintf(str, "%.128s\t%.*s\t%.*s\t%.*s\n", ent->pubk, ent->name_len, ent->name,
|
||||
ent->last_addr_len, ent->last_addr, ent->last_port_len, ent->last_port);
|
||||
return str;
|
||||
}
|
||||
|
||||
int addrbook_entry_from_string(addrbook_entry_t *const ent, char * str) {
|
||||
char *tok = strtok(str, "\t\n");
|
||||
if (tok == NULL)
|
||||
return -1;
|
||||
memcpy(ent->pubk, tok, CRYPT_PKEY_HEXLEN);
|
||||
|
||||
tok = strtok(NULL, "\t\n");
|
||||
ent->name = strdup(tok);
|
||||
ent->name_len = strlen(ent->name);
|
||||
|
||||
tok = strtok(NULL, "\t\n");
|
||||
ent->last_addr = strdup(tok);
|
||||
ent->last_addr_len = strlen(ent->last_addr);
|
||||
|
||||
tok = strtok(NULL, "\t\n");
|
||||
ent->last_port = strdup(tok);
|
||||
ent->last_port_len = strlen(ent->last_port);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int addrbook_init(addrbook_t *const ab) {
|
||||
ab->entries = (addrbook_entry_t *)malloc(sizeof(addrbook_entry_t));
|
||||
if (ab->entries == NULL)
|
||||
return -1;
|
||||
ab->entries_len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void addrbook_destroy(addrbook_t *const ab) {
|
||||
free(ab->entries);
|
||||
ab->entries_len = 0;
|
||||
}
|
||||
|
||||
int addrbook_load(addrbook_t *ab, FILE *in) {
|
||||
char *line = NULL;
|
||||
size_t line_len = 0;
|
||||
size_t lines = 0;
|
||||
|
||||
while (getline(&line, &line_len, in) != -1)
|
||||
++lines;
|
||||
|
||||
if (ab->entries != NULL)
|
||||
free(ab->entries);
|
||||
ab->entries = (addrbook_entry_t *)malloc(lines * sizeof(addrbook_entry_t));
|
||||
if (ab->entries == NULL)
|
||||
return -1;
|
||||
ab->entries_len = 0;
|
||||
|
||||
fseek(in, 0, SEEK_SET);
|
||||
|
||||
addrbook_entry_t ent;
|
||||
|
||||
while (getline(&line, &line_len, in) != -1) {
|
||||
if (addrbook_entry_from_string(&ent, line) == -1) {
|
||||
addrbook_destroy(ab);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (addrbook_add(ab, &ent) == -1) {
|
||||
addrbook_destroy(ab);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int addrbook_store(addrbook_t *ab, FILE *out) {
|
||||
for (size_t i = 0; i < ab->entries_len; ++i) {
|
||||
char *ent_str = addrbook_entry_to_string(&ab->entries[i]);
|
||||
fputs(ent_str, out);
|
||||
free(ent_str);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent) {
|
||||
if (addrbook_get(ab, ent->pubk) != NULL)
|
||||
return -1;
|
||||
if (ab->entries_len > 0)
|
||||
ab->entries = realloc(ab->entries, ++ab->entries_len);
|
||||
else
|
||||
++ab->entries_len;
|
||||
ab->entries[ab->entries_len-1] = *ent;
|
||||
return 0;
|
||||
}
|
||||
|
||||
addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]) {
|
||||
for (size_t i = 0; i < ab->entries_len; ++i)
|
||||
if (strncmp(ab->entries[i].pubk, pkey, CRYPT_PKEY_HEXLEN) == 0)
|
||||
return &ab->entries[i];
|
||||
return NULL;
|
||||
}
|
37
src/addrbook.h
Normal file
37
src/addrbook.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef _ADDRBOOK_H_
|
||||
#define _ADDRBOOK_H_
|
||||
|
||||
#include "crypt.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define ADDRBOOK_NAME_MAXLEN 64
|
||||
#define ADDRBOOK_PORT_MAXLEN 5
|
||||
|
||||
typedef struct addrbook_entry_t {
|
||||
char pubk[CRYPT_PKEY_HEXLEN];
|
||||
char *name;
|
||||
char *last_addr;
|
||||
char *last_port;
|
||||
unsigned char name_len;
|
||||
unsigned char last_addr_len;
|
||||
unsigned char last_port_len;
|
||||
} addrbook_entry_t;
|
||||
|
||||
char *addrbook_entry_to_string(const addrbook_entry_t *const ent);
|
||||
int addrbook_entry_from_string(addrbook_entry_t *const ent, char * str);
|
||||
|
||||
typedef struct addrbook_t {
|
||||
addrbook_entry_t *entries;
|
||||
size_t entries_len;
|
||||
} addrbook_t;
|
||||
|
||||
int addrbook_init(addrbook_t *const ab);
|
||||
void addrbook_destroy(addrbook_t *const ab);
|
||||
|
||||
int addrbook_load(addrbook_t *ab, FILE *in);
|
||||
int addrbook_store(addrbook_t *ab, FILE *out);
|
||||
|
||||
int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent);
|
||||
addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]);
|
||||
|
||||
#endif /* _ADDRBOOK_H_ */
|
19
src/main.c
19
src/main.c
@ -1,6 +1,9 @@
|
||||
// #include "audio.h"
|
||||
#include "addrbook.h"
|
||||
#include "crypt.h"
|
||||
#include "net.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CHANNELS 1
|
||||
#define SAMPLE_RATE 48000
|
||||
@ -17,6 +20,21 @@ void usage(void) {
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
addrbook_t ab = {0};
|
||||
|
||||
FILE *in = fopen("addressbook_key", "r");
|
||||
|
||||
addrbook_load(&ab, in);
|
||||
|
||||
for (size_t i = 0; i < ab.entries_len; ++i)
|
||||
printf("%ld: %.128s %s %s %s\n", i, ab.entries[i].pubk, ab.entries[i].name, ab.entries[i].last_addr, ab.entries[i].last_port);
|
||||
|
||||
fclose(in);
|
||||
addrbook_destroy(&ab);
|
||||
|
||||
return 0;
|
||||
|
||||
if (argc < 4) {
|
||||
usage();
|
||||
return -1;
|
||||
@ -66,6 +84,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
cleanup:
|
||||
|
||||
addrbook_destroy(&ab);
|
||||
net_destroy(&n);
|
||||
crypt_session_destroy(&s);
|
||||
crypt_key_destroy(&ok);
|
||||
|
Loading…
Reference in New Issue
Block a user