Ejemplo n.º 1
0
int ReadString2(SOCKET s, char *buffer)
{
	int		iRead = 0;
	char	response[1025] = {0};

	memset(buffer, 0, 1024);
	int ret = RESPONSE_WAIT(s, 2);
	if(ret > 0)
	{
		iRead = recv(s, response, 1024, 0);
		if(iRead <= 0)
		{
			return -1;
		}
		response[iRead] = 0;

		strncpy(buffer, response, iRead);

		return *buffer - 0x30;
	}
	else if(ret == 0)
	{
		strcpy(buffer, "Timeout");
		return 9;
	}
	else
	{
		strcpy(buffer, "Select Error");
		return 10;
	}
}
Ejemplo n.º 2
0
int cisco_command(SOCKET hSocket, char *cmd, char *prompt,LPGDATA pgd)
{
    int		n = 0;
    char	buffer[4096] = {0};
    char	*scan = NULL;
    int		flag = 0;

    strcpy(buffer, cmd);
    strcat(buffer, "\r\n");

    send(hSocket, buffer, strlen(buffer), 0);

    memset(pgd->databuffer, 0, BUF_SIZE);
    pgd->datalen = 0;
    while(RESPONSE_WAIT(hSocket, READ_TIMEOUT))
    {
        memset(buffer, 0, sizeof(buffer));
        n = recv(hSocket, buffer, 4096, 0);
        if(n == SOCKET_ERROR) return -2;
        if(n == 0) return -2;
        buffer[n] = 0;

        if(strstr(pgd->databuffer, cmd))
        {
            if(strstr(buffer, prompt))
            {
                char	buf[255] = {0};
                strncpy(buf, buffer + n - strlen(prompt), strlen(prompt));
                if(!strcmp(buf, prompt)) flag = 1;
            }
        }

        scan = buffer;
        while(n--)
        {
            telnet_protocol(hSocket, *scan,pgd);
            TRACE("%c", *scan);
            scan ++;
        }

        char *ca = NULL;
        if(ca = strstr(pgd->databuffer, "--More--"))
        {
            memset(ca, 0x20, 8);
            send(hSocket, " ", 1, 0);
        }

        if(flag) break;
    }

    return pgd->datalen;
}
Ejemplo n.º 3
0
int WaitVersionInfo(SOCKET s, int nSec)
{
	int		n = 0;
	char	*ca = NULL, *cb = NULL;
	char	buffer[1024] = {0};

	memset(buffer, 0, 1024);
	ca = buffer;
	while(RESPONSE_WAIT(s, nSec))
	{
		char	response[1025] = {0};
		n = recv(s, response, 1024, 0);
		if(n <= 0)
			return 1;

		response[n] = 0;
		strncat(buffer, response, n);

w:		if(isdigit(*ca) && isdigit(*(ca+1)) && isdigit(*(ca+2)) && *(ca+3) == ' ')
		{
			if(*ca - 0x30 == 0x02)
				return 0;

			return 1;
		}
		else
		{
			if(cb = strstr(ca, "\r\n"))
			{
				ca = cb + 2;
				goto w;
			}
		}
	}
	/*
	char	buffer[1024] = {0};

	if(RESPONSE_WAIT(s, nSec))
	{
		int n = recv(s, buffer, 1024, 0);
		if(n <= 0)
			return 1;

		return 0;
	}
	*/
	return 1;
}
Ejemplo n.º 4
0
int FTP_COMMAND(SOCKET s, CString strCmd, CString &strMessage)
{
	int		iRead = 0;
	char	buff[1024] = {0};
	int		iSent = 0;
	char	*ca = NULL, *cb = NULL;
	
	iSent = send(s, strCmd, strCmd.GetLength(), 0);
	if(iSent == SOCKET_ERROR)
		return -1;

	memset(buff, 0, 1024);
	ca = buff;
	while(RESPONSE_WAIT(s, 20))
	{
		char	response[1025] = {0};
		iRead = recv(s, response, 1024, 0);
		if(iRead == SOCKET_ERROR)
			return -1;
		if(iRead == 0)
			return -1;
		response[iRead] = 0;
		strncat(buff, response, iRead);

w:		if(isdigit(*ca) && isdigit(*(ca+1)) && isdigit(*(ca+2)) && *(ca+3) == ' ')
		{
			strMessage = buff;
			return *ca - 0x30;
		}
		else
		{
			if(cb = strstr(ca, "\r\n"))
			{
				ca = cb + 2;
				goto w;
			}
			else if(iRead == 0)
			{
				return -1;
			}
		}
	}

	return -1;
}
Ejemplo n.º 5
0
int SVUnixOpMonitor::telnet_command()
{
	int		n = 0;
	char	buffer[4096] = {0};
	char	*scan = NULL;
	char	*ca = NULL, *cb = NULL;

	sprintf(buffer, "echo %s;%s;echo %s\r\n", C_STA, strCmd, C_END);

	send(hSocket, buffer, strlen(buffer), 0);

	memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
	pgd->datalen = 0;
	while(RESPONSE_WAIT(hSocket, READ_TIMEOUT))
	{
		n = recv(hSocket, buffer, 4095, 0);
		if(n == SOCKET_ERROR) return TELNETCOMMANDERROR;
		if(n == 0) return TELNETCOMMANDERROR;
		buffer[n] = 0;

		scan = buffer;
		while(n--) telnet_protocol(hSocket, *scan++,pgd);
	
		char match_1[20] = {0}, match_2[20] = {0};
		sprintf(match_1, "\r\n%s", C_STA);
		sprintf(match_2, "\r\n%s", C_END);
		if(ca = strstr(pgd->databuffer, match_1)) 
		{
			if(cb = strstr(pgd->databuffer, match_2))
			{
				ca += strlen(C_STA) + 4;
				cb += 2;
				strncpy(pgd->databuffer, ca, cb - ca);
				pgd->databuffer[cb - ca] = 0;
				break;
			}
		}
	}

	pgd->datalen=strlen(pgd->databuffer);
	return TELNETSUCCESS;
}
Ejemplo n.º 6
0
BOOL PORT_MONITOR(char *server, int port, char *sendstring, char *matchstring, 
				  int timeout, char *pserver, int pport, char *puid, char *ppwd, 
				  char *custpath, char *szReturn)
{
	DWORD	dwBeginTime = 0,
			dwEndTime = 0;
	int		n = 0, ret = 0;
	SOCKET	s = 0;

	dwBeginTime = GetTickCount();

	// Create Socket
	s = socket(AF_INET,SOCK_STREAM,0);
	if (s == INVALID_SOCKET)
	{
		// Create Socket Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_1%>"));//<%IDS_Port_1%>
		return FALSE;
	}

	if(*pserver)
	{
		if(socks5_protocol(s, server, port, pserver, pport, puid, ppwd, timeout))
		{
			// Sock5 Proxy Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_2%>"));//<%IDS_Port_2%>
			return FALSE;
		}
	}
	else
	{
		struct sockaddr_in	serv;
		struct hostent *hp;

		hp = gethostbyname(server);
		if(hp == NULL)	
		{
			// DNS Resolve Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_3%>"));//<%IDS_Port_3%>
			return FALSE;
		}

		// Connect to the host specialed nonblocking
		memcpy(&serv.sin_addr, hp->h_addr_list[0], hp->h_length);
		serv.sin_family = hp->h_addrtype;
		serv.sin_port=htons(port);
		if (connect_timeo(s, (struct sockaddr *)&serv, sizeof(serv), timeout))
		{
			shutdown(s, 0x02);
			closesocket(s);
			// Server Port Connect Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_4%>"));//<%IDS_Port_4%>
			return FALSE;
		}
	}

	if(*sendstring)
	{
		CString strSend = sendstring;
		strSend.Replace("\\r", "\r");
		strSend.Replace("\\n", "\n");

		n = send(s, strSend, strSend.GetLength(), 0);
		if(n == SOCKET_ERROR)
		{
			// Send Data Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_5%>"));//<%IDS_Port_5%>
			shutdown(s, 0x02);
			closesocket(s);
			return FALSE;
		}
	}

	if(*matchstring)
	{
		char reply[8192] = {0};
		ret = RESPONSE_WAIT(s, timeout);
		if(ret > 0)
		{
			memset(reply, 0, 8192);
			n = recv(s, reply, 8192, 0);
			if(n <= 0)
			{
				shutdown(s, 0x02);
				closesocket(s);
				// Content Match Error
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_6%>"));//<%IDS_Port_6%>
				return FALSE;
			}

			if(!strstr(reply, matchstring))
			{
				shutdown(s, 0x02);
				closesocket(s);
				// Content Match Error
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_7%>"));//<%IDS_Port_7%>
				return FALSE;
			}
		}
		else if(ret == 0)
		{
			shutdown(s, 0x02);
			closesocket(s);
			// Read Data Timeout
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_8%>"));//<%IDS_Port_8%>
			return FALSE;
		}
		else
		{
			shutdown(s, 0x02);
			closesocket(s);
			// Read Data Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_9%>"));//<%IDS_Port_9%>
			return FALSE;
		}
	}

	shutdown(s, 0x02);
	closesocket(s);

	dwEndTime = GetTickCount();
	sprintf(szReturn, "TotalTime=%ld$", dwEndTime - dwBeginTime);

	return TRUE;
}
Ejemplo n.º 7
0
int TcpConnectPort(char *Host, int Port, char *SendStr, char *MatchStr, int Timeout, char *szReturn)
{
	struct sockaddr_in	serv;
	LPHOSTENT lpHost;
	int	tcpSocket, n = 0, ret = 0;
	char request[100] = {0};
	char reply[100] = {0};

	if(*SendStr)
	{
		if(strlen(SendStr) <= 100)
			strcpy(request, SendStr);
		else
			strncpy(request, SendStr, 100);
	}

	lpHost = gethostbyname(Host);
	if(lpHost == NULL)	
	{
		// DNS Resolve Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_10%>"));//<%IDS_Port_10%>
		return -1;
	}

	// Create Socket
	tcpSocket = socket(AF_INET,SOCK_STREAM,0);
	if (tcpSocket < 0)
	{
		// Create Socket Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_11%>"));//<%IDS_Port_11%>
		return -1;
	}

	// Connect to the host specialed nonblocking
	memcpy(&serv.sin_addr,lpHost->h_addr_list[0],lpHost->h_length);
	serv.sin_family = lpHost->h_addrtype;
	serv.sin_port=htons(Port);
	if (connect_timeo(tcpSocket, (struct sockaddr *)&serv, sizeof(serv), 10))
	{
		shutdown(tcpSocket, 0x02);
		closesocket(tcpSocket);
		// Server Port Connect Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_12%>"));//<%IDS_Port_12%>
		return -1;
	}
	
	if(*request)
	{
		n = send(tcpSocket, request, strlen(request), 0);
		if(n == SOCKET_ERROR)
		{
			// Send Data Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_13%>"));//<%IDS_Port_13%>
			shutdown(tcpSocket, 0x02);
			closesocket(tcpSocket);
			return -1;
		}
	}

	if(*MatchStr)
	{
		ret = RESPONSE_WAIT(tcpSocket, Timeout);
		if(ret > 0)
		{
			n = recv(tcpSocket, reply, 100, 0);
			if(n == SOCKET_ERROR)
			{
				shutdown(tcpSocket, 0x02);
				closesocket(tcpSocket);
				// Read Data Error
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_14%>"));//<%IDS_Port_14%>
				return -1;
			}
			if(n == 0)
			{
				shutdown(tcpSocket, 0x02);
				closesocket(tcpSocket);
				// Content Match Error
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_15%>"));//<%IDS_Port_15%>
				return -1;
			}
			if(!strstr(reply, MatchStr))
			{
				shutdown(tcpSocket, 0x02);
				closesocket(tcpSocket);
				// Content Match Error
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_16%>"));//<%IDS_Port_16%>
				return -1;
			}
		}
		else
		{
			shutdown(tcpSocket, 0x02);
			closesocket(tcpSocket);
			// Read Data Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Port_17%>"));//<%IDS_Port_17%>
			return -1;
		}
	}

	shutdown(tcpSocket, 0x02);
	closesocket(tcpSocket);
	return 0;
}
Ejemplo n.º 8
0
BOOL DHCP_MONITOR(int timeout, char *re_addr, char *custpath, char *szReturn)
{
	int		i;
	SOCKET	s;
	struct	sockaddr_in lsa, rsa;
	DWORD	dwbTime, dweTime;

	int		reqlen = 0;
	u_char	reqmessage[1000] = {0};
	u_char	resmessage[1000] = {0};

	dwbTime = ::GetTickCount();

	DHCPMessage reqdm, resdm;

	srand((unsigned)time(NULL));

	memset(&reqdm, 0, sizeof(struct DHCP_MESSAGE));
	reqdm.op = 0x01;
	reqdm.htype = 0x01;
	reqdm.hlen = 0x06;

	// Physical Address
	GetMacAddress(reqdm.ch_addr);

	for(i = 0;i < 4;i ++)
		reqdm.xid[i] = rand() % 0xff;

	reqdm.opts.oplen = 0;

	// magic cookie
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x63;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x82;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x53;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x63;

	// DHCP Message Type
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x35;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;

	reqdm.opts.opstr[reqdm.opts.oplen++] = 0xFB;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;

	// Client-identifier
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x3D;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x07;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;
	GetMacAddress(reqdm.opts.opstr + reqdm.opts.oplen);
	reqdm.opts.oplen += 6;

	// Requested IP Address
	if(*re_addr)
	{
		int		f_1 = 0, f_2 = 0, f_3 = 0, f_4 = 0;

		if(sscanf(re_addr, "%d.%d.%d.%d", &f_1, &f_2, &f_3, &f_4) != 4)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_1%>"));//<%IDS_DHCP_1%>
			return FALSE;
		}

		reqdm.opts.opstr[reqdm.opts.oplen++] = 0x32;
		reqdm.opts.opstr[reqdm.opts.oplen++] = 0x04;
		reqdm.opts.opstr[reqdm.opts.oplen++] = f_1;
		reqdm.opts.opstr[reqdm.opts.oplen++] = f_2;
		reqdm.opts.opstr[reqdm.opts.oplen++] = f_3;
		reqdm.opts.opstr[reqdm.opts.oplen++] = f_4;
	}

	// Host Name Option
	TCHAR lhost[80];
	gethostname(lhost, sizeof(lhost));
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x0C;
	reqdm.opts.opstr[reqdm.opts.oplen++] = strlen(lhost);
	for(i = 0;i < (int)strlen(lhost);i ++)
		reqdm.opts.opstr[reqdm.opts.oplen++] = lhost[i];

	// Class-identifier
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x3C;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x08;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x44;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x46;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x2E;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x46;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x72;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x61;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x6E;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x6B;

	// Parameter Request List
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x37;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x0A;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x01;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x0F;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x03;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x06;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x2C;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x2E;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x2F;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x1F;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x21;
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0x2B;

	// end
	reqdm.opts.opstr[reqdm.opts.oplen++] = 0xFF;

	// padding
	if(reqdm.opts.oplen < 64)
	{
		int left = 64 - reqdm.opts.oplen;
		for(i = 0;i < left;i ++)
			reqdm.opts.opstr[reqdm.opts.oplen++] = 0x00;
	}

	reqlen = topacket(&reqdm, reqmessage);

	if((s = socket(PF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_2%>"));//<%IDS_DHCP_2%>
		return FALSE;
	}

	// set socket broadcast flag
	{
	const int on = 1;
	setsockopt(s, SOL_SOCKET, SO_BROADCAST, (const char *)&on, sizeof(on));
	}

	memset(&lsa, 0, sizeof(lsa));
	lsa.sin_family = AF_INET;
	lsa.sin_addr.s_addr = htonl(inet_addr(DHCP_LOCAL_ADDRESS));
	lsa.sin_port=htons(DHCP_CLIENT_PORT);
	if(bind(s, (struct sockaddr *)&lsa, sizeof(lsa)) == SOCKET_ERROR)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_3%>"));//<%IDS_DHCP_3%>
		return FALSE;
	}

    rsa.sin_family = AF_INET;
	rsa.sin_addr.s_addr = htonl(inet_addr(DHCP_MULTICAST_ADDRESS));
    rsa.sin_port = htons(DHCP_SERVER_PORT);

	int dw, n;
	if((n = sendto(s, (char *)reqmessage, reqlen, 0, (struct sockaddr *)&rsa, sizeof(rsa))) == SOCKET_ERROR)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_4%>"));//<%IDS_DHCP_4%>
		return FALSE;
	}
	
	if(RESPONSE_WAIT(s, timeout) > 0)
	{
		dw = sizeof(rsa);
		if((n = recvfrom(s, (char *)resmessage, 1000, 0, (struct sockaddr *)&rsa, &dw)) == SOCKET_ERROR)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_5%>"));//<%IDS_DHCP_5%>
			return FALSE;
		}

		if(n == 0)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_6%>"));//<%IDS_DHCP_6%>
			return FALSE;
		}

		if(tostruct(&resdm, resmessage, n))
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_7%>"));//<%IDS_DHCP_7%>
			return FALSE;
		}

		if(resdm.op != 2 || resdm.htype != 1 || resdm.hlen != 6)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_8%>"));//<%IDS_DHCP_8%>
			return FALSE;
		}

		if(!resdm.siaddr)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_9%>"));//<%IDS_DHCP_9%>
			return FALSE;
		}

		dweTime = ::GetTickCount();
		sprintf(szReturn, "roundTripTime=%ld", dweTime - dwbTime);
		return TRUE;

	}

	sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DHCP_10%>"));//<%IDS_DHCP_10%>
	return FALSE;
}
Ejemplo n.º 9
0
int SVUnixOpMonitor::telnet_connect()
{
	int		n = 0;
	char	buffer[4096] = {0};
	char	*scan = NULL;
	int		telnetprogress = 0;
	int		count = 0;

	memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
	pgd->datalen = 0;
	int nTime=0;
    DataProc = terminal[(pgd->term_index==NUM_TERMINALS)?(NUM_TERMINALS-1):pgd->term_index].termproc;
	while(1)
	{
LOOP:
		memset(buffer, 0, 4096);

		if(RESPONSE_WAIT(hSocket, READ_TIMEOUT) <= 0)
		{
			if(nTime<1)
			{
				char buf[10]={0};
				sprintf(buf,"\r\n");
				send(hSocket, buf, strlen(buf),0);
				nTime++;
				goto LOOP;
			}

			if(telnetprogress == 0)
				return FISTCONNECTERROR;
			if(telnetprogress == 1) 
				return USERLOGINERROR;
			if(telnetprogress == 2) 
				return PWDLOGINERROR;

		}



		n = recv(hSocket, buffer, 4096, 0);

		if(n == SOCKET_ERROR)
			return LOGINSOCKETERROR;

		buffer[n] = 0;

		scan = buffer;
		while(n--)
		{
			telnet_protocol(hSocket, *scan++,pgd);
			//TRACE("%c", *scan);
		}

		switch(telnetprogress)
		{
		case 0:

			if(strstr(pgd->databuffer, strUserPrompt))
			{

				char	buf[255] = {0};
				strcpy(buf, strUser);

				for(int nS=0;nS<strlen(buf);nS++)
				{

						Sleep(20);
						send(hSocket, &buf[nS], 1,0);	
				}
				send(hSocket,"\r\n",2,0);

//				strcat(buf, "\r\n");
//				Sleep(100);			// delay
//				send(hSocket, buf, strlen(buf),0);
				telnetprogress ++;
				count = 0;
				memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
				pgd->datalen = 0;
			}
			break;
		case 1:
			if(strstr(pgd->databuffer, strPwdPrompt))
			{
				char	buf[255] = {0};
				strcpy(buf, strPwd);
				//---wangpeng
				for(int nS=0;nS<strlen(buf);nS++)
				{
					Sleep(20);
					send(hSocket, &buf[nS], 1,0);	
				}
				 send(hSocket,"\r\n",2,0);
				//---------结束------------------
				telnetprogress ++;
				count = 0;
				memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
				pgd->datalen = 0;
			}
			break;
		case 2:
			if(strstr(pgd->databuffer, "ogin incorrect"))
			{
				memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
				pgd->datalen = 0;
				return LOGININCORRECTERROR;
			}

			int		i = 0;
			char	prompt[256];
			while(i < PR_NUM && *arprompt[i])
			{
				memset(prompt, 0, sizeof(prompt));
				strcpy(prompt, arprompt[i]);

				if(strstr(pgd->databuffer, prompt))
				{
					memset(pgd->databuffer, 0, TELNET_BUF_SIZE);
					pgd->datalen = 0;
					return TELNETSUCCESS;
				}

				i ++;
			}
			break;
		}

		if(++count > 80)
		{
			if(telnetprogress == 0)
				return FISTCONNECTERROR;
			if(telnetprogress == 1) 
				return USERLOGINERROR;
			if(telnetprogress == 2) 
				return PWDLOGINERROR;
		}
		//改变默认的telnet行的长度
        telnet_setlinewidth(0x00, 0xef);
	}

}
Ejemplo n.º 10
0
BOOL RADIUS_MONITOR(char *server, int port, char *secret, char *uid, char *pwd, 
					int timeout, char *match, char *custpath, char *szReturn)
{
	int		i = 0, 
			j = 0;
	int		ulen = strlen(uid), 
			plen = strlen(pwd), 
			slen = strlen(secret);

	SOCKET	s;
	struct	hostent *hp = NULL;
	struct	sockaddr_in	serv;

	DWORD	dwBeginTime = 0, 
			dwEndTime = 0;

	u_char	request[1000] = {0};
	int		reqlen = 0;
	u_char	response[1000] = {0};

	srand((unsigned)time(NULL));

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if(s == INVALID_SOCKET)
	{
		// Create Communication Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_1%>"));//<%IDS_Radius_1%>
		return FALSE;
	}

	hp = gethostbyname(server);
	if(hp == NULL)
	{
		// DNS Resolve Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_2%>"));//<%IDS_Radius_2%>
		return FALSE;
	}

	memcpy(&serv.sin_addr,hp->h_addr_list[0],hp->h_length);
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);

	// construct access-request packet
	request[reqlen++] = 0x01;				// Code
	request[reqlen++] = rand() % 0xFF;		// Identifier
	reqlen += 2;							// Length
	request[reqlen++] = rand() % 0xFF;		// Request Authenticator
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;
	request[reqlen++] = rand() % 0xFF;

	request[reqlen++] = 0x01;				// Attribute Username
	request[reqlen++] = 0x02 + ulen;
	for(i = 0;i < ulen;i ++)
		request[reqlen++] = uid[i];

	request[reqlen++] = 0x02;				// Attribute Password (crypt)
	request[reqlen++] = 0x02 + (plen % 0x10 ? (plen / 0x10 + 1) * 0x10 : plen);

	/*******************************************************************
	 *  shared secret: S
	 *	pseudo-random 128-bit request authenticator: RA
	 *	password chuncks: P1, P2, P3 ...
	 *
	 *		B1 = MD5(S + RA)		C(1) = P1 xor B1
	 *		B2 = MD5(S + C(1))		C(2) = P2 xor B2
	 *		.						.
	 *		.						.
	 *		Bi = MD5(S + C(i - 1))	C(i) = Pi xor Bi
	 *
	 *	result: C(1) + C(2) + ... + C(i)
	 *
	 *	[RFC2058]
	 *******************************************************************/
	unsigned char output[16];
	unsigned char input[1024];
	unsigned char ilen;
	for(i = 0;i < plen;i += 16)
	{
		memset(input, 0, sizeof(input));
		ilen = 0;

		strcpy((char *)input, secret);
		ilen += slen;

		if(i == 0)
			for(j = 0;j < 16;j ++) input[ilen++] = request[4 + j];
		else
			for(j = 0;j < 16;j ++) input[ilen++] = output[j];

		MD5(output, input, ilen);

        int m = 0;
		for(m = 0, j = i;j < (plen - i < 16 ? plen : 16 + i);j ++, m++)
			output[m] ^= pwd[j];

		for(j = 0;j < 16;j ++)
			request[reqlen++] = output[j];
	}

	request[2] = reqlen / 0xFF;
	request[3] = reqlen % 0xFF;

	dwBeginTime = ::GetTickCount();

	// parse radius response
	int dw, n;
	n = sendto(s, (char *)request, reqlen, 0, (struct sockaddr *)&serv, sizeof(serv));
	if(n == SOCKET_ERROR) 
	{
		// Send Data Error
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_3%>"));//<%IDS_Radius_3%>
		return FALSE;
	}
	
w:	if(RESPONSE_WAIT(s, timeout) > 0)
	{
		dw = sizeof(serv);
		n = recvfrom(s, (char *)response, 1000, 0, (struct sockaddr *)&serv, &dw);
		if(n == SOCKET_ERROR) 
		{
			// Receive Data Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_4%>"));//<%IDS_Radius_4%>
			return FALSE;
		}

		if(n == 0)
		{
			// No Data Responsed From Server
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_5%>"));//<%IDS_Radius_5%>
			return FALSE;
		}

		if(response[1] != request[1]) goto w;

		if(response[0] == 0x02)
		{
			dwEndTime = ::GetTickCount();
			sprintf(szReturn, "roundTripTime=%ld$", dwEndTime - dwBeginTime);
			return TRUE;
		}
		else if(response[0] == 0x03)
		{
			if(response[20] == 0x12)
				// Request Refused
				sprintf(szReturn, "error=%s: %s$", ::FuncGetStringFromIDS("<%IDS_Radius_6%>"), response + 22);//<%IDS_Radius_6%>
			else
				// Request Refused
				sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_7%>"));//<%IDS_Radius_7%>

			return FALSE;
		}
		else
		{
			// Unknown Error
			sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_8%>"));//<%IDS_Radius_8%>
			return FALSE;
		}
	}
	else
	{
		// Request Timeout
		sprintf(szReturn, "error=%s", ::FuncGetStringFromIDS("<%IDS_Radius_9%>"));//<%IDS_Radius_9%>
		return FALSE;
	}

	return TRUE;
}
Ejemplo n.º 11
0
BOOL DNS_MONITOR(char *server, int port, char *domain, CStringList &lstAddress, 
				 char *custpath, char *szReturn)
{
	u_char reply[1000];
	SOCKET udpSocket;
    struct sockaddr_in serv;
	unsigned int n;
	int dw, ret, Count = 0, iCount = 0;
	LONG	dwBeginTime, dwElapseTime;

	u_char request[1000] =
				{ 
				0x01, 0x01, 0x01, 0x00,
				0x00, 0x01, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00
				};
	Count = 12;

	for(n = 0;n < strlen(domain);n ++)
	{
		if(domain[n] != '.')
		{
			iCount ++;
			request[Count + iCount] = domain[n];
		}
		else
		{
			request[Count] = iCount;
			Count = Count + iCount + 1;
			iCount = 0;
		}
	}
	request[Count] = iCount;
	Count = Count + iCount + 1;
	request[Count] = 0x00;
	request[Count + 1] = 0x00;
	request[Count + 2] = 0x01;
	request[Count + 3] = 0x00;
	request[Count + 4] = 0x01;
	Count += 5;

	udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
	if(udpSocket == INVALID_SOCKET)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_1%>"));//<%IDS_DNS_1%>
		return FALSE;
	}

	struct hostent *hp;
	hp = gethostbyname(server);
	if(!hp)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_2%>"));//<%IDS_DNS_2%>
		return FALSE;
	}

	memcpy(&serv.sin_addr,hp->h_addr_list[0],hp->h_length);
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);
	
	dwBeginTime = GetTickCount();
    n = sendto(udpSocket, (char *)request, Count, 0, (struct sockaddr *)&serv, sizeof(serv));
	if(n == SOCKET_ERROR)
	{
		sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_3%>"));//<%IDS_DNS_3%>
		return FALSE;
	}
	
	ret = RESPONSE_WAIT(udpSocket, DNS_TIMEOUT);
	if(ret > 0)
	{
		dw = sizeof(serv);
		if((n = recvfrom(udpSocket, (char *)reply, 1000, 0, (struct sockaddr *)&serv, &dw)) < 0)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_4%>"));//<%IDS_DNS_4%>
			return FALSE;
		}

		switch(reply[3] & 0x0F)
		{
		case 0:
			break;
		case 1:
			// Malformed DNS Request Packet
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_5%>"));//<%IDS_DNS_5%>
			return FALSE;
		case 2:
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_6%>"));//<%IDS_DNS_6%>
			return FALSE;
		case 3:
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_7%>"));//<%IDS_DNS_7%>
			return FALSE;
		}

		if(reply[0] == request[0] && reply[1] == request[1])
		{
			if(reply[6] * 256 + reply[7] == 0)
			{
				sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_8%>"));//<%IDS_DNS_8%>
				return FALSE;
			}

			//DNSMessageParser(reply, n);

			if(lstAddress.GetCount() != 0)
			{
				ret = VerifyDNS(reply, n, lstAddress);
				if(ret == 0)
				{
					dwElapseTime = GetTickCount() - dwBeginTime;
					sprintf(szReturn, "roundTripTime=%ld$", dwElapseTime);
					return TRUE;
				}
				else if(ret == -1)
				{
					sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_9%>"));//<%IDS_DNS_9%>
					return FALSE;
				}
				else if(ret == -2)
				{
					sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_11%>"));	//Verify Failed,<%IDS_DNS_11%> 
					return FALSE;
				}
				else
				{
					sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_12%>"));//<%IDS_DNS_12%>
					return FALSE;
				}
			}
			else
			{
				dwElapseTime = GetTickCount() - dwBeginTime;
				sprintf(szReturn, "roundTripTime=%ld$", dwElapseTime);
			}
		}
		else
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_13%>"));//<%IDS_DNS_13%>
			return FALSE;
		}
	}
	else
	{
		if(ret == 0)
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_14%>"));	//<%IDS_DNS_14%>
			return FALSE;
		}
		else
		{
			sprintf(szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_DNS_15%>"));//<%IDS_DNS_15%>
			return FALSE;
		}
	}

	return TRUE;
}
Ejemplo n.º 12
0
BOOL TFTP_MONITOR(char *server, int port, char *file, int timeout, char *custpath, 
				  char *szReturn)
{
	SOCKET	s;
	int		nAckSeq = 0;
	int		nSaveFlag = 0;
	int		datalen = 0;
	DWORD	dwBytes = 0;
	int		i = 0, n = 0, dw = 0;
	struct	hostent *hp = NULL;
	struct	sockaddr_in serv;

	DWORD	dwbTime = 0, dweTime = 0;
	DWORD	dwbReceive = 0, dweReceive = 0;

	int		reqlen = 0;
	u_char	req[1024] = {0};
	u_char	res[1024] = {0};

	if((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
	{
		sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SOCKET",
            "SOCKET_CREATE_FAILED"));
		return FALSE;
	}

	if((hp = gethostbyname(server)) == NULL)
	{
		sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_DSN",
            "DNS_DOMAIN_NAME_PARSE_ERROR"));
		return FALSE;
	}

	memcpy(&serv.sin_addr,hp->h_addr_list[0],hp->h_length);
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);

	// RRQ
	req[reqlen ++] = 0x00;
	req[reqlen ++] = 0x01;

	// Filename
	for(i = 0;i < (int)strlen(file);i ++)
		req[reqlen ++] = file[i];
	req[reqlen ++] = 0x00;

	// Mode
	req[reqlen ++] = 'O';
	req[reqlen ++] = 'C';
	req[reqlen ++] = 'T';
	req[reqlen ++] = 'E';
	req[reqlen ++] = 'T';
	req[reqlen ++] = 0x00;

	dwbTime = ::GetTickCount();

	if((n = sendto(s, (char *)req, reqlen, 0, (struct sockaddr *)&serv, sizeof(serv))) == SOCKET_ERROR)
	{
		sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SOCKET",
            "SOCKET_SEND_DATA_ERROR"));
		return FALSE;
	}

#ifdef SAVEASFILE
	CFile	datafile;
	char	szFile[256] = {0};
	
	sprintf(szFile, ".\\%s", file);
	if(!datafile.Open(szFile, CFile::modeWrite|CFile::modeCreate)) 
	{
		sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_BASIC",
            "BASIC_CREATE_FILE_FAIL"));
		return FALSE;
	}
#endif

	dwbReceive = ::GetTickCount();

	while(1)
	{
		if(RESPONSE_WAIT(s, timeout) <= 0)
		{
			sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SOCKET",
                "SOCKET_RECV_DATA_TIMEOUT"));
			return FALSE;
		}

		dw = sizeof(serv);
		n = recvfrom(s, (char *)res, sizeof(res), 0, (struct sockaddr *)&serv, &dw);
		if(n == SOCKET_ERROR || n == 0)
		{
			sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SCOKET",
                "SOCKET_RECV_DATA_ERROR"));
			return FALSE;
		}

		int nCode = res[0] * 0x100 + res[1];
		if(nCode == 0x05)
		{
			sprintf(szReturn, "error=%s [%d]$", res + 4, res[2] * 0x100 + res[3]);
			return FALSE;
		}

		if(nCode != 0x03)
		{
			sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SOCKET",
                "SOCKET_UNKNOWN_ERROR"));
			return FALSE;
		}

		datalen = n;

		if(res[2] * 0x100 + res[3] > nAckSeq)
		{
			nAckSeq ++;
			nSaveFlag = 1;
		}

		reqlen = 0;
		req[reqlen ++] = 0x00;
		req[reqlen ++] = 0x04;
		req[reqlen ++] = res[2];
		req[reqlen ++] = res[3];
		if((n = sendto(s, (char *)req, reqlen, 0, (struct sockaddr *)&serv, sizeof(serv))) == SOCKET_ERROR)
		{
			sprintf(szReturn, "error=%s$", FuncGetStringFromIDS("SV_SOCKET",
                "SOCKET_SEND_DATA_EEROR"));
			return FALSE;
		}

		if(nSaveFlag) dwBytes += datalen - 4;

#ifdef SAVEASFILE
		if(nSaveFlag) datafile.Write(res + 4, datalen - 4);
#endif
		nSaveFlag = 0;

		if(datalen - 4 < 512) break;
	}
	
#ifdef SAVEASFILE
	datafile.Close();
#endif

	dweTime = ::GetTickCount();
	dweReceive = ::GetTickCount();

	DWORD dwInterval = dweReceive - dwbReceive;
	if(dwInterval == 0) dwInterval = 1;

	sprintf(szReturn, "roundTripTime=%.3f$bytes=%ld$bytesPerSec=%.3f", 
			(dweTime - dwbTime) / 1000.0, dwBytes, (dwBytes / 1024.0) / (dwInterval / 1000.0));

	return TRUE;
}
Ejemplo n.º 13
0
int cisco_connect(SOCKET hSocket, char *pwd, char *pwdprompt, char *prompt,LPGDATA pgd)
{
    int		n = 0;
    char	buffer[8192] = {0};
    char	*scan = NULL;
    int		telnetprogress = 0;
    int		count = 0;

    memset(buffer, 0, sizeof(buffer));
    memset(pgd->databuffer, 0, BUF_SIZE);
    pgd->datalen = 0;
    while(1)
    {
        char buff[8192] = {0};

        if(RESPONSE_WAIT(hSocket, READ_TIMEOUT) <= 0)
            return -1;

        n = recv(hSocket, buff, sizeof(buff), 0);
        if(n == SOCKET_ERROR)
            return -2;
        buff[n] = 0;

        strcat(buffer, buff);

        scan = buff;
        while(n--)
        {
            telnet_protocol(hSocket, *scan,pgd);
            TRACE("%c", *scan);
            scan ++;
        }
        switch(telnetprogress)
        {
        case 0:
            if(strstr(pgd->databuffer, pwdprompt))
            {
                char	buf[255] = {0};
                strcpy(buf, pwd);
                strcat(buf, "\r\n");
                send(hSocket, buf, strlen(buf), 0);
                telnetprogress ++;
                count = 0;
                memset(buffer, 0, sizeof(buffer));
                memset(pgd->databuffer, 0, BUF_SIZE);
                pgd->datalen = 0;
            }
            break;
        case 1:
            if(strstr(pgd->databuffer, prompt))
            {
                memset(buffer, 0, sizeof(buffer));
                memset(pgd->databuffer, 0, BUF_SIZE);
                pgd->datalen = 0;
                return 0;
            }
            break;
        }

        if(++count > 50)
        {
            if(telnetprogress == 0)
                return -3;
            if(telnetprogress == 1)
                return -4;
        }
    }
}