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

📄 wsp.cpp

📁 WAP协议1.2源码!!!!!!!!!!!!!!!!!!!!
💻 CPP
字号:
#include "WSP.H"
#include "WTP.H"
#include "UDP.H"
#include "TIMER.H"

enum {
	Bad_PDU = -1,
	Connect_PDU = 0x01,
	ConnectReply_PDU = 0x02,
	Redirect_PDU = 0x03,
	Reply_PDU = 0x04,
	Disconnect_PDU = 0x05,
	Push_PDU = 0x06,
	ConfirmedPush_PDU = 0x07,
	Suspend_PDU = 0x08,
	Resume_PDU = 0x09,
	Get_PDU = 0x40,
	Options_PDU = 0x41,
	Head_PDU = 0x42,
	Delete_PDU = 0x43,
	Trace_PDU = 0x44,
	Post_PDU = 0x60,
	Put_PDU = 0x61
};

int g_target;

// parse wsp result pdu
static int ParseWspResult(char *data, int len, int *status);

/* wap browser capabilities */
unsigned char brwcap[] = {
	                        0x04, 0x80, 0x8c, 0x9a, 0x40, /*client sdu size*/
                          0x04, 0x81, 0x8c, 0x9a, 0x40, /*server sdu size*/
                          0x02, 0x83, 0x01,             /*method MOR*/
                          0x02, 0x84, 0x01,             /*push MOR*/
                          };           

/* wap browser header */
unsigned char brwhead[] = {
	0x80, 0x82, /*text/html*/

	0x80, 0x94, /*application/x-hdmlc*/
	0x80, 0x83, /*text/plain*/
	0x80, 0x8c, /*multipart/mixed*/
	0x80, 0x9d, /*image/gif*/
	0x80, 0x9e, /*image/jpeg*/
	0x80, 0xa1, /*image/vnd.wap.wbmp*/
  0x80, 0xa9, /*application/vnd.wap.wbxml*/
	0x80, 0xad, /*text/vnd.wap.si*/
	0x80, 0xaf, /*text/vnd.wap.sl*/
	0x80, 0xb3, /*application/vnd.wap.multipart.related*/
	0x80, 0xb4, /*application/vnd.wap.sia*/

	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x6d, 0x69, 0x64, 0x69, 0x00, /*audio/midi*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x6d, 0x69, 0x64, 0x00,	    /*audio/mid*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x78, 0x2d, 0x6d, 0x69, 0x64, 0x69, 0x00, /*audio/x-midi*/	
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x78, 0x2d, 0x6d, 0x69, 0x64, 0x00, /*audio/x-mid*/

  0x80, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 
  0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x77, 0x61, 0x70, 0x2e, 0x6d, 0x6d, 0x73, 
  0x2d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00, /*application/vnd.wap.mms-message*/
	
	0x80, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75, 
	0x6e, 0x2e, 0x6a, 0x32, 0x6d, 0x65, 0x2e, 0x61, 0x70, 0x70, 0x2d, 0x64, 0x65, 0x73, 0x63, 
  0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x00,  /*text/vnd.sun.j2me.app-descriptor*/
	
	0x80, 0x61, 0x70, 0x70, 0x6c, 
  0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6a, 0x61, 0x76, 0x61, 
  0x00, /*application/java*/
	
	0x80, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 
  0x6e, 0x2f, 0x6a, 0x61, 0x76, 0x61, 0x2d, 0x61, 0x72, 0x63, 0x68, 0x69, 
  0x76, 0x65, 0x00, /*application/java-archive*/
	
	0x80, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 
  0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x2d, 0x6a, 0x61, 0x76, 0x61, 0x2d, 0x61, 
  0x72, 0x63, 0x68, 0x69, 0x76, 0x65, 0x00, /*application/x-java-archive*/

	/* drm */
	0x80, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 
	0x69, 0x6f, 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x6d, 0x61, 0x2e, 
	0x64, 0x72, 0x6d, 0x2e, 0x6d, 0x65, 0x73, 0x73, 
	0x61, 0x67, 0x65, 0x00, /* application/vnd.oma.drm.message*/
    
	0x80,0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x7a, 0x69, 0x79, 0x69, 0x74, 0x6f, 
  0x6e, 0x67, 0x2e, 0x64, 0x72, 0x6d,0x00,/*image/vnd.ziyitong.drm->flash*/
    
  0x80,0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x7a, 0x69, 0x79, 0x69, 0x74, 0x6f, 
  0x6e, 0x67, 0x2e, 0x76, 0x69, 0x73,0x00,/*image/vnd.ziyitong.vis->flash*/
   
	0x80,0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x7a, 0x69, 0x79, 0x69, 0x74, 0x6f, 
  0x6e, 0x67, 0x2e, 0x7a, 0x64, 0x64,0x00,/*image/vnd.ziyitong.zdd->flash*/

  0x80, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x43, 0x68, 0x61, 0x6f, 
	0x74, 0x65, 0x78, 0x2d, 0x77, 0x66, 0x64, 0x00, /*application/Chaotex-wfd*/

	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x6d, 0x70, 0x65, 0x67,0x00,         /*audio/mpeg*/
  0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x6d, 0x70, 0x33, 0x00,	           /*audio/mp3*/
  0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x61, 0x61, 0x63, 0x00,	           /*audio/aac*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x61, 0x64, 0x70, 0x63, 0x6d, 0x00,  /*audio/adpcm*/
	0x80, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x2f, 0x33, 0x67, 0x70, 0x70, 0x00,        /* video/3gpp*/
	0x80, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x2f, 0x33, 0x67, 0x70, 0x00,			   /*video/3gpp*/
	0x80, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x2f, 0x6d, 0x70, 0x34, 0x00,			   /*video/mp4*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x2f, 0x78, 0x2d, 0x6d, 0x73, 0x2d, 0x77, 0x6d, 0x61, 0x00, /*audio /x-ms-wma*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x78, 0x2d, 0x77, 0x61, 0x76, 0x00,  /*audio/wav*/
	0x80, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x78, 0x2d, 0x77, 0x61, 0x76, 0x00,  /*audio/x-wav*/

	0x83, 0x99, /*language*/
	0x81, 0x02, 0xea, 0x51, 

  };

/* mms capabilities  */
unsigned char mmscap[] = {
     0x04,0x80,0x8F,0xF8,0x00,        /*client sdu size*/
     0x04,0x81,0x8F,0xF8,0x00,        /*server sdu size*/
     0x02,0x82,0xC0,				          /*protocal options*/
     0x02,0x83,0x02,				          /*method MOR*/
     0x02,0x84,0x01                   /*push MOR*/
    };

/* mms header */
unsigned char mmshead[] = {
	  0x80 , 0x80 ,
	  0xa1 , 0x09 ,
	  0x80 , 0x77 ,
	  0x61 , 0x70 , 0x00 ,
	  0x77 , 0x61 , 0x70 ,0x00 , 
	  0x83 , 0xc1 , 
	  0x81 , 0x83 ,
	  0x81 , 0x84 ,
	  0x81 , 0xea ,
	  0x81 , 0x03 , 0x02 , 0x03 ,0xe8,

	  0xB3 ,0x80 ,
	  0xB2, 0x81 ,
	  0xB2 ,0x82 ,
	  0xB2 ,0x84 ,

	  0x89 ,0x45 ,

	  0x6E ,0x63 ,0x6F ,0x64 ,0x69 ,0x6E ,0x67 ,0x2D ,0x56,
	  0x65 ,0x72 ,0x73 ,0x69 ,0x6F ,0x6E ,0x00 ,0x45 ,0x6E ,0x63 ,0x6F ,0x64 ,0x69 ,0x6E ,0x67 ,0x2D,
	  0x56 ,0x65 ,0x72 ,0x73 ,0x69 ,0x6F ,0x6E ,0x00 ,0x31 ,0x2E ,0x33 ,0x00
};

// wsp init
int WspInit(int target, char *host, int port )
{  
	  int ret = 0;
	  
	  g_target = target;
	
	  //wtp init
	  ret = WtpInit(host, port);
	  
	  return ret;
}

// connect
int WspConnect()
{
	 char pdu[500];
   int ret = 0;
	 
	 memset(pdu, 0, 500);
	
	 if (g_target == TARGET_WAP_BROWSER)
	 {
		  // pdu type, conenct pdu
		  pdu[0] = 0x01;

		  // version
		  pdu[1] = 0x10;

		  // capabilities length, 16
		  pdu[2] = 0x10;

		  // header length, 473
		  pdu[3] = 0x83;
		  pdu[4] = 0x59;

		  // capabilities, static field
		  memcpy(pdu + 5, brwcap, 16);

		  // headers, static field 
		  memcpy(pdu + 21, brwhead, 473);

		  // wtp invoke request, wsp pdu length 494
		  ret = WtpInvokeReq(pdu, 494);
	 }
	 else
	 {

		 // pdu type, conenct pdu
		 pdu[0] = 0x01;

		 // version
		 pdu[1] = 0x10;

		 // capabilities length, 19
		 pdu[2] = 0x13;

		 // header length, 73
		 pdu[3] = 0x49;
		 
         // capabilities, static field
		 memcpy(pdu + 4, mmscap, 19);

		 // headers, static field 
		 memcpy(pdu + 23, mmshead, 73);

		 // wtp invoke request, wsp pdu length 96
		 ret = WtpInvokeReq(pdu, 96);

	 }

	 return ret;
}

int WspIsConnectEstablished()
{
	  int ret = 0;
	  int status;
	  int type;
	  char data[1460];
	  
	  memset(data, 0, 1460);
	
	  // wtp invoke result
	  ret = WtpInvokeResult(data, 1460);
	  
	  if (ret == 0)
	  {
	  	  return 0;
	  }
	  
	  type = ParseWspResult(data, 1460, &status);
	  
	  if (type != ConnectReply_PDU)
	  {
	  	  return 0;
	  }
	  
	  return 1;
	  
}

// disconnect
int WspDisconenct()
{
	  int ret = 0;
	  
	  // wtp abort
    ret = WtpAbort();
	  
	  return ret;
}

// method invoke
int WspMethodInvoke( int method, char *url, char *data, int len)
{
	  char pdu[2000];
	  int ret = 0;
	  int url_len;
	  
	  char content_type[] = {'a','p','p','l','i','c','a','t','i',
		                       'o','n','/','v','n','d','.','w','a',
							             'p','.','m','m','s','-','m','e','s',
							             's','a','g','e','\0'
	                         };

	 char header[] = {(char)0x80,(char)0x61,(char)0x70,(char)0x70,(char)0x6C,(char)0x69,(char)0x63,(char)0x61,
				            (char)0x74,(char)0x69,(char)0x6F,(char)0x6E,(char)0x2F,(char)0x76,(char)0x6E,(char)0x64,
				            (char)0x2E,(char)0x77,(char)0x61,(char)0x70,(char)0x2E,(char)0x6D,(char)0x6D,(char)0x73,
					          (char)0x2D,(char)0x6D,(char)0x65,(char)0x73,(char)0x73,(char)0x61,(char)0x67,(char)0x65,
					          (char)0x00,(char)0x88,(char)0x80
	                 };
	
    memset(pdu, 0, 2000);
	  url_len = (int)strlen(url);
	
	  if (method == METHOD_GET) 
    {
       // method type get
		   pdu[0] = 0x40;

       // url length
		   pdu[1] = (char)url_len;

		   // url
		   memcpy(pdu + 2, url, url_len);

		   // wtp invoke request
		   ret = WtpInvokeReq(pdu, url_len + 2);

    } 
    else /* post method, for MMS */
    {
		  
		   // method type post
		   pdu[0] = 0x60;

       // url length
		   pdu[1] = (char)url_len;
		   
		   // headers length
		   pdu[2] = 0x43;

		   // url
		   memcpy(pdu + 3, url, url_len);
		   
		   // content type
		   memcpy(pdu + url_len + 3, 
		          content_type, sizeof(content_type));
		   
		   // header
		   memcpy(pdu + url_len + sizeof(content_type) + 3, 
		          header, sizeof(header)); 

		   // data
		   memcpy(pdu + url_len + sizeof(content_type) + sizeof(header) + 3, 
		          data, len); 
		   
		   // wtp invoke request
		   ret = WtpSegInvokeReq(pdu, 
		                         url_len + sizeof(content_type) 
		                         + sizeof(header) + len + 3);
    
    }
	
	  return ret;
}

// result reply
int WspReply(int *status, char *data, int len)
{
	int ret = 0;
	int type = 0;
  
  // wtp invoke result
	ret = WtpInvokeResult(data, len);
	
	 if (ret == 0)
	  {
	  	  return 0;
	  }
	  
	  type = ParseWspResult(data, 1460, status);
	  
	  if (type != Reply_PDU)
	  {
	  	  return 0;
	  }
	  
	  return 1;
	
	//parse wsp status
       //	ParseWspResult(data, len, status);

       //	return ret;
	
}

// wsp close
int WspClose()
{
    int ret = 0;
	   
	  ret = WtpClose();
	  
	  return ret;
  	
}

// parse wsp result pdu
static int ParseWspResult(char *data, int len, int *status)
{
	   int type;
	   
	   // get wsp result pdu type
	   type = (int)data[0];
	   
	   //printf("type:%d\r\n", type);
	   
	   if (type == Reply_PDU)
	   {
	   	   *status = (int)data[1];
	    }
	    else
	    {
	    	 *status = 0;
	    }
	   
	   return type;
	
}

⌨️ 快捷键说明

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