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

📄 xtunnelsxcipher.cpp

📁 xtunnel nat/fw traversal source code
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*	File:       XTunnelsXCipher.cpp	Contains:   X-Cipher protocol definitions and implementation for X-Tunnels server	Copyright:  (c) 2003 by Xten Networks, Inc., all rights reserved.*/#if DEBUG#include <iostream>#endif // DEBUG#include <signal.h>#include <string.h>#include <netinet/in.h>#define BIND_8_COMPAT 1#include <arpa/nameser.h>#include <arpa/inet.h>//#include <arpa/nameser8_compat.h>#include <resolv.h>#include "restart.h"#include "uici.h"#include "XTunnelsFamilyData.h"#include "XTunnelsChildData.h"#include "XTunnelsXCipher.h"#if DEBUGusing std::cout;using std::endl;#endif // DEBUGusing std::list;using namespace XTunnels;#ifndef	TRUE#define TRUE	1#endif	/* TRUE */#ifndef	FALSE#define FALSE	0#endif	/* FALSE */namespace {#if DEBUGtime_t g_tStartTime = 0;void StartTiming(char* szMessage)   {   cout << "Starting timing -- " << szMessage << endl;   g_tStartTime = time(NULL);   }void TimeCheck(char* szMessage)   {   time_t tNow = time(NULL);   cout << "TimeCheck -- " << tNow - g_tStartTime << " seconds -- " << szMessage << endl;   }#endif // DEBUGtypedef struct{    u_int16_t 	type;    u_int16_t 	m_usClass;    u_int32_t 	ttl;    u_int16_t 	length;} rr_header;typedef struct{    u_int16_t 	priority;    u_int16_t 	weight;    u_int16_t 	port;} srv_header;static int decode_name(char** location, char* buffer, int len, char * decodedName)    // on input, *location is a pointer to the first byte to parse    // on return, *location is a pointer after the last byte we consumed   {    char 		name[64];    u_int8_t* 	name_loc = (u_int8_t*)*location;    u_int8_t* 	last_loc = name_loc;    u_int8_t 	name_len;    u_int16_t 	offset;    int 		hit_pointer;        name_len = *name_loc;        hit_pointer = FALSE;        if (decodedName)    decodedName[0] = '\0';        while (name_len != 0)    {        if ((name_len & INDIR_MASK) == INDIR_MASK)        {            // If this is the first pointer we've encountered, set             // *location to point to after the pointer.                if (!hit_pointer)                *location = (char *) (name_loc + 2);                hit_pointer = TRUE;                offset = ((*name_loc) & ~INDIR_MASK) << 8 | *(name_loc + 1);            name_loc = ((u_int8_t *) buffer) + offset;                if ((char *)name_loc >= *location)            {#if DEBUG            cout << "decode error, compression offset invalid!" << endl;#endif // DEBUG                return -2;            }                if (name_loc == last_loc)               {#if DEBUG               cout << "decode error, compression offset yields loop!" << endl;#endif // DEBUG               }                last_loc = name_loc;        }        else        {            int i = 0;                if ( ((char *) name_loc) + name_len > buffer + len)            {#if DEBUG               cout << "decode error, name extends past end of packet" << endl;#endif // DEBUG                return -3;            }                for (i = 0; i < name_len; i++)                name[i] = name_loc[i + 1];            name[i] = 0;                if (decodedName)            {                strcat(decodedName, name);                strcat(decodedName, ".");            }                name_loc += name_len + 1;        }            name_len = *name_loc;    }        // If the name contains no pointers, then we have to set     // *location to point to after the null terminator.        if (!hit_pointer)    *location = (char*)(name_loc + 1);        return 0;   }int decode_srv(char** location, char* buffer, int len, char * target, u_int16_t * port)   {    rr_header* header;        if (*location >= buffer + len || *location < buffer)      {#if DEBUG      cout << "decode error, ran off the buffer" << endl;#endif // DEBUG      return -1;      }    if (decode_name(location, buffer, len, NULL) != 0)        return -2;    header = (rr_header*)*location;    if (*location + sizeof(rr_header) >= buffer + len)      {#if DEBUG      cout << "decode error, packet too short for type, class, ttl, and length" << endl;#endif // DEBUG      return -3;      }        *location += sizeof(rr_header) - 2;    if (*location + ntohs(header->length) > buffer + len)        {#if DEBUG         cout << "decode error, packet too short for reported rr length" << endl;#endif // DEBUG         }             if (ntohs(header->type) == 33) // SRV      {      srv_header * srv = (srv_header *)*location;      char* name = *location + sizeof(srv_header);      if (port)         *port = ntohs(srv->port);      decode_name(&name, buffer, len, target);      }        *location += ntohs(header->length);    return 0;   }int skip_question(char** location, char* buffer, int len){    if (*location >= buffer + len || *location < buffer)    {#if DEBUG        cout << "decode error, ran off the buffer" << endl;#endif // DEBUG        return -1;    }    if (decode_name(location, buffer, len, NULL) != 0)        return -2;    if (*location + 4 > buffer + len)    {#if DEBUG        cout << "decode error, packet too short for type and class" << endl;#endif // DEBUG        return -4;    }    /* move past "type" */    *location += sizeof(u_int16_t);    /* move past "class" */    *location += sizeof(u_int16_t);    return 0;}#if DEBUGvoid CheckResult(int iResult, char* szMessage)   {   if (!iResult)      return;   if ((iResult >= XCipher::EErrorBadRemoteHost) && (iResult < XCipher::ELastProtocolError))      return;   cout << "X-Cipher: WARNING: CheckResult got unknown " << iResult << " error from '" << szMessage << "'! " << endl;   }#endif DEBUGint ConnectToGlobalServer()   {   static bool s_bCalledResInit = false;   if (!s_bCalledResInit)      res_init();   #define HARDCODE_GLOBAL_SERVER 0#if HARDCODE_GLOBAL_SERVER      return u_connect(      XCipher::EGlobalServer_PORT_XCIPHER_GLOBAL_SERVER,      "192.168.2.110"   );#endif // HARDCODE_GLOBAL_SERVER      int iGlobalServerSocket = -1;      // get ip and port here using SRV lookup   //const char* szSRVRequest = "_xcipherglobal._tcp.xten.net";   const char* szSRVRequest = XCipher::g_szXCipherGlobalServerSRVRequest;/*AlexG5:~ alex$ dig _xtunnels._tcp.xten.net SRV IN; <<>> DiG 9.2.2 <<>> _xtunnels._tcp.xten.net SRV IN;; global options:  printcmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3452;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; QUESTION SECTION:;_xtunnels._tcp.xten.net.       IN      SRV;; ANSWER SECTION:_xtunnels._tcp.xten.net. 3600   IN      SRV     10 0 13838 xtunnels.xten.net.;; ADDITIONAL SECTION:xtunnels.xten.net.      3600    IN      A       64.69.91.5;; Query time: 29 msec;; SERVER: 64.69.91.10#53(64.69.91.10);; WHEN: Mon Nov 10 13:10:39 2003;; MSG SIZE  rcvd: 86; <<>> DiG 9.2.2 <<>> _xcipherglobal._tcp.test.xten.net SRV IN;; global options:  printcmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62669;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 1;; QUESTION SECTION:;_xcipherglobal._tcp.test.xten.net. IN  SRV;; ANSWER SECTION:_xcipherglobal._tcp.test.xten.net. 3600 IN SRV  10 0 13842 dsl.xten.net.;; AUTHORITY SECTION:xten.net.               135642  IN      NS      dns2.gridfx.com.xten.net.               135642  IN      NS      dns1.gridfx.com.;; ADDITIONAL SECTION:dsl.xten.net.           3600    IN      A       64.180.255.194;; Query time: 42 msec;; SERVER: 192.168.2.1#53(192.168.2.1);; WHEN: Wed Nov 26 09:39:29 2003;; MSG SIZE  rcvd: 147*/   if (!szSRVRequest || !szSRVRequest[0])      return -1;      unsigned char pSRVReply[10 * 1024] = { 0 };   HEADER* hdr = (HEADER*)pSRVReply;   int iResultLength = res_query(szSRVRequest, C_ANY, /* SRV */ 33, pSRVReply, sizeof(pSRVReply));   if (iResultLength < (int)sizeof(HEADER))      {#if DEBUG    cout << "res_query() FAILED!" << endl;#endif // DEBUG      return -1;      }      // need to swap these from big endian   hdr->qdcount = ntohs(hdr->qdcount);   hdr->ancount = ntohs(hdr->ancount);   hdr->nscount = ntohs(hdr->nscount);   hdr->arcount = ntohs(hdr->arcount);   /*#if DEBUG   cout << "X-Cipher: ConnectToGlobalServer got " << iResultLength << " byte SRV record for " << szSRVRequest << endl;   cout << "   hdr->id " << (unsigned long)hdr->id << endl;   cout << "   hdr->qr " << (unsigned long)hdr->qr << endl;   cout << "   hdr->opcode " << (unsigned long)hdr->opcode << endl;   cout << "   hdr->aa " << (unsigned long)hdr->aa << endl;   cout << "   hdr->tc " << (unsigned long)hdr->tc << endl;   cout << "   hdr->rd " << (unsigned long)hdr->rd << endl;   cout << "   hdr->ra " << (unsigned long)hdr->ra << endl;   cout << "   hdr->unused " << (unsigned long)hdr->unused << endl;   cout << "   hdr->rcode " << (unsigned long)hdr->rcode << endl;   cout << "   hdr->qdcount " << (unsigned long)hdr->qdcount << endl;   cout << "   hdr->ancount " << (unsigned long)hdr->ancount << endl;   cout << "   hdr->nscount " << (unsigned long)hdr->nscount << endl;   cout << "   hdr->arcount " <<(unsigned long)hdr->arcount << endl;#endif // DEBUG

⌨️ 快捷键说明

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