⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 routerassistserver.c

📁 linux下串口的访问
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "wcommon.h"
#include "isocket.h"
#include <pthread.h>
#include "wtimer.h"
#include <signal.h>

#include <sys/ipc.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "syslog.h"

#define GatewayPort	10000
#define BufferLen	2048
#define GatewayIP	"192.168.1.1"

// Send to client
enum DataType {
	DATA_BEGIN = 1,
	DATA_CARD_TYPE = 1,
	DATA_SIGNAL_VALUE,
	DATA_WAN_PROTOCOL,
	DATA_UP_FLOW,
	DATA_DOWN_FLOW,
//	DATA_DIAL_LOG,
	DATA_END,
};


// Receice data from client
#define DATA_TCP		11
#define DATA_TELNET		12
#define DATA_VIEW_LOG		13
#define DATA_DEBUG_RUN_APP	14
#define DATA_DEBUG_LOG_INFO	15


// for telnet
#define TEMP_NAME		"/tmp/cmd_result.txt"
#define RESULT_LENGTH		1024*128
#define NO_RESULT_RETURN	"no result return"
#define APP_PATH		"/tmp/RouterAssistServer"


// for view log file
#define VIEW_LOG_FILE		"/var/log/messages"


int	gClientFd = -1;
int	gListenFd;

extern int GetFlowData( int unit, int *rx_bytes, int *tx_bytes );

char * GetDataValue( unsigned char type )
{
	static char str[4096];
	static BOOL bNoCard = FALSE;
	char *p = NULL;
	int   rx, tx;
	int   ret;

// 	if ( (type != DATA_CARD_TYPE) && (type != DATA_WAN_PROTOCOL) && bNoCard )
// 	{
// 		str[0] = '\0';
// 		return str;
// 	}

	switch ( type )
	{
	case DATA_CARD_TYPE:
		p = nvram_get( NVRAM_CARD_TYPE );
		if ( p==NULL  ) 
			p = "Get card type failure.";
		else if ( strcmp(p, CARD_TYPE_NO)==0 ) 
			bNoCard = TRUE;
		else
			bNoCard = FALSE;
		break;
	case DATA_SIGNAL_VALUE:
		if ( bNoCard )
			p = "";
		else 
			p = "Unkown";
		break;
	case DATA_WAN_PROTOCOL:
		p = nvram_get( "wan_proto" );
		if (p==NULL)
			p = "Get wan proto failure.";
		break;
	case DATA_UP_FLOW:
		ret = -1; //GetFlowData( 0, &rx, &tx );
		if ( ret<0 )
			p = "get flow failure";
		else
		{
			sprintf( str, "%d", tx );
			return str;
		}
		break;
	case DATA_DOWN_FLOW:
		ret = -1; //GetFlowData( 0, &rx, &tx );
		if ( ret<0 )
			p = "get flow failure";
		else
		{
			sprintf( str, "%d", rx );
			return str;
		}
		break;
// 	case DATA_DIAL_LOG:
// 		//p = (char *)malloc( 4096 );
// 		//if ( p!=NULL )
// 		//	NvLogGet(p);
// 		if (p==NULL)
// 			p = "get dial log failure";
// 		break;
	}
	strcpy( str, p );
	return str;
}

void  TimerHandle(int x)
{
	char buf[4096];
	int  len;
	int  ret;
	int  i;
	//	pthread_t  tid;
	char *data;
	
	if ( gClientFd == -1 )
		return;
	//dPrintS( "TimerHandle" );
	
	for (i=DATA_BEGIN; i<DATA_END; i++ )
	{
		data = GetDataValue( i );
		
		//dPrintStr( data );
		
		len = SetRouterData( buf, i, strlen(data), data );
		
		//if ( gClientFd != -1)
		{
			//dPrintInt( len );
			ret = nWriten( gClientFd, buf, len );
			if ( ret<=0 )
			{
				perror("nWriten");
				return;
			}
		}
	}	
}


int ProcessFtpFile( int fd, char * filename, int filelen )
{
	FILE *	fp;
	BYTE	buf[BufferLen] = {0};
	//int	slen = 0;
	int	tlen;
	
	if ( filename==NULL )
		return -1;
	
	// if file exist then delete it 
	{
		sprintf( buf, "rm -f %s > /dev/null", filename );
		system( buf );
	}
	
	// add server path judge.

	// open file
	fp = fopen( filename, "w" );
	if ( fp==NULL )
	{
		perror( "fopen error" );
		return -2;
	}

	dPrintS("=====Start Transmisstion=====");
	while ( (tlen = nRecv( fd, buf, BufferLen ) ) > 0 )
	{
		PrintInt( tlen );
		//slen += tlen;
		filelen -= tlen;
		if ( filelen<0 )
			tlen += filelen;
		fwrite( buf, tlen, 1, fp );
		if ( filelen<=0 )
		{
			PrintInt( filelen );
			break;
		}
	}
	if ( tlen==0 )
		PrintS( "Client disconnect." );
	else if ( tlen<0 )
	{
		PrintInt( tlen );
		perror( "nRecv" );
	}
	dPrintS( "=====End Transmisstion=====" );
	fclose( fp );
	
	sprintf( buf, "chmod 777 %s", filename );
	system( buf );

	return 0;
}

// . Data format: DataType(1B) | Length(4B) | DataContent(N)
// First send: DataType=ftp, len, "DataType:file FileName:xx FileLen:xx".
// then send file content.
int ProcessFtpData( int fd, unsigned int len )
{
	int	ret;
	char	buf[256] = "";
	char	datatype[32];
	char	filename[256];
	int     filelen;

	dPrintInt( len );

	ret = nReadn( fd, buf, len );	
	if ( ret != len )
	{
		dPrintInt( ret );
		perror( "read data error" );
		return -1;
	}

	// "DataType:file FileName:xx FileLen:xx"
	sscanf( buf, "DataType:%s FileName:%s FileLen:%d", datatype, filename, &filelen );
	dPrintStr( datatype );
	dPrintStr( filename );
	dPrintInt( filelen );

// 	ret = nSend( fd, "ok", 3 );
// 	if ( ret<0 )
// 	{
// 		PrintS( "send error\n" );
// 		return -2;
// 	}	
	if ( strcmp( datatype, "file")==0 )
	{
		ret = ProcessFtpFile( fd, filename, filelen );

		return ret;
	}
	PrintS("data error.");
	PrintStr( buf );
	return -3;
}

int SetRouterData( BYTE * buf, BYTE type, UINT len, const BYTE *data )
{
	if ( buf==NULL || data==NULL || len<0 )
		return -1;
	
	buf[0] = type;
	memcpy( buf+1, &len, 4 );
	memcpy( buf+5, data, len );
	
	return len+5;
}

int SetRouterDataAndSend( BYTE type, UINT len, const BYTE *data )
{
	BYTE * buf = NULL;

	if ( data==NULL || len<0 || gClientFd==-1 )
		return -1;

	buf = (BYTE*)malloc( len + 5 );
	if ( buf==NULL )
		return -2;
	
	buf[0] = type;
	memcpy( buf+1, &len, 4 );
	memcpy( buf+5, data, len );
	
	dPrint("Send data to client..., len:%d", len+5 );
	if ( nWriten( gClientFd, buf, len+5 ) < 0 )
	{
		PrintS("nWriten failure.");
		return -3;
	}
	
	free( buf );
	return len+5;
}

// . Data format: DataType(1B) | Length(4B) | DataContent(N)
// First send: DataType=telnet, len, "DataType:file FileName:xx FileLen:xx".
// then send file content.
int ProcessTelnetData( int newfd, unsigned int len )
{

 	int	ret;
 	char	buf[RESULT_LENGTH] = "";
	char	filecontent[RESULT_LENGTH];
	BOOL	flag = TRUE;
	//int     len;

	dPrintInt( len );
	
	ret = nReadn( newfd, buf, len );	
	if ( ret != len )
	{
		dPrintInt( ret );
		perror( "read data error" );
		return -1;
	}
	
	dPrintStr( buf );
	if ( strncmp(buf, "cd ", 3)==0 )
	{
		//flag = FALSE;
		char *p = buf+3;
		while (*p==' ' && *p != '\0' )	p++;
		if ( *p!='\0' )			chdir( p );
		len = SetRouterData( buf, DATA_TELNET, 
			strlen(NO_RESULT_RETURN)+1, NO_RESULT_RETURN );
		goto Send;
	}

	// execute cmd
	if ( flag )
	{
		strcat( buf, " 1> " );
		strcat( buf, TEMP_NAME );
		strcat( buf, " 2> " );
		strcat( buf, TEMP_NAME );
	}
	
	dPrintStr( buf );
        setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
	ret = system ( buf );
	dPrintInt( ret );
	//ret = execl( buf, 0 );
	if ( ret<0 )
	{
		PrintInt( ret );
		perror( "system error" );
		return -2;
	}
	if ( flag )
	{
		// return result
		ret = FileReadWhole( TEMP_NAME, filecontent, RESULT_LENGTH );
		//PrintStr( filecontent );
		// 		sprintf( buf, "rm %s", TEMP_NAME );
		// 		system( buf );
		unlink( TEMP_NAME );
		
		if ( ret==-3 )	// data too long.
		{
			ret = sprintf( filecontent, "Error: data too long" );
		}
		else if ( ret<0 )
		{
			return -4;
		}
	}

	if ( ret==0 || !flag )  // no result
	{
		len = SetRouterData( buf, DATA_TELNET, 
			strlen(NO_RESULT_RETURN)+1, NO_RESULT_RETURN );
	}
	else
	{
		len = SetRouterData( buf, DATA_TELNET, ret, filecontent );
	}
Send:
	ret = nWriten( newfd, buf, len );
	//memset( filecontent, 0, RESULT_LENGTH );
	if ( ret<0 )
	{
		PrintS( "send error." );
		return ret-10;
	}

	return 0;
}

int ProcessViewLog(int fd)
{
	int	ret;
	char 	*szFile = NULL;
	BYTE    buf[RESULT_LENGTH];

	dPrintS("===Start ProcessViewLog===");
	// get file length
	ret = GetFileLength(VIEW_LOG_FILE);
	if (ret<0)
	{
		szFile = "Get file length failure.";
		goto sEnd;
	}
	dPrintInt( ret );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -