/* Stop any network activity. */ void network_close(void) { /* If already closed, do nothing. */ if (network_mutex == NULL) return; /* Force-close the PCAP module. */ net_pcap_close(); /* Force-close the SLIRP module. */ net_slirp_close(); /* Close the network events. */ if (poll_data.wake_poll_thread != NULL) { thread_destroy_event(poll_data.wake_poll_thread); poll_data.wake_poll_thread = NULL; } if (poll_data.poll_complete != NULL) { thread_destroy_event(poll_data.poll_complete); poll_data.poll_complete = NULL; } /* Close the network thread mutex. */ thread_close_mutex(network_mutex); network_mutex = NULL; network_mac = NULL; network_log("NETWORK: closed.\n"); }
/* Handle the receiving of frames from the channel. */ static void poll_thread(void *arg) { uint8_t *mac = (uint8_t *)arg; const uint8_t *data = NULL; struct pcap_pkthdr h; uint32_t mac_cmp32[2]; uint16_t mac_cmp16[2]; event_t *evt; pclog("PCAP: polling thread started, arg %08lx\n", arg); /* Create a waitable event. */ evt = thread_create_event(); /* As long as the channel is open.. */ while (pcap != NULL) { /* Wait for the next packet to arrive. */ data = f_pcap_next(pcap, &h); if (data != NULL) { /* Received MAC. */ mac_cmp32[0] = *(uint32_t *)(data+6); mac_cmp16[0] = *(uint16_t *)(data+10); /* Local MAC. */ mac_cmp32[1] = *(uint32_t *)mac; mac_cmp16[1] = *(uint16_t *)(mac+4); if ((mac_cmp32[0] != mac_cmp32[1]) || (mac_cmp16[0] != mac_cmp16[1])) { if (poll_rx != NULL) poll_rx(poll_arg, (uint8_t *)data, h.caplen); } else { /* Mark as invalid packet. */ data = NULL; } } /* If we did not get anything, wait a while. */ if (data == NULL) thread_wait_event(evt, 10); } thread_destroy_event(evt); poll_tid = NULL; pclog("PCAP: polling stopped.\n"); }