I think that's much nicer
This commit is contained in:
parent
e8ddd504d7
commit
6adb4f098b
9 changed files with 109 additions and 88 deletions
|
@ -23,8 +23,14 @@ enum patty_ax25_event {
|
|||
PATTY_AX25_IO_WRITE
|
||||
};
|
||||
|
||||
enum patty_ax25_sock_status {
|
||||
PATTY_AX25_SOCK_OPEN,
|
||||
PATTY_AX25_SOCK_LISTENING,
|
||||
PATTY_AX25_SOCK_ESTABLISHED
|
||||
};
|
||||
|
||||
typedef struct _patty_ax25_sock {
|
||||
enum patty_ax25_obj_type type;
|
||||
enum patty_ax25_sock_status status;
|
||||
|
||||
patty_ax25_if * iface;
|
||||
patty_ax25_address * local;
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define PATTY_AX25_ADDRESS_CALLSIGN_LEN 6
|
||||
#define PATTY_AX25_ADDRESS_SIZE 7
|
||||
|
||||
typedef struct _patty_ax25_address {
|
||||
char callsign[7];
|
||||
|
||||
|
|
|
@ -6,14 +6,6 @@
|
|||
#include <patty/list.h>
|
||||
#include <patty/dict.h>
|
||||
|
||||
enum patty_ax25_obj_type {
|
||||
PATTY_AX25_OBJ_UNKNOWN,
|
||||
PATTY_AX25_OBJ_IF,
|
||||
PATTY_AX25_OBJ_SOCKET,
|
||||
PATTY_AX25_OBJ_PORT,
|
||||
PATTY_AX25_OBJ_LINK
|
||||
};
|
||||
|
||||
typedef struct _patty_ax25_stats {
|
||||
size_t rx;
|
||||
size_t tx;
|
||||
|
|
|
@ -21,9 +21,8 @@ enum patty_ax25_if_type {
|
|||
};
|
||||
|
||||
typedef struct _patty_ax25_if {
|
||||
enum patty_ax25_obj_type type;
|
||||
enum patty_ax25_if_type type_if;
|
||||
patty_ax25_stats stats;
|
||||
enum patty_ax25_if_type type;
|
||||
patty_ax25_stats stats;
|
||||
|
||||
char name[8];
|
||||
|
||||
|
|
69
src/ax25.c
69
src/ax25.c
|
@ -1,4 +1,3 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
@ -34,12 +33,46 @@ error_list_new_ifaces:
|
|||
return -1;
|
||||
}
|
||||
|
||||
static int destroy_if(patty_ax25_if *iface, void *ctx) {
|
||||
patty_ax25_if_destroy(iface);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patty_ax25_stop(patty_ax25 *ax25) {
|
||||
patty_dict_destroy(ax25->fd_lookup);
|
||||
patty_list_destroy(ax25->socks);
|
||||
|
||||
patty_ax25_each_if(ax25, destroy_if, NULL);
|
||||
patty_list_destroy(ax25->ifaces);
|
||||
}
|
||||
|
||||
static void *fd_lookup(patty_ax25 *ax25, int fd) {
|
||||
void *obj;
|
||||
|
||||
if ((obj = patty_dict_get(ax25->fd_lookup, &fd, sizeof(fd))) == NULL) {
|
||||
errno = EBADF;
|
||||
|
||||
goto error_dict_get;
|
||||
}
|
||||
|
||||
return obj;
|
||||
|
||||
error_dict_get:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int fd_store(patty_ax25 *ax25, void *obj) {
|
||||
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), obj) == NULL) {
|
||||
goto error_dict_set;
|
||||
}
|
||||
|
||||
return ax25->fd++;
|
||||
|
||||
error_dict_set:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int patty_ax25_open(patty_ax25 *ax25, const char *ifname) {
|
||||
patty_ax25_if *iface;
|
||||
|
||||
|
@ -47,19 +80,15 @@ int patty_ax25_open(patty_ax25 *ax25, const char *ifname) {
|
|||
goto error_get_if;
|
||||
}
|
||||
|
||||
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), iface) == NULL) {
|
||||
goto error_dict_set;
|
||||
}
|
||||
return fd_store(ax25, iface);
|
||||
|
||||
return ax25->fd++;
|
||||
|
||||
error_dict_set:
|
||||
error_get_if:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int patty_ax25_socket(patty_ax25 *ax25) {
|
||||
patty_ax25_sock *sock;
|
||||
int fd;
|
||||
|
||||
if ((sock = malloc(sizeof(*sock))) == NULL) {
|
||||
goto error_malloc_sock;
|
||||
|
@ -67,13 +96,15 @@ int patty_ax25_socket(patty_ax25 *ax25) {
|
|||
|
||||
memset(sock, '\0', sizeof(*sock));
|
||||
|
||||
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), sock) == NULL) {
|
||||
goto error_dict_set;
|
||||
sock->status = PATTY_AX25_SOCK_OPEN;
|
||||
|
||||
if ((fd = fd_store(ax25, sock)) < 0) {
|
||||
goto error_fd_store;
|
||||
}
|
||||
|
||||
return ax25->fd++;
|
||||
return fd;
|
||||
|
||||
error_dict_set:
|
||||
error_fd_store:
|
||||
free(sock);
|
||||
|
||||
error_malloc_sock:
|
||||
|
@ -83,10 +114,8 @@ error_malloc_sock:
|
|||
int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
|
||||
patty_ax25_sock *sock;
|
||||
|
||||
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) {
|
||||
errno = EBADF;
|
||||
|
||||
goto error_dict_get;
|
||||
if ((sock = fd_lookup(ax25, socket)) == NULL) {
|
||||
goto error_fd_lookup;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -107,7 +136,7 @@ int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid
|
|||
|
||||
error_address_create:
|
||||
error_exists:
|
||||
error_dict_get:
|
||||
error_fd_lookup:
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -121,10 +150,8 @@ int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ss
|
|||
int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
|
||||
patty_ax25_sock *sock;
|
||||
|
||||
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) {
|
||||
errno = EBADF;
|
||||
|
||||
goto error_dict_get;
|
||||
if ((sock = fd_lookup(ax25, socket)) == NULL) {
|
||||
goto error_fd_lookup;
|
||||
}
|
||||
|
||||
if (sock->remote != NULL) {
|
||||
|
@ -141,6 +168,6 @@ int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int s
|
|||
|
||||
error_address_create:
|
||||
error_exists:
|
||||
error_dict_get:
|
||||
error_fd_lookup:
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ static ssize_t frame_decode_station(patty_ax25_address *address, void *data, off
|
|||
* First, unpack each callsign octet, ensuring all are within the 7 bit
|
||||
* ASCII space and do not have the extended bit set to 1.
|
||||
*/
|
||||
for (i=0; i<6; i++) {
|
||||
for (i=0; i<PATTY_AX25_ADDRESS_CALLSIGN_LEN; i++) {
|
||||
uint8_t b = ((uint8_t *)data + offset)[i];
|
||||
uint8_t c = b >> 1;
|
||||
|
||||
|
@ -53,7 +53,7 @@ static ssize_t frame_decode_station(patty_ax25_address *address, void *data, off
|
|||
address->ssid = PATTY_AX25_ADDRESS_SSID_NUMBER(ssid);
|
||||
address->last = PATTY_AX25_ADDRESS_OCTET_LAST(ssid);
|
||||
|
||||
return 7;
|
||||
return PATTY_AX25_ADDRESS_SIZE;
|
||||
|
||||
error:
|
||||
return -1;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <patty/hash.h>
|
||||
|
||||
uint32_t patty_hash(void *key, size_t size) {
|
||||
uint32_t hash = 0, i;
|
||||
uint32_t hash = 0xffffffdf, i;
|
||||
|
||||
for (i=0; i<size; i++) {
|
||||
hash += ((uint8_t *)key)[i];
|
||||
|
|
4
src/if.c
4
src/if.c
|
@ -21,7 +21,7 @@ static patty_ax25_if *create_tnc(const char *device) {
|
|||
goto error_kiss_tnc_open;
|
||||
}
|
||||
|
||||
iface->type_if = PATTY_AX25_IF_KISS_TNC_TTY;
|
||||
iface->type = PATTY_AX25_IF_KISS_TNC_TTY;
|
||||
|
||||
snprintf(iface->name, sizeof(iface->name), "%s%d", prefix, number++);
|
||||
|
||||
|
@ -63,8 +63,6 @@ patty_ax25_if *patty_ax25_if_create(int opts, void *info) {
|
|||
goto error;
|
||||
}
|
||||
|
||||
iface->type = PATTY_AX25_OBJ_IF;
|
||||
|
||||
return iface;
|
||||
|
||||
error:
|
||||
|
|
94
src/test.c
94
src/test.c
|
@ -22,72 +22,68 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
static int callsign_fprint(FILE *stream, const patty_ax25_address *address) {
|
||||
fprintf(stream, "%s", address->callsign);
|
||||
|
||||
if (address->ssid) {
|
||||
fprintf(stream, "/%d", address->ssid);
|
||||
}
|
||||
|
||||
return 0;
|
||||
int address_callback(patty_ax25_address *address, void *ctx) {
|
||||
return printf(" %s/%d\n", address->callsign, address->ssid);
|
||||
}
|
||||
|
||||
static int address_fprint(FILE *stream, const patty_ax25_frame *frame) {
|
||||
int i;
|
||||
int iface_callback(patty_ax25_if *iface, void *ctx) {
|
||||
printf("%s:\n", iface->name);
|
||||
|
||||
callsign_fprint(stream, &frame->src);
|
||||
fprintf(stream, " > ");
|
||||
callsign_fprint(stream, &frame->dest);
|
||||
|
||||
for (i=0; i<frame->hops; i++) {
|
||||
fprintf(stream, " < ");
|
||||
callsign_fprint(stream, &frame->repeaters[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return patty_ax25_if_each_address(iface, address_callback, ctx);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
patty_kiss_tnc *tnc;
|
||||
ssize_t len;
|
||||
void *data;
|
||||
int port;
|
||||
|
||||
patty_ax25 ax25;
|
||||
patty_ax25_if *iface;
|
||||
int fd;
|
||||
|
||||
if (argc != 2) {
|
||||
usage(argc, argv, "No TNC device provided");
|
||||
}
|
||||
|
||||
if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) {
|
||||
perror("Unable to open TNC");
|
||||
if (patty_ax25_init(&ax25) < 0) {
|
||||
perror("Unable to initialize AX.25 stack");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
while ((len = patty_kiss_tnc_recv(tnc, &data, &port)) > 0) {
|
||||
patty_ax25_frame frame;
|
||||
if ((iface = patty_ax25_if_create(PATTY_AX25_IF_KISS_TNC_TTY, argv[1])) == NULL) {
|
||||
perror("Unable to create interface");
|
||||
|
||||
if (patty_ax25_frame_decode(&frame, data, len) < 0) {
|
||||
perror("Unable to decode frame");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
address_fprint(stderr, &frame);
|
||||
|
||||
if (frame.type == PATTY_AX25_FRAME_INFO) {
|
||||
fprintf(stderr, " poll %d", PATTY_AX25_CONTROL_POLL(frame.control));
|
||||
fprintf(stderr, " ns %d", PATTY_AX25_CONTROL_SEQ_SEND(frame.control));
|
||||
fprintf(stderr, " nr %d", PATTY_AX25_CONTROL_SEQ_RECV(frame.control));
|
||||
}
|
||||
|
||||
if (frame.payload) {
|
||||
fprintf(stderr, " proto %02x", frame.proto);
|
||||
}
|
||||
|
||||
fprintf(stderr, " %ld bytes\n", len);
|
||||
exit(127);
|
||||
}
|
||||
|
||||
patty_kiss_tnc_close(tnc);
|
||||
if (patty_ax25_if_add_address(iface, "WX3RKR", 0) < 0) {
|
||||
perror("Unable to add address");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
if (patty_ax25_add_if(&ax25, iface) < 0) {
|
||||
perror("Unable to add interface");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
if ((fd = patty_ax25_socket(&ax25)) < 0) {
|
||||
perror("Unable to create new socket");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
if (patty_ax25_bind(&ax25, fd, "WX3RKR", 0) < 0) {
|
||||
perror("Unable to bind socket to local address");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
if (patty_ax25_connect(&ax25, fd, "N5TX", 0) < 0) {
|
||||
perror("Unable to connect");
|
||||
|
||||
exit(127);
|
||||
}
|
||||
|
||||
patty_ax25_stop(&ax25);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue