patty/src/ax25.c

125 lines
2.5 KiB
C
Raw Normal View History

2015-07-21 21:49:53 +00:00
#include <stdio.h>
2015-07-20 23:21:45 -05:00
#include <stdlib.h>
2015-07-20 22:33:59 -05:00
#include <string.h>
2015-07-20 23:21:45 -05:00
#include <errno.h>
2015-07-20 22:33:59 -05:00
#include <patty/ax25.h>
int patty_ax25_init(patty_ax25 *ax25) {
memset(ax25, '\0', sizeof(*ax25));
2015-07-24 21:34:11 -05:00
if ((ax25->ifaces = patty_list_new()) == NULL) {
goto error_list_new_ifaces;
2015-07-20 22:33:59 -05:00
}
if ((ax25->socks = patty_list_new()) == NULL) {
goto error_list_new_socks;
2015-07-20 22:33:59 -05:00
}
2015-07-24 21:34:11 -05:00
if ((ax25->fd_lookup = patty_dict_new()) == NULL) {
goto error_dict_new_fd_lookup;
2015-07-21 21:49:53 +00:00
}
2015-07-24 21:34:11 -05:00
ax25->fd = 0;
2015-07-21 21:49:53 +00:00
2015-07-24 21:34:11 -05:00
return 0;
2015-07-21 21:49:53 +00:00
2015-07-24 21:34:11 -05:00
error_dict_new_fd_lookup:
patty_list_destroy(ax25->socks);
2015-07-21 21:49:53 +00:00
error_list_new_socks:
2015-07-24 21:34:11 -05:00
patty_list_destroy(ax25->ifaces);
2015-07-21 21:49:53 +00:00
2015-07-24 21:34:11 -05:00
error_list_new_ifaces:
return -1;
2015-07-21 21:49:53 +00:00
}
2015-07-21 23:01:49 -05:00
2015-07-24 21:34:11 -05:00
void patty_ax25_stop(patty_ax25 *ax25) {
patty_dict_destroy(ax25->fd_lookup);
patty_list_destroy(ax25->socks);
2015-07-24 21:34:11 -05:00
patty_list_destroy(ax25->ifaces);
2015-07-21 23:01:49 -05:00
}
2015-07-25 14:43:45 -05:00
int patty_ax25_open(patty_ax25 *ax25, const char *ifname) {
patty_ax25_if *iface;
if ((iface = patty_ax25_get_if(ax25, ifname)) == NULL) {
goto error_get_if;
}
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), iface) == NULL) {
goto error_dict_set;
}
return ax25->fd++;
error_dict_set:
error_get_if:
return -1;
}
2015-07-25 14:47:57 -05:00
int patty_ax25_socket(patty_ax25 *ax25) {
patty_ax25_sock *sock;
if ((sock = malloc(sizeof(*sock))) == NULL) {
goto error_malloc_sock;
}
memset(sock, '\0', sizeof(*sock));
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), sock) == NULL) {
goto error_dict_set;
}
return ax25->fd++;
error_dict_set:
free(sock);
error_malloc_sock:
return -1;
}
2015-07-25 19:39:35 -05:00
int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
patty_ax25_sock *sock;
patty_ax25_address *addr;
2015-07-25 19:39:35 -05:00
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) {
goto error_dict_get;
}
/*
* If there is already a local address associated with this socket, then
* that's a problem.
2015-07-25 19:39:35 -05:00
*/
if (sock->local != NULL) {
errno = EEXIST;
2015-07-25 19:39:35 -05:00
goto error_exists;
2015-07-25 19:39:35 -05:00
}
if ((addr = malloc(sizeof(*addr))) == NULL) {
goto error_malloc_addr;
2015-07-25 19:39:35 -05:00
}
strncpy(addr->callsign, callsign, sizeof(addr->callsign));
2015-07-25 19:39:35 -05:00
addr->ssid = ssid;
addr->last = 0;
addr->c = 0;
2015-07-25 19:39:35 -05:00
return 0;
2015-07-25 19:39:35 -05:00
error_malloc_addr:
error_exists:
2015-07-25 19:39:35 -05:00
error_dict_get:
return -1;
}
int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
/*
* Stub
*/
return 0;
}