int checkdccchat() { short lcount=0,offset=0,sln; short mcto=0; char *dp; char temps[2048]; int16 stat; while(mcto<30){ if(chan[mcto].stat>2){ stat = CNbyte_count(chan[mcto].cn); if (chan[mcto].stat == SAWAIT && stat >= E_NODATA) {chan[mcto].stat = SACTIVE; sendout("--DCC chat opened",cwin,col[CDCCINFO]); wn[chan[mcto].win].chan=mcto; dowindinfo(chan[mcto].win);} if (stat < E_NODATA && chan[mcto].stat !=SAWAIT) { sendout("--DCC chat terminated by remote",cwin,col[CDCCINFO]); closchan(mcto); } else{ if (stat > 0){ strcpy(dccp,"\0"); CNget_block(chan[mcto].cn,(char *)dccp,stat); *(dccp+stat)=0; sln=(int16)strlen(dccp)+1; dp=dccp; while(offset<sln){ if(*(dp+lcount)==9){*(dp+lcount)=' ';} if(*(dp+lcount)==10){ *(dp+lcount)=0; sprintf(temps,"[DCC:%s] %s",chan[mcto].name,dp); sendout(temps,chan[mcto].win,col[CDCCINFO]);dp=dp+lcount+1; lcount=-1; } offset++; lcount++; } if(offset!=sln){ if(!conf.boldnicks) sprintf(temps,"[DCC:%s] ",chan[mcto].name); else sprintf(temps,"[DCC:%s] ",chan[mcto].name); strncat(temps,dp,strlen(dp)-1); sendout(temps,chan[mcto].win,col[CDCCINFO]); } } } } mcto++; } return 0; }
int dcc_send(void) { short count=0; int16 tstat; long stat; char temps[2048],temps2[2048]; while(count<10){ if(dcc[count].dir==DCOUT && dcc[count].stat){ tstat=CNbyte_count(dcc[count].cn); if(tstat<E_NODATA && dcc[count].stat==SACTIVE){ sprintf(temps2,"**DCC: Error in send to %s",dcc[count].avec); sendout(temps2,cwin,col[CERRORS]); resetadcc(count); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); return 0; } if(dcc[count].stat==SAWAIT && tstat>=E_NODATA){ sprintf(temps,"--DCC send to %s connected",dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); dcc[count].stat=SACTIVE; dcc[count].lack=dcc[count].cnt; } if(tstat>=4 && dcc[count].stat==SACTIVE){ CNget_block(dcc[count].cn,(char *)&dcc[count].lack,4); } if(dcc[count].lack==dcc[count].cnt && dcc[count].dcfh<0){ sprintf(temps,"--DCC send to %s completed",dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); resetadcc(count); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); return 0; } if(dcc[count].stat==SACTIVE && dcc[count].lack==dcc[count].cnt){ stat=Fread(dcc[count].dcfh,(long)dctbufsize,(char *)&dcctempbuffer); if(stat>0)TCP_send(dcc[count].cn,(char *)&dcctempbuffer,(int16)stat); dobar(dcc[count].dcw,(long)dcc[count].cnt,dcc[count].len,count); if(stat<dctbufsize){ Fclose(dcc[count].dcfh); dcc[count].dcfh=-1; } dcc[count].cnt=dcc[count].cnt+stat; } } count++; } return 0; }
void smthread_scanner_t::run() { stid_t root_iid; // root index ID // record file info in the root index : this stores some // attributes of the file in general W_COERCE(ss_m::vol_root_index(vid, root_iid)); file_info_t info2; W_COERCE(find_file_info(vid, root_iid, info2 )); append_only = info2.append_only; rid_t first_rid = info2.first_rid; stid_t fid = info2.fid; num_rec = info2.num_rec; outstream << "********** Created scanner for key " << key << (append_only?" append-only" : " any order") << endl; sendout(); { W_COERCE(ssm->begin_xct()); ss_m::concurrency_t cc = ss_m::t_cc_file; this->scan_i_scan(fid, num_rec, cc); W_COERCE(ssm->commit_xct()); } }
/* called by usb_core_transfer_complete() */ void usb_serial_transfer_complete(int ep,int dir, int status, int length) { (void)ep; (void)length; switch (dir) { case USB_DIR_OUT: logf("serial: %s", receive_buffer); /* Data received. TODO : Do something with it ? */ /* Get the next bit */ usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer); break; case USB_DIR_IN: /* Data sent out. Update circular buffer */ if(status == 0) { /* TODO: Handle (length != buffer_transitlength) */ buffer_start=(buffer_start+buffer_transitlength)%BUFFER_SIZE; buffer_transitlength = 0; } if(buffer_length>0) sendout(); break; } }
int dcc_recv(void) { short count=0; int16 tstat; char temps[2048]; while(count<10){ if(dcc[count].dir==DCIN && dcc[count].stat==RAWAIT){ tstat=CNbyte_count(dcc[count].cn); if(tstat>0){ CNget_block(dcc[count].cn,(char *)&dcctempbuffer,tstat); Fwrite(dcc[count].dcfh,(long)tstat,(char *)&dcctempbuffer); dcc[count].cnt=dcc[count].cnt+tstat; dobar(dcc[count].dcw,(long)dcc[count].cnt,dcc[count].len,count); TCP_send(dcc[count].cn,(char *)&dcc[count].cnt,4); } if(tstat<E_NODATA){ printf("%d",tstat); if(dcc[count].cnt<dcc[count].len){ sprintf(temps,"**DCC: %s from %s incompleted",dcc[count].fname,dcc[count].avec); sendout(temps,cwin,col[CERRORS]); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); resetadcc(count); return 0; } sprintf(temps,"++DCC:%s received from %s",dcc[count].fname,dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); sprintf(temps,"++Bytes: %lu",dcc[count].cnt); sendout(temps,cwin,col[CDCCINFO]); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); resetadcc(count); return 0; } } count++; } return 0; }
void usb_serial_init_connection(void) { /* prime rx endpoint */ usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer); /* we come here too after a bus reset, so reset some data */ buffer_transitlength = 0; if(buffer_length>0) { sendout(); } active=true; }
void server() { int confd; pthread_t tid; struct sockaddr_in servaddr, cliaddr; int len; char addrp[160]; char clientname[HOSTNAME_MAXLENGTH]; int clientname_len; char buf[1000]; if (pthread_create(&screen_read_tid, NULL, screen_read_thread, NULL) != 0) printf("thread!\n"); listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(APP_PORT); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); if (listen(listenfd, CONNECT_MAXNUM) == -1) puts("listen failed"); puts("listening"); while (1) { confd = accept(listenfd, (struct sockaddr *)&cliaddr, &len); if (confd == -1) { puts("quit the application"); break; } inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, addrp, len); clientname_len = read(confd, clientname, HOSTNAME_MAXLENGTH); clientname[clientname_len] = '\0'; write(confd, gethostname(), gethostnamelen()); memset(buf, 0, sizeof(buf)); sprintf(buf, "%s joined the network\n", clientname); printf("%s", buf); sendout(buf, strlen(buf), -1); store_con(confd, clientname, clientname_len, addrp, strlen(addrp)); if (pthread_create(&tid, NULL, con_agent_server_thread, &confd) != 0) puts("thread creation failed\n"); } }
int initiatedcc(char *usr,char *ftg){ short temp; struct FILEINFO finf; uint32 length; uint32 cip; char temps2[2048]; if(!strlen(ftg)){form_alert(1,"[1][DCC Error:|Please specify a file][Okay]");return 0;} if(!strlen(usr)){form_alert(1,"[1][DCC Error:|Please specify recipient][Okay]");return 0;} temp=findfreedc(); if(temp==-1){form_alert(1,"[1][DCC Error:|No free channels!][Okay]");return 0;} dcc[temp].dcfh=Fopen(ftg,0); if(dcc[temp].dcfh<0){form_alert(1,"[DCC Error:|Unable to open file][Okay]");return 0;} dcc[temp].cn=TCP_open(0L,0,0,4096); if(dcc[temp].cn<0){form_alert(1,"[1][DCC Error:|Unable to open socket][Okay]");return 0;} dcc[temp].stat=SAWAIT; dcc[temp].dir=DCOUT; Fsetdta(&finf); Fsfirst(ftg,0); length=finf.size; strcpy(dname,ftg); basename(dname); cb=CNgetinfo(cn[wn[cwin].cnn].cn); cip=stik_cfg->client_ip; if(cip==0||cip==0x7F000001) cip=cb->lhost; cb=CNgetinfo(dcc[temp].cn); sprintf(temps2,":%s PRIVMSG %s :\001DCC SEND %s %lu %u %lu\001\n",cn[wn[cwin].cnn].nick,usr,dname,cip,cb->lport,length); TCP_send(cn[wn[cwin].cnn].cn,(char *)&temps2,(int16)strlen(temps2)); dcc[temp].rport=cb->lport; dcc[temp].rip=cip; strcpy(dcc[temp].fname,dname); dcc[temp].len=length; strcpy(dcc[temp].avec,usr); if(!conf.boldnicks) sprintf(temps2,"--Initiating DCC send to %s: %s",usr,dname); else sprintf(temps2,"--Initiating DCC send to %s: %s",usr,dname); sendout(temps2,cwin,col[CDCCINFO]); if(conf.autodcd)opendccsenddialog(temp,dname); return 0; }
void usb_serial_send(const unsigned char *data,int length) { int freestart, available_end_space, i; if (!active||length<=0) return; i=buffer_start+buffer_length+buffer_transitlength; freestart=i%BUFFER_SIZE; available_end_space=BUFFER_SIZE-i; if (0>=available_end_space) { /* current buffer wraps, so new data can't wrap */ int available_space = BUFFER_SIZE - (buffer_length + buffer_transitlength); length = MIN(length,available_space); memcpy(&send_buffer[freestart],data,length); buffer_length+=length; } else { /* current buffer doesn't wrap, so new data might */ int first_chunk = MIN(length,available_end_space); memcpy(&send_buffer[freestart],data,first_chunk); length-=first_chunk; buffer_length+=first_chunk; if(length>0) { /* wrap */ memcpy(&send_buffer[0],&data[first_chunk],MIN(length,buffer_start)); buffer_length+=MIN(length,buffer_start); } } if (buffer_transitlength==0) sendout(); /* else do nothing. The transfer completion handler will pick it up */ }
/* * looks up file info in the root index */ w_rc_t smthread_main_t::find_file_info( vid_t _vid, stid_t root_iid, file_info_t &info //out ) { W_DO(ssm->begin_xct()); bool found; W_DO(ss_m::vol_root_index(_vid, root_iid)); smsize_t info_len = sizeof(info); const vec_t key_vec_tmp(key, strlen(key)); W_DO(ss_m::find_assoc(root_iid, key_vec_tmp, &info, info_len, found)); if (!found) { cerr << "No file information found, looking for " << key << " lvid= " << _vid << " root_iid= " << root_iid <<endl; return RC(fcASSERT); } else { if(debug) { outstream << " found assoc " << key << " --> " << info << endl; sendout(); } } W_DO(ssm->commit_xct()); return RCOK; }
void smthread_scanner_t::scan_i_scan(const stid_t& fid, int num_recs, ss_m::concurrency_t cc) { outstream << "********** Starting scanning file " << fid << " expecting " << num_recs << " records" << endl; sendout(); scan_file_i scan(fid, cc); w_rc_t rc = scan.error_code(); if(rc.is_error()) { cerr << "Could not create scan_i with fid " << fid << " error is " << rc << endl; ::exit(1); } pin_i *handle; bool eof = false; int i = START; do { w_rc_t rc; rc = scan.error_code(); if(rc.is_error()) { cerr << "Could not create scan_i with fid " << fid << " error is " << rc << endl; ::exit(1); } w_assert1(scan.error_code().is_error() == false); rc = scan.next(handle, 0, eof); if(rc.is_error()) { W_COERCE(rc); } if(debug) { outstream << " scanned i " << i << ": " << scan.curr_rid << " eof=" << eof << endl; sendout(); } if(eof) break; w_assert1(handle->pinned()); const char *hdr =handle->hdr(); smsize_t hdrsize=handle->hdr_size(); /// alignment should be ok: vec_t ref(hdr, hdrsize); int refi; ref.copy_to(&refi, sizeof (refi)); const char *datum =handle->body(); smsize_t datasize=handle->body_size(); outstream << handle->rid(); outstream << " header " << refi ; if(append_only) { w_assert1(refi == i); // if we used append-only on the file creation, // we had better find the order preserved. outstream << "; data (size " << datasize << ") " << datum ; outstream << " A:OK " << endl; } else { rid_t r; memcpy(&r, datum, sizeof(r)); w_assert1(r == handle->rid()); outstream << "; data (size " << datasize << ") " << r ; outstream << " rid:OK " << endl; } sendout(); /* const char *body =handle->body(); smsize_t bodysize=handle->body_size(); */ i++; } while (1) ; if(num_recs == i-1) { if(debug) { outstream << "scan_i scan complete, OK" << endl; sendout(); } } else { outstream << "ERROR IN COUNT: scan_i scan complete, i=" << i-1 << " num_rec expected =" << num_recs << endl; sendout(); } assert(i-START == num_recs); }
void smthread_creator_t::run() { if(debug) { outstream << "Created creator with key " << key ; outstream << ((append_only) ? " (append only)": " (any order)") << endl; sendout(); } file_info_t info; strcpy(info.key, key); // create and fill file to scan outstream << "Creating a file with " << num_rec << " records of size " << rec_size << " vid " << vid << ((append_only) ? " (append only)": " (any order)") << endl; sendout(); W_COERCE(ssm->begin_xct()); W_COERCE(ssm->create_file(vid, info.fid, smlevel_3::t_regular)); rid_t rid; char* dummy = new char[rec_size]; memset(dummy, '\0', rec_size); // put either the creator's key or the rid in the record's body vec_t data(dummy, rec_size); if(append_only) { // put the creator's key in the data just for yuks strncpy(dummy, key, rec_size); info.append_only=true; append_file_i iter(info.fid); for (int j = START; j <= num_rec; j++) { const vec_t hdr(&j, sizeof(j));; iter.create_rec(hdr, rec_size, data, rid); if (j == START) { info.first_rid = rid; } } } else { // put the rid of each record in the datum for (int j = START; j <= num_rec; j++) { const vec_t hdr(&j, sizeof(j));; W_COERCE(ssm->create_rec(info.fid, hdr, rec_size, data, rid)); memcpy(dummy, &rid, sizeof(rid)); // Now replace the datum with the W_COERCE(ssm->update_rec(rid, 0, data)); if (j == START) { info.first_rid = rid; } } } delete [] dummy; info.num_rec = num_rec; info.rec_size = rec_size; stid_t root_iid; // root index ID // record file info in the root index : this stores some // attributes of the file in general // outstream << "calling vol_root_id lvid= " << vid << endl; W_COERCE(ss_m::vol_root_index(vid, root_iid)); const vec_t key_vec_tmp(key, strlen(key)); const vec_t info_vec_tmp(&info, sizeof(info)); #if defined(USING_VALGRIND) // I did once have this at the beginning but then we // croak because we haven't called set_lsn_ck yet if(RUNNING_ON_VALGRIND) { check_valgrind_errors(__LINE__, __FILE__); check_definedness(key, strlen(key)); check_valgrind_errors(__LINE__, __FILE__); check_definedness(&info, sizeof(info)); check_valgrind_errors(__LINE__, __FILE__); } #endif W_COERCE(ss_m::create_assoc(root_iid, key_vec_tmp, info_vec_tmp)); if(debug) { outstream << " Creating root index entry " << key << " --> " << info << endl; sendout(); } W_COERCE(ssm->commit_xct()); file_info_t info2; W_COERCE(find_file_info(vid, root_iid, info2 )); { if(info.first_rid != info2.first_rid) { cerr << "first_rid : " << info.first_rid << " stored info has " << info2.first_rid << endl; W_COERCE( RC(fcASSERT) ); } if(info.fid != info2.fid) { cerr << "fid : " << info.fid << " stored info has " << info2.fid << endl; W_COERCE( RC(fcASSERT) ); } if(info.append_only != info2.append_only) { cerr << "append_only : " << info.append_only << " stored info has " << info2.append_only << endl; W_COERCE( RC(fcASSERT) ); } } outstream << "********** Done creating file " << info.fid << endl; sendout(); threadptr subthread = new smthread_scanner_t(vid, i, num_rec, rec_size); subthread->fork(); subthread->join(); delete subthread; }
void smthread_main_t::run() { rc_t rc; // pointers to options we will create for the grid server program option_t* opt_device_name = 0; option_t* opt_device_quota = 0; option_t* opt_num_rec = 0; option_t* opt_rec_size = 0; if(debug) { outstream << "processing configuration options ..." << " argc " << argc << endl; for(int i=0; i < argc; i++) { outstream << " -------- argv["<<i<<"]=" << argv[i] << endl; } sendout(); } const int option_level_cnt = 3; option_group_t options(option_level_cnt); W_COERCE(options.add_option("device_name", "device/file name", NULL, "device containg volume holding file to scan", true, option_t::set_value_charstr, opt_device_name)); W_COERCE(options.add_option("device_quota", "# > 1000", "2000", "quota for device", false, option_t::set_value_long, opt_device_quota)); W_COERCE(options.add_option("num_rec", "# > 0", NULL, "number of records in file", true, option_t::set_value_long, opt_num_rec)); W_COERCE(options.add_option("rec_size", "# > 0", "7800", "size for records", false, option_t::set_value_long, opt_rec_size)); // have the SSM add its options to the group W_COERCE(ss_m::setup_options(&options)); rc = init_config_options(options, "server", argc, argv); if (rc.is_error()) { usage(options); retval = 1; return; } int cmdline_nrecords(-1); // trumps config options if set int nrecords(0); // set by config options // process command line: looking for the "-i" flag bool init_device = false; int option; int nthreads(1); while ((option = getopt(argc, argv, "Adn:hit:")) != -1) { if(debug) { outstream << "option " << option << endl; sendout(); } switch (option) { case 'A' : append_only = true; if(debug) { outstream << "append only " << endl; sendout(); } break; case 'd' : debug = true; break; case 'n' : cmdline_nrecords = strtol(optarg, 0, 0); break; case 'h' : usage(options); retval = 1; return; break; case 'i' : { if (init_device) { cerr << "Error only one -i parameter allowed" << endl; usage(options); retval = 1; return; } init_device = true; } break; case 't' : nthreads = strtol(optarg, 0, 0); break; default: usage(options); retval = 1; return; break; } } outstream << "Starting SSM and performing recovery ..." << endl; sendout(); ssm = new ss_m(); if (!ssm) { cerr << "Error: Out of memory for ss_m" << endl; retval = 1; return; } smksize_t quota = strtol(opt_device_quota->value(), 0, 0); nrecords = strtol(opt_num_rec->value(), 0, 0); smsize_t rec_size = strtol(opt_rec_size->value(), 0, 0); rid_t start_rid; stid_t fid; if(cmdline_nrecords > 0) { outstream << " num rec of " << cmdline_nrecords << " overrides config option num rec of " << nrecords << endl; sendout(); nrecords = cmdline_nrecords; } rc = setup_device_and_volume(opt_device_name->value(), init_device, quota); if (rc.is_error()) { cerr << "could not set up device/volume due to: " << endl; cerr << rc << endl; delete ssm; rc = RCOK; // force deletion of w_error_t info hanging off rc // otherwise a leak for w_error_t will be reported retval = 1; if(rc.is_error()) W_COERCE(rc); // avoid error not checked. return; } outstream << "************ vid " << vid << endl; sendout(); outstream << "************ NTHREADS " << nthreads << endl; sendout(); /* fork off the correct number of threads */ threadptr * subthreads = new threadptr [nthreads]; if(init_device) { for(int i=0; i<nthreads; i ++) { int nrecs = nrecords/(i?i:1) +1; subthreads[i] = new smthread_creator_t(vid, i, nrecs, rec_size, append_only); #if defined(DEBUG_DESPERATE) && defined(USING_VALGRIND) if(RUNNING_ON_VALGRIND) { check_definedness(subthreads[i], sizeof(smthread_creator_t)); check_valgrind_errors(__LINE__, __FILE__); } #endif } } else { for(int i=0; i<nthreads; i ++) { int nrecs = nrecords/(i?i:1) +1; subthreads[i] = new smthread_scanner_t(vid, i, nrecs, rec_size); } } for(int i=0; i<nthreads; i ++) { subthreads[i]->fork(); } for(int i=0; i<nthreads; i ++) { subthreads[i]->join(); } for(int i=0; i<nthreads; i ++) { delete subthreads[i]; } delete[] subthreads; outstream << "\nShutting down SSM ..." << endl; sendout(); delete ssm; outstream << "*******************************************" << endl; outstream << "Finished! return value=" << retval << endl; outstream << "*******************************************" << endl; sendout(); }
rc_t smthread_main_t:: setup_device_and_volume(const char* device_name, bool init_device, smksize_t quota ) { devid_t devid; u_int vol_cnt; rc_t rc; vid = 10; file_info_t info; if (init_device) { if(debug) { outstream << "Formatting device: " << device_name << " with a " << quota << "KB quota ..." << endl; sendout(); } W_DO(ssm->format_dev(device_name, quota, true)); if(debug) { outstream << "Mounting device: " << device_name << endl; sendout(); } // mount the new device W_DO(ssm->mount_dev(device_name, vol_cnt, devid)); if(debug) { outstream << "Mounted device: " << device_name << " volume count " << vol_cnt << " device " << devid << endl; sendout(); } // generate a volume ID for the new volume we are about to // create on the device lvid_t lvid; if(debug) { outstream << "Generating new lvid: " << endl; sendout(); } W_DO(ssm->generate_new_lvid(lvid)); if(debug) { outstream << "Generated lvid " << lvid << endl; sendout(); } if(debug) { // create the new volume outstream << "Creating a new volume on the device" << endl; outstream << " with a " << quota << "KB quota ..." << endl; outstream << " with local handle(phys volid) " << vid << endl; sendout(); } W_DO(ssm->create_vol(device_name, lvid, quota, false, vid)); if(debug) { outstream << "Created vid " << vid << endl; sendout(); } } else { outstream << "Using already existing device: " << device_name << endl; sendout(); // mount already existing device w_rc_t rc = ssm->mount_dev(device_name, vol_cnt, devid, vid); if (rc.is_error()) { cerr << "Error: could not mount device: " << device_name << endl; cerr << " Did you forget to run the server with -i the first time?" << endl; return rc; } // find ID of the volume on the device lvid_t* lvid_list; u_int lvid_cnt; W_DO(ssm->list_volumes(device_name, lvid_list, lvid_cnt)); if (lvid_cnt == 0) { cerr << "Error, device has no volumes" << endl; ::exit(1); } else { outstream << "Device has volumes:" ; for(unsigned i=0; i < lvid_cnt; i++) { outstream << lvid_list[i] << " " ; } outstream << endl; sendout(); } W_DO(ss_m::lvid_to_vid(lvid_list[0], vid)); delete [] lvid_list; } return RCOK; }