I think that's much nicer

This commit is contained in:
XANTRONIX Development 2015-07-28 02:16:56 +00:00 committed by XANTRONIX Industrial
parent e8ddd504d7
commit 6adb4f098b
9 changed files with 109 additions and 88 deletions

View file

@ -23,8 +23,14 @@ enum patty_ax25_event {
PATTY_AX25_IO_WRITE 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 { typedef struct _patty_ax25_sock {
enum patty_ax25_obj_type type; enum patty_ax25_sock_status status;
patty_ax25_if * iface; patty_ax25_if * iface;
patty_ax25_address * local; patty_ax25_address * local;

View file

@ -4,6 +4,9 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#define PATTY_AX25_ADDRESS_CALLSIGN_LEN 6
#define PATTY_AX25_ADDRESS_SIZE 7
typedef struct _patty_ax25_address { typedef struct _patty_ax25_address {
char callsign[7]; char callsign[7];

View file

@ -6,14 +6,6 @@
#include <patty/list.h> #include <patty/list.h>
#include <patty/dict.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 { typedef struct _patty_ax25_stats {
size_t rx; size_t rx;
size_t tx; size_t tx;

View file

@ -21,9 +21,8 @@ enum patty_ax25_if_type {
}; };
typedef struct _patty_ax25_if { typedef struct _patty_ax25_if {
enum patty_ax25_obj_type type; enum patty_ax25_if_type type;
enum patty_ax25_if_type type_if; patty_ax25_stats stats;
patty_ax25_stats stats;
char name[8]; char name[8];

View file

@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -34,12 +33,46 @@ error_list_new_ifaces:
return -1; 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) { void patty_ax25_stop(patty_ax25 *ax25) {
patty_dict_destroy(ax25->fd_lookup); patty_dict_destroy(ax25->fd_lookup);
patty_list_destroy(ax25->socks); patty_list_destroy(ax25->socks);
patty_ax25_each_if(ax25, destroy_if, NULL);
patty_list_destroy(ax25->ifaces); 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) { int patty_ax25_open(patty_ax25 *ax25, const char *ifname) {
patty_ax25_if *iface; patty_ax25_if *iface;
@ -47,19 +80,15 @@ int patty_ax25_open(patty_ax25 *ax25, const char *ifname) {
goto error_get_if; goto error_get_if;
} }
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), iface) == NULL) { return fd_store(ax25, iface);
goto error_dict_set;
}
return ax25->fd++;
error_dict_set:
error_get_if: error_get_if:
return -1; return -1;
} }
int patty_ax25_socket(patty_ax25 *ax25) { int patty_ax25_socket(patty_ax25 *ax25) {
patty_ax25_sock *sock; patty_ax25_sock *sock;
int fd;
if ((sock = malloc(sizeof(*sock))) == NULL) { if ((sock = malloc(sizeof(*sock))) == NULL) {
goto error_malloc_sock; goto error_malloc_sock;
@ -67,13 +96,15 @@ int patty_ax25_socket(patty_ax25 *ax25) {
memset(sock, '\0', sizeof(*sock)); memset(sock, '\0', sizeof(*sock));
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), sock) == NULL) { sock->status = PATTY_AX25_SOCK_OPEN;
goto error_dict_set;
if ((fd = fd_store(ax25, sock)) < 0) {
goto error_fd_store;
} }
return ax25->fd++; return fd;
error_dict_set: error_fd_store:
free(sock); free(sock);
error_malloc_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) { int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
patty_ax25_sock *sock; patty_ax25_sock *sock;
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { if ((sock = fd_lookup(ax25, socket)) == NULL) {
errno = EBADF; goto error_fd_lookup;
goto error_dict_get;
} }
/* /*
@ -107,7 +136,7 @@ int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid
error_address_create: error_address_create:
error_exists: error_exists:
error_dict_get: error_fd_lookup:
return -1; 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) { int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
patty_ax25_sock *sock; patty_ax25_sock *sock;
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { if ((sock = fd_lookup(ax25, socket)) == NULL) {
errno = EBADF; goto error_fd_lookup;
goto error_dict_get;
} }
if (sock->remote != NULL) { 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_address_create:
error_exists: error_exists:
error_dict_get: error_fd_lookup:
return -1; return -1;
} }

View file

@ -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 * First, unpack each callsign octet, ensuring all are within the 7 bit
* ASCII space and do not have the extended bit set to 1. * 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 b = ((uint8_t *)data + offset)[i];
uint8_t c = b >> 1; 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->ssid = PATTY_AX25_ADDRESS_SSID_NUMBER(ssid);
address->last = PATTY_AX25_ADDRESS_OCTET_LAST(ssid); address->last = PATTY_AX25_ADDRESS_OCTET_LAST(ssid);
return 7; return PATTY_AX25_ADDRESS_SIZE;
error: error:
return -1; return -1;

View file

@ -4,7 +4,7 @@
#include <patty/hash.h> #include <patty/hash.h>
uint32_t patty_hash(void *key, size_t size) { 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++) { for (i=0; i<size; i++) {
hash += ((uint8_t *)key)[i]; hash += ((uint8_t *)key)[i];

View file

@ -21,7 +21,7 @@ static patty_ax25_if *create_tnc(const char *device) {
goto error_kiss_tnc_open; 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++); 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; goto error;
} }
iface->type = PATTY_AX25_OBJ_IF;
return iface; return iface;
error: error:

View file

@ -22,72 +22,68 @@ static void usage(int argc, char **argv, const char *message, ...) {
exit(1); exit(1);
} }
static int callsign_fprint(FILE *stream, const patty_ax25_address *address) { int address_callback(patty_ax25_address *address, void *ctx) {
fprintf(stream, "%s", address->callsign); return printf(" %s/%d\n", address->callsign, address->ssid);
if (address->ssid) {
fprintf(stream, "/%d", address->ssid);
}
return 0;
} }
static int address_fprint(FILE *stream, const patty_ax25_frame *frame) { int iface_callback(patty_ax25_if *iface, void *ctx) {
int i; printf("%s:\n", iface->name);
callsign_fprint(stream, &frame->src); return patty_ax25_if_each_address(iface, address_callback, ctx);
fprintf(stream, " > ");
callsign_fprint(stream, &frame->dest);
for (i=0; i<frame->hops; i++) {
fprintf(stream, " < ");
callsign_fprint(stream, &frame->repeaters[i]);
}
return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
patty_kiss_tnc *tnc; patty_ax25 ax25;
ssize_t len; patty_ax25_if *iface;
void *data; int fd;
int port;
if (argc != 2) { if (argc != 2) {
usage(argc, argv, "No TNC device provided"); usage(argc, argv, "No TNC device provided");
} }
if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) { if (patty_ax25_init(&ax25) < 0) {
perror("Unable to open TNC"); perror("Unable to initialize AX.25 stack");
exit(127); exit(127);
} }
while ((len = patty_kiss_tnc_recv(tnc, &data, &port)) > 0) { if ((iface = patty_ax25_if_create(PATTY_AX25_IF_KISS_TNC_TTY, argv[1])) == NULL) {
patty_ax25_frame frame; perror("Unable to create interface");
if (patty_ax25_frame_decode(&frame, data, len) < 0) { exit(127);
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);
} }
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; return 0;
} }