#include #include #include #include #include int patty_ax25_init(patty_ax25 *ax25) { memset(ax25, '\0', sizeof(*ax25)); if ((ax25->ifaces = patty_list_new()) == NULL) { goto error_list_new_ifaces; } if ((ax25->ports = patty_list_new()) == NULL) { goto error_list_new_ports; } if ((ax25->links = patty_list_new()) == NULL) { goto error_list_new_links; } if ((ax25->fd_lookup = patty_dict_new()) == NULL) { goto error_dict_new_fd_lookup; } ax25->fd = 0; return 0; error_dict_new_fd_lookup: patty_list_destroy(ax25->links); error_list_new_links: patty_list_destroy(ax25->ports); error_list_new_ports: patty_list_destroy(ax25->ifaces); error_list_new_ifaces: return -1; } void patty_ax25_stop(patty_ax25 *ax25) { patty_dict_destroy(ax25->fd_lookup); patty_list_destroy(ax25->links); patty_list_destroy(ax25->ports); patty_list_destroy(ax25->ifaces); } 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; } 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; } int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ssid) { patty_ax25_port *port; patty_ax25_sock *sock; if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { goto error_dict_get; } /* * If there is already a port associated with this socket, then that's a * problem. */ if (sock->port != NULL) { errno = EBUSY; goto error_busy; } if ((port = patty_ax25_port_create(callsign, ssid)) == NULL) { goto error_port_create; } if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), port) == NULL) { goto error_dict_set; } sock->port = port; return ax25->fd++; error_dict_set: patty_ax25_port_destroy(port); error_port_create: error_busy: error_dict_get: return -1; }