static void process(bl_t bl) { struct sockaddr_storage rss; socklen_t rsl; char rbuf[BUFSIZ]; bl_info_t *bi; struct conf c; struct dbinfo dbi; struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { (*lfun)(LOG_ERR, "clock_gettime failed (%m)"); return; } if ((bi = bl_recv(bl)) == NULL) { (*lfun)(LOG_ERR, "no message (%m)"); return; } if (getremoteaddress(bi, &rss, &rsl) == -1) goto out; if (debug) { sockaddr_snprintf(rbuf, sizeof(rbuf), "%a:%p", (void *)&rss); (*lfun)(LOG_DEBUG, "processing type=%d fd=%d remote=%s msg=%s" " uid=%lu gid=%lu", bi->bi_type, bi->bi_fd, rbuf, bi->bi_msg, (unsigned long)bi->bi_uid, (unsigned long)bi->bi_gid); } if (conf_find(bi->bi_fd, bi->bi_uid, &rss, &c) == NULL) { (*lfun)(LOG_DEBUG, "no rule matched"); goto out; } if (state_get(state, &c, &dbi) == -1) goto out; if (debug) { char b1[128], b2[128]; (*lfun)(LOG_DEBUG, "%s: db state info for %s: count=%d/%d " "last=%s now=%s", __func__, rbuf, dbi.count, c.c_nfail, fmttime(b1, sizeof(b1), dbi.last), fmttime(b2, sizeof(b2), ts.tv_sec)); } switch (bi->bi_type) { case BL_ADD: dbi.count++; dbi.last = ts.tv_sec; if (dbi.id[0]) { /* * We should not be getting this since the rule * should have blocked the address. A possible * explanation is that someone removed that rule, * and another would be that we got another attempt * before we added the rule. In anycase, we remove * and re-add the rule because we don't want to add * it twice, because then we'd lose track of it. */ (*lfun)(LOG_DEBUG, "rule exists %s", dbi.id); (void)run_change("rem", &c, dbi.id, 0); dbi.id[0] = '\0'; } if (c.c_nfail != -1 && dbi.count >= c.c_nfail) { int res = run_change("add", &c, dbi.id, sizeof(dbi.id)); if (res == -1) goto out; sockaddr_snprintf(rbuf, sizeof(rbuf), "%a", (void *)&rss); (*lfun)(LOG_INFO, "blocked %s/%d:%d for %d seconds", rbuf, c.c_lmask, c.c_port, c.c_duration); } break; case BL_DELETE: if (dbi.last == 0) goto out; dbi.last = 0; break; default: (*lfun)(LOG_ERR, "unknown message %d", bi->bi_type); } if (state_put(state, &c, &dbi) == -1) goto out; out: close(bi->bi_fd); }
int hu_usb_start (byte ep_in_addr, byte ep_out_addr) { int ret = 0; if (iusb_state == hu_STATE_STARTED) { logd ("CHECK: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (0); } //#include <sys/prctl.h> //ret = prctl (PR_GET_DUMPABLE, 1, 0, 0, 0); // 1 = SUID_DUMP_USER //logd ("prctl ret: %d", ret); iusb_state = hu_STATE_STARTIN; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); iusb_best_vendor = 0; int tries = 0; while (iusb_best_vendor != USB_VID_GOO && tries ++ < 4) { //2000) { ret = iusb_init (ep_in_addr, ep_out_addr); if (ret < 0) { loge ("Error iusb_init"); iusb_deinit (); iusb_state = hu_STATE_STOPPED; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (-1); } logd ("OK iusb_init"); #if 0 if (iusb_best_vendor == USB_VID_GOO) { logd ("Already OAP/AA mode, no need to call iusb_oap_start()"); iusb_state = hu_STATE_STARTED; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (0); } #endif ret = iusb_oap_start (); if (ret < 0) { loge ("Error iusb_oap_start"); iusb_deinit (); iusb_state = hu_STATE_STOPPED; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (-2); } logd ("OK iusb_oap_start"); if (iusb_best_vendor != USB_VID_GOO) { iusb_deinit (); ms_sleep (4000);//!!!!!!!!!!!!!! (1000); // 600 ms not enough; 700 seems OK logd ("Done iusb_best_vendor != USB_VID_GOO ms_sleep()"); } else logd ("Done iusb_best_vendor == USB_VID_GOO"); } if (iusb_best_vendor != USB_VID_GOO) { loge ("No Google AA/Accessory mode iusb_best_vendor: 0x%x", iusb_best_vendor); iusb_deinit (); iusb_state = hu_STATE_STOPPED; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (-3); } iusb_state = hu_STATE_STARTED; logd (" SET: iusb_state: %d (%s)", iusb_state, state_get (iusb_state)); return (0); }
/** * Loops through the graph and spreads the infection * from sick individuals to their neighbors */ void spread_infection(igraph_t *graph, igraph_t *new_graph) { igraph_vector_t neighbors; double T; float random; // Random number to compare against transmission probability T to see if the infection is passed on int num_neighbors; // Loop through all vertices looking for infected individuals for(int i = 0; i < NETWORK_SIZE; i++) { // LATENT if(state_get(graph, i) == LATENT) { // If it is after its first day in latent state, become infectious if(state_counter_get(graph, i) == DAYS_LATENT) { state_set(&new_graph, i, INFECTIOUS); state_counter_set(&new_graph, i, 0); } } // INFECTIOUS else if(state_get(graph, i) == INFECTIOUS) { // If it has been infectious for the specified time, move to recovered class if(state_counter_get(graph, i) == DAYS_INFECTIOUS) { state_set(&new_graph, i, RECOVERED); state_counter_set(&new_graph, i, 0); } else { igraph_neighbors(graph, &neighbors, i, IGRAPH_ALL); num_neighbors = igraph_vector_size(&neighbors); // The probability of the disease spreading to a given neighbor // is a function of the infected vertex's degree. // // The function: T = 1 - (1 - R0/k) * (1/d) // // Where T is the transmission probability // R0 is a fixed constant representing the R0 of the disease // k is the degree // and d is the number of days spent in the infectious class T = 1 - (1 - ((R0/num_neighbors) / (double)DAYS_INFECTIOUS)); // Loop through the neighbors of the infected individual for(int n = 0; n < num_neighbors; n++) { // Only infect the neighbor if they are susceptible if(state_get(graph, (int)VECTOR(neighbors)[n]) == SUSCEPTIBLE) { // Generates float from 0.0-1.0 inclusive random = (float)rand()/(float)RAND_MAX; // Pass on the transmission with probability T if(random < T) { state_set(&new_graph, (int)VECTOR(neighbors)[n], LATENT); state_counter_set(&new_graph, (int)VECTOR(neighbors)[n], 0); } } } } } // RECOVERED else if(state_get(graph, i) == RECOVERED) { if(state_counter_get(graph, i) == DAYS_RECOVERED) { state_set(&new_graph, i, SUSCEPTIBLE); state_counter_set(&new_graph, i, 0); } } } igraph_vector_destroy(&neighbors); state_counter_increment(&new_graph); igraph_copy(graph, &new_graph); igraph_destroy(&new_graph); }
int HUServer::hu_aap_unenc_send (int retry,int chan, byte * buf, int len, int overrideTimeout) { // Encrypt data and send: type,... if (iaap_state != hu_STATE_STARTED && iaap_state != hu_STATE_STARTIN) { logw ("CHECK: iaap_state: %d (%s)", iaap_state, state_get (iaap_state)); //logw ("chan: %d len: %d buf: %p", chan, len, buf); //hex_dump (" W/ hu_aap_enc_send: ", 16, buf, len); // Byebye: hu_aap_enc_send: 00000000 00 0f 08 00 return (-1); } byte base_flags = 0; uint16_t message_type = be16toh(*((uint16_t*)buf)); if (chan != AA_CH_CTR && message_type >= 2 && message_type < 0x8000) { // If not control channel and msg_type = 0 - 255 = control type message base_flags |= HU_FRAME_CONTROL_MESSAGE; // Set Control Flag (On non-control channels, indicates generic/"control type" messages //logd ("Setting control"); } logd("Sending hu_aap_unenc_send %i bytes", len); for (int frag_start = 0; frag_start < len; frag_start += MAX_FRAME_PAYLOAD_SIZE) { byte flags = base_flags; if (frag_start == 0) { flags |= HU_FRAME_FIRST_FRAME; } int cur_len = MAX_FRAME_PAYLOAD_SIZE; if ((frag_start + MAX_FRAME_PAYLOAD_SIZE) >= len) { flags |= HU_FRAME_LAST_FRAME; cur_len = len - frag_start; } logd("Frame %i : %i bytes",(int)flags, cur_len); #ifndef NDEBUG // if (ena_log_verbo && ena_log_aap_send) { if (log_packet_info) { // && ena_log_aap_send) char prefix [MAX_FRAME_SIZE] = {0}; snprintf (prefix, sizeof (prefix), "S %d %s %1.1x", chan, chan_get (chan), flags); // "S 1 VID B" int rmv = hu_aad_dmp (prefix, "HU", chan, flags, &buf[frag_start], cur_len); } #endif enc_buf [0] = (byte) chan; // Encode channel and flags enc_buf [1] = flags; *((uint16_t*)&enc_buf[2]) = htobe16(cur_len); int header_size = 4; if ((flags & HU_FRAME_FIRST_FRAME) & !(flags & HU_FRAME_LAST_FRAME)) { //write total len *((uint32_t*)&enc_buf[header_size]) = htobe32(len); header_size += 4; } memcpy(&enc_buf[header_size], &buf[frag_start], cur_len); int ret = 0; return hu_aap_tra_send (retry, enc_buf, cur_len + header_size, overrideTimeout < 0 ? iaap_tra_send_tmo : overrideTimeout); // Send encrypted data to AA Server } return (0); }
int iusb_bulk_transfer (int ep, byte * buf, int len, int tmo) { // 0 = unlimited timeout char * dir = "recv"; if (ep == iusb_ep_out) dir = "send"; if (iusb_state != hu_STATE_STARTED) { logd ("CHECK: iusb_bulk_transfer start iusb_state: %d (%s) dir: %s ep: 0x%02x buf: %p len: %d tmo: %d", iusb_state, state_get (iusb_state), dir, ep, buf, len, tmo); return (-1); } if (ena_log_extra) { logd ("Start dir: %s ep: 0x%02x buf: %p len: %d tmo: %d", dir, ep, buf, len, tmo); } #ifndef NDEBUG if (ena_log_send && ep == iusb_ep_out) hex_dump ("US: ", 16, buf, len); #endif int usb_err = -2; int total_bytes_xfrd = 0; int bytes_xfrd = 0; // You should also check the transferred parameter for bulk writes. Not all of the data may have been written. // Also check transferred when dealing with a timeout error code. libusb may have to split your transfer into a number of chunks to satisfy underlying O/S requirements, // meaning that the timeout may expire after the first few chunks have completed. libusb is careful not to lose any data that may have been transferred; // do not assume that timeout conditions indicate a complete lack of I/O. errno = 0; int continue_transfer = 1; while (continue_transfer) { usb_err = libusb_bulk_transfer (iusb_dev_hndl, ep, buf, len, & bytes_xfrd, tmo); continue_transfer = 0; if (bytes_xfrd > 0) total_bytes_xfrd += bytes_xfrd; if (bytes_xfrd > 0 && usb_err == LIBUSB_ERROR_TIMEOUT) { continue_transfer = 1; loge ("CONTINUE dir: %s len: %d bytes_xfrd: %d usb_err: %d (%s) errno: %d (%s)", dir, len, bytes_xfrd, usb_err, iusb_error_get (usb_err), errno, strerror (errno)); buf += bytes_xfrd; len -= bytes_xfrd; } else if (usb_err < 0 && usb_err != LIBUSB_ERROR_TIMEOUT) loge ("Done dir: %s len: %d bytes_xfrd: %d usb_err: %d (%s) errno: %d (%s)", dir, len, bytes_xfrd, usb_err, iusb_error_get (usb_err), errno, strerror (errno)); else if (ena_log_verbo && usb_err != LIBUSB_ERROR_TIMEOUT && (ena_log_send || ep == iusb_ep_in)) logd ("Done dir: %s len: %d bytes_xfrd: %d usb_err: %d (%s) errno: %d (%s)", dir, len, bytes_xfrd, usb_err, iusb_error_get (usb_err), errno, strerror (errno)); else if (ena_log_extra) logd ("Done dir: %s len: %d bytes_xfrd: %d usb_err: %d (%s) errno: %d (%s)", dir, len, bytes_xfrd, usb_err, iusb_error_get (usb_err), errno, strerror (errno)); bytes_xfrd = 0; } if (total_bytes_xfrd > 16384) { // Previously caused problems that seem fixed by raising USB Rx buffer from 16K to 64K // Streaming mode by first reading packet length may be better but may also create sync or other issues //loge ("total_bytes_xfrd: %d ???????????????? !!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ?????????????????????????", total_bytes_xfrd); } if (total_bytes_xfrd <= 0 && usb_err < 0) { if (errno == EAGAIN || errno == ETIMEDOUT || usb_err == LIBUSB_ERROR_TIMEOUT) return (0); hu_usb_stop (); // Other errors here are fatal, so stop USB return (-1); } //if (ena_log_verbo) // logd ("Done dir: %s len: %d total_bytes_xfrd: %d", dir, len, total_bytes_xfrd); if (ep == iusb_ep_in) { #ifndef NDEBUG hex_dump ("UR: ", 16, buf, total_bytes_xfrd); #endif } return (total_bytes_xfrd); }
int HUServer::hu_aap_recv_process (int tmo) { // // Terminate unless started or starting (we need to process when starting) if (iaap_state != hu_STATE_STARTED && iaap_state != hu_STATE_STARTIN) { loge ("CHECK: iaap_state: %d (%s)", iaap_state, state_get (iaap_state)); return (-1); } int ret = 0; errno = 0; int min_size_hdr = 4; int have_len = 0; // Length remaining to process for all sub-packets plus 4/8 byte headers temp_assembly_buffer.clear(); bool has_last = false; bool has_first = false; int chan = -1; while (!has_last) { // While length remaining to process,... Process Rx packet: have_len = hu_aap_tra_recv (enc_buf, min_size_hdr, tmo); if (have_len == 0 && !has_first) { return 0; } if (have_len < min_size_hdr) { // If we don't have a full 6 byte header at least... loge ("Recv have_len: %d", have_len); return (-1); } if (ena_log_verbo) { logd ("Recv while (have_len > 0): %d", have_len); hex_dump ("LR: ", 16, enc_buf, have_len); } int cur_chan = (int) enc_buf [0]; // Channel if (cur_chan != chan && chan >= 0) { loge ("Interleaved channels"); return (-1); } chan = cur_chan; int flags = enc_buf [1]; // Flags int frame_len = be16toh(*((uint16_t*)&enc_buf[2])); logd("Frame flags %i len %i", flags, frame_len); if (frame_len > MAX_FRAME_PAYLOAD_SIZE) { loge ("Too big"); return (-1); } int header_size = 4; bool has_total_size_header = false; if ((flags & HU_FRAME_FIRST_FRAME) & !(flags & HU_FRAME_LAST_FRAME)) { //if first but not last, next 4 is total size has_total_size_header = true; header_size += 4; } int remaining_bytes_in_frame = (frame_len + header_size) - have_len; while(remaining_bytes_in_frame > 0) { logd("Getting more %i", remaining_bytes_in_frame); int got_bytes = hu_aap_tra_recv (&enc_buf[have_len], remaining_bytes_in_frame, tmo); // Get Rx packet from Transport if (got_bytes < 0) { // If we don't have a full 6 byte header at least... loge ("Recv got_bytes: %d", got_bytes); return (-1); } have_len += got_bytes; remaining_bytes_in_frame -= got_bytes; } if (!has_first && !(flags & HU_FRAME_FIRST_FRAME)) { loge ("No HU_FRAME_FIRST_FRAME"); return (-1); } has_first = true; has_last = (flags & HU_FRAME_LAST_FRAME) != 0; if (has_total_size_header) { uint32_t total_size = be32toh(*((uint32_t*)&enc_buf[4])); logd("First only, total len %u", total_size); temp_assembly_buffer.reserve(total_size); } else { temp_assembly_buffer.reserve(frame_len); } if (flags & HU_FRAME_ENCRYPTED) { size_t cur_vec = temp_assembly_buffer.size(); temp_assembly_buffer.resize(cur_vec + frame_len); //just incase int bytes_written = BIO_write (hu_ssl_rm_bio, &enc_buf[header_size], frame_len); // Write encrypted to SSL input BIO if (bytes_written <= 0) { loge ("BIO_write() bytes_written: %d", bytes_written); return (-1); } if (bytes_written != frame_len) loge ("BIO_write() len: %d bytes_written: %d chan: %d %s", frame_len, bytes_written, chan, chan_get (chan)); else if (ena_log_verbo) logd ("BIO_write() len: %d bytes_written: %d chan: %d %s", frame_len, bytes_written, chan, chan_get (chan)); int bytes_read = SSL_read (hu_ssl_ssl, &temp_assembly_buffer[cur_vec], frame_len); // Read decrypted to decrypted rx buf if (bytes_read <= 0 || bytes_read > frame_len) { loge ("SSL_read() bytes_read: %d errno: %d", bytes_read, errno); hu_ssl_ret_log (bytes_read); return (-1); // Fatal so return error and de-initialize; Should we be able to recover, if Transport data got corrupted ?? } if (ena_log_verbo) logd ("SSL_read() bytes_read: %d", bytes_read); temp_assembly_buffer.resize(cur_vec + bytes_read); } else { temp_assembly_buffer.insert(temp_assembly_buffer.end(), &enc_buf[header_size], &enc_buf[frame_len+header_size]); } } const int buf_len = temp_assembly_buffer.size(); if (buf_len >= 2) { uint16_t msg_type = be16toh(*reinterpret_cast<uint16_t*>(temp_assembly_buffer.data())); ret = iaap_msg_process (chan, msg_type, &temp_assembly_buffer[2], buf_len - 2); // Decrypt & Process 1 received encrypted message if (ret < 0 && iaap_state != hu_STATE_STOPPED) { // If error... loge ("Error iaap_msg_process() ret: %d ", ret); return (ret); } } return (ret); // Return value from the last iaap_recv_dec_process() call; should be 0 }
int HUServer::hu_aap_enc_send (int retry,int chan, byte * buf, int len, int overrideTimeout) { // Encrypt data and send: type,... if (iaap_state != hu_STATE_STARTED) { logw ("CHECK: iaap_state: %d (%s)", iaap_state, state_get (iaap_state)); //logw ("chan: %d len: %d buf: %p", chan, len, buf); //hex_dump (" W/ hu_aap_enc_send: ", 16, buf, len); // Byebye: hu_aap_enc_send: 00000000 00 0f 08 00 return (-1); } byte base_flags = HU_FRAME_ENCRYPTED; uint16_t message_type = be16toh(*((uint16_t*)buf)); if (chan != AA_CH_CTR && message_type >= 2 && message_type < 0x8000) { // If not control channel and msg_type = 0 - 255 = control type message base_flags |= HU_FRAME_CONTROL_MESSAGE; // Set Control Flag (On non-control channels, indicates generic/"control type" messages //logd ("Setting control"); } for (int frag_start = 0; frag_start < len; frag_start += MAX_FRAME_PAYLOAD_SIZE) { byte flags = base_flags; if (frag_start == 0) { flags |= HU_FRAME_FIRST_FRAME; } int cur_len = MAX_FRAME_PAYLOAD_SIZE; if ((frag_start + MAX_FRAME_PAYLOAD_SIZE) >= len) { flags |= HU_FRAME_LAST_FRAME; cur_len = len - frag_start; } #ifndef NDEBUG // if (ena_log_verbo && ena_log_aap_send) { if (log_packet_info) { // && ena_log_aap_send) char prefix [MAX_FRAME_SIZE] = {0}; snprintf (prefix, sizeof (prefix), "S %d %s %1.1x", chan, chan_get (chan), flags); // "S 1 VID B" int rmv = hu_aad_dmp (prefix, "HU", chan, flags, &buf[frag_start], cur_len); } #endif int bytes_written = SSL_write (hu_ssl_ssl, &buf[frag_start], cur_len); // Write plaintext to SSL if (bytes_written <= 0) { loge ("SSL_write() bytes_written: %d", bytes_written); hu_ssl_ret_log (bytes_written); hu_ssl_inf_log (); hu_aap_stop (); return (-1); } if (bytes_written != cur_len) loge ("SSL_write() cur_len: %d bytes_written: %d chan: %d %s", cur_len, bytes_written, chan, chan_get (chan)); else if (ena_log_verbo && ena_log_aap_send) logd ("SSL_write() cur_len: %d bytes_written: %d chan: %d %s", cur_len, bytes_written, chan, chan_get (chan)); enc_buf [0] = (byte) chan; // Encode channel and flags enc_buf [1] = flags; int header_size = 4; if ((flags & HU_FRAME_FIRST_FRAME) & !(flags & HU_FRAME_LAST_FRAME)) { //write total len *((uint32_t*)&enc_buf[header_size]) = htobe32(len); header_size += 4; } int bytes_read = BIO_read (hu_ssl_wm_bio, & enc_buf [header_size], sizeof (enc_buf) - header_size); // Read encrypted from SSL BIO to enc_buf + if (bytes_read <= 0) { loge ("BIO_read() bytes_read: %d", bytes_read); hu_aap_stop (); return (-1); } if (ena_log_verbo && ena_log_aap_send) logd ("BIO_read() bytes_read: %d", bytes_read); *((uint16_t*)&enc_buf[2]) = htobe16(bytes_read); int ret = 0; ret = hu_aap_tra_send (retry, enc_buf, bytes_read + header_size, overrideTimeout < 0 ? iaap_tra_send_tmo : overrideTimeout); // Send encrypted data to AA Server if (retry) return (ret); } return (0); }
/* * called after a successful write, just mark that we've put something * in the skb and will need to uncork on the next write. */ void kgio_autopush_send(VALUE io) { if (state_get(io) == AUTOPUSH_STATE_WRITER) state_set(io, AUTOPUSH_STATE_WRITTEN); }
/* * call-seq: * * io.kgio_autopush? -> true or false * * Returns the current autopush state of the Kgio::SocketMethods-enabled * socket. * * Only available on systems with TCP_CORK (Linux) or * TCP_NOPUSH (FreeBSD, and maybe other *BSDs). */ static VALUE autopush_get(VALUE io) { return state_get(io) <= 0 ? Qfalse : Qtrue; }
/* Read and dispatch incoming request(s). */ static int gotrequest(void * cookie, int status) { struct dispatch_state * D = cookie; struct proto_lbs_request * R; uint32_t blklen; uint64_t nextblk; /* We're no longer waiting for a packet to arrive. */ D->read_cookie = NULL; /* If the wait failed, the connection is dead. */ if (status) goto drop; /* Read packets until there are no more or an error occurs. */ do { /* Allocate space for a request. */ if ((R = malloc(sizeof(struct proto_lbs_request))) == NULL) goto err0; /* Attempt to read a request. */ if (proto_lbs_request_read(D->readq, R)) goto drop1; /* If we have no request, stop looping. */ if (R->type == PROTO_LBS_NONE) break; /* Handle the request. */ switch (R->type) { case PROTO_LBS_PARAMS: warn0("PROTO_LBS_PARAMS is not implemented"); warn0("Update to a newer version of kvlds"); goto drop1; case PROTO_LBS_PARAMS2: state_params(D->S, &blklen, &nextblk); if (proto_lbs_response_params2(D->writeq, R->ID, blklen, nextblk, nextblk - 1)) goto err1; free(R); break; case PROTO_LBS_GET: D->npending += 1; if (state_get(D->S, R, callback_get, D)) goto err1; break; case PROTO_LBS_APPEND: state_params(D->S, &blklen, &nextblk); if (R->r.append.blklen != blklen) goto drop2; if ((R->r.append.blkno != nextblk) || (D->appendip != 0)) { if (proto_lbs_response_append(D->writeq, R->ID, 1, 0)) goto err2; break; } D->npending += 1; D->appendip = 1; if (state_append(D->S, R, callback_append, D)) goto err1; break; case PROTO_LBS_FREE: if (deleteto_deleteto(D->D, R->r.free.blkno)) goto err1; if (proto_lbs_response_free(D->writeq, R->ID)) goto err1; free(R); break; default: /* proto_lbs_request_read broke. */ assert(0); } } while (1); /* Free the (unused) request structure. */ free(R); /* Wait for more requests to arrive. */ if ((D->read_cookie = wire_readpacket_wait(D->readq, gotrequest, D)) == NULL) { warnp("Error reading request from connection"); goto err0; } /* Success! */ return (0); drop2: free(R->r.append.buf); drop1: free(R); drop: /* We didn't get a valid request. Drop the connection. */ dropconnection(D); /* All is good. */ return (0); err2: free(R->r.append.buf); err1: free(R); err0: /* Failure! */ return (-1); }
/* Initializes the functionality that Eruta exposes to Ruby. */ int tr_init(mrb_state * mrb) { // luaL_dostring(lua, "print 'Hello!' "); struct RClass *krn; struct RClass *pth; struct RClass *sto; struct RClass *gra; struct RClass *spr; struct RClass *thi; struct RClass *eru; struct RClass *aud; struct RClass *cam; struct RClass *sky; mrb->ud = state_get(); eru = mrb_define_module(mrb, "Eruta"); pth = mrb_define_class_under(mrb, eru, "Path" , mrb->object_class); MRB_SET_INSTANCE_TT(pth, MRB_TT_DATA); /* Define some constants. */ TR_CONST_INT(mrb, eru, "FLIP_HORIZONTAL", ALLEGRO_FLIP_HORIZONTAL); TR_CONST_INT(mrb, eru, "FLIP_VERTICAL" , ALLEGRO_FLIP_VERTICAL); TR_CONST_INT(mrb, eru, "ALIGN_LEFT" , ALLEGRO_ALIGN_LEFT); TR_CONST_INT(mrb, eru, "ALIGN_CENTRE" , ALLEGRO_ALIGN_CENTRE); TR_CONST_INT(mrb, eru, "ALIGN_CENTER" , ALLEGRO_ALIGN_CENTER); TR_CONST_INT(mrb, eru, "ALIGN_RIGHT" , ALLEGRO_ALIGN_RIGHT); TR_CONST_INT(mrb, eru, "ALIGN_INTEGER" , ALLEGRO_ALIGN_INTEGER); TR_CLASS_METHOD_NOARG(mrb, eru, "quit" , tr_state_done); krn = mrb_module_get(mrb, "Kernel"); if(!krn) return -1; TR_METHOD_ARGC(mrb, krn, "test" , tr_test , 1); TR_METHOD_ARGC(mrb, krn, "warn" , tr_warn , 1); TR_METHOD_ARGC(mrb, krn, "warning" , tr_warn , 1); TR_METHOD_ARGC(mrb, krn, "log" , tr_log , 1); TR_METHOD_ARGC(mrb, krn, "log_to" , tr_log_to , 2); TR_METHOD_ARGC(mrb, krn, "log_enable" , tr_log_disable , 1); TR_METHOD_ARGC(mrb, krn, "log_disable" , tr_log_enable , 1); TR_METHOD_ARGC(mrb, krn, "script" , tr_script , 1); cam = mrb_define_module_under(mrb, eru, "Camera"); TR_METHOD_ARGC(mrb, krn, "camera_track" , tr_camera_track, 1); TR_METHOD_ARGC(mrb, krn, "camera_lockin", tr_lockin_maplayer, 1); TR_METHOD_NOARG(mrb, krn, "camera_x" , tr_camera_x); TR_METHOD_NOARG(mrb, krn, "camera_y" , tr_camera_y); TR_METHOD_NOARG(mrb, krn, "camera_w" , tr_camera_w); TR_METHOD_NOARG(mrb, krn, "camera_h" , tr_camera_h); TR_METHOD_NOARG(mrb, krn, "camera_alpha", tr_camera_alpha); TR_METHOD_NOARG(mrb, krn, "camera_theta", tr_camera_theta); TR_METHOD_NOARG(mrb, krn, "camera_fov" , tr_camera_fov); TR_CLASS_METHOD_NOARG(mrb, cam, "x" , tr_camera_x); TR_CLASS_METHOD_NOARG(mrb, cam, "y" , tr_camera_y); TR_CLASS_METHOD_NOARG(mrb, cam, "z" , tr_camera_z); TR_CLASS_METHOD_NOARG(mrb, cam, "w" , tr_camera_w); TR_CLASS_METHOD_NOARG(mrb, cam, "h" , tr_camera_h); TR_CLASS_METHOD_NOARG(mrb, cam, "alpha", tr_camera_alpha); TR_CLASS_METHOD_NOARG(mrb, cam, "theta", tr_camera_theta); TR_CLASS_METHOD_NOARG(mrb, cam, "fov" , tr_camera_fov); TR_CLASS_METHOD_ARGC(mrb, cam, "x=" , tr_camera_x_, 1); TR_CLASS_METHOD_ARGC(mrb, cam, "y=" , tr_camera_y_, 1); TR_CLASS_METHOD_ARGC(mrb, cam, "z=" , tr_camera_z_, 1); TR_CLASS_METHOD_ARGC(mrb, cam, "alpha=", tr_camera_alpha_, 1); TR_CLASS_METHOD_ARGC(mrb, cam, "theta=", tr_camera_theta_, 1); TR_CLASS_METHOD_ARGC(mrb, cam, "fov=" , tr_camera_fov_, 1); sky = mrb_define_module_under(mrb, eru, "Sky"); TR_CLASS_METHOD_ARGC(mrb, sky, "set_texture", tr_skybox_set_texture, 2); TR_CLASS_METHOD_ARGC(mrb, sky, "set_rgb" , tr_skybox_set_rgb, 5); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_UP); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_DOWN); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_NORTH); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_EAST); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_SOUTH); TR_CONST_INT_EASY(mrb, sky, SKYBOX_, DIRECTION_WEST); TR_CONST_INT(mrb, eru, "FLIP_HORIZONTAL", ALLEGRO_FLIP_HORIZONTAL); /* */ TR_CLASS_METHOD_NOARG(mrb, eru, "show_fps", tr_show_fps); TR_CLASS_METHOD_NOARG(mrb, eru, "show_area", tr_show_area); TR_CLASS_METHOD_NOARG(mrb, eru, "show_graph", tr_show_graph); TR_CLASS_METHOD_ARGC(mrb, eru, "show_fps=" , tr_show_fps_, 1); TR_CLASS_METHOD_ARGC(mrb, eru, "show_area=" , tr_show_area_, 1); TR_CLASS_METHOD_ARGC(mrb, eru, "show_graph=", tr_show_graph_, 1); TR_CLASS_METHOD_ARGC(mrb, eru, "show_mouse_cursor=", tr_show_mouse_cursor_, 1); TR_CLASS_METHOD_NOARG(mrb, eru, "time", tr_get_time); /* Set up submodules. */ tr_sprite_init(mrb, eru); tr_store_init(mrb, eru); tr_graph_init(mrb, eru); tr_audio_init(mrb, eru); // must restore gc area here ???? mrb_gc_arena_restore(mrb, 0); return 0; }
int hu_aap_recv_process () { // Process 1 encrypted receive message set, from reading encrypted message from USB to reacting to decrypted message if (iaap_state != hu_STATE_STARTED && iaap_state != hu_STATE_STARTIN) { // Need to recv when starting) { loge ("CHECK: iaap_state: %d (%s)", iaap_state, state_get (iaap_state)); return (-1); } byte * buf = rx_buf; int ret = 0; errno = 0; int len_remain = hu_aap_usb_recv (rx_buf, sizeof (rx_buf), iaap_recv_tmo); // Get Rx packet from USB // Length remaining for all sub-packets plus 4/8 byte headers if (len_remain == 0) { // If no data, then done w/ no data return (0); } if (len_remain < 0) { loge ("Recv len_remain: %d", len_remain); hu_aap_stop (); return (-1); } int ctr = 0; while (len_remain > 0) { // While length remaining,... Process Rx packet: //if (ctr ++ > 0) // Multiple subpackets work OK now // loge ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); if (ena_log_verbo) { logd ("Recv while (len_remain > 0): %d", len_remain); #ifndef NDEBUG hex_dump ("LR: ", 16, buf, len_remain); #endif } /*if (len_remain % 16384 == 0) { // Incomplete 16K packets don't happen now with 64K USB Rx buffers loge ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); loge ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); loge ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); }*/ int chan = (int) buf [0]; // Channel int flags = buf [1]; // Flags int len = (int) buf [3]; // Encoded length of bytes to be decrypted (minus 4/8 byte headers) len += ((int) buf [2] * 256); int type = (int) buf [5]; // Message Type (or post handshake, mostly indicator of SSL encrypted data) type += ((int) buf [4] * 256); len_remain -= 4; // Length starting at byte 4: Unencrypted Message Type or Encrypted data start buf += 4; // buf points to data to be decrypted if (chan == AA_CH_VID && flags == 9) { // If First fragment Video... Packet is encrypted so we can't get the real msg_type or check for 0, 0, 0, 1 int total_size = (int) buf [3]; total_size += ((int) buf [2] * 256); total_size += ((int) buf [1] * 256 * 256); total_size += ((int) buf [0] * 256 * 256 * 256); if (max_assy_size < total_size) // Seen as big as 151 Kbytes so far max_assy_size = total_size; // See: jni/hu_aap.c: byte assy [65536 * 16] = {0}; // up to 1 megabyte loge ("First fragment total_size: %d max_assy_size: %d", total_size, max_assy_size); // & jni/hu_uti.c: #define gen_buf_BUFS_SIZE 65536 * 4 // Up to 256 Kbytes // & src/ca/yyx/hu/hu_tro.java: byte [] assy = new byte [65536 * 16]; // & src/ca/yyx/hu/hu_tra.java: res_buf = new byte [65536 * 4]; len_remain -= 4; // Remove 4 length bytes inserted into first video fragment buf += 4; } if (flags & 0x08 != 0x08) { loge ("NOT ENCRYPTED !!!!!!!!! len_remain: %d len: %d buf: %p chan: %d flags: 0x%x type: %d", len_remain, len, buf, chan, flags, type); hu_aap_stop (); return (-1); } if (len_remain < len) { int need = len - len_remain; logd ("len_remain: %d < len: %d need: %d !!!!!!!!!", len_remain, len, need); //ms_sleep (50); // ?? Wait a bit for data to come ?? int need_ret = hu_aap_usb_recv (& buf [len_remain], need, iaap_recv_tmo); // Get Rx packet from USB // Length remaining for all sub-packets plus 4/8 byte headers if (need_ret != need) { loge ("Recv need_ret: %d", need_ret); hu_aap_stop (); return (-1); } len_remain = len; } ret = iaap_recv_dec_process (chan, flags, buf, len); // Decrypt & Process 1 received encrypted message if (ret < 0) { loge ("Error iaap_recv_dec_process() ret: %d len_remain: %d len: %d buf: %p chan: %d flags: 0x%x type: %d", ret, len_remain, len, buf, chan, flags, type); hu_aap_stop (); return (ret); } logd ("OK iaap_recv_dec_process() ret: %d len_remain: %d len: %d buf: %p chan: %d flags: 0x%x type: %d", ret, len_remain, len, buf, chan, flags, type); len_remain -= len; // Consume processed sub-packet and advance to next, if any buf += len; } return (ret); // Return from the last; should be 0 }