📄 nsmgp.c
字号:
static const char rcsid[] = "$Header: /base/cvsroot/wam/sms/platform/src/smgp3/nsmgp.c,v 1.3 2007/08/01 08:08:38 haowen Exp $";
/*
SMGP 3.0 protocol program 1.3
Author : xiangbo
Modify : yanghaowen.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/errno.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <poll.h>
#include <md5.h>
#include "UNICODE_2_GBK.h"
#include "mylog.h"
#include "nsmgp.h"
#include "utils.h"
extern int errno;
extern char logfile[];
void phex(const unsigned char *src, const int len)
{
int i;
char buf[1024];
char *p;
if (len <= 0)
return;
strcpy(buf, "\n[DEBUG]\t\t[");
p = buf+11;
i = 0;
while (i < len) {
sprintf(p, "%02x ", *src);
p += 3;
src++;
i++;
}
strcpy(p, "]\n");
INFO(buf);
}
/***********************************************/
/* */
/* Generate the message packet head */
/* Function name: MakeHead */
/* */
/* Argument: char *buffer */
/* unsigned int PacketLength */
/* unsigned int RequestID */
/* unsigned int SequenceID */
/* */
/* Return : int offset */
/* */
/***********************************************/
void MakeHead(char *buffer, unsigned int PacketLength, unsigned int RequestID, unsigned int SequenceID)
{
unsigned char *p;
unsigned int temp = 0;
p = buffer;
/* PacketLength : Integer(4) */
temp = htonl(PacketLength);
memcpy(p, (unsigned char *)&temp, 4);
p += 4;
/* RequestID : Integer(4) */
temp = htonl(RequestID);
memcpy(p, (unsigned char *)&temp, 4);
p += 4;
/* SequenceID : Integer(4) */
temp = htonl(SequenceID);
memcpy(p, (unsigned char *)&temp, 4);
p += 4;
}
/***********************************************/
/* */
/* Connect to SMG */
/* Function name: ConnectSMG */
/* */
/* Argument: unsigned int *Socket_fd */
/* char *IP */
/* int Port */
/* */
/* Return : int */
/* 0 : right */
/* -1 : arguments error */
/* -2 : connect error */
/***********************************************/
int ConnectSMG(unsigned int *Socket_fd, char *IP, int Port)
{
struct sockaddr_in server;
int pr,ret;
/* Generate Sockaddr */
memset((char *)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(IP);
server.sin_port = htons(Port);
if( server.sin_addr.s_addr == -1)
{
return -1;
}
/* Create Socket */
if ( (*Socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
return -2;
}
// Connect ISMG
ret= connect(*Socket_fd, (struct sockaddr *)&server, sizeof(server));
if ( ret == -1 )
{
INFO("[INFO]: %s\n",strerror(errno));
return -2;
}
//-----addby 11-02------------------------------------------------------
if (ret == 0)
{ //set a fd to non-block mode ,and ignore SIGPIPE signal.
pr = fcntl ( *Socket_fd, F_GETFL);
if (pr == -1)
{
return -1;
}
pr = pr | O_NONBLOCK;
pr = fcntl ( *Socket_fd, F_SETFL, pr);
if (pr == -1)
{
return -1;
}
//Writing to a close or bad fd, SIGPIPE signal is received .
//And system SIGPIPE's default action is Out,
//So ignore this signal and return -1 when this case is happened.
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR)
{
return -1;
}
}
//----------------------------------------------------------
return (0);
}
/***********************************************/
/* */
/* Login to SMG */
/* Function name: LoginSMG */
/* */
/* Argument: int Socket_fd */
/* char *User */
/* char *Secrect */
/* int LoginMode */
/* Return : int */
/* 0 : right */
/* -1 : send errror */
/***********************************************/
int LoginSMG(int Socket_fd, char *ClientID, char *Secrect, int LoginMode, int Version, unsigned int SequenceID)
{
unsigned int Command_ID = ID_SMGP_LOGIN;
unsigned int Packet_Length = LEN_SMGP_HEAD +LEN_SMGP_LOGIN;
unsigned char *p;
unsigned char buffer[128];
unsigned int offset=0;
char ch;
unsigned char digest[16];
MD5_CTX c;
char SP[50];
char temp[10];
unsigned int tmp=0;
unsigned int timedig = 0;
/* Get Current Time */
memset(temp,0,10);
GetTimeStamp(temp, &tmp);
//GetTimeStamp(temp);
// printf("temp %s, tmp=%d\n",temp,tmp);
//tmp = GetTimeStamp_i();
timedig = htonl(tmp);
/*Make the AutherticatorSP by MD5*/
memset(SP, 0, 50);
memset(digest, 0, 16);
offset = 0;
memcpy(SP, ClientID, strlen(ClientID));
offset += strlen(ClientID);
offset += 7;
memcpy(SP+offset, Secrect, strlen(Secrect));
offset += strlen(Secrect);
memcpy(SP+offset,temp,10);
offset += 10;
MD5Init(&c);
MD5Update(&c, SP, offset);
MD5Final(digest, &c);
/* Allocate Message Buffer */
//buffer = (char *)malloc(Packet_Length);
memset(buffer, 0, sizeof(buffer));
/* Generate Packet Head */
MakeHead(buffer, Packet_Length, Command_ID, SequenceID);
p = buffer +LEN_SMGP_HEAD;
/* ClientID : Octet String(8) */
memcpy(p, ClientID, strlen(ClientID));
//printf("ClientID:%s\n",p);
p += 8;//strlen(ClientID) 一般小于8个字节,这里要添8
/* AuthenticatorClient : Octet String(16) */
memcpy(p, digest, 16);
//printf("digest:%s\n",p);
p += 16;
/* LoginMode : Integer(1) */
ch = LoginMode;
memcpy(p, (char*)&ch, 1);
//printf("LoginMode:%d\n",*p);
p += 1;
/* TimeStamp : Integer(4) */
// printf("timedig=%d\n",timedig);
memcpy(p, (char*)&timedig, 4);
//printf("Time:%d\n",p);
p += 4;
/* Version : Integer(1) */
ch = Version;
memcpy(p, (char*)&ch, 1);
//printf("Version:%d\n",*p);
p += 1;
//printf("login msg:%x\n",buffer);
//pirntf("
//printf("context:%s\n",buffer +LEN_SMGP_HEAD);
if ( write(Socket_fd, buffer, Packet_Length) != Packet_Length )
{
return -1;
}
return 0;
}
/************************************************************************************/
/* */
/* Get Message from SMG */
/* Function name: GetMessageHead */
/* */
/* Argument: char *socketbuffer */
/* unsigned int *Body_Length */
/* unsigned int *RequestID */
/* unsigned int *SequenceID */
/* Return : int */
/* 1 : right */
/* */
/************************************************************************************/
int GetMessageHead(char *socketbuffer, unsigned int *Body_Length, unsigned int *RequestID, unsigned int *SequenceID)
{
SMGP_HEAD Head;
memset(&Head, 0, sizeof(SMGP_HEAD));
memcpy(&Head, socketbuffer, 12);
*Body_Length = ntohl(Head.Packetlength) - 12;
*RequestID = ntohl(Head.RequestID);
*SequenceID = ntohl(Head.SequenceID);
return (1);
}
/************************************************************************************/
/* */
/* Recv login Resp from SMG */
/* Function name: RecvLoginResp */
/* */
/* Argument: char *socketbuffer */
/* unsigned int *Result */
/* Return : int */
/* 0 : right */
/************************************************************************************/
int RecvLoginResp(char *socketbuffer, unsigned int Body_Length, unsigned int *Status, unsigned int *Version)
{
unsigned char buffer[30];
unsigned int temp = 0;
memset(buffer, 0, sizeof(buffer));
memcpy(buffer, socketbuffer, Body_Length);
memcpy((char*)&temp, buffer, 4);
*Status = ntohl(temp);
*Version = buffer[20];
return (0);
}
/************************************************************************************/
/* */
/* Submit a message to SMG */
/* Function name: Submit */
/* */
/* Argument: int Socket_fd */
/* .............................. */
/* Return : int */
/* 0 : right */
/* -1 : send errror */
/************************************************************************************/
int Submit(int Socket_fd, unsigned int SequenceNum, SMGP3_SUBMIT *submit,
unsigned char *content,
unsigned char *linkid,
unsigned char subMsgType,
unsigned char spDealRes )
{
char dServiceID[10+1]; //业务代码
char dFeeType[2+1]; //收费类型
char dFeeCode[6+1]; //资费
char dFixedFee[6+1]; //包月费/封顶费
char dValidTime[17+1]; //短消息有效时间: 0
char dAtTime[17+1]; //短消息定时发送时间:0
char dSrcTermID[21+1]; // 短信息发送方的电话号码
char dChargeTermID[21+1]; // 付费号码
char dDestTermID[21+1]; // 接收该短消息的手机号
char dContent[256];
int res, ret, bytes_written = 0, i;
// unsigned short temp2=0;
unsigned short var1=0;
unsigned int Total_Length;
unsigned int Command_ID = ID_SMGP_SUBMIT;
unsigned char buffer[512];
// unsigned char ch;
unsigned char *p;
struct pollfd poll_list;
//--------------------------
TLV_LinkID TLinkID;
TLV TChargeUserType;
TLV TSubmitMsgType;
TLV TSpDealResult ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -