diff --git a/include/patty/ax25.h b/include/patty/ax25.h index 4b58224..684709f 100644 --- a/include/patty/ax25.h +++ b/include/patty/ax25.h @@ -60,8 +60,7 @@ typedef struct _patty_ax25_sock { typedef struct _patty_ax25_event { enum patty_ax25_event_type type; - - int fd; + patty_ax25_sock * sock; } patty_ax25_event; struct _patty_ax25 { @@ -70,11 +69,12 @@ struct _patty_ax25 { int unix_fdmax; fd_set unix_fds; - fd_set unix_fds_pending_read; fd_set unix_fds_pending_write; fd_set unix_fds_pending_error; + patty_dict * unix_fd_lookup; + patty_dict * fd_lookup; int fd; int current; diff --git a/src/ax25.c b/src/ax25.c index 42ea112..42ec499 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -21,8 +21,15 @@ int patty_ax25_init(patty_ax25 *ax25) { goto error_dict_new_fd_lookup; } + if ((ax25->unix_fd_lookup = patty_dict_new()) == NULL) { + goto error_dict_new_unix_fd_lookup; + } + return 0; +error_dict_new_unix_fd_lookup: + patty_dict_destroy(ax25->fd_lookup); + error_dict_new_fd_lookup: patty_list_destroy(ax25->socks); @@ -40,6 +47,7 @@ static int destroy_if(patty_ax25_if *iface, void *ctx) { } void patty_ax25_stop(patty_ax25 *ax25) { + patty_dict_destroy(ax25->unix_fd_lookup); patty_dict_destroy(ax25->fd_lookup); patty_list_destroy(ax25->socks); @@ -188,7 +196,7 @@ error_fd_lookup: return -1; } -int patty_ax25_next_event(patty_ax25 *ax25, patty_ax25_event *ev) { +static int next_unix_fd_event(patty_ax25 *ax25, enum patty_ax25_event_type *type) { int fd; if (ax25->current == 0) { @@ -199,38 +207,34 @@ int patty_ax25_next_event(patty_ax25 *ax25, patty_ax25_event *ev) { if (select(ax25->unix_fdmax, &ax25->unix_fds_pending_read, &ax25->unix_fds_pending_write, - &ax25->unix_fds_pending_error, - NULL) < 0) { + &ax25->unix_fds_pending_error, NULL) < 0) { goto error_select; } } for (fd=ax25->current; fdunix_fdmax; fd++) { if (FD_ISSET(fd, &ax25->unix_fds_pending_read)) { - ev->type = PATTY_AX25_EVENT_RECV; - ev->fd = fd; + *type = PATTY_AX25_EVENT_RECV; FD_CLR(fd, &ax25->unix_fds_pending_read); - return 0; + return fd; } if (FD_ISSET(fd, &ax25->unix_fds_pending_write)) { - ev->type = PATTY_AX25_EVENT_SEND; - ev->fd = fd; + *type = PATTY_AX25_EVENT_SEND; FD_CLR(fd, &ax25->unix_fds_pending_write); - return 0; + return fd; } if (FD_ISSET(fd, &ax25->unix_fds_pending_error)) { - ev->type = PATTY_AX25_EVENT_ERROR; - ev->fd = fd; + *type = PATTY_AX25_EVENT_ERROR; FD_CLR(fd, &ax25->unix_fds_pending_error); - return 0; + return fd; } } diff --git a/src/if.c b/src/if.c index e65fb1c..af55fbb 100644 --- a/src/if.c +++ b/src/if.c @@ -246,6 +246,10 @@ int patty_ax25_add_if(patty_ax25 *ax25, patty_ax25_if *iface) { } } + if (patty_dict_set(ax25->unix_fd_lookup, &fd, sizeof(fd), iface) == NULL) { + goto error_dict_set; + } + if (patty_list_append(ax25->ifaces, iface) == NULL) { goto error_list_append; } @@ -253,6 +257,7 @@ int patty_ax25_add_if(patty_ax25 *ax25, patty_ax25_if *iface) { return 0; error_list_append: +error_dict_set: return -1; } @@ -280,10 +285,15 @@ int patty_ax25_delete_if(patty_ax25 *ax25, patty_ax25_if *iface) { if (ax25->unix_fdmax == fd) { ax25->unix_fdmax--; } + + if (patty_dict_delete(ax25->unix_fd_lookup, &fd, sizeof(fd)) == NULL) { + goto error_dict_delete; + } } return 0; +error_dict_delete: error_list_splice: return -1; }