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

📄 sl_cli.c

📁 UNIX/LINUX平台下面SMS网管原代码
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <signal.h>#include <memory.h>#include <time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>static int  timeout ;static long WinNo ;#define MSGSIZE 1024#define SocketTimeOut 40void get_curr_time( long *ll ) ;long GetTime();int sl_fd ;int sl_putmsg( int app_type, u_char *buf, int len){	long ll ;	int times, offset ;	unsigned char OutBuf[ 2 * MSGSIZE] ;	unsigned char tmp[ 10] ;/*	ll = 0 ;	get_curr_time( &ll) ;	srand( ll) ;*/	WinNo = RandNum();	memset( OutBuf, '\0', sizeof( OutBuf)) ;	offset = 0 ;	OutBuf[ 0] = 0xfe ;	OutBuf[ 1] = 0xf6 ;	offset += 2 ;	sprintf( (char *)OutBuf + offset, "%06d", WinNo) ;	offset += 6 ;	sprintf( (char *)OutBuf + offset, "%04d", app_type) ;	offset += 4 ;	if ( len > 1024 * 2 ) {		sprintf( (char *)OutBuf + offset, "%04d", MSGSIZE * 2) ;		offset += 4 ;		memcpy( (char *)OutBuf + offset, buf, MSGSIZE * 2) ;		offset += MSGSIZE * 2 ;	} else {		sprintf( (char *)OutBuf + offset, "%04d", len) ;		offset += 4 ;		memcpy( (char *)OutBuf + offset, buf, len) ;		offset += len ;	}	if ( writen( sl_fd, OutBuf, offset) == offset)		return( 0) ;	return( -1) ;}int sl_getmsg( int *app_type, u_char *buf, int *len, int waittime){	unsigned char tmp[ 12] ;	long TiMe ;	int offset ;	time( &TiMe) ;	while ( 1 ) {		if ( (time( (long *)0) - TiMe) > waittime )			return( -2) ;		memset( buf, 0x00, sizeof(buf) );		if ( alreadn( sl_fd, buf, 16) != 16 )			continue ;		if ( buf[0] != 0xfe || buf[1] != 0xf6 ) 			continue ;		offset = 2 ;		memset( tmp, '\0', sizeof( tmp)) ;		memcpy( tmp, buf + offset, 6) ;		if ( atol( (char *)tmp) == WinNo ) 			break ;	}	offset += 6 ;	memset( tmp, '\0', sizeof( tmp)) ;	memcpy( tmp, buf + offset, 4) ;	(*app_type) = atoi( (char *)tmp) ;	offset += 4 ;	memset( tmp, '\0', sizeof( tmp)) ;	memcpy( tmp, buf + offset, 4) ;	(*len) = atoi( (char *)tmp) ;	memset( buf, '\0', sizeof( buf)) ;	if ( alreadn( sl_fd, buf, (*len)) != (*len) )		return( -1) ;	return( 0) ;}int sl_connect( const char *hostname, int HostTcpPort){	void (* sigfn)() ;	void settimeout() ;	struct sockaddr_in server ;	struct protoent *proto ;	struct hostent *host ;	struct linger mylinger ;	int optval= 16384 ;	proto = getprotobyname( "tcp") ;	sl_fd = -1 ;	if ( (sl_fd=socket( AF_INET, SOCK_STREAM, proto->p_proto)) < 0 )		return( -2) ;	mylinger.l_onoff = 1 ;	mylinger.l_linger = 0 ;	setsockopt(sl_fd, SOL_SOCKET, SO_SNDBUF, (char *)&optval, 4) ;	setsockopt(sl_fd, SOL_SOCKET, SO_RCVBUF, (char *)&optval, 4) ;	setsockopt(sl_fd, SOL_SOCKET, SO_LINGER, (char *)&mylinger, sizeof(struct linger)) ;	if ( (host=gethostbyname( hostname)) == 0 ) {		close( sl_fd) ;		return( -3) ;	}	bzero( &server, sizeof( server)) ;	server.sin_family = AF_INET ;	memcpy( (char *)&server.sin_addr, (char *)host->h_addr, host->h_length);	server.sin_port = htons( HostTcpPort) ;	timeout = 0 ;	sigfn = signal( SIGALRM, settimeout) ;	alarm( 3) ;	if ( connect( sl_fd,( struct sockaddr *)&server, sizeof( server)) < 0)	{		alarm( 0) ;		signal( SIGALRM, sigfn) ;		close( sl_fd) ;		return( -1) ;	}	alarm( 0) ;	return( 0) ;}int sl_close(){	shutdown( sl_fd, 2) ;	close( sl_fd) ;	return 0 ;}/* ------------------------------------------------------------------------    如果返回值 <= 0 时, 表明流套出错,               < nbytes 时, 表明数据包写入流出错,               = nbytes 时, 表明数据包写入流完整--------------------------------------------------------------------------- */int  writen( int fd, char *ptr, int nbytes) {	int  nleft, nwriten ;	nleft = nbytes ;	while ( nleft > 0 )	{		nwriten = write( fd, ptr, nleft) ;		if ( nwriten <= 0 )			return( nwriten) ;		nleft = nleft - nwriten ;		ptr += nwriten ;	}	return( nbytes - nleft) ;}/* ------------------------------------------------------------------------    如果返回值 < 0 时, 表明系统出错,               = 0 时, 表明客户挂连接,               < nbytes 时, 表明数据包接收不完整,               = nbytes 时, 表明数据包接收完整--------------------------------------------------------------------------- */int  alreadn( int fd, char *ptr, int nbytes){	void (* sigfn)() ;	int  nleft, nread ;	void settimeout() ;	nleft = nbytes ;	while ( nleft > 0 )	{		timeout = 0 ;		sigfn = signal( SIGALRM, settimeout) ;		alarm( SocketTimeOut) ;		nread = read( fd, ptr, nleft) ;		alarm( 0) ;		signal( SIGALRM, sigfn) ;		if ( timeout == 1 )			return ( -1) ;		if ( nread < 0 )			return ( nread) ;		if ( nread == 0 )			break ;		nleft = nleft - nread ;		ptr = ptr + nread ;	}	return ( nbytes - nleft) ;}void settimeout(){	timeout = 1 ;}/* ------------------------------------------------------------------------    如果返回值 < 0 时, 表明系统出错,               = 0 时, 表明客户挂连接,               < nbytes 时, 表明数据包接收不完整,               = nbytes 时, 表明数据包接收完整--------------------------------------------------------------------------- */int  readn( int fd, char *ptr, int nbytes){	int  nleft, nread ;	nleft = nbytes ;	while ( nleft > 0 )	{		nread = read( fd, ptr, nleft) ;		if ( nread < 0 )			return ( nread) ;		if ( nread == 0 )			break ;					nleft = nleft - nread ;		ptr = ptr + nread ;	}	return ( nbytes - nleft) ;		}unsigned short int cal_crc( char *ch, int len) ;int VerifyCrc( char *buf, char *crc, int len) {	union temp {		char c[ 2];		unsigned short int crc ;	} c ;	unsigned short int _crc ;	c.c[ 1] = crc[ 0] ;	c.c[ 0] = crc[ 1] ; 	_crc = cal_crc( buf, len) ;	if ( c.crc != _crc )		return( -1) ;	return( 0) ;}int CalCrc( char *buf, char *crc, int len){	union temp {		char c[ 2];		unsigned short int crc ;	} c ;	c.crc = cal_crc( buf, len) ;	len += 2 ;	crc[ 0] = c.c[ 1] ;	crc[ 1] = c.c[ 0] ;	return( 0) ;}void updcrc( unsigned char x, unsigned short int *_crc){	unsigned shifter, flag;	for( shifter = 0x80; shifter; shifter >>= 1) {		flag = ( (*_crc) & 0x8000) ;		(*_crc) <<= 1 ;		(*_crc) |= ( ( shifter & x) ? 1: 0) ;		if (flag)			(*_crc) ^= 0x1021 ;	}}unsigned short int cal_crc( char *ch, int len){	int i ;	char *chp ;	unsigned short int _crcaccum ;	_crcaccum = 0 ;	chp = ch ;	for( i = 0; i < len; i++) {		updcrc( *chp, &_crcaccum) ;		chp++ ;	}	return( _crcaccum) ;}void get_curr_time( long *ll ){	struct tm *current_time, time_buf ;	time_t t1 ;	char str[ 7] ;	current_time = &time_buf ;	time( &t1) ;	current_time = localtime ( &t1) ;	sprintf( str, "%02d%02d%02d",			current_time -> tm_hour,			current_time -> tm_min,			current_time -> tm_sec) ;	*ll = ( long)atol( str) ;	return ;}void get_curr_date( char *currdate){	struct tm *current_time, time_buf ;	time_t t1 ;	char str[ 11] ;	current_time = &time_buf ;	time( &t1) ;	current_time = localtime ( &t1) ;	memset( str, '\0', sizeof( str)) ;	currdate =&str[ 0] ;	sprintf( str, "%04d%02d%02d",			current_time -> tm_year,			1 + current_time -> tm_mon,			current_time -> tm_mday) ;	return ;}/* sl_tcpip 说明:返回: 		0 : 表示成功;	-1: 表示失败 ;参数: 	mgid : 表示通信失败原因 ;	app_type: 999 除外 	sbuf : 指向请求串指针 ;	slen : 请求串的长度, 最大值为 2k ;	rbuf : 存放结果串起始地址, 最大值为 2k ;	rlen : 响应结果串的长度 ;		flag : 'S' -> 表示单包	       'F' -> 表示多包中的第一个包	       'M' -> 表示多包中的中间包	       'L' -> 表示多包中的最后一个包	       'C' -> 表示终止与服务连接	mgid error info :	 -1 -> 在hosts 中没有定义主机名 或者是没有正确的服务端口 或者是 connect 网关进程失败 ;	-2 -> 发送数据失败 ;	-3 -> 接收数据失败 ;*/int sl_tcpip( int *mgid, int *app_type, const char *rhost, short port, u_char *buf, int *len, char flag, int timeout){	unsigned char p_buf[ 6] ;	int ret ;	if ( flag == 'C' ) {		close( sl_fd) ;		return( 0) ;	}	if ( flag == 'S' || flag == 'F' ) {		if ( sl_connect(rhost, port) != 0 ) {			(*mgid) = -1 ;			return( -1) ;		}		if( sl_putmsg( *app_type, buf, *len) != 0) {			(*mgid) = -2 ;			sl_close() ;			return ( -1) ;		}	}	memset( buf, '\0', sizeof(buf) );	if( (ret=sl_getmsg( app_type, buf, len, timeout)) != 0) {			(*mgid) = -3 ;			sl_close() ;			return ( -1) ;	}	if ( flag == 'S' || flag == 'L' )		close( sl_fd) ;	(*mgid) = 0 ;	return( 0) ;}int RandNum(char *sNum){	int iRand1, iRand2, iSeed, iNum;	iSeed = GetTime();	srand(iSeed);	iRand1 = rand();	srand(iSeed + 1);	iRand2 = rand();	iNum = ((iRand2 % 10 ) * 100000 + iRand1)% 1000000;	return iNum;}long GetTime(){    char temp[10];    struct tm *tm;    time_t t1;    t1 = time(NULL);    tm = localtime(&t1);    sprintf(temp, "%02d%02d%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);    return atol(temp);}/*========================== END ===========================*/

⌨️ 快捷键说明

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