Exemplo n.º 1
0
/* Return values:
     NULL - error converting value to string
     "string" - converted string
*/
char *valuetostring(dhcpctl_data_string *p_val, omprop proptype, char *buf, int bufsz){
    dhcpctl_data_string value;
    int i,j;
    time_t thetime;
    struct in_addr convaddr;
    char *statestring;
    uint32_t hostint,netint;

    value=*p_val;
    switch(proptype){
    case op_ends:
    case op_tstp:
    case op_tsfp:
    case op_cltt:
	memcpy(&netint, value->value, value->len);
	hostint=ntohl(netint);
	ctime_r((time_t *) &hostint,buf);
	buf[strlen(buf)-1]='\0';
	break;
    case op_ipaddr:
	memcpy(&convaddr.s_addr,value->value,4);
	if((void *)inet_ntop(AF_INET,&convaddr,buf,MAXLEN + 1)==NULL){
	    return NULL;
	}
	break;
    case op_client_hostname:
    case op_name:
	if(value->len > (bufsz-1)) return "error: name too long";
	memcpy(buf,value->value,value->len);
	buf[value->len]='\0';
	break;
    case op_statements:
 	if(value->len > (bufsz-1)) return "error: statement too long";
	memcpy(buf,value->value,value->len);
	buf[value->len]='\0';
    case op_dhcp_client_id:
    case op_hwaddr:
	if((value->len)*3 > (bufsz)) return "error: value too long";
	for(i=0,j=0;i<(value->len)-1;i++,j+=3){
	    sprintf(&buf[j],"%2.2x:",value->value[i]);
	}
	sprintf(&buf[j],"%2.2x",value->value[i]);
	break;
    case op_hwtype:
	memcpy(&netint,value->value,4);
	hostint=ntohl(netint);
	sprintf(buf,"%d",hostint);
	break;
    case op_state:
	memcpy(&netint,value->value,4);
	hostint=ntohl(netint);
	statestring=statetostring((omstate)hostint);
	strncpy(buf, statestring, bufsz);
	break;
    default:
	return NULL;
    }
    return buf;
}
void TorrentDownloadManager::HandleAlerts(){
	bool done=false;
	do{
		std::auto_ptr<alert> alrt=this->s.pop_alert();
		alert* a=alrt.get();
		if(a!=0){

			if(typeid(*a)==typeid(listen_failed_alert)){

				syslog(LOG_CRIT,"Not able to open listen port: %s",alrt->what());
			}
			else if(typeid(*a)==typeid(portmap_error_alert)){

				syslog(LOG_ERR, "Portmap error: %s", alrt->what());

			}else if(typeid(*a)==typeid(portmap_alert)){

			}
#if 0
			//TODO: not supported by libtorrent in jaunty
			else if(typeid(*a)==typeid(portmap_log_alert)){
				portmap_log_alert* pla=dynamic_cast<portmap_log_alert*>(a);
				syslog(LOG_INFO, "Portmap alert: (%d) %s",pla->type,pla->msg.c_str());
			}
#endif
			else if(typeid(*a)==typeid(file_error_alert)){

				// Read or write for torrent failed.
				// TODO: implement some notification
				syslog(LOG_CRIT,"File errror: %s",alrt->what());

			}else if(typeid(*a)==typeid(file_renamed_alert)){

				syslog(LOG_DEBUG,"Filname renamed alert");

			}else if(typeid(*a)==typeid(file_rename_failed_alert)){

				syslog(LOG_DEBUG,"Filname renamed error alert");

			}else if(typeid(*a)==typeid(tracker_announce_alert)){

				syslog(LOG_INFO,"Tracker announce sent");

				tracker_announce_alert* taa=dynamic_cast<tracker_announce_alert*>(a);

				// Notify downloader that we should be up and running
				TorrentDownloader* td=this->FindDownloader(taa->handle);
				if(td){
					td->ChangePermissions();
				}else{
					syslog(LOG_ERR,"Change permissions, downloader not found");
				}

			}else if(typeid(*a)==typeid(tracker_error_alert)){

				tracker_error_alert* ta=dynamic_cast<tracker_error_alert*>(a);
				syslog(LOG_NOTICE,"Tracker announce failed: %s",ta->what());

			}else if(typeid(*a)==typeid(tracker_reply_alert)){

				tracker_reply_alert* tra=dynamic_cast<tracker_reply_alert*>(a);
				syslog(LOG_INFO,"Tracker announce success, peers: %d",tra->num_peers);

			}else if(typeid(*a)==typeid(tracker_warning_alert)){

				tracker_warning_alert* twa=dynamic_cast<tracker_warning_alert*>(a);
				syslog(LOG_WARNING,"Tracker warning: %s",twa->what());

			}else if(typeid(*a)==typeid(scrape_reply_alert)){

				syslog(LOG_INFO,"Scrape reply");

			}else if(typeid(*a)==typeid(scrape_failed_alert)){

				syslog(LOG_NOTICE,"Scrape failed");

			}else if(typeid(*a)==typeid(url_seed_alert)){

				url_seed_alert* usa=dynamic_cast<url_seed_alert*>(a);
				syslog(LOG_WARNING,"Url seed alert, url: %s",usa->url.c_str());

			}else if(typeid(*a)==typeid(hash_failed_alert)){

				// hash_failed_alert* hfa=dynamic_cast<hash_failed_alert*>(a);
				syslog(LOG_INFO,"Hash failed for torrent");

			}else if(typeid(*a)==typeid(peer_ban_alert)){

				// peer_ban_alert* pba=dynamic_cast<peer_ban_alert*>(a);
				syslog(LOG_INFO,"Peer ban alert");

			}else if(typeid(*a)==typeid(peer_error_alert)){

				// peer_error_alert* pea=dynamic_cast<peer_error_alert*>(a);
				syslog(LOG_DEBUG,"Peer error, invalid data received");

			}else if(typeid(*a)==typeid(invalid_request_alert)){

				//invalid_request_alert* ira=dynamic_cast<invalid_request_alert*>(a);
				syslog(LOG_DEBUG,"Invalid piece request");

			}else if(typeid(*a)==typeid(torrent_finished_alert)){

				torrent_finished_alert* tfa=dynamic_cast<torrent_finished_alert*>(a);

				this->dlmutex.Lock();
				map<torrent_handle,TorrentDownloader*>::iterator mIt;
				mIt=this->handle_map.find(tfa->handle);
				if(mIt!=this->handle_map.end()){
					(*mIt).second->DownloadDone();
				}
				this->dlmutex.Unlock();

			}else if(typeid(*a)==typeid(performance_alert)){

				syslog(LOG_DEBUG,"Performance alert: %s",alrt->what());

			}else if(typeid(*a)==typeid(state_changed_alert)){

				state_changed_alert* sca=dynamic_cast<state_changed_alert*>(a);
				syslog(LOG_DEBUG,"State changed: %s",statetostring(sca->state));

			}else if(typeid(*a)==typeid(metadata_failed_alert)){

				//metadata_failed_alert* mfa=dynamic_cast<metadata_failed_alert*>(a);
				syslog(LOG_INFO,"Metadata failed");


			}else if(typeid(*a)==typeid(metadata_received_alert)){

				//metadata_received_alert* mra=dynamic_cast<metadata_received_alert*>(a);
				syslog(LOG_INFO,"Metadata received");

			}else if(typeid(*a)==typeid(fastresume_rejected_alert)){

				//fastresume_rejected_alert* fra=dynamic_cast<fastresume_rejected_alert*>(a);
				syslog(LOG_INFO,"Fast resume rejected");

			}else if(typeid(*a)==typeid(peer_blocked_alert)){

				syslog(LOG_INFO,"Peer blocked");

			}else if(typeid(*a)==typeid(storage_moved_alert)){

				syslog(LOG_INFO,"Storage move complete");

			}else if(typeid(*a)==typeid(torrent_paused_alert)){

				syslog(LOG_DEBUG,"Torrent paused: %s",alrt->what());

			}else if(typeid(*a)==typeid(torrent_resumed_alert)){

				syslog(LOG_DEBUG,"Torrent resumed: %s",alrt->what());

			}else if(typeid(*a)==typeid(save_resume_data_alert)){

				syslog(LOG_DEBUG,"Save resume: %s",alrt->what());
				save_resume_data_alert* srda=dynamic_cast<save_resume_data_alert*>(a);

				TorrentDownloader* td=this->FindDownloader(srda->handle);
				if(td){
					td->DoWriteResumeData(srda->resume_data);
					td->m_resumewritten.Notify();
				}else{
					syslog(LOG_ERR,"Save resume data failed, downloader not found");
				}

			}else if(typeid(*a)==typeid(save_resume_data_failed_alert)){

				syslog(LOG_ERR,"Save resume failed: %s",alrt->what());
				save_resume_data_failed_alert* srdf=dynamic_cast<save_resume_data_failed_alert*>(a);
				TorrentDownloader* td=this->FindDownloader(srdf->handle);
				if(td){
					td->m_resumewritten.Notify();
				}else{
					syslog(LOG_ERR,"Save resume data failed AND downloader not found");
				}


			}else if(typeid(*a)==typeid(external_ip_alert)){

				external_ip_alert* eia=dynamic_cast<external_ip_alert*>(a);
				syslog(LOG_DEBUG,"External IP received: %s",eia->external_address.to_string().c_str());

			}else{

				syslog(LOG_DEBUG,"Unknown alert: %s",alrt->what());
			}
		}else{
			done=true;
		}
	}while(!done);

}