| 
									
										
										
										
											2020-06-25 20:37:12 -04:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <patty/hash.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <patty/ax25.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | patty_ax25_route *patty_ax25_route_new(patty_ax25_if *iface, | 
					
						
							|  |  |  |                                        const char *callsign, | 
					
						
							|  |  |  |                                        uint8_t ssid) { | 
					
						
							|  |  |  |     patty_ax25_route *route; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((route = malloc(sizeof(*route))) == NULL) { | 
					
						
							|  |  |  |         goto error_malloc_route; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memset(route, '\0', sizeof(*route)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (callsign) { | 
					
						
							|  |  |  |         if (patty_ax25_pton(callsign, ssid, &route->dest) < 0) { | 
					
						
							|  |  |  |             goto error_pton; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     route->iface = iface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return route; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_pton: | 
					
						
							|  |  |  |     free(route); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_malloc_route: | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | patty_ax25_route *patty_ax25_route_new_default(patty_ax25_if *iface) { | 
					
						
							|  |  |  |     return patty_ax25_route_new(iface, NULL, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | int patty_ax25_route_add_hop(patty_ax25_route *route, | 
					
						
							|  |  |  |                              const char *callsign, | 
					
						
							|  |  |  |                              uint8_t ssid) { | 
					
						
							|  |  |  |     if (route->nhops == PATTY_AX25_MAX_HOPS) { | 
					
						
							|  |  |  |         errno = ENOMEM; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         goto error_max_hops; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return patty_ax25_pton(callsign, ssid, &route->hops[route->nhops++]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_max_hops: | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | patty_ax25_route_table *patty_ax25_route_table_new() { | 
					
						
							|  |  |  |     return patty_dict_new(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 22:20:39 -04:00
										 |  |  | void patty_ax25_route_table_destroy(patty_ax25_route_table *table) { | 
					
						
							|  |  |  |     patty_dict_destroy(table); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 20:37:12 -04:00
										 |  |  | patty_ax25_route *patty_ax25_route_table_find(patty_ax25_route_table *table, | 
					
						
							|  |  |  |                                               patty_ax25_addr *dest) { | 
					
						
							|  |  |  |     uint32_t hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     patty_hash_init(&hash); | 
					
						
							|  |  |  |     patty_ax25_addr_hash(&hash, dest); | 
					
						
							|  |  |  |     patty_hash_end(&hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return patty_dict_get_with_hash(table, hash); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int patty_ax25_route_table_add(patty_ax25_route_table *table, | 
					
						
							|  |  |  |                                patty_ax25_route *route) { | 
					
						
							|  |  |  |     uint32_t hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     patty_hash_init(&hash); | 
					
						
							|  |  |  |     patty_ax25_addr_hash(&hash, &route->dest); | 
					
						
							|  |  |  |     patty_hash_end(&hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (patty_ax25_route_table_find(table, &route->dest) != NULL) { | 
					
						
							|  |  |  |         errno = EEXIST; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         goto error_exists; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (patty_dict_set_with_hash(table, | 
					
						
							|  |  |  |                                  &route->dest, | 
					
						
							|  |  |  |                                  sizeof(route->dest), | 
					
						
							|  |  |  |                                  route, | 
					
						
							|  |  |  |                                  hash) == NULL) { | 
					
						
							|  |  |  |         goto error_dict_set_with_hash; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_dict_set_with_hash: | 
					
						
							|  |  |  | error_exists: | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } |