// initialize servos
void HerkulexClass::initialize()
{
        conta=0;
		lenghtString=0;
        delay(100);       
        clearError(BROADCAST_ID);	// clear error for all servos
        delay(10);
        ACK(1);						// set ACK
        delay(10);
        torqueON(BROADCAST_ID);		// torqueON for all servos
        delay(10);
		
}
Esempio n. 2
0
int
main(int argc, char **argv)
{

	struct layer *proto;
	struct MAC localmac,gwmac,cli_mac;
	uint32_t real_ip,spoof_ip,gw_ip,client_ip,server_ip,dly,
		dly_serv;
	struct TCPSocket ts;
	struct datalink icmp_dl,dl;
	uint16_t start_port,end_port,server_port,
		ip_id_a,ip_id_b,ip_id_d;
	unsigned long i;
	unsigned short guess_port,min_delta=-1;
	unsigned long guess_serv_seq,serv_seq=0;
	uint32_t start_guess,end_guess;
	int guess_inc;
	char icmp_filter[256];
	if( argc < 9 )
		usage(*argv);
	if( argc >=  10){
		errno = 0;
		serv_seq = strtoul(argv[9],NULL,10);
		if(errno)
			serv_seq =0;
	}

	srand(time(NULL));
	memset(&dl,0,sizeof(dl));
	if( if_openbyname(&dl,argv[1]) < 0 ){
		fprintf(stderr,"open_link_byname failed\n");
		return 1;
	}

	memset(&icmp_dl,0,sizeof(dl));
	if( if_openbyname(&icmp_dl,argv[1]) < 0 ){
		fprintf(stderr,"open_link_byname failed\n");
		return 1;
	}
	guess_port = start_port;
	str_to_ip(argv[2],&real_ip);	
	str_to_ip(argv[3],&gw_ip);	
	str_to_ip(argv[4],&spoof_ip);	
	str_to_ip(argv[5],&server_ip);	
	memcpy(&localmac.mac,dl.dl_mac,6);

	snprintf(icmp_filter,sizeof(icmp_filter),"icmp and icmp[0] = 0 and "
		"((src %s and dst %s) or (src %s and dst %s))",
		argv[4],argv[2],argv[5],argv[2]);
	filterDatalink(&icmp_dl,icmp_filter);
	if( dl.dl_pcap->linktype == DLT_EN10MB ){
		if( ARPRequest(&dl,&localmac,&gwmac,real_ip,server_ip,5) < 0 ){
			fprintf(stderr,"lan gateway did not reply arp\n");
		exit(1);
		}



		if( ARPRequest(&dl,&localmac,&cli_mac,real_ip,spoof_ip,5) < 0 ){
			fprintf(stderr,"lan gateway did not reply arp\n");
		exit(1);
		}

	}
	start_port = atoi(argv[6]);
	end_port = atoi(argv[7]);
	server_port = atoi(argv[8]);
	createSocket(&ts,&localmac,&gwmac,spoof_ip,
		server_ip,start_port,server_port);
	ts.rcvwin = 0;
	
	ip_id_a = ip_id_b = ip_id_d = 0;
	echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,server_ip,
		&dly,&ip_id_a);
	printf("delay to server= %lu\n",dly_serv);		

	echo_get_id(&icmp_dl,&localmac,&cli_mac,real_ip,spoof_ip,
		&dly,&ip_id_a);
	printf("delay = %lu\n",dly);		
	for( i = start_port; i<= end_port; i++ ){

		SYN(&ts,&dl);
		
		usleep((dly+dly_serv));
                echo_get_id(&icmp_dl,&localmac,&cli_mac,real_ip,spoof_ip,
                &dly,&ip_id_b);

		ip_id_d = ip_id_b - ip_id_a;
		ip_id_a = ip_id_b;
		if(ip_id_d < min_delta){
			min_delta = ip_id_d;
			guess_port = i;
		}

		printf("for port %d ip_id delta = %x\n",ts.port,ip_id_d);			if(ip_id_d == 0 ){
			printf( " the client port is: %d\n",ts.port);	exit(0);
		}

		ts.port++;
		ts.seq++;
	}
	printf("guessed port is %d\n",guess_port);
	ts.ip = server_ip;
	ts.port = server_port;
	ts.hostip = spoof_ip;
	ts.hostport = guess_port;

	printf("finding serv.seq using 16k window\n");
	min_delta = -1;
if(serv_seq != 0 ){
	ts.seq = serv_seq+65536;
	ts.gatewaymac = cli_mac;

	echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
		
		&dly,&ip_id_a);


	ts.ack = 0;
	ACK(&ts,&dl);
 	//ts.ack = 2<<30;
	//ACK(&ts,&dl);
		
        echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
                &dly,&ip_id_b);

	ip_id_d = ip_id_b - ip_id_a;

	printf("for seq %lu delta = %d\n",serv_seq,ip_id_d);



	ts.seq = serv_seq;

	echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
		
		&dly,&ip_id_a);


	ts.ack = 0;
	ACK(&ts,&dl);
 	//ts.ack = 2<<30;
	//ACK(&ts,&dl);
		
        echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
                &dly,&ip_id_b);

	ip_id_d = ip_id_b - ip_id_a;

	printf("for seq %lu delta = %d\n",serv_seq+65536,ip_id_d);
	closeDatalink(&dl);
	closeDatalink(&icmp_dl);
	exit(0);
}
	ip_id_a = ip_id_b = ip_id_d = 0;

	ts.gatewaymac = cli_mac;
	echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
		
		&dly,&ip_id_a);
	start_guess = 0xffffffff;
	end_guess = 16385;
	guess_inc = -16384;
	for( i = start_guess; abs(end_guess-i)>=0 ; i +=guess_inc ){


		ts.ack = 0;
		ts.seq = i;
		ACK(&ts,&dl);
	 	//ts.ack = 2<<30;
		//ts.seq=i;
		ACK(&ts,&dl);
		
                echo_get_id(&icmp_dl,&localmac,&gwmac,real_ip,spoof_ip,
                &dly,&ip_id_b);

		ip_id_d = ip_id_b - ip_id_a;
		ip_id_a = ip_id_b;
		if(ip_id_d < min_delta){
			min_delta = ip_id_d;
			guess_serv_seq = i;
			if(min_delta == 1)
			{

			printf("for seq %lu ip_id delta = %x\n",ts.seq,ip_id_d);		

			RST(&ts,&dl);	
			exit(0);
			}

		}

		printf("for seq %lu ip_id delta = %x\n",ts.seq,ip_id_d);		

	}

	printf("guessed sequence = %lu\n",guess_serv_seq);

	ts.seq = guess_serv_seq;
	
	RST(&ts,&dl);	
	closeDatalink(&dl);
	closeDatalink(&icmp_dl);
	return 0;
}
Esempio n. 3
0
void
main(int argc, char *argv[])
{
	char *devdir;
	int i, rv, netfd, bsize;
	int datafd;

#ifndef plan9

	void (*oldhandler)();

#endif

	devdir = nil;
	/* make connection */
	if (argc != 2) {
		fprint(stderr, "usage: %s network!destination!service\n", argv[0]);
		exits("incorrect number of arguments");
	}

	/* read options line from stdin into lnbuf */
	i = readline(0);

	/* read stdin into tempfile to get size */
	datafd = tempfile();
	bsize = prereadfile(datafd);

	/* network connection is opened after data is in to avoid timeout */
	if ((netfd=dial(argv[1], 0, 0, 0)) < 0) {
		fprint(stderr, "dialing %s\n", devdir);
		perror("dial");
		exits("can't dial");
	}

	/* write out the options we read above */
	if (write(netfd, lnbuf, i) != i) {
		error(0, "write error while sending options\n");
		exits("write error while sending options");
	}

	/* send the size of the file to be sent */
	sprint(lnbuf, "%d\n", bsize);
	i = strlen(lnbuf);
	if ((rv=write(netfd, lnbuf, i)) != i) {
		perror("write error while sending size");
		error(0, "write returned %d\n", rv);
		exits("write error while sending size");
	}

	if (seek(datafd, 0L, 0) < 0) {
		error(0, "error seeking temp file\n");
		exits("seek error");
	}
	/* mirror performance in readfile() in lpdaemon */

#ifdef plan9

	atnotify(alarmhandler, 1);

#else

	oldhandler = signal(SIGALRM, alarmhandler);

#endif

	dbgstate = 1;
	if(!recvACK(netfd)) {
		error(0, "failed to receive ACK before sending data\n");
		exits("recv ack1 failed");
	}
	dbgstate = 2;
	if ((i=pass(datafd, netfd, bsize)) != 0) {
		NAK(netfd);
		error(0, "failed to send %d bytes\n", i);
		exits("send data failed");
	}
	ACK(netfd);
	dbgstate = 3;
	if(!recvACK(netfd)) {
		error(0, "failed to receive ACK after sending data\n");
		exits("recv ack2 failed");
	}

	/* get response, as from lp -q */
	dbgstate = 4;
	while((rv=read(netfd, jobbuf, RDSIZE)) > 0) {
		if((write(1, jobbuf, rv)) != rv) {
			error(0, "write error while sending to stdout\n");
			exits("write error while sending to stdout");
		}
	}
	dbgstate = 5;

#ifdef plan9

	atnotify(alarmhandler, 0);
	/* close down network connections and go away */
	exits("");

#else

	signal(SIGALRM, oldhandler);
	exit(0);

#endif

}
Esempio n. 4
0
void PubUpYLDataOld(void)
{
       while(1)
      	{
        switch (FV.Other.POSSTATFLAG)
        {
        case 0:
            break;
        case 1:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求下载参数");
            PubDisplay(2, "请按确认键继续");
			PubuiInkey(0);  
            if ( PubDownPara() == NORMAL) 
			{
				ACK(4,"00");
			}
			else
			{
				NAK_info(4,"XX","交易失败");
			}
            continue;
        case 2:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求上传状态");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            if (PubUploadState() == NORMAL) 
			{
				ACK(4,"00");
			}
			else
			{
				NAK_info(4,"XX","交易失败");
			}
            continue;
        case 3:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求重新签到");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            if (PubBankLogin() == NORMAL)
			{
				ACK(4,"00");
			}
			else
			{
				PubDialOff (&CommInfo);
				NAK_info(4,"XX","交易失败");
			}
            continue;

#ifdef EMVIC
        case 4:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求公钥下载");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            //if(PubYlConnect(&CommInfo)!=NORMAL)
			//    continue;
	    	if (PubQueryPubilcKey () == 1)
			{
			    PubDownPubilcKey ();
			}
			ACK(4, "00");
		    //PubDialOff (&CommInfo);
			continue;     
        case 5:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求更新参数");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
	        if(PubYlConnect(&CommInfo)!=NORMAL)
			    continue;
//add by baijz 20110906 下载过程断电,需要删除EMV参数文件,否则会重复下载
			EA_ucPFDel (FEmvPara);
			EA_ucPFCreate (FEmvPara, (sizeof (struct EMVPARA)));
//add end 
		    PubDownEmvPara ();
		    PubDialOff (&CommInfo);
            continue;
#endif

#ifdef TMS
		case 6:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
			EA_vCls();
            PubDisplay(1, "要求下载TMS参数");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(60);
	        TmsDownPara();
			continue;
#endif
			
			case 7:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
			EA_vCls();
            PubDisplay(1, "要求下载卡BIN黑名单");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(60);
	        PubDownCardBin();
			continue;
			//JP add
        default:
            break;
        }
	FV.Other.POSSTATFLAG = 0;//chenjs20070104 add防止不小心死循环
	CheckPOSSTATFLAGTimes = 0;
	WRITE_nVAR(FV.Other.POSSTATFLAG);
	return ;
}
}
Esempio n. 5
0
/* bjartek atreus layout for norwegian keyboard
 * - all the mods are oneshot. see here for information about this. https://github.com/tmk/tmk_keyboard/blob/master/tmk_core/doc/keymap.md
 * - layer one has norwegian special characters at AEO positions
 * - needs us mac keyboard layout
 */

const uint16_t PROGMEM actionmaps[][MATRIX_ROWS][MATRIX_COLS] = {
KEYMAP(
Q       , W        , E        , R        , T          ,                       Y         , U         , I       , O         , P        , \
A       , S        , D        , F        , G          ,                       H         , J         , K       , L         , SCLN     , \
Z       , X        , C        , V        , B          ,                       N         , M         , COMM    , DOT       , SLSH     , \
ESC     , OSM(RALT), OSM(LCTL), OSM(LSFT), TAP(1,BSPC), OSM(LGUI), OSM(LALT), TAP(2,SPC), TAP(3,TAB), MINS    , QUOT      , ENT     ),

KEYMAP(
AGK(F7) , GUI(F12) , RA(QUOT) , ACK(R)   , AGSK(L)    ,                       SH(6)     , SH(LBRC)  , SH(RBRC), RA(O)     , SH(1)    , \
RA(A)   , GUI(LBRC), CSK(ENT) , GUI(RBRC), GSK(T)     ,                       0         , SH(9)     , SH(0)   , SH(BSLS)  , SH(4)    , \
GSK(A)  , GSK(F)   , CTRL(T)  , ALT(V)   , GUI(F9)    ,                       GRAVE     , LBRC      , RBRC    , SH(7)     , SH(GRAVE), \
GA      , OSM(RALT), OSM(LCTL), OSM(LSFT), TRNS       , OSM(LGUI), OSM(LALT), CTRL(F2)  , OFF(1)    , SH(2)   , QUOT      , OSM(LGUI)),

KEYMAP(
INS     , HOME     , UP       , END      , PGUP       ,                       SH(5)     , 7         , 8       , 9         , SH(8)    , \
DEL     , LEFT     , DOWN     , RIGHT    , PGDN       ,                       MINS      , 4         , 5       , 6         , SH(EQUAL), \
VOLU    , MPRV     , MPLY     , MNXT     , GCK(Q)     ,                       SH(4)     , 1         , 2       , 3         , BSLS     , \
VOLD    , MUTE     , OSM(LCTL), OSM(LSFT), CSK(SPC)   , OSM(LGUI), OSM(LALT), TRNS      , OFF(1)    , DOT     , 0         , KP_EQUAL),

KEYMAP(
F1      , F2       , F3       , F4       , F5         ,                       WH_D      , BTN1      , MS_U    , BTN2      , SH(3)    , \
F6      , F7       , F8       , F9       , F10        ,                       WH_U      , MS_L      , MS_D    , MS_R      , BTN3     , \
F11     , F12      , F13      , F14      , BOOT       ,                       NO        , ACL0      , NO      , NO        , NO       , \
GA      , OSM(RALT), OSM(LCTL), OSM(LSFT), GS         , OSM(LGUI), OSM(LALT), SPC       , TRNS      , MINS    , SH(BSLS)  , KP_EQUAL)
Esempio n. 6
0
int dlm_main(char *CMDBUF , int *nCmdLen)
{
    char szGetBuffer[30],szTmpBuf[30];
	char szBuffer[100];

 //   char cWaitRet;
    uint uiKey , uiRet,nRet;
 //   uchar ucRetVal;
//    uchar ucFd;
    uchar ucRet;

    int nScreen = 0;
    char cMenuTime;
 //   uint uiCount;
    uint uiOption;
	uint puiConfigInfo;
//  uchar i,  ucIfExist, ucStatus; 

    const char *pcModuleName = MODULE_FILE_NAME;
//    const char *pcCommModuleName = "applib";
    DevHandle phPinpad;
//    DevHandle hVoiceHandle;
#ifdef EPT_A9L
//	uint_64 llCnt = 0;
//	char szData[100];
#endif

#ifdef EMVIC
	EA_EMV_vInit();
#endif
    //设备句柄清空 
    gbPrintHandle = EM_NULL;
	//清空所有全局设备句柄,add by zhuchentao 060831
	gbhdlMagCard = EM_NULL;;	//磁卡模块 
	gbhdlUserCard = EM_NULL;
	gbhdlMasterCard = EM_NULL;
	gbhdlSAM1Card = EM_NULL;
	gbhdlSAM2Card = EM_NULL;
	gbhdlSAM3Card = EM_NULL;
	gbhdlSAM4Card = EM_NULL;
	gbMifCardHandle = EM_NULL;
#ifdef EMVIC 
	gbhdlPINPAD = EM_NULL;   
	gbhdlRS232 = EM_NULL;
	gbMifCardHandle = EM_NULL; 
#endif
	cRunState = 1;
    CommInfo.gbHdl = EM_NULL;
    memset(&iso, 0, sizeof(ISODataStru));
    memset(&iso1, 0, sizeof(ISODataStru));
    memset(&printiso, 0, sizeof(ISODataStru));
    memset(&upiso, 0, sizeof(ISODataStru));
	gbcECDefault = 0;
#ifndef EPT_A9L
#ifdef EPT_M3
	if (EA_uiLoadAndExecuteModule("LibRFReader.dlm", "") != EM_SUCCESS)
#endif
#ifdef EPT56X0
	if (EA_uiLoadAndExecuteModule("LibRFReader", "") != EM_SUCCESS)
#endif
    {
    	
        PubDisplay(3, "载入LibRFReader.dlm失败");
        PubDisplay(4, "请下载LibRFReader.dlm文件");
		PubuiInkey(0);
		return EXCEPTION;
    }
#endif
	EA_vCls();
	PubucLoadAllMfVar((char*)MODULE_FILE_NAME, &FV, sizeof(FV));

// #ifdef EPT56X0
// 	EA_ucSetStopFlag(EM_DISABLE_STOP);
// #endif
// #ifdef EPT_A9L
// 	EA_ucSetStopFlag(EM_DISABLE_STOP);
// #endif
// 银商允许修改参数标志,提交程序的时候分别2种情况编译提交 
//	setup.c 中还有一个地方要设置,勿忘!!!
#ifdef TMS
   		     cSetupModifyFlag = 0;// TMS服务器版 不允许修改参数
#else
  			 cSetupModifyFlag = 1;// 下载版 允许修改参数
#endif

#ifndef EPT5610
	EA_vSetLCDLightTimer(60);
#endif
    EA_vCls();
    if (PubucFirstRunPara(pcModuleName, "parafile") == EM_SUCCESS)
    {
		PubGetPinpadPara();// 先读取参数文件中密码键盘标签
              FV.gbnParaFirstFlag = YES;
		WRITE_nVAR(FV.gbnParaFirstFlag);
    }
	PubUsePinpadInside(FV.Other.SetPinpadType);
// 	PubUsePinpadInside(1);
    PubGetPrinterType(&iPrinterType); 
    PubGetPinpadType(&iPinpadType);
    while ((ucRet = PubOpenDevice("PINPAD", EM_io_IPP, &phPinpad)) != EM_SUCCESS)
    {
		PubGetPosType();
		if (FV.gbnPosTypeFlag == YES)
		{
			FV.Other.SetPinpadType = 1;
			PubUsePinpadInside(1);
			WRITE_nVAR(FV.Other.SetPinpadType);
		}
		else
		{
	        PubDisplayInv(2, "打开密码键盘错");
	        PubDisplay(3, "请接入密码键盘");
	        PubDisplay(4, "[%d][%02x][%d]", ucRet, ucRet,FV.gbnPosTypeFlag);
	        EA_vBeepMs(50);
			uiRet = PubuiInkey(1);
	        if (uiRet == EM_key_FUNC1)
			{
				PubSetUsePinpadType();
			}
			if (uiRet == EM_key_FUNC9||EM_key_CANCEL)
			{
				return EM_ERROR;
			}
		}
    }
    while ((PubGetPinpadVer(phPinpad ,&iPinpadVer)) == 3)
    {
        PubDisplayInv(2, "请接好密码键盘");
        EA_vBeepMs(50);
        PubuiInkey(1);
    }
#ifndef EPT5610
    //20070914  by wei 将安全级别设置成默认级别
	if ( iPinpadVer == 2 )  
	{
		EA_pci_ucGetSecureConfig(phPinpad, EM_NULL, &puiConfigInfo);
		if ( puiConfigInfo != EM_pin_DEFAULTCONFIG)
		{
			EA_pci_ucSetSecureConfig(phPinpad, EM_NULL, EM_pin_DEFAULTCONFIG);
		}
	}
#endif
    EA_ucCloseDevice(&phPinpad);

    nFirstSetupFlag = NO;

    if (PubucFirstRunProg(pcModuleName) == EM_SUCCESS)
	{
        FV.gbnFirstFlag = YES;
		WRITE_nVAR(FV.gbnFirstFlag);
	}

    // 注册headcommon 
/*
    if (PubLoadAndExecuteModule(pcCommModuleName, "F0506") != EM_SUCCESS)
    {
        EA_vCls();
        PubDisplay(3, "装载公共模块失败!");   
        //因为PubDisplay是在applib中的,注册applib失败也就无法使用PubDisplay函数,只能用EA_vDisplay函数
        PubuiInkey(0);
        return EXCEPTION;
    }

*/
    // 导入def8583.h中的8583包定义
    if (PubImport8583(YlIso8583Def) == EXCEPTION)
    {
        EA_vCls();
        PubDisplay(3, "8583包导入出错");
        PubDisplay(4, "请检查程序重新下装");
        PubuiInkey(0);
        return EXCEPTION;
    }
    // 导入结束 
	PubGetMifsType(&iMifsType);	//增加POS模块判断/必须放在initcomm之前,initcomm可能会影响底层的判断

    if (FV.gbnFirstFlag == YES)
    {
		PubGetPosType();
		PubSetupFirst();
#ifdef EMVIC
   		setup_first ();
#endif   		
		WRITE_nVAR(FV); //这里把setupfirst里面的内容统一写入一次
		nFirstSetupFlag = YES;
		FV.gbnFirstFlag = NO;
		WRITE_nVAR(FV.gbnFirstFlag);

		FV.gbnParaFirstFlag = YES;
		WRITE_nVAR(FV.gbnParaFirstFlag);

		if (FV.gbnParaFirstFlag == YES)
		{
		PubReadPara() ;
		}
		FV.gbnParaFirstFlag = NO;
		WRITE_nVAR(FV.gbnParaFirstFlag);
    }
    if (FV.gbnParaFirstFlag == YES)
    {
        PubReadPara();
    }
    if (nFirstSetupFlag == YES)
    {
		PubInitGlobal();
        PubSetup(10);
        nFirstSetupFlag = NO;
    }
	else
	{    
		PubInitGlobal();
	}
#ifdef TMS
	//读取主控应用参数
	ProReadBasePara();
#endif
	// by wei 每次进入应用不需要重新操作员签到 modify by baijz 20110723 BCTC要求每次开机操作员签到
     BUFCLR(szGetBuffer);
//      VERIFY_NORMAL(PubGetPoscfgcPosState(szGetBuffer));
//      if (szGetBuffer[0] == LOGON)
//          PubSetPoscfgcPosState(NO_LOGON);

	EA_vCls();
	PubKeyOn();

#ifdef BZ       /*标准版*/
	PubDisplayInv( 1, "中国银联"); 
#endif
#ifdef Largevalue   /*大额支付*/	
       PubDisplayInv( 1, "中国银联-大额"); 
#endif
#ifdef NMLS   /*内蒙移动零售*/
	PubDisplayInv( 1, "内蒙移动零售"); 
#endif
#ifdef NMJF   /*内蒙移动缴费*/
	PubDisplayInv( 1, "内蒙移动缴费"); 
#endif 
	PubDisplayCen(2, "CUP2.0");
	PubDisplayCen(3, "自检...");
	szGetCmdFlag = 0 ;
	szCMDChangeAppFlag = 0 ;
    //ucRet = EA_ucOpenDevice("PINPAD",EM_io_IPP, &phPinpad);
    while ((ucRet = PubOpenDevice("PINPAD", EM_io_IPP, &phPinpad)) != EM_SUCCESS)
	{
        PubDisplayInv(2, "打开密码键盘错");
        PubDisplay(3, "[%d][%02x]", ucRet, ucRet);
        EA_vBeepMs(50);
        PubuiInkey(1);
	}
    while ((ucRet = PubResetPinpad(phPinpad)) != EM_SUCCESS)
	{
        PubDisplayInv(2, "请接好密码键盘");
        PubDisplay(3, "[%d][%02x]", ucRet, ucRet);
        EA_vBeepMs(50);
        PubuiInkey(1);
	}
	// 为多应用,怕其他应用使用和银联相同的mackey workkey的ID号,这里重新load一次。
	PubucSelectKey(phPinpad, EM_pin_MASTERKEY, FV.bank.nMKeyIndex);
	if(FV.Other.gbMacKeyLen)
		PubucLoadKey(phPinpad,EM_pin_DEFAULTMODE ,EM_pin_MACKEY, 0, FV.Other.gbMacKeyLen, FV.Other.gbszMacKey);
	if(FV.Other.gbWorkKeyLen)
		PubucLoadKey(phPinpad, EM_pin_DEFAULTMODE ,EM_pin_WORKKEY, 0, FV.Other.gbWorkKeyLen, FV.Other.gbszWorkKey);
    EA_ucCloseDevice(&phPinpad);
    if (PubuiInkeyMs(500) == EM_key_CANCEL)
        PubLogin();
  
	PubGetMifsType(&iMifsType);	//增加POS模块判断/必须放在initcomm之前,initcomm可能会影响底层的判断
    PubInitComm(&CommInfo, AUTOCHECK);
    BUFCLR(szGetBuffer);
    VERIFY_NORMAL(PubGetPoscfgcBatchState(szGetBuffer));

    if (szGetBuffer[0] == YES)
    {
        EA_vCls();
        PubDisplayInv(2, "请先结算再作交易");
        PubuiInkey(1);
    }
	//打开MISPOS的通讯接口 
#ifdef EPT_A9L
	
	ucRet = EA_ucOpenDevice(FV.Other.gbcMISPort,0, &gbhdCMDRS232);
	if (ucRet != EM_SUCCESS)
	{
		ucRet = EA_ucOpenDevice("COM3",0, &gbhdCMDRS232);
		if (ucRet == EM_SUCCESS)
		{
			memcpy(FV.Other.gbcMISPort,"COM3",4);
			WRITE_nVAR(FV.Other.gbcMISPort);
		}
	}
	if (ucRet != EM_SUCCESS)
	{
		 EA_vCls();
		 PubDisplay(1,"请先设置COMPOS 端口");
		 EA_vBeepMs(1000);
		 PubuiInkey(0);
		return ProFuncBankszComPos();
		/*memcpy(FV.Other.gbcMISPort,"COM2",4);
		WRITE_nVAR(FV.Other.gbcMISPort);
		EA_vCls();
		PubDisplay(1,"无法打开通信端口!");
		PubDisplay(2,"请重启设备");
		EA_vBeepMs(1000);
		PubuiInkey(2);
		return EXCEPTION;*/
	}
	else
	{
		ucRet = EA_ucInitRS232(gbhdCMDRS232,FV.gbRSInfo.ucBps,EM_232_NOPAR ,EM_232_8DBS);
		//ucRet = EA_ucCloseDevice(&gbhdCMDRS232);
		//gbhdCMDRS232 = EM_NULL;
	}
#endif

	if ( nCmdLen != NULL && CMDBUF != NULL)
	{
		CMDBUFLen = *nCmdLen;
		if (CMDBUFLen >0 )
		{				      
			memset(szCMDBuffer , 0 , sizeof(szCMDBuffer));
			memcpy(szCMDBuffer , CMDBUF , CMDBUFLen);			
			memset(&TlvSendValue,0,sizeof(struct TLVVALUE));
			CMDProcess(szCMDBuffer, (char*)&TlvSendValue, &gbctlvflag);
			szGetCmdFlag = 15;
		}
	}

    BUFCLR(szGetBuffer);
	BUFCLR(gbPosTags);
	gbTags = 0; 


	      gbnDisplayTime = 0;
#ifdef EMVIC
	PubSetExeReader(1);
#endif
    while (1)
    {
		PubDispIcon();//显示一下5650电池电量和信号
#ifdef TMS
		if (ProJudgeBaseDlaVer() == EM_SUCCESS)
		{
			ProGetBaseDialFix();
			ucRet = PubReturnUpdate() ;
			if (ucRet != EM_ERROR)
			{
				PubDelAllBit(&iso);
				close_all_dev();
				return ucRet;
			}
			if (PubCheckRunState() == EM_SUCCESS)
			{
				PubDelAllBit(&iso);
				close_all_dev();
				return EM_ERROR;
			}
		}
#endif
	memset(&gbTrkInfo, 0, sizeof(struct TrackInfo));
	memset(&gbCardinfo, 0, sizeof(struct CardConfig));
//	PubDisplay(4 , "%d  %d  %d   %d",EM_VOC_LEVEL0,EM_VOC_LEVEL1,EM_VOC_LEVEL2,EM_VOC_LEVEL3);
//	PubuiInkey(60);
//    FV.Other.POSSTATFLAG = 6;//chenjs20060915 ls add for test TMS 
//	EA_ucAudioSetVolume(FV.Other.ucNewVolume);
//	EA_ucAudioPlay("qing.wav");
//	EA_ucAudioPlay("shuaka.wav");
	

	
#if 0
    ucRet = EA_ucOpenDevice("EVOICE", 0, &hVoiceHandle); //打开外置语音设备

    if(ucRet != EM_SUCCESS)
    {
        EA_vDisplays(2,1, "Test Open Err:%02x", ucRet);
        EA_uiInkey(0);
        return EM_ERROR;
    }
    else
    {
        EA_vDisplays(2,1, "Open OK");
        EA_uiInkey(2);
    }
    ucRet = EA_ucIfVoiceExist(hVoiceHandle, &ucIfExist);
    if(ucRet != EM_SUCCESS)
    {
        EA_vDisplays(2,1, "Test IfExist Err:%02x", ucRet);
        EA_ucCloseDevice(&hVoiceHandle);
        EA_uiInkey(0);
        return EM_ERROR;
    }
    else
    {
         if(ucIfExist == 1) //If Exist
        {
            for(i = 0; i < 8; i++)
            {
                ucRet = EA_ucPlayVoice(hVoiceHandle, i);
                if(ucRet != EM_SUCCESS)
                {
                    EA_vDisplays(2,1, "Test play%d Err:%02x",i, ucRet);
                    EA_uiInkey(0);
                }
                else
                {
                    EA_vDisplays(2,1, "Test play%d OK", i);
                }
                do
                {
                    EA_ucGetPlayStatus(hVoiceHandle, &ucStatus);
                }while(ucStatus == EM_VOICE_PLAYING);
            }
        }
        else
        {
            EA_vDisplays(2,1, "No voice module exists");
            EA_uiInkey(0);
        }
        EA_ucCloseDevice(&hVoiceHandle);
    }
#endif	
#if 0		
        switch (FV.Other.POSSTATFLAG)
        {
        case 0:
            break;
        case 1:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求下载参数");
            PubDisplay(2, "请按确认键继续");
			PubuiInkey(0);  
            if ( PubDownPara() == NORMAL) 
			{
				ACK(4,"00");
			}
			else
			{
				NAK_info(4,"XX","交易失败");
			}
            continue;
        case 2:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求上传状态");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            if (PubUploadState() == NORMAL) 
			{
				ACK(4,"00");
			}
			else
			{
				NAK_info(4,"XX","交易失败");
			}
            continue;
        case 3:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求重新签到");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            if (PubBankLogin() == NORMAL)
			{
				ACK(4,"00");
			}
			else
			{
				NAK_info(4,"XX","交易失败");
			}
            continue;

#ifdef EMVIC
        case 4:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求公钥下载");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
            //if(PubYlConnect(&CommInfo)!=NORMAL)
			//    continue;
	    	if (PubQueryPubilcKey () == 1)
			{
			    PubDownPubilcKey ();
			}
			ACK(4, "00");
		    //PubDialOff (&CommInfo);
			continue;     
        case 5:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
            EA_vCls();
            PubDisplay(1, "系统要求更新参数");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(0);
	        if(PubYlConnect(&CommInfo)!=NORMAL)
			    continue;
//add by baijz 20110906 下载过程断电,需要删除EMV参数文件,否则会重复下载
			EA_ucPFDel (FEmvPara);
			EA_ucPFCreate (FEmvPara, (sizeof (struct EMVPARA)));
//add end 
		    PubDownEmvPara ();
		    PubDialOff (&CommInfo);
            continue;
#endif

#ifdef TMS
		case 6:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
			EA_vCls();
            PubDisplay(1, "要求下载TMS参数");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(60);
	        TmsDownPara();
			continue;
#endif
			
			case 7:
			if (ProCheckPOSSTATFLAG()!= NORMAL)
			{
				break;
			}
			EA_vCls();
            PubDisplay(1, "要求下载卡BIN黑名单");
            PubDisplay(2, "请按确认键继续");
            PubuiInkey(60);
	        PubDownCardBin();
			continue;
			//JP add
        default:
            break;
        }
		FV.Other.POSSTATFLAG = 0;//chenjs20070104 add防止不小心死循环
		CheckPOSSTATFLAGTimes = 0;

		WRITE_nVAR(FV.Other.POSSTATFLAG);

#endif
        EA_ucGetDateTime(&datetime);
        sprintf(szTmpBuf , "%04d%02d%02d", datetime.usYear, datetime.ucMonth, datetime.ucDay);
        PubDialOff(&CommInfo);
        // 判断打印是否完成 
        PubJudePrintFinish(1);


//		 如果隔日了,就要求POS的操作员签到。
		if(memcmp( szTmpBuf , FV.Other.gbLoginDate, 8 ))
			PubSetPoscfgcPosState(NO_LOGON);

        BUFCLR(szGetBuffer);
        VERIFY_NORMAL(PubGetPoscfgcPosState(szGetBuffer));
           
/*        if (szGetBuffer[0] == NO_LOGON)
        {
#ifdef TMS
			EA_vCls();
			PubDisplayInv(1,"银联EMV应用");
			PubDisplayCen(3,"按任意键签到");
			PubDisplayCen(4,"按取消键切换应用");
			uiRet = PubuiInkey(10);
			if (uiRet == EM_key_FUNC9|| uiRet == EM_key_CANCEL)
			{
				EA_vCls();
                PubDisplayCen(2, "应用切换");
                PubuiInkeyMs(500);
                PubDelAllBit(&iso);
				close_all_dev();
				EA_vCls();
                return EM_ERROR;
			}
#endif
            if (PubLogin() != NORMAL)
                continue;
        }
*/	
        

//   20060119
//for test dudj
/*
PubDisplay(3, "是否联机签到?");
 if (PubuiInkey(2)!=EM_key_ENTER)
	sprintf( FV.Other.gbLoginDate , "%s", szTmpBuf);
PubDisplay(3, "");
*/
		// 测试的时候打开。发布的封掉
 		/*sprintf( FV.Other.gbLoginDate , "%s", szTmpBuf);
        if(memcmp( szTmpBuf , FV.Other.gbLoginDate, 8 ))
        {
                if (PubBankLogin() != NORMAL)
                {
                    PubSetPoscfgcPosState(NO_LOGON);
                }
                else
                {
			        EA_ucGetDateTime(&datetime);
					sprintf(szTmpBuf , "%04d%02d%02d", datetime.usYear, datetime.ucMonth, datetime.ucDay);
                    sprintf( FV.Other.gbLoginDate , "%s", szTmpBuf);
					WRITE_nVAR(FV.Other.gbLoginDate);
                }
			    continue;
        }*/           
        
        BUFCLR(szGetBuffer);
        VERIFY_NORMAL(PubGetPoscfgcPosState(szGetBuffer));

        if (szGetBuffer[0] == LOCK)
        {
            PubLock();
            continue;
        }
        PubGetPoscfgcMenuTime(&cMenuTime);
	    PubGetTermCapSet();     //add by chenjis 20130311

        while (1)
        {
			if (szCMDChangeAppFlag == 2)//强制切换到应用2
			{
				PubDelAllBit(&iso);
				close_all_dev();
				return 2;
			}
// 			PubSetExeReader(0);
			gbcECDefault = 0;
			PubDispIcon();
#ifdef TMS
			if (ProJudgeBaseDlaVer() == EM_SUCCESS)
			{		
				// for 主控应用更新,添加一个新返回值2
				ucRet = PubReturnUpdate() ;
				if (ucRet != EM_ERROR)
				{
					PubDelAllBit(&iso);
					close_all_dev();
					return ucRet;
				}
				if (PubCheckRunState() == EM_SUCCESS)
				{
					PubDelAllBit(&iso);
					close_all_dev();
					return EM_ERROR;
				}

			}
#endif
            PubSetPoscfgcSettleEnd(1);  /*设置结算未结束标志 */
            EA_vCls();
            nScreen = 0 ;
            //chenjs20060725 add down
            memset(&gbTrkInfo, 0, sizeof(struct TrackInfo));
            memset(&gbCardinfo, 0, sizeof(struct CardConfig));
            gbTrkInfo.cDoneFlag = 0;
			PubSetBankcMacChk(MAC_ECB);
#ifdef EMVIC          
            //ecash   by wei
			gblTranAmt = 0l;
			BUFCLR(gbszTermData);
			gbszTermData[0] = FV.EmvStru.gbcMSD;//loyes
#endif
			strcpy(gbszErrCode, "");

	        while (1)
	        {
				gbnComposFlag =1;
         		gbnSettleFlag=0;
				if (szCMDChangeAppFlag == 2)//强制切换到应用2
				{
					PubDelAllBit(&iso);
					close_all_dev();
					return 2;
				}
	            PubSetPoscfgcSettleEnd(1);  /*设置结算未结束标志 */
	            //EA_vCls();
	            nScreen = 0 ;
	            //chenjs20060725 add down
	            memset(&gbTrkInfo, 0, sizeof(struct TrackInfo));
	            memset(&gbCardinfo, 0, sizeof(struct CardConfig));
	            gbTrkInfo.cDoneFlag = 0;
				if (CMDCONT.Type == CMD_NONE) // CMD_NONE表示无命令			
				{
					nRet = GetCMD();
					if (szCMDChangeAppFlag == 15)
					{
						PubDelAllBit(&iso);
						close_all_dev();
						EA_ucCloseDevice(&gbhdCMDRS232);
						memcpy(CMDBUF , szCMDBuffer , CMDBUFLen);
						CMDBUF[CMDBUFLen] = 0 ;
						*nCmdLen = CMDBUFLen;
						return 15;
					}
					if (nRet != NORMAL)
					{
					
						//PubDisdoter();
	 					PubDispLogo();
						if( EA_ucKBHit_ped() == EM_key_HIT )
						{
						      uiKey = PubuiInkeyMs(500);
#ifdef BZ  /*标准版*/
						  if (uiKey == ENTER)
#endif
#ifdef Largevalue   /*大额支付*/
						   if (uiKey == ENTER)
#endif
#ifdef NMLS   /*内蒙移动零售*/
						   if (uiKey == EM_key_F1)     /*应内蒙银商要求F1进入查询菜单wujianyuan modify 20130712*/
#endif
#ifdef NMJF   /*内蒙移动缴费*/
						   if (uiKey == EM_key_F1)    /*应内蒙银商要求F1进入查询菜单wujianyuan modify 20130712*/
#endif 
						     {
							EA_vCls();
							PubDisplay(2, "请输入安全密码:");
							BUFCLR(szGetBuffer);
							PubGetSAFEPWD(szGetBuffer, sizeof(szGetBuffer));
							PubKeyOff();
							
							BUFCLR(szBuffer);
							if (PubGetNStr(3, BIGFONT, szBuffer, 6) == NORMAL &&
								memcmp(szGetBuffer, szBuffer, 6) == 0)
							{
								PubKeyOn();	
								EA_ucCloseDevice(&gbhdCMDRS232);
								gbhdCMDRS232 = EM_NULL;
								PubSetupNew();
								 EA_vCls();
							}	
							 EA_vCls();
							PubKeyOn();					
						
							}
							if(uiKey==EM_key_9)
							{
							if (PubuiInkey(1) == '5')
							if (PubuiInkey(1) == '9')
							{
		                                      EA_vSystemManage(EM_RESET);//add by wujy  20111020 实现重启
							}
							}
							if (uiKey == EM_key_FUNC9)  //多应用切换
							{
								EA_ucCloseDevice(&gbhdCMDRS232);
								gbhdCMDRS232 = EM_NULL;
								PubDelAllBit(&iso);
								close_all_dev();
								EA_vCls();
								return EM_SUCCESS;				
							}
							if (uiKey == EM_key_FUNC)
							{

								gbnComposFlag =0;
								memset(&TlvSendValue,0,sizeof(struct TLVVALUE));
								PubGetBankszTerminal(TlvSendValue.szTerminal, 8);
								PubGetBankszCust(TlvSendValue.szCust, 15);
						        BUFCLR(szGetBuffer);
						        VERIFY_NORMAL(PubGetPoscfgcPosState(szGetBuffer));

						        if (szGetBuffer[0] == NO_LOGON)
						        {
					            	if(PubLogin()!=NORMAL)
					        		{
										EA_vCls();
					        			continue;
									}
								
					        	}
						        BUFCLR(szGetBuffer);
						        VERIFY_NORMAL(PubGetPoscfgcLoginState(szGetBuffer));

						        if (szGetBuffer[0] == NO_LOGON)
					        	{
									VERIFY_NORMAL(PubSetPoscfgcPosState(NO_LOGON));
									if(PubBankLogin()== NORMAL)
									{
										VERIFY_NORMAL(PubSetPoscfgcPosState(LOGON));
						        		       VERIFY_NORMAL(PubSetPoscfgcLoginState(LOGON));
										EA_ucGetDateTime(&datetime);
										BUFCLR(FV.Other.gbLoginDate);
										sprintf(FV.Other.gbLoginDate , "%04d%02d%02d", datetime.usYear, datetime.ucMonth, datetime.ucDay);
										WRITE_nVAR(FV.Other.gbLoginDate);
									}
									else
					        		      {
										PubDialOff (&CommInfo);
										EA_vCls();
										continue;
									}
					        	}
								EA_vCls();
								PubDisplay (1, "1.消  费   2.撤  销");
								PubDisplay (2, "3.退  货   4.预授权");
								PubDisplay (3, "5.离  线   6.打  印");
								PubDisplay (4, "7.管  理   8.其  它");

								uiKey = PubuiInkey((char) cMenuTime);
								if(uiKey>='1'&&uiKey<='8')
									uiOption=uiKey-'0';
								else
								{
									EA_vCls();
									continue;
								}
								
								PubUpYLDataOld();

						              PubProcessOld(uiOption);
								EA_vCls();
								continue;				
							}						
						}
						continue;
					}
				}
				
				if (CMDCONT.Type != CMD_NONE)
				{
				break;
				}
				
	        }      

	         if (PubProcess(CMDCONT.Type) != CMD_INTERRUPT)
	         {
	 	     ClearCMD();
	         }
	         EA_vLCDLightOff();
	    }
	    return EM_SUCCESS;
	}
}
Esempio n. 7
0
/*
 * connection リスト各パケットを表示。
 * オプションフラグによっては通常出力に加えて、
 * sequence 番号チェックを表示
 */
int
view_conn()
{ 
    connection_t *conn;
    stream_t *streams;
    stream_t *streams_check;
    uint32_t exp_ack; /* packet が期待する ACK 値 */
    uint32_t self_seq; /* Sefl packet SEQ 値 */
    uint32_t len; /* TCP の data の長さ*/
    double stream_init_time; /* connection 毎の開始時間*/
    double receive_time; /* 個々の packet の到着時間 */
    double previous_time = 0; /* 一つ前の packet の到着時間 */
    double acked_elapse ; /* ack を受けるまでの時間 */
    uint32_t next_seq[2];   /* Diag 用の 一つ前のパケットまでの SEQ の進行状況 */
    
    /* conn_head は空なので、次から・・*/
    conn = conn_head->conn_next ;
    if(conn == NULL )
        return(0);

    printf("\n====================================\n");
    printf("        Check each connection                \n");
    printf("====================================\n");
    for( ; conn != NULL ; conn = conn->conn_next) {
        printf("\n====================================\n");
        printf("Number of packets  : %d\n\n", conn->conn_count);
        printf("Addr 0: %s : Port: %d",inet_ntoa(conn->addr0),conn->port0);	
        printf("\t\t\t\t\t");
        printf("Addr 1: %s : Port: %d\n",inet_ntoa(conn->addr1),conn->port1);	        
        printf("---------------------------------------------------------------");
        printf("----------------------------------------------------------------\n");
        stream_init_time = TIMEVAL_TO_SEC(conn->stream->plist->php->pktime);
        previous_time = stream_init_time;

        /*
         * 以下 個々の packet の処理
         */
        for(streams = conn->stream ; streams != NULL ; streams = streams->stream_next ){
            receive_time = TIMEVAL_TO_SEC(streams->plist->php->pktime);
            INDENT(streams);
            /*
             * 以下 summary 表示部
             */
            printf("%d: ",streams->plist->packet_number);
            printf("%5.3f ",receive_time - previous_time);
            if (ntohs(streams->ip->ip_off) & (8191)){
                /*
                 * fragment offset が 0 以外(fragmentしていて、TCP header が無い) IP data gram を判定
                 */
                printf(" IP fragment");
                printf(" IPID: %u",ntohs(streams->ip->ip_id));
                printf(" Len:%4d", IPLEN(streams));                
                printf(" Flag: 0x%x",ntohs(streams->ip->ip_off));
                printf(" Offset: %u",((ntohs(streams->ip->ip_off)) & (8191))<<3);
                if (ntohs(streams->ip->ip_off) & IP_MF)
                    printf(" MF");
                if (ntohs(streams->ip->ip_off) & IP_DF)
                    printf(" DF");
                printf("\n");
            } else {
                /*
                 * IP fragment していない、もしくは最初の fragment の tcp packet
                 */
                printf("%u",  SEQ(streams));            
                printf("(%u)", ACK(streams));            
                printf(" Win:%d", ntohs(streams->tcphdr->th_win));
                printf(" Len:%u", TCPLEN(streams));
                if (ntohs(streams->ip->ip_off) & IP_MF)
                    printf(" MF");
                if (ntohs(streams->ip->ip_off) & IP_DF)
                    printf(" DF");                                            
                printf(" ");
                if(streams->tcphdr->th_flags & TH_FIN)
                    printf("FIN ");
                if(streams->tcphdr->th_flags & TH_SYN)
                    printf("SYN ");
                if(streams->tcphdr->th_flags & TH_RST)
                    printf("RST ");
                if(streams->tcphdr->th_flags & TH_PUSH)
                    printf("PSH ");
                if(streams->tcphdr->th_flags & TH_ACK)
                    printf("ACK ");
                if(streams->tcphdr->th_flags & TH_URG)
                    printf("URG ");
                printf("\n");
            }
            previous_time = receive_time ;            
                //printf("\t\t%s\n",streams->direction ? "<------------------\n" : "------------------>\n");

            
            if(!(optflag & DIAG))
                continue;
            
            /************** ここからは DIAG フラグがついていた場合だけ ******************/

            /*
             * more fragmen が立っている packet は ack の調査をしない                            
             * なぜなら、fragment の途中では TCP segment としての total length がわからないので
             */
            if (ntohs(streams->ip->ip_off) & IP_MF){
                //INDENT(streams);
                //printf("\t> IP fragment packet.(can't check ack packet)\n");
                continue;
            }
            
            self_seq = SEQ(streams);
            /*
             * len は fragmen/non-fragment パケット双方の データ長が入る
             */
            len = TCPLEN(streams);

            /*
             * SACK(Selective Ack) Option の確認
             * TCP ヘッダー長が 5(=20bytes) より大きければ、なんらかの
             * TCP Option が設定されている
             */
            if(streams->tcphdr->th_off > 5 ){
                    uint16_t tcphdrlen;
                    char *tcpopt, *tcpopt_head;
                    uint8_t optlen;
                    struct sackval 
                    {
                        uint32_t leftedge;
                        uint32_t rightedge;
                    } *sackval;

                    tcphdrlen = streams->tcphdr->th_off <<2;
                    
                    tcpopt_head = tcpopt = (char *)streams->tcphdr + 20 ; 
                    while(*tcpopt != 0 && (tcpopt - tcpopt_head) < tcphdrlen - 20){
                        switch(*tcpopt){
                            case 1: /* NOP。次のoption へ*/
                                tcpopt++; 
                                if(optflag & VERBOSE) { INDENT(streams); printf("\t> NOP option found\n");}
                                break;
                            case 4: /* SACK Permitted option */
                                tcpopt = tcpopt + 2; /* SACK OK option 次の option へ */
                                INDENT(streams);                                                                
                                printf("\t> sack-permitted option found\n");
                                break;                                
                            case 5: /* SACK OPTION */{
                                int i;
                                char *pointer; /* 処理用のポインタ */

                                pointer = tcpopt + 1 + 1; /* type と lenght の分を進める*/
                                optlen = *(uint8_t *)(tcpopt + 1);
                                sackval = malloc(sizeof(struct sackval));

                                /*
                                 * Left edge と Right edige のペア(8bytes)が繋がっている。
                                 * optlen -2 / 8 回分だけループ
                                 */
                                for ( i = optlen - 2 ; i > 0 ; i = i - 8){
                                    memcpy(sackval, pointer, 8);
                                    INDENT(streams);                                
                                    printf("\t> sack = %u - %u\n", sackval->leftedge, sackval->rightedge);
                                    pointer = pointer + 8;
                                }
                                tcpopt = tcpopt + optlen;
                                free(sackval); /* もう使わないので free */
                                break;
                            }
                            default : /* 他の Option */
                                if(optflag & VERBOSE){ INDENT(streams); printf("\t> TCP option found\n");}
                                optlen = *(uint8_t *)(tcpopt + 1); 
                                tcpopt = tcpopt + optlen; 
                                break;
                        } /* switch end */
                    } /* while end */
                } /* if tcphdr > 20 end */
            
            /*
             * 次にくると期待されていた SEQ と、このパケットの SEQ を比較
             * もし、期待値よりも大きければ、順番が入れ替わったか、または
             * パケットのドロップの可能性がある
             */
            if(conn->snd_nxt[streams->direction] == 0){
                    /* 期待する SEQ(SND_NXT)が 0 つまりここは snoop でのこの TCP */
                    /* connction の最初の packet だけが該当する                  */
                conn->snd_nxt[streams->direction] = SEQ(streams) + len + SYNFIN(streams->tcphdr);
            } else {
                next_seq[streams->direction] = conn->snd_nxt[streams->direction];
                if( next_seq[streams->direction] < SEQ(streams))
                {
                    /* 期待しているより、大きい SEQ 番号がきた */
                    INDENT(streams);
                    printf("\t> out of order data packet. expected SEQ = %u\n",next_seq[streams->direction]);
                    
                }
                else if (next_seq[streams->direction] == SEQ(streams)){
                    /* 期待通りのパケットが来た。snd_nxt を更新                       */
                    conn->snd_nxt[streams->direction] = SEQ(streams) + len + SYNFIN(streams->tcphdr);
                }
                else{
                        /* 期待値よりも小さい SEQ。再送?*/

                    INDENT(streams);
                    printf("\t> retransmission packet?\n");
                }

                /*
                 * これより前の packet に SND_NXT の SEQ が含まれているか調べる & 再送のチェック
                 * TODO:ここはデータがある全 packet が通るため、大変負荷が高い。要改善
                 */
                if( len > 0){
                    for(streams_check =  conn->stream ; streams_check != streams ; streams_check = streams_check->stream_next){
                         /* Self packet のみをチェック*/
                        if (streams_check->direction != streams->direction) 
                            continue;
                        /* last fragment(IP_MF の立ってない)パケットのみをチェック */
                        if (ntohs(streams_check->ip->ip_off) & IP_MF) 
                            continue;

                        /* データがあって、SEQ と SND_NXT が同じ packet を調べる*/	
                        if ( TCPLEN(streams_check) > 0 && SEQ(streams_check) == conn->snd_nxt[streams->direction]){
                            conn->snd_nxt[streams->direction] =
                                SEQ(streams_check) + TCPLEN(streams_check)  + SYNFIN(streams_check->tcphdr);
                            INDENT(streams);
                            printf("\t> SEQ = %u was already sent by pakcet %d\n",
                                   SEQ(streams_check), streams_check->plist->packet_number);
                                /* まだ、他にもあるかもしれないので、最初から調べ直す。*/
                            streams_check = conn->stream;
                            continue;
                        }
                        /* SEQ が同じで、データを持っている packet を調べる*/	
                        if( (SEQ(streams_check) == self_seq) && (TCPLEN(streams_check) != 0)){
                            INDENT(streams);
                            printf("\t> may retransmission packet of packet%d\n",streams_check->plist->packet_number);
                        }
                    } /* 以前の packet のチェックのループ終わり*/
                } /* もしデータがあったら・・*/
                
            }/* if SND_NXT == 0 else .. end */

            
            if(streams->stream_next == NULL){ /* つぎの packet が無い*/
                INDENT(streams);
                printf("\t> ...won't check ack packet. No more packets\n");
                continue;
            }            

            /*
             * まず、ACK が必要かどうか(データ有り、または FIN or SYN)を確認 
             * 必要無ければ、他の packet を調べない。必要なら次の for() ループへ
             */
            if(len != 0){
                /*
                 * データがある場合
                 */
                exp_ack = SEQ(streams) + len ;
                if(SYNFIN(streams->tcphdr)){
                    /*
                     * FIN or SYN の場合
                     */
                    exp_ack++;
                }
                INDENT(streams);                
                printf("\t> expecting ACk = %u\n",exp_ack);
            } else {
                /*
                 * データがない場合
                 */                
                if(SYNFIN(streams->tcphdr)){
                    /*
                     * FIN or SYN の場合
                     */
                    exp_ack = SEQ(streams) + 1;
                    INDENT(streams);
                    printf("\t> expecting ACK = %u\n",exp_ack);
                } else {
                    /*
                     * ただの ACK パケット。
                     * なので、以下のACK のチェックも、再送のチェックも行わない
                     */
                    INDENT(streams);
                    printf("\t> doesn't expect to be acked\n");
                    continue;
                }
            }

            /*
             * これより後の packet から期待する ACK があるかどうかをチェック
             */
            for(streams_check = streams->stream_next ; streams_check != NULL ; streams_check = streams_check->stream_next){
                if (streams_check->direction == streams->direction){ /* 相手からの packet のみをチェック*/
                    if(streams_check->stream_next == NULL){ /* もしこれが最後の packet なら ACK されていないと言うこと*/
                        INDENT(streams);                        
                        printf("\t> not acked!!!\n");
                    }
                    continue;
                }

                /* 相手からの packet の fragment の有無をチェックする必要は無い。*/
                /* 期待する ack をもつ packet を調べる*/	
                if( ACK(streams_check) == exp_ack ){
                    acked_elapse = TIMEVAL_TO_SEC(streams_check->plist->php->pktime);
                    INDENT(streams);
                    printf("\t> exactly acked by %d(%f Sec)\n",
                           streams_check->plist->packet_number, acked_elapse - receive_time);
                    break;
                } else if (ACK(streams_check) > exp_ack ){
                    acked_elapse = TIMEVAL_TO_SEC(streams_check->plist->php->pktime);
                    INDENT(streams);
                    printf("\t> acked by %d(%f Sec)\n",
                           streams_check->plist->packet_number,acked_elapse - receive_time);
                    break;
                }

                if(streams_check->stream_next == NULL){ /* 最後まで来たら ACK されていないと言うこと*/
                    INDENT(streams);
                    printf("\t> not acked!!!\n");
                }
            } /* loop for searching ack end */
        } /* loop for stream end */
    } /* loop for connection list end */
}
#define AC_NO_PLUS KC_MINS
#define AC_NO_BSLS KC_EQUAL
#define AC_NO_QUOTE KC_BSLS
#define AC_NO_MINS KC_SLSH
#define AC_NO_PIPE KC_GRAVE


const uint16_t PROGMEM actionmaps[][MATRIX_ROWS][MATRIX_COLS] = {
  KEYMAP( /* 0: mostly letters */
    Q,   W,         E,         R,         T,   					   /*|,, |*/         Y,              U,         I,   	          O,    P,    \
    A,   S,         D,         F,         G,   					   /*|,, |*/         H,              J,         K,   	          L,    COMM, \
		Z,   X,         C,         V,         B,               /*|,, |*/         N,              M,         COMM,           DOT,  KP_SLASH, \
    ESC, TAPT(3),   OSM(LGUI), OSM(LSFT), MK(LCTL,BSPC), TAPT(1), TAPT(2),   MK(LALT,SPC),   TACK(TAB), TACSK(SLSH), SH(2), ENT
  ),
  KEYMAP( /* layer one is mostly for programming and shell. lots of idea shortcute on left, not sure how much i will use them.*/
    ACK(7),  CTRL(W),    NO_AE,      SH(F10),     ACSK(L),                     SH(NO_ACNT), RA(7), RA(0), NO_OE,   SH(1), \
    NO_AA,   ACK(LEFT),  CSK(ENT),   ACK(RIGHT),  ALT(INS),                    0,           SH(8), SH(9), NO_PIPE, RA(4), \
    CSK(A),  CSK(F),     ACSK(T),    ALT(V),      CTRL(F9),                    SH(NO_BSLS), RA(8), RA(9), SH(6),   RA(NO_ACNT), \
    ESC,     TRNS,       OSM(LGUI),  OSM(LSFT),   TRNS,    TRNS,    TRNS,      SPC,         OFF(1),RA(2), NO_QUOTE,KP_EQUAL
  ),
  KEYMAP( /* hold space brings up move pad and numpad */
    INS,      HOME,   UP  ,      END  ,     PGUP,                          SH(5),         7     , 8  , 9, SH(NO_QUOTE),     \
    DEL,      LEFT,   DOWN,      RIGHT,     PGDN,                          NO_MINS,       4     , 5  , 6, NO_PLUS, \
    GUI(1),   GUI(2), GUI(3),    GUI(4),    GUI(5),                        RA(4),         1     , 2  , 3, NO_BSLS,      \
    ACK(DEL), TRNS, OSM(LGUI), OSM(LSFT),   GUI(D) , TRNS, TRNS,           MK(LALT,SPC),  OFF(1), DOT, 0, KP_EQUAL
  ),
  KEYMAP( /* hold tab to have fpad and mouse */
    F1,     F2,    F3,        F4,         F5,                             WH_D ,           BTN1   ,  MS_U ,         BTN2 ,      SH(3) , \
    F6,     F7,    F8,        F9,         F10,                            WH_U ,           MS_L   ,  MS_D ,         MS_R ,      BTN3 , \
    F11,    F12,   F13,       F14,        BOOT,                           NO   ,           ACL0   ,  NO_LT,         SH(NO_LT) , BTN4 , \
    BOOT, TRNS,   OSM(LGUI), OSM(LSFT),   MK(LCTL,BSPC), TRNS, TRNS,      MK(LALT,SPC),    TACK(TAB),TACSK(MINS) , SH(NO_BSLS), KP_EQUAL
Esempio n. 9
0
/*
 *	Write captured packet to file
 *
 *	uint8_t *flag : filter is set or not
 *	const uint8_t *packet : captured packet
 *	char *fltrload : filter for payload
 *	cosnt int payloadlen : length of captured payload
 */
static void printfile(uint8_t *flag, const uint8_t *packet, char *fltrload, const int payloadlen)
{
	FILE *fp = fopen(LOGFILE, "a");
	const uint8_t *http = NULL;
	char *timestamp;
	struct ether_header *eth;
	struct ip *iphdr;
	struct tcphdr *tcphdr;
	struct udphdr *udphdr;
	uint8_t prot;
	int hdrlen, i;

	flag += 2;  // remove unused member

	eth = (struct ether_header *)packet;
	hdrlen = sizeof(struct ether_header);

	iphdr = (struct ip *)(packet + hdrlen);
	hdrlen += sizeof(struct ip);

	prot = iphdr->ip_p;
	if( prot == 17 ) {
		udphdr = (struct udphdr *)(packet + hdrlen);
		hdrlen += sizeof(struct udphdr);
	}else {
		tcphdr = (struct tcphdr *)(packet + hdrlen);
		hdrlen += sizeof(struct tcphdr);

		// HTTP request check 
		if( PROTOCOL(flag) ) {
			if( prot == 3 ) {
				if( HTTPTEST("GET",3) == false )
					return;

			}else if( prot == 5 ) {
				if( HTTPTEST("PUT",3) == false )
					return;

			}else if(prot == 7) {
				if( HTTPTEST("POST",4) == false )
					return;
			}
		}
	}

	
	

	// Timestamp
	timestamp = (char *)gettime();
	fprintf(fp, "%s ------\n", timestamp);
	free(timestamp);

	/* Ethernet Header */
	i = 0;
	if( DSTMAC(flag) ) {
		fprintf(fp, "\x1b[45mDestination MAC\x1b[0m\t: \x1b[45m");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_dhost[i++]);
		fprintf(fp, "%02X\x1b[0m\n", eth->ether_dhost[i]);
	}else {
		fprintf(fp, "Destination MAC\t: ");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_dhost[i++]);
		fprintf(fp, "%02X\n", eth->ether_dhost[i]);
	}

	i = 0;
	if( SRCMAC(flag) ) {
		fprintf(fp, "\x1b[45mSource MAC\x1b[0m\t: \x1b[45m");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_shost[i++]);
		fprintf(fp, "%02X\x1b[0m\n", eth->ether_shost[i]);
	}else {
		fprintf(fp, "Source MAC\t: ");
		while( i < 5 )
			fprintf(fp, "%02X:", eth->ether_shost[i++]);
		fprintf(fp, "%02X\n", eth->ether_shost[i]);
	}

	if( ETHERTYPE(flag) )
		fprintf(fp, "\x1b[45mEthernet Type\x1b[0m\t: \x1b[45m%s\x1b[0m\n", gettype(eth->ether_type));
	else
		fprintf(fp, "Ethernet Type\t: %s\n", gettype(eth->ether_type));

	/* IP Header */
	if( VERSION(flag) & 0xf0 )
		fprintf(fp, "\x1b[44mVersion\x1b[0m\t\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_v);
	else
		fprintf(fp, "Version\t\t: %d\n", iphdr->ip_v);

	if( IPHLEN(flag) & 0x0f )
		fprintf(fp, "\x1b[44mIP Header length\x1b[0m: \x1b[44m%d\x1b[0m\n", iphdr->ip_hl);
	else
		fprintf(fp, "IP Header length: %d\n", iphdr->ip_hl);

	if( TOS(flag) )
		fprintf(fp, "\x1b[44mType of Service\x1b[0m\t: \x1b[44m%s\x1b[0m\n", gettos(iphdr->ip_tos));
	else
		fprintf(fp, "Type of Service\t: %s\n", gettos(iphdr->ip_tos));

	if( IPLEN(flag) )
		fprintf(fp, "\x1b[44mTotal length\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_len));
	else
		fprintf(fp, "Total length\t: %d\n", ntohs(iphdr->ip_len));

	if( IPID(flag) )
		fprintf(fp, "\x1b[44mIdentification\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_id));
	else
		fprintf(fp, "Identification\t: %d\n", ntohs(iphdr->ip_id));

	if( FRAGMENT(flag) )
		fprintf(fp, "\x1b[44mFragment\x1b[0m\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_off);
	else
		fprintf(fp, "Fragment\t: %d\n", iphdr->ip_off);

	if( TTL(flag) )
		fprintf(fp, "\x1b[44mTime to live\x1b[0m\t: \x1b[44m%d\x1b[0m\n", iphdr->ip_ttl);
	else
		fprintf(fp, "Time to live\t: %d\n", iphdr->ip_ttl);

	if( PROTOCOL(flag) )
		fprintf(fp, "\x1b[44mProtocol\x1b[0m\t: \x1b[44m%s\x1b[0m\n", getprot(iphdr->ip_p));
	else
		fprintf(fp, "Protocol\t: %s\n", getprot(iphdr->ip_p));

	if( IPCKSUM(flag) )
		fprintf(fp, "\x1b[44mChecksum\x1b[0m\t: \x1b[44m%d\x1b[0m\n", ntohs(iphdr->ip_sum));
	else
		fprintf(fp, "Checksum\t: %d\n", ntohs(iphdr->ip_sum));

	if( SRCIP(flag) )
		fprintf(fp, "\x1b[44mSource IP\x1b[0m\t: \x1b[44m%s\x1b[0m\n", inet_ntoa(iphdr->ip_src));
	else
		fprintf(fp, "Source IP\t: %s\n", inet_ntoa(iphdr->ip_src));

	if( DSTIP(flag) )
		fprintf(fp, "\x1b[44mDestination IP\x1b[0m\t: \x1b[44m%s\x1b[0m\n", inet_ntoa(iphdr->ip_dst));
	else
		fprintf(fp, "Destination IP\t: %s\n", inet_ntoa(iphdr->ip_dst));

	if( iphdr->ip_p == 17 ) {
	/* UDP Header */
		if( SRCPORT(flag) )
			fprintf(fp, "\x1b[42mSource Port\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->source));
		else
			fprintf(fp, "Source Port\t: %d\n", ntohs(udphdr->source));

		if( DSTPORT(flag) )
			fprintf(fp, "\x1b[42mDestination Port\x1b[0m: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->dest));
		else
			fprintf(fp, "Destination Port: %d\n", ntohs(udphdr->dest));

		if( UDPLEN(flag) )
			fprintf(fp, "\x1b[42mTotal length\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->len));
		else
			fprintf(fp, "Total length\t: %d\n", ntohs(udphdr->len));

		if( UDPCKSUM(flag) )
			fprintf(fp, "\x1b[42mCheckSum\x1b[0m\t: \x1b[42m%d\x1b[0m\n", ntohs(udphdr->check));
		else
			fprintf(fp, "Checksum\t: %d\n", ntohs(udphdr->check));
	}else {
	/* TCP Header */
		if( SRCPORT(flag) )
			fprintf(fp, "\x1b[31;43mSource Port\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->source));
		else
			fprintf(fp, "Source Port\t: %d\n", ntohs(tcphdr->source));

		if( DSTPORT(flag) )
			fprintf(fp, "\x1b[31;43mDestination Port\x1b[0m: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->dest));
		else
			fprintf(fp, "Destination Port: %d\n", ntohs(tcphdr->dest));

		if( SEQ(flag) )
			fprintf(fp, "\x1b[31;43mSequence Number\x1b[0m\t: \x1b[31;43m%u\x1b[0m\n", ntohl(tcphdr->seq));
		else
			fprintf(fp, "Sequence Number\t: %u\n", ntohl(tcphdr->seq));

		if( ACK(flag) )
			fprintf(fp, "\x1b[31;43mAcknowledgement\x1b[0m\t: \x1b[31;43m%u\x1b[0m\n", ntohl(tcphdr->ack));
		else
			fprintf(fp, "Acknowledgement\t: %u\n", ntohl(tcphdr->ack));

		if( TCPOFF(flag) & 0x0f )
			fprintf(fp, "\x1b[31;43mOffset\x1b[0m\t\t: \x1b[31;43m%d\x1b[0m\n", tcphdr->doff);
		else
			fprintf(fp, "Offset\t\t: %d\n", tcphdr->doff);

		if( TCPRES(flag) & 0xf0 )
			fprintf(fp, "\x1b[31;43mReserved\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", tcphdr->res1);
		else
			fprintf(fp, "Reserved\t: %d\n", tcphdr->res1);

		char *tmp = getflag(*(packet + 47), tcphdr->res1, tcphdr->res2);
		if( TCPFLAG(flag) )
			fprintf(fp, "\x1b[31;43mFlags\x1b[0m\t\t: \x1b[31;43m%s\x1b[0m\n", tmp);
		else
			fprintf(fp, "Flags\t\t: %s\n", tmp);
		free(tmp);

		if( WINDOW(flag) )
			fprintf(fp, "\x1b[31;43mWindow size\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->window));
		else
			fprintf(fp, "Window size\t: %d\n", ntohs(tcphdr->window));

		if( TCPCKSUM(flag) )
			fprintf(fp, "\x1b[31;43mChecksum\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->check));
		else
			fprintf(fp, "Checksum\t: %d\n", ntohs(tcphdr->check));

		if( URGPTR(flag) )
			fprintf(fp, "\x1b[31;43mUrgent Pointer\x1b[0m\t: \x1b[31;43m%d\x1b[0m\n", ntohs(tcphdr->urg_ptr));
		else
			fprintf(fp, "Urgent Pointer\t: %d\n", ntohs(tcphdr->urg_ptr));
	}

	/* Payload */
	if( fltrload != NULL ) {
		const uint8_t *match = NULL;
		uint8_t highlight[MAXPAYLOAD];
		int fltrloadlen, unmatchlen = 0;
		int colorhex = 0, colorstr = 0;
		int i = 0, str = 0, colorlen = 0, highlen = 0;
		char ch;

		memset(highlight, 0, MAXPAYLOAD);

		packet += hdrlen;
		fltrloadlen = strlen(fltrload);

		fprintf(fp, "*** Payload ***\n");

		if( http != NULL ) {
			if( prot == 3 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "GET\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 5 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "PUT\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 7 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "POST\x1b[0m", 8);
				highlen += 13;
				i += 4;
			}
		}

		while( i < payloadlen ) {
			// move to next matching string
			match = memcmp_cont(packet + i, fltrload, fltrloadlen, payloadlen - i);
			if( match == NULL ) {
				unmatchlen = payloadlen - i;

				memcpy(highlight + highlen, packet + i, unmatchlen);
				highlen += unmatchlen;

				break;
			}else {

				if( http != NULL && match - packet > http - packet ) {
				// HTTP/1.1
					unmatchlen = http - (packet + i);

					memcpy(highlight + highlen, packet + i, unmatchlen);
					highlen += unmatchlen;
					i += unmatchlen;

					memcpy(highlight + highlen, "\x1b[46mHTTP/1.1\x1b[0m", 17);
					highlen += 17;
					i += 8;
					http = NULL;
				}else {
					unmatchlen = match - (packet + i);

					memcpy(highlight + highlen, packet + i, unmatchlen);
					highlen += unmatchlen;
					i += unmatchlen;

					memcpy(highlight + highlen, PAYLOADCOLOR, 5);
					highlen += 5;

					memcpy(highlight + highlen, packet + i, fltrloadlen);
					highlen += fltrloadlen;
					i += fltrloadlen;

					memcpy(highlight + highlen, NORMALCOLOR, 4);
					highlen += 4;
				}
			}
		}

		i = 0;
		while( i < highlen ) {
			ch = *(highlight + i);

			if( ch == '\x1b' ) {
				if( COLORTEST(i, PAYLOADCOLOR) == true ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 41;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, HTTPCOLOR) == true ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 46;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, NORMALCOLOR) == true ) {
					fprintf(fp, NORMALCOLOR);
					colorhex = 0;
					i += 4;
					colorlen += 4;
					continue;
				}
			}

			fprintf(fp, "%02X ", ch);
			i++;

			if( ++str == 16 ) {
				if( colorhex != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "   ");

				if( colorstr == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorstr = 0;

				}else if( colorstr == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorstr = 0;
				}

				while( colorlen > 0 ) {
					ch = *(highlight + i - colorlen - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;

							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;

							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;

							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					colorlen--;
				}

				while( str > 0 ) {
					ch = *(highlight + i - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					str--;
				}

				if( colorstr != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "\n");

				if( colorhex == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 0;

				}else if( colorhex == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 0;
				}
			}
		}

		if( str != 0 ) {
			int padd = 17;
			if( colorhex != 0 )
				fprintf(fp, NORMALCOLOR);

			while( str < padd-- )
				fprintf(fp, "   ");

			if( colorstr == 41 ) {
				fprintf(fp, PAYLOADCOLOR);
				colorstr = 0;

			}else if( colorstr == 46 ) {
				fprintf(fp, HTTPCOLOR);
				colorstr = 0;
			}

			while( colorlen > 0 ) {
				ch = *(highlight + i - colorlen - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				colorlen--;
			}

			while( str > 0 ) {
				ch = *(highlight + i - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				str--;
			}
			fprintf(fp, "\x1b[0m\n");
		}
	}else { // fltrload == NULL
		uint8_t highlight[MAXPAYLOAD];
		int unmatchlen = 0;
		int colorhex = 0, colorstr = 0;
		int i = 0, str = 0, colorlen = 0, highlen = 0;
		char ch;

		memset(highlight, 0, MAXPAYLOAD);

		packet += hdrlen;

		fprintf(fp, "*** Payload ***\n");

		if( http != NULL ) {
			if( prot == 3 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "GET\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 5 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "PUT\x1b[0m", 7);
				highlen += 12;
				i += 3;

			}else if( prot == 7 ) {
				memcpy(highlight, HTTPCOLOR, 5);
				memcpy(highlight + 5, "POST\x1b[0m", 8);
				highlen += 13;
				i += 4;
			}
		}

		while( i < payloadlen ) {
		// move to next matching string
			if( http != NULL ) {
			// HTTP/1.1
				unmatchlen = http - (packet + i);

				memcpy(highlight + highlen, packet + i, unmatchlen);
				highlen += unmatchlen;
				i += unmatchlen;

				memcpy(highlight + highlen, "\x1b[46mHTTP/1.1\x1b[0m", 17);
				highlen += 17;
				i += 8;
				http = NULL;
			}else {
				memcpy(highlight + highlen, packet + i, payloadlen - i);
				highlen += unmatchlen;
				i += unmatchlen;
			}
		}

		i = 0;
		while( i < highlen ) {
			ch = *(highlight + i);

			if( ch == '\x1b' ) {
				if( COLORTEST(i, PAYLOADCOLOR) == true ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 41;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, HTTPCOLOR) == true ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 46;
					i += 5;
					colorlen += 5;
					continue;

				}else if( COLORTEST(i, NORMALCOLOR) == true ) {
					fprintf(fp, NORMALCOLOR);
					colorhex = 0;
					i += 4;
					colorlen += 4;
					continue;
				}
			}

			fprintf(fp, "%02X ", ch);
			i++;

			if( ++str == 16 ) {
				if( colorhex != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "   ");

				if( colorstr == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorstr = 0;

				}else if( colorstr == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorstr = 0;
				}

				while( colorlen > 0 ) {
					ch = *(highlight + i - colorlen - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					colorlen--;
				}

				while( str > 0 ) {
					ch = *(highlight + i - str);
					if( ch > 126 || ch < 32 ) {
						if( ch == 27 ) {
							if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
								colorstr = 41;
							else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
								colorstr = 46;
							else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
								colorstr = 0;
							else
								ch = '.';
						}else
							ch = '.';
					}
					fprintf(fp, "%c", ch);
					str--;
				}

				if( colorstr != 0 )
					fprintf(fp, NORMALCOLOR);

				fprintf(fp, "\n");

				if( colorhex == 41 ) {
					fprintf(fp, PAYLOADCOLOR);
					colorhex = 0;

				}else if( colorhex == 46 ) {
					fprintf(fp, HTTPCOLOR);
					colorhex = 0;
				}
			}
		}

		if( str != 0 ) {
			int padd = 17;
			if( colorhex != 0 )
				fprintf(fp, NORMALCOLOR);

			while( str < padd-- )
				fprintf(fp, "   ");

			if( colorstr == 41 ) {
				fprintf(fp, PAYLOADCOLOR);
				colorstr = 0;

			}else if( colorstr == 46 ) {
				fprintf(fp, HTTPCOLOR);
				colorstr = 0;
			}

			while( colorlen > 0 ) {
				ch = *(highlight + i - colorlen - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				colorlen--;
			}

			while( str > 0 ) {
				ch = *(highlight + i - str);
				if( ch > 126 || ch < 32 ) {
					if( ch == 27 ) {
						if( COLORTEST(i - colorlen - str, PAYLOADCOLOR) == true )
							colorstr = 41;
						else if( COLORTEST(i - colorlen - str, HTTPCOLOR) == true )
							colorstr = 46;
						else if( COLORTEST(i - colorlen - str, NORMALCOLOR) == true )
							colorstr = 0;
						else
							ch = '.';
					}else
						ch = '.';
				}
				fprintf(fp, "%c", ch);
				str--;
			}
			fprintf(fp, "\x1b[0m\n");
		}
	}
	fprintf(fp, "----------------------------\n");
	fclose(fp);
}
Esempio n. 10
0
static bool
std_req(setup_t *sp)
{
  switch(sp->req)
  {
    case SR_SET_ADDRESS:
      PUTS("set_addr\n");
      rregn(FNADDR, NULL, 0, true);
      break;
    case SR_GET_DESCRIPTOR:
      PUTS("get_desc: ");
      usb_send_desc(sp);
      break;
    case SR_SET_FEATURE:
      /* this either sets that RWU is to be enabled or to halt an EP,
       * which is mandatory for bulk and interrupt EPs */
      PUTS("set_feature\n");
      break;
    case SR_CLEAR_FEATURE:
      /* clear one of the features which have been set by SET_FEATURE */
      PUTS("clear_feature\n");
      break;
    case SR_GET_STATUS:
      PUTS("get_status\n");
      get_status(sp);
      break;
    case SR_SET_INTERFACE:
      /* this is used to set alternative interfaces. For example when
       * the CDC device will be put up */
      PUTS("setif");
      ACK();
      break;
    case SR_GET_INTERFACE:
      PUTS("getif");
      /* always report the same alternative: 1 */
      {
        uint8_t i = 0x01;
        reg_t st = {0x00};
        do { rreg(EPIRQ, &st); } while( !st.EPIRQ.IN0BAVIRQ );
        wregn(EP0FIFO, &i, sizeof(uint8_t), false);
        i=sizeof(uint8_t);
        wregn(EP0BC, &i, i, true);
      }
      break;
    case SR_SET_CONFIGURATION:
      PUTS("set_conf\n");
      usbdev.status.configuration = sp->asconfreq.conf;
      ACK();
      return true;
      break;
    case SR_GET_CONFIGURATION:
      PUTS("get_conf\n");
      {
        uint8_t len = sizeof(usbdev.status.configuration);
        reg_t st = {0x00};
        do { rreg(EPIRQ, &st); } while( !st.EPIRQ.IN0BAVIRQ );
        wregn(EP0FIFO, &usbdev.status.configuration, len, false);
        wregn(EP0BC, &len, sizeof(len), true);
      }
      break;
    default:
      PUTS("unknown std_req()");
      STALLEP0();
  }

  return false;
}