int test_communicators( void ) { MPI_Comm dup_comm_world, d2; ptrdiff_t world_rank; int world_size, key_1; int err; MPI_Aint value; int rank; MPI_Comm_rank( MPI_COMM_WORLD, &rank ); world_rank=rank; MPI_Comm_size( MPI_COMM_WORLD, &world_size ); if (world_rank == 0) { printf( "*** Attribute copy/delete return codes ***\n" ); } MPI_Comm_dup( MPI_COMM_WORLD, &dup_comm_world ); MPI_Barrier( dup_comm_world ); MPI_Errhandler_set( dup_comm_world, MPI_ERRORS_RETURN ); value = - 11; if ((err=MPI_Keyval_create( copybomb_fn, deletebomb_fn, &key_1, &value ))) abort_msg( "Keyval_create", err ); err = MPI_Attr_put( dup_comm_world, key_1, (void *)world_rank ); if (err) { printf( "Error with first put\n" ); } err = MPI_Attr_put( dup_comm_world, key_1, (void *)(2*world_rank) ); if (err == MPI_SUCCESS) { printf( "delete function return code was MPI_SUCCESS in put\n" ); } /* Because the attribute delete function should fail, the attribute should *not be removed* */ err = MPI_Attr_delete( dup_comm_world, key_1 ); if (err == MPI_SUCCESS) { printf( "delete function return code was MPI_SUCCESS in delete\n" ); } err = MPI_Comm_dup( dup_comm_world, &d2 ); if (err == MPI_SUCCESS) { printf( "copy function return code was MPI_SUCCESS in dup\n" ); } if (err && d2 != MPI_COMM_NULL) { printf( "dup did not return MPI_COMM_NULL on error\n" ); } delete_flag = 1; MPI_Comm_free( &dup_comm_world ); return 0; }
/* Copy data from internal wifi storage * update tick * insert ptr into opn/wep/wpa tables */ struct AP_HT_Entry *entry_from_ap(Wifi_AccessPoint * ap) { struct AP_HT_Entry *new_ht_ap; Wifi_AccessPoint *ap_copy; ap_copy = (Wifi_AccessPoint *) malloc(sizeof(Wifi_AccessPoint)); if (!ap_copy) abort_msg("Alloc failed !"); memcpy(ap_copy, ap, sizeof(Wifi_AccessPoint)); new_ht_ap = (struct AP_HT_Entry *)malloc(sizeof(struct AP_HT_Entry)); if (!new_ht_ap) abort_msg("Alloc failed !"); new_ht_ap->ap = ap_copy; new_ht_ap->tick = curtick; new_ht_ap->next = NULL; return new_ht_ap; }
void do_realloc(int type) { /* realloc needed */ if (num[type] >= sizes[type]) { sizes[type] += DEFAULT_ALLOC_SIZE; ap[type] = (struct AP_HT_Entry **)realloc(ap[type], sizes[type]); if (!ap[type]) abort_msg("Alloc failed !"); #ifdef DEBUG print_to_debug("realloc'd"); #endif } }
/* Insert the new AP in the fast access list and update the NULL entries if needed */ void insert_fast(int type, struct AP_HT_Entry *new_ap) { /* Any NULL entry (timeouts) ? */ if (num_null[type] > 0) { num_null[type]--; new_ap->array_idx = first_null[type]; ap[type][first_null[type]] = new_ap; if (num_null[type] > 0) { while (first_null[type] < sizes[type] && ap[type][++first_null[type]]) ; if (first_null[type] >= sizes[type]) abort_msg ("out of bound while looking for NULL"); } else first_null[type] = -1; } else { new_ap->array_idx = num[type]; ap[type][num[type]] = new_ap; } num[type]++; }
int test_communicators(void) { MPI_Comm dup_comm_world, d2; int world_rank, world_size, key_1; int err, errs = 0; MPI_Aint value; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); #ifdef DEBUG if (world_rank == 0) { printf("*** Attribute copy/delete return codes ***\n"); } #endif MPI_Comm_dup(MPI_COMM_WORLD, &dup_comm_world); MPI_Barrier(dup_comm_world); MPI_Errhandler_set(dup_comm_world, MPI_ERRORS_RETURN); value = -11; if ((err = MPI_Keyval_create(copybomb_fn, deletebomb_fn, &key_1, &value))) abort_msg("Keyval_create", err); err = MPI_Attr_put(dup_comm_world, key_1, (void *) (MPI_Aint) world_rank); if (err) { errs++; printf("Error with first put\n"); } err = MPI_Attr_put(dup_comm_world, key_1, (void *) (MPI_Aint) (2 * world_rank)); if (err == MPI_SUCCESS) { errs++; printf("delete function return code was MPI_SUCCESS in put\n"); } /* Because the attribute delete function should fail, the attribute * should *not be removed* */ err = MPI_Attr_delete(dup_comm_world, key_1); if (err == MPI_SUCCESS) { errs++; printf("delete function return code was MPI_SUCCESS in delete\n"); } err = MPI_Comm_dup(dup_comm_world, &d2); if (err == MPI_SUCCESS) { errs++; printf("copy function return code was MPI_SUCCESS in dup\n"); } #ifndef USE_STRICT_MPI /* Another interpretation is to leave d2 unchanged on error */ if (err && d2 != MPI_COMM_NULL) { errs++; printf("dup did not return MPI_COMM_NULL on error\n"); } #endif delete_flag = 1; MPI_Comm_free(&dup_comm_world); MPI_Keyval_free(&key_1); return errs; }
void wardriving_loop() { int num_aps, i, index, flags, pressed; touchPosition touchXY; Wifi_AccessPoint cur_ap; u32 lasttick; char state, display_state; /* Vars for AP_DISPLAY */ int entry_n; struct AP_HT_Entry *entry = NULL; print_to_debug("Setting scan mode..."); Wifi_ScanMode(); state = STATE_SCANNING; display_state = STATE_CONNECTING; for (i = 0; i < 3; i++) { sizes[i] = DEFAULT_ALLOC_SIZE; num[i] = num_null[i] = 0; first_null[i] = -1; ap[i] = (struct AP_HT_Entry **) malloc(sizes[i] * sizeof(struct AP_HT_Entry *)); if (ap[i] == NULL) abort_msg("alloc failed"); } flags = DISP_WPA | DISP_OPN | DISP_WEP; memset(modes, 0, sizeof(modes)); strcpy(modes, "OPN+WEP+WPA"); index = 0; TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1024; TIMER1_CR = TIMER_ENABLE | TIMER_CASCADE; lasttick = tick(); while (1) { switch (state) { case STATE_SCANNING: curtick = tick(); /* Wait for VBL just before key handling and redraw */ swiWaitForVBlank(); scanKeys(); pressed = keysDown(); /* Handle stylus press to display more detailed infos * handle this before AP insertion, to avoid race * conditions */ if (pressed & KEY_TOUCH) { touchRead(&touchXY); /* Entry number : 8 pixels for text, 3 lines */ entry_n = touchXY.py / 8 / 3; entry = cur_entries[entry_n]; #ifdef DEBUG printf_to_debug("Entry : Y : %d\n", entry_n); printf_to_debug("SSID : %s\n", entry->ap->ssid); #endif if (entry) { state = STATE_AP_DISPLAY; //display_state = STATE_PACKET_INIT; display_state = STATE_CONNECTING; print_to_debug("Packet scan mode"); print_to_debug(" A : try to connect"); print_to_debug(" B : back to scan"); break; } } num_aps = Wifi_GetNumAP(); for (i = 0; i < num_aps; i++) { if (Wifi_GetAPData(i, &cur_ap) != WIFI_RETURN_OK) continue; insert_ap(&cur_ap); } /* Check timeouts every second */ if (timeout && (curtick - lasttick > 1000)) { lasttick = tick(); clean_timeouts(lasttick); } if (pressed & KEY_RIGHT) timeout += 5000; if (pressed & KEY_LEFT && timeout > 0) timeout -= 5000; if (pressed & KEY_DOWN) index++; if (pressed & KEY_UP && index > 0) index--; if (pressed & KEY_R && (index + (DISPLAY_LINES - 1)) <= numap) index += DISPLAY_LINES - 1; if (pressed & KEY_L && index >= DISPLAY_LINES - 1) index -= DISPLAY_LINES - 1; if (pressed & KEY_B) flags ^= DISP_OPN; if (pressed & KEY_A) flags ^= DISP_WEP; if (pressed & KEY_X) flags ^= DISP_WPA; /* Update modes string */ if (pressed & KEY_B || pressed & KEY_A || pressed & KEY_X) { modes[0] = 0; if (flags & DISP_OPN) strcat(modes, "OPN+"); if (flags & DISP_WEP) strcat(modes, "WEP+"); if (flags & DISP_WPA) strcat(modes, "WPA+"); modes[strlen(modes) - 1] = 0; /* remove the + */ } display_list(index, flags); break; case STATE_AP_DISPLAY: switch (display_state) { case STATE_CONNECTING: /* TODO: * 1) default to packet display * 2) try DHCP [DONE] * 3) try default IPs * 4) handle WEP ? */ /* Try to connect */ if (!(entry->ap->flags & WFLAG_APDATA_WPA) && !(entry->ap->flags & WFLAG_APDATA_WEP)) { print_to_debug ("Trying to connect to :"); print_to_debug(entry->ap->ssid); if (entry->ap->rssi <= 40) print_to_debug ("Warning : weak signal"); print_to_debug("Press B to cancel"); switch (connect_ap(entry->ap)) { case ASSOCSTATUS_ASSOCIATED: display_state = STATE_CONNECTED_FIRST; break; default: print_to_debug("Cnx failed"); state = STATE_SCANNING; Wifi_ScanMode(); } } else { print_to_debug ("WEP/WPA AP not supported"); state = STATE_SCANNING; break; } break; case STATE_CONNECTED_FIRST: display_ap(entry->ap, 1); display_state = STATE_CONNECTED; break; case STATE_CONNECTED: display_ap(entry->ap, 0); break; case STATE_PACKET_INIT: memcpy(mac_filter, entry->ap->macaddr, 6); Wifi_SetChannel(entry->ap->channel); Wifi_RawSetPacketHandler(cap_handler); Wifi_SetPromiscuousMode(1); display_state = STATE_PACKET; break; case STATE_PACKET: Wifi_Update(); if (valid_packet) print_to_debug("Un paquet !\n"); else print_to_debug("No paquet !\n"); break; } scanKeys(); if (keysDown() & KEY_A && state == STATE_PACKET) { state = STATE_CONNECTING; } if (keysDown() & KEY_B) { print_to_debug("Back to scan mode"); state = STATE_SCANNING; Wifi_RawSetPacketHandler(NULL); Wifi_SetPromiscuousMode(0); Wifi_ScanMode(); } swiWaitForVBlank(); break; } } }
int getfile() { struct sockaddr_in sin; struct pollfd fds[1]; struct uftp_h *header; struct fileinfo *info; char buf[PACKETSIZE],temppath[200],destpath[200]; int poll_ret,count,attempt,resend,addr_len; time_t t; header=(struct uftp_h *)&(buf[0]); info=(struct fileinfo *)&(buf[sizeof(struct uftp_h)]); if (!getfileseg()) { close(file); if (unicast) return 0; else exit(1); } close(file); attempt=1; resend=1; fds[0].fd=receiver; fds[0].events=POLLIN; while (attempt<=5) { if (resend) { bzero(buf,sizeof(buf)); header->uftp_id=UFTP_ID; header->func=STATUS; header->tx_id=txID; header->nak_count=-1; fprintf(stderr,"[%d]: Getting confirmation...\n",getpid()); head_hton(header); if (sendto(receiver,buf,sizeof(buf),0,(struct sockaddr *)&dest,sizeof(dest))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error sending STATUS"); attempt++; continue; } resend=0; } if ((poll_ret=poll(fds,1,latency2))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Poll failed"); attempt++; continue; } if (poll_ret==0) { attempt++; resend=1; } else if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) { fprintf(stderr,"[%d]: poll error, revents = 0x%X\n",getpid(),fds[0].revents); attempt++; } else if (fds[0].revents & POLLIN) { addr_len=sizeof(sin); if (recvfrom(receiver,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addr_len)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error receiving DONE_CONF"); attempt++; continue; } head_ntoh(header); if (header->uftp_id!=UFTP_ID) { fprintf(stderr,"[%d]: Invalid DONE_CONF message: not uftp packet\n",getpid()); continue; } if (header->tx_id!=txID) { if (unicast&&(header->func==ANNOUNCE)) { abort_msg(receiver,&sin,header->tx_id,"Currently receiving unicast file, can't receive others until complete"); fprintf(stderr,"[%d]: Denied transfer from %08X, unicast in progress\n",getpid(),header->tx_id); } else { fprintf(stderr,"[%d]: Invalid txID: %08X, should be %08X\n",getpid(),header->tx_id,txID); } continue; } if (header->func==DONE) { continue; } if (header->func!=DONE_CONF) { fprintf(stderr,"[%d]: Invalid DONE_CONF message: wrong func: %s\n",getpid(),strfunc(header->func)); continue; } if (info->addr_list[0].s_addr==m_interface.s_addr) { t=time(NULL); fprintf(stderr,"[%d]: File transfer confirmed at %s",getpid(),ctime(&t)); break; } } else { fprintf(stderr,"[%d]: unknown poll error\n",getpid()); attempt++; } } if (strcmp(tempdir,"")) { sprintf(destpath,"%s/%s",destdir,filename); sprintf(temppath,"%s/%s",tempdir,filename); unlink(destpath); if (link(temppath,destpath)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Couldn't move file"); } else { unlink(temppath); } } if (attempt>5) { fprintf(stderr,"[%d]: Recept unconfirmed by server\n",getpid()); if (unicast) return 0; else exit(1); } else { if (unicast) return 0; else exit(0); } }
int getfileseg() { struct pollfd fds[1]; struct uftp_h *header; struct fileinfo *info; struct sockaddr_in sin; char buf[PACKETSIZE],*data; int i,done,wait,addr_len; long nak_count,last; off_t prev_offset,offset,ret_val; header=(struct uftp_h *)&(buf[0]); info=(struct fileinfo *)&(buf[sizeof(struct uftp_h)]); data=&(buf[sizeof(struct uftp_h)]); naklist=(char *)malloc(numblocks); for (i=0;i<numblocks;i++) naklist[i]=1; fds[0].fd=receiver; fds[0].events=POLLIN; done=0; wait=1; last=-1; prev_offset=0; timeout*=1000; lseek(file,0,SEEK_SET); while (!done) { if ((ret_val=poll(fds,1,timeout))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Poll failed"); continue; } if (ret_val==0) { fprintf(stderr,"[%d]: Transfer timed out\n",getpid()); abort_msg(receiver,&dest,txID,"Transfer timed out"); free(naklist); if (unicast) return 0; else exit(1); } if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) { fprintf(stderr,"[%d]: poll error, revents = 0x%X\n",getpid(),fds[0].revents); continue; } if (fds[0].revents & POLLIN) { bzero(buf,sizeof(buf)); addr_len=sizeof(sin); if (recvfrom(receiver,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addr_len)!=PACKETSIZE) { fprintf(stderr,"[%d]: ",getpid()); perror("Error receiving FILESEG"); continue; } } else { fprintf(stderr,"[%d]: unknown poll return\n",getpid()); continue; } head_ntoh(header); if (header->uftp_id!=UFTP_ID) { fprintf(stderr,"[%d]: error receiving file: not uftp packet\n",getpid()); continue; } if (header->tx_id!=txID) { if (unicast&&(header->func==ANNOUNCE)) { abort_msg(receiver,&sin,header->tx_id,"Currently receiving unicast file, can't receive others until complete"); fprintf(stderr,"[%d]: Denied transfer from %08X, unicast in progress\n",getpid(),header->tx_id); } else { fprintf(stderr,"[%d]: Invalid txID: %08X, should be %08X\n",getpid(),header->tx_id,txID); } continue; } if (header->func==FILESEG) { if (verbose>=2) { if (header->pass!=1) fprintf(stderr,"[%d]: Got packet %d\n",getpid(),header->seq_num); } else if (verbose==1) { if (header->seq_num!=last+1) fprintf(stderr,"[%d]: Got packet %d, last was %d\n",getpid(),header->seq_num,last); last=header->seq_num; } offset=(off_t)header->seq_num*BLOCKSIZE; if ((ret_val=lseek(file,offset-prev_offset,SEEK_CUR))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("lseek failed for file"); } if (ret_val!=offset) { if (sizeof(off_t)>4) fprintf(stderr,"[%d]: offset is %lld, should be %lld\n",getpid(),ret_val,offset); else fprintf(stderr,"[%d]: offset is %d, should be %d\n",getpid(),ret_val,offset); continue; } if ((header->blsize!=BLOCKSIZE)&&(header->seq_num!=numblocks-1)) { fprintf(stderr,"[%d]: Bad block size: %d\n",getpid(),header->blsize); continue; } if ((ret_val=write(file,data,header->blsize))==-1) { fprintf(stderr,"[%d]: Write failed for segment %d\n",getpid(),header->seq_num); continue; } prev_offset=offset+ret_val; if (ret_val!=header->blsize) { fprintf(stderr,"[%d]: Write failed for segment %d, only wrote %d bytes\n",getpid(),header->seq_num,ret_val); continue; } naklist[header->seq_num]=0; } else if (header->func==DONE) { fprintf(stderr,"[%d]: Got DONE message for pass %d section %d\n",getpid(),header->pass,header->section_num); i=0; while (info->addr_list[i].s_addr!=0) { if (info->addr_list[i].s_addr==m_interface.s_addr) { if ((nak_count=sendstatus(header->pass,header->section_num,wait))==-2) { fprintf(stderr,"[%d]: Error sending NAKs",getpid()); wait=1; } else if (nak_count==-3) { fprintf(stderr,"[%d]: Waiting for last packet in section...\n",getpid()); wait=0; } else if (nak_count==-1) { fprintf(stderr,"[%d]: File transfer complete\n",getpid()); done=1; } else { fprintf(stderr,"[%d]: Sent %d NAKs for pass %d section %d\n",getpid(),nak_count,header->pass,header->section_num); } break; } i++; } } else if (header->func==ABORT) { fprintf(stderr,"[%d]: Error: Transfer aborted by server: %s\n",getpid(),data); free(naklist); if (unicast) return 0; else exit(1); } else if ((header->func==REG_CONF)||(header->func==DONE_CONF)) { if (info->addr_list[0].s_addr==m_interface.s_addr) { fprintf(stderr,"[%d]: Error receiving file: wrong func: %s\n",getpid(),strfunc(header->func)); } } else { fprintf(stderr,"[%d]: Error receiving file: wrong func: %s\n",getpid(),strfunc(header->func)); } } free(naklist); return 1; }
int getannounce() { struct uftp_h *header; struct fileinfo *info; struct client_mes *client; struct sockaddr_in sin; struct ip_mreq multi; struct hostent *hp; char buf[PACKETSIZE],*data,filepath[200]; int option,poll_ret,addr_len,i,j,k,attempt,found,resend,spair[2]; long buffer; struct pollfd fds[MAXLIST]; time_t t; pid_t pid; bzero(buf,sizeof(buf)); bzero(&dest,sizeof(dest)); addr_len=sizeof(dest); header=(struct uftp_h *)&(buf[0]); client=(struct client_mes *)&(buf[0]); info=(struct fileinfo *)&(buf[sizeof(struct uftp_h)]); data=&(buf[sizeof(struct uftp_h)]); while (1) { fds[0].fd=listener; fds[0].events=POLLIN; for (i=0,j=1;i<MAXLIST;i++) { if (id_list[i].id!=0) { fds[j].fd=id_list[i].fd; fds[j++].events=POLLIN; } } if ((poll_ret=poll(fds,j,-1))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Poll failed"); continue; } if (poll_ret==0) { attempt++; resend=1; } else if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) { fprintf(stderr,"[%d]: poll error on listener, revents = 0x%X, exiting\n",getpid(),fds[0].revents); exit(0); } else if (fds[0].revents & POLLIN) { if (recvfrom(listener,buf,sizeof(buf),0,(struct sockaddr *)&dest,&addr_len)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error receiving in server"); continue; } head_ntoh(header); if (header->uftp_id==UFTP_ID) { found=0; for (i=0;i<MAXLIST;i++) { if (header->tx_id==id_list[i].id) { found=1; break; } } if (found) continue; if (header->func!=ANNOUNCE) { if (verbose>=2) fprintf(stderr,"[%d]: %s: %08X, %d, %d, %d, %d\n",getpid(),strfunc(header->func),header->tx_id,header->seq_num,header->section_num,header->nak_count,header->pass); continue; } info_ntoh(info); if (info->open==0) { i=0; found=0; while ((info->addr_list[i].s_addr!=0)&&(!found)) { if (info->addr_list[i].s_addr==m_interface.s_addr) found=1; i++; } if (!found) { fprintf(stderr,"[%d]: Name not in host list for id %08X\n",getpid(),header->tx_id); continue; } } if (info->mcast.s_addr==0) { /* unicast transfer requested, make sure we're idle */ found=0; for (i=0;i<MAXLIST;i++) { if (id_list[i].id!=0) { abort_msg(listener,&dest,header->tx_id,"Can't receive unicast file while receiving other files"); fprintf(stderr,"[%d]: Denied unicast transfer from %08X, multicast in progress\n",getpid(),header->tx_id); found=1; break; } } if (!found) { unicast=1; } else { unicast=0; continue; } } else { unicast=0; } if (unicast) { break; } else { if (socketpair(AF_UNIX,SOCK_STREAM,0,spair)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Couldn't open socket pair"); continue; } if ((pid=fork())==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Couldn't fork"); close(spair[0]); close(spair[1]); continue; } else if (pid!=0) { for (i=0;i<MAXLIST;i++) { if (id_list[i].id==0) { id_list[i].id=header->tx_id; id_list[i].fd=spair[0]; id_list[i].multi=info->mcast; close(spair[1]); break; } } if (i>=MAXLIST) { fprintf(stderr,"[%d]: Client list overflow, exiting\n",getpid()); exit(0); } else { if (verbose>=1) fprintf(stderr,"[%d]: Client list index for %08X is %d\n",getpid(),header->tx_id,i); } continue; } else { close(listener); close(spair[0]); parent=spair[1]; atexit(client_exit); break; } } } else { fprintf(stderr,"[%d]: Invalid message: not uftp packet\n",getpid()); } } for (k=1;k<j;k++) { if (fds[k].revents & (POLLERR|POLLHUP|POLLNVAL)) { if (fds[k].revents & (POLLERR|POLLNVAL)) { fprintf(stderr,"[%d]: poll error on child %d, revents = 0x%X\n",getpid(),k,fds[k].revents); } else { if (verbose>=1) fprintf(stderr,"[%d]: client end from poll\n",getpid()); } for (i=0;i<MAXLIST;i++) { if ((fds[k].fd==id_list[i].fd)&&(id_list[i].id!=0)) { id_list[i].id=0; close(id_list[i].fd); break; } } } else if (fds[k].revents & POLLIN) { if (recv(fds[k].fd,buf,sizeof(buf),0)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error receiving from client in server"); continue; } if (client->client_id==CLIENT_ID) { if (verbose>=1) fprintf(stderr,"[%d]: Reading client request...\n",getpid()); for (i=0;i<MAXLIST;i++) { if (client->tx_id==id_list[i].id) { break; } } if (i>=MAXLIST) { if (verbose>=1) fprintf(stderr,"[%d]: Couldn't find client\n",getpid()); continue; } if (client->state==0) { if (verbose>=1) fprintf(stderr,"[%d]: client start, id=%08X\n",getpid(),id_list[i].id); } else if (client->state==1) { if (verbose>=1) fprintf(stderr,"[%d]: client end\n",getpid()); close(id_list[i].fd); for (i=0;i<MAXLIST;i++) { if (id_list[i].id==client->tx_id) { id_list[i].id=0; break; } } } else { fprintf(stderr,"[%d]: Invalid client state: %d\n",getpid(),client->state); } } else { fprintf(stderr,"[%d]: Invalid message: not client packet\n",getpid()); } } } } hp=gethostbyaddr((char *)&dest.sin_addr,sizeof(struct in_addr),AF_INET); strcpy(filename,basename(info->name)); if (!strcmp(tempdir,"")) { sprintf(filepath,"%s/%s",destdir,filename); } else { sprintf(filepath,"%s/%s",tempdir,filename); } numblocks=info->block_total; numsections=info->section_total; if (info->fsize==0) { filesize=(off_t)info->hilargefsize<<32; filesize|=info->lolargefsize; } else { filesize=info->fsize; } if (info->rxlatency1) latency1=info->rxlatency1; else latency1=DEF_LATENCY1; if (info->rxlatency2) latency2=info->rxlatency2; else latency2=DEF_LATENCY2; txID=header->tx_id; t=time(NULL); fprintf(stderr,"[%d]: Received request at %s",getpid(),ctime(&t)); fprintf(stderr,"[%d]: Request from %s id %08X\n",getpid(),(hp)?(hp->h_name):(inet_ntoa(dest.sin_addr)),txID); fprintf(stderr,"[%d]: Name of file to receive: %s\n",getpid(),filename); if (sizeof(off_t)>4) fprintf(stderr,"[%d]: Bytes: %lld, Blocks: %d, Sections: %d\n",getpid(),filesize,numblocks,numsections); else fprintf(stderr,"[%d]: Bytes: %d, Blocks: %d, Sections: %d\n",getpid(),filesize,numblocks,numsections); fprintf(stderr,"[%d]: Using private multicast address %s\n",getpid(),inet_ntoa(info->mcast)); if ((file=open(filepath,O_WRONLY|O_CREAT|O_TRUNC,0644))==-1) { /* need to rethink this error */ fprintf(stderr,"[%d]: ",getpid()); perror("Error opening data file"); abort_msg(receiver,&dest,txID,"Error opening data file"); if (unicast) return 0; else exit(1); } if (unicast) { receiver=listener; } else { sin.sin_family=AF_INET; sin.sin_addr.s_addr=htonl(INADDR_ANY); sin.sin_port=port; if ((receiver=socket(AF_INET,SOCK_DGRAM,0))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error creating socket for receiver"); exit(1); } multi.imr_multiaddr=info->mcast; multi.imr_interface=m_interface; if (setsockopt(receiver,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char *)&multi,sizeof(multi))== -1) { perror("Error joining multicast group for receiver"); exit(1); } option=1; if (setsockopt(receiver,SOL_SOCKET,SO_REUSEADDR,(char *)&option,sizeof(option))== -1) { perror("Error setting reuse option"); close(receiver); exit(1); } if (bind(receiver,(struct sockaddr *)&sin,sizeof(sin))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error binding socket for receiver"); exit(1); } buffer=262144; if (setsockopt(receiver,SOL_SOCKET,SO_RCVBUF,(char *)&buffer,sizeof(buffer))== -1) { perror("Error setting receive buffer size"); exit(1); } client->client_id=CLIENT_ID; client->tx_id=txID; client->state=0; if (send(parent,buf,sizeof(buf),0)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error sending to server"); exit(1); } } attempt=1; resend=1; while (attempt<=5) { if (resend) { header->uftp_id=UFTP_ID; header->func=REGISTER; header->tx_id=txID; head_hton(header); if (sendto(receiver,buf,sizeof(buf),0,(struct sockaddr *)&dest,sizeof(dest))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error sending REGISTER"); attempt++; continue; } fprintf(stderr,"[%d]: REGISTER sent\n",getpid()); resend=0; } fds[0].fd=receiver; fds[0].events=POLLIN; if ((poll_ret=poll(fds,1,latency1))==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Poll failed"); attempt++; continue; } if (poll_ret==0) { attempt++; resend=1; } else if (fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) { fprintf(stderr,"[%d]: poll error, revents = 0x%X\n",getpid(),fds[0].revents); } else if (fds[0].revents & POLLIN) { addr_len=sizeof(sin); if (recvfrom(receiver,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addr_len)==-1) { fprintf(stderr,"[%d]: ",getpid()); perror("Error receiving REG_CONF"); continue; } head_ntoh(header); if (header->uftp_id!=UFTP_ID) { fprintf(stderr,"[%d]: Invalid REG_CONF message: not uftp packet\n",getpid()); continue; } if (header->tx_id!=txID) { if (unicast&&(header->func==ANNOUNCE)) { abort_msg(receiver,&sin,header->tx_id,"Currently receiving unicast file, can't receive others until complete"); fprintf(stderr,"[%d]: Denied transfer from %08X, unicast in progress\n",getpid(),header->tx_id); } else { fprintf(stderr,"[%d]: Invalid txID: %08X, should be %08X\n",getpid(),header->tx_id,txID); } continue; } if (header->func==ANNOUNCE) { continue; } if (header->func==ABORT) { fprintf(stderr,"[%d]: Error: Transfer aborted by server: %s\n",getpid(),data); close(file); return 0; } if (header->func!=REG_CONF) { fprintf(stderr,"[%d]: Invalid REG_CONF message: wrong func: %s\n",getpid(),strfunc(header->func)); continue; } if (info->addr_list[0].s_addr==m_interface.s_addr) { fprintf(stderr,"[%d]: Registration confirmed\n",getpid()); return 1; } } } if (attempt>5) { fprintf(stderr,"[%d]: Registration unconfirmed by server\n",getpid()); abort_msg(receiver,&dest,txID,"Registration unconfirmed"); close(file); if (unicast) return 0; else exit(1); } return 1; }