struct rt2501buffer *rt2501_receive(void) { struct rt2501buffer *r; while(1) { disable_ohci_irq(); if(head == NULL) { enable_ohci_irq(); return NULL; } r = head; head = head->next; if(head == NULL) queue = NULL; enable_ohci_irq(); /* sprintf(dbg_buffer,"rt2501_receive: %x\r\n", r); DBG(dbg_buffer); */ if((r->length < LLC_LENGTH) || (memcmp(r->data, eapol_llc, LLC_LENGTH) != 0)) /* Not an EAPOL frame. Return it to the application. */ return r; /* EAPOL frame, process it. */ eapol_input(r->data, r->length); disable_ohci_irq(); hcd_free(r); enable_ohci_irq(); } }
static void hub_connect_async(hub_connect_data_t *hcd, struct in_addr *addr) { struct sockaddr_in saddr; saddr.sin_family = AF_INET; saddr.sin_port = htons(hcd->port); memcpy(&saddr.sin_addr, addr, sizeof(struct in_addr)); hcd->resolved_ip = strdup( inet_ntoa(saddr.sin_addr) ); xerr_t *err = 0; int rc = io_connect_async(&saddr, hub_connect_event, hcd, &err); if(rc != 0) { WARNING("Failed to connect to hub '%s': %s", hcd->address, xerr_msg(err)); ui_send_status_message(NULL, hcd->address, "Failed to connect to hub '%s': %s", hcd->address, xerr_msg(err)); xerr_free(err); hcd_free(hcd); } }
int hub_connect(const char *hubname, const char *nick, const char *email, const char *description, const char *speed, bool passive, const char *password, const char *encoding) { return_val_if_fail(nick, -1); return_val_if_fail(hubname, -1); char *host = 0; int port = 0; if(split_host_port(hubname, &host, &port) != 0) { return -1; } else if(port < 0) { ui_send_status_message(NULL, hubname, "Invalid port in hub address: %s", hubname); free(host); return -1; } else if(port == 0) { port = 411; /* default port */ } ui_send_status_message(NULL, hubname, "Connecting to %s...", hubname); hub_connect_data_t *hcd = calloc(1, sizeof(hub_connect_data_t)); hcd->nick = strdup(nick); hcd->email = xstrdup(email); hcd->description = xstrdup(description); hcd->speed = xstrdup(speed); hcd->address = strdup(hubname); hcd->passive = passive; hcd->password = xstrdup(password); hcd->encoding = xstrdup(encoding); hcd->port = port; struct in_addr xaddr; if(inet_aton(host, &xaddr)) { /* host already given as an IP address */ hub_connect_async(hcd, &xaddr); free(host); } else { int rc = evdns_resolve_ipv4(host, 0, hub_lookup_event, hcd); free(host); if(rc != DNS_ERR_NONE) { WARNING("Failed to lookup '%s': %s", hubname, evdns_err_to_string(rc)); ui_send_status_message(NULL, hubname, "Failed to lookup '%s': %s", hubname, evdns_err_to_string(rc)); hcd_free(hcd); return -1; } } return 0; }
static void hub_connect_event(int fd, int error, void *user_data) { hub_connect_data_t *hcd = user_data; if(error == 0) { ui_send_status_message(NULL, hcd->address, "Connected to hub %s", hcd->address); hub_t *hub = hub_find_by_address(hcd->address); if(hub) { /* found a hub with same address, check if it's a reconnection * attempt */ if(hub->reconnect_attempt > 0) { /* yes, remove the old one and create a new */ /* Reset kick counter after 1 minute. */ time_t now = time(0); if(hub->kick_counter && hub->kick_time + 60 < now) { hub->kick_counter = 0; } int kick_counter = 0; if(hub->reconnect_attempt == 1) { /* We got reconnected on the first attempt, this * might actually be a kick. */ kick_counter = hub->kick_counter + 1; } DEBUG("replacing reconnection hub"); hub_close_connection(hub); hub = hub_new_from_hcd(hcd); /* Restore the saved kick_counter. If we got * disconnected because we we're being banned or kicked, we * shouldn't keep hammering on the door. Also set the time * of the first kick. */ hub->kick_counter = kick_counter; if(hub->kick_counter == 1) hub->kick_time = time(0); } else { /* no, strange... discard the new hub connection */ ui_send_status_message(NULL, hcd->address, "Already connected to hub %s", hcd->address); close(fd); hub = NULL; } } else { hub = hub_new_from_hcd(hcd); } if(hub) { hub_attach_io_channel(hub, fd); user_set_ip(hub->me, extip_get(hub->fd, hub->hubip)); } } else { /* must be an error */ WARNING("connection failed: %s", strerror(error)); ui_send_status_message(NULL, hcd->address, "Failed to connect to hub %s: %s", hcd->address, strerror(error)); ui_send_connect_failed(NULL, hcd->address); close(fd); /* look for a reconnecting hub */ hub_t *hub = hub_find_by_address(hcd->address); if(hub && hub->reconnect_attempt > 0) { DEBUG("found reconnection hub with address [%s]", hub->address); hub_schedule_reconnect_event(hub); } } hcd_free(hcd); }