static void forward_ethernet(struct session *session, DNDSMessage_t *msg) { uint8_t *frame; size_t frame_size; uint8_t macaddr_src[ETHER_ADDR_LEN]; uint8_t macaddr_dst[ETHER_ADDR_LEN]; uint8_t macaddr_dst_type; struct session *session_dst = NULL; struct session *session_src = NULL; struct session *session_list = NULL; if (session->state != SESSION_STATE_AUTHED) return; DNDSMessage_get_ethernet(msg, &frame, &frame_size); /* New mac address ? Add it to the lookup table */ inet_get_mac_addr_src(frame, macaddr_src); session_src = ftable_find(session->context->ftable, macaddr_src); if (session_src == NULL) { memcpy(session->mac_addr, macaddr_src, ETHER_ADDR_LEN); ftable_insert(session->context->ftable, macaddr_src, session); session_src = session; session_add_mac(session, macaddr_src); } /* Lookup the destination */ inet_get_mac_addr_dst(frame, macaddr_dst); macaddr_dst_type = inet_get_mac_addr_type(macaddr_dst); session_dst = ftable_find(session->context->ftable, macaddr_dst); if (session_src != NULL && session_dst != NULL && (session_src == session_dst)) { /* prevent loops */ return; } if (macaddr_dst_type == ADDR_MULTICAST) { /* Multicast is not supported yet */ return; } /* Switch forwarding */ if (macaddr_dst_type == ADDR_UNICAST /* The destination address is unicast */ && session_dst != NULL && session_dst->netc != NULL) { /* AND the session is up */ /*jlog(L_DEBUG, "forwarding the packet to [%s]", session_dst->ip);*/ net_send_msg(session_dst->netc, msg); int lnk_state = 0; lnk_state = linkst_joined(session_src->context->linkst, session_src->id, session_dst->id); if (lnk_state != 1) { p2pRequest(session_src, session_dst); linkst_join(session_src->context->linkst, session_src->id, session_dst->id); } /* Switch flooding */ } else if (macaddr_dst_type == ADDR_BROADCAST || /* This packet has to be broadcasted */ session_dst == NULL) { /* OR the fib session is down */ session_list = session->context->session_list; while (session_list != NULL) { net_send_msg(session_list->netc, msg); /*jlog(L_DEBUG, "flooding the packet to [%s]", session_list->ip);*/ session_list = session_list->next; } } else { jlog(L_WARNING, "unknown packet"); } }
int main() { int init_matrix_size = 1; linkst_t *linkst = NULL; /* there is 4 active nodee */ int idx_a, idx_b, idx_c, idx_d; idx_a = 1; idx_b = 2; idx_c = 3; idx_d = 100; linkst = linkst_new(100, 3); linkst_join(linkst, idx_a, idx_b); if (linkst_joined(linkst, idx_a, idx_b) != 1) { printf("%d // %d\n", idx_a, idx_b); goto out; } linkst_join(linkst, idx_a, idx_d); if (linkst_joined(linkst, idx_a, idx_d) != 1) { printf("%d // %d\n", idx_a, idx_d); goto out; } int state; int i = 4; while (i--) { state = linkst_joined(linkst, idx_a, idx_b); printf("state [%d] || [%d] %s [%d]\n", state, idx_a, state == 1 ? "<==>": "//", idx_b); state = linkst_joined(linkst, idx_a, idx_c); printf("state [%d] || [%d] %s [%d]\n", state, idx_a, state == 1 ? "<==>": "//", idx_c); printf("\n\n"); sleep(1); } printf("out of loop\n"); linkst_join(linkst, idx_a, idx_b); linkst_join(linkst, idx_a, idx_d); linkst_disjoin(linkst, idx_a); state = linkst_joined(linkst, idx_a, idx_b); printf("state [%d] || [%d] %s [%d]\n", state, idx_a, state == 1 ? "<==>": "//", idx_b); state = linkst_joined(linkst, idx_a, idx_c); printf("state [%d] || [%d] %s [%d]\n", state, idx_a, state == 1 ? "<==>": "//", idx_c); printf("\n\n"); out: linkst_free(linkst); }