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

📄 stun.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 5 页
字号:
      else if (atrType == SA_CHANGEREQUEST)      {            msg->hasChangeRequest = TRUE;            if (stunParseAtrChangeRequest( body, attrLen, &msg->changeRequest) == FALSE)            {               ortp_error("stun: problem parsing SA_CHANGEREQUEST\n");               return FALSE;            }            else            {               ortp_debug("stun: SA_CHANGEREQUEST = %i\n", msg->changeRequest.value);            }      }      else if (atrType == SA_SOURCEADDRESS)      {            msg->hasSourceAddress = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->sourceAddress )== FALSE )            {               ortp_error("stun: problem parsing SA_SOURCEADDRESS\n");               return FALSE;            }            else            {               ortp_debug("stun: SA_SOURCEADDRESS = %s\n", ipaddr(&msg->sourceAddress.ipv4) );            }      }      else if (atrType == SA_CHANGEDADDRESS)      {            msg->hasChangedAddress = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->changedAddress )== FALSE )            {               ortp_error("stun: problem parsing SA_CHANGEDADDRESS\n");               return FALSE;            }            else            {               ortp_debug("stun: SA_CHANGEDADDRESS = %s\n", ipaddr(&msg->changedAddress.ipv4));            }      }      else if (atrType == SA_USERNAME)      {            msg->hasUsername = TRUE;            if (stunParseAtrString( body, attrLen, &msg->username) == FALSE)            {               ortp_error("stun: problem parsing SA_USERNAME");               return FALSE;            }            else            {               ortp_debug("stun: SA_USERNAME = %s\n", msg->username.value );            }					      }      else if (atrType == SA_PASSWORD)      {            msg->hasPassword = TRUE;            if (stunParseAtrString( body, attrLen, &msg->password) == FALSE)            {               ortp_error("stun: problem parsing SA_PASSWORD");               return FALSE;            }            else            {               ortp_debug("stun: SA_PASSWORD = %s\n", msg->password.value );            }      }      else if (atrType == SA_MESSAGEINTEGRITY)      {            msg->hasMessageIntegrity = TRUE;            if (stunParseAtrIntegrity( body, attrLen, &msg->messageIntegrity) == FALSE)            {               ortp_error("stun: problem parsing SA_MESSAGEINTEGRITY");               return FALSE;            }					      }      else if (atrType == SA_ERRORCODE)      {            msg->hasErrorCode = TRUE;            if (stunParseAtrError(body, attrLen, &msg->errorCode) == FALSE)            {               ortp_error("stun: problem parsing SA_ERRORCODE");               return FALSE;            }            else            {               ortp_debug("stun: SA_ERRORCODE = %i %i %s\n",                                   msg->errorCode.errorClass ,                                   msg->errorCode.number ,                                   msg->errorCode.reason );            }					       }      else if (atrType == SA_UNKNOWNATTRIBUTE)      {           msg->hasUnknownAttributes = TRUE;            if (stunParseAtrUnknown(body, attrLen, &msg->unknownAttributes) == FALSE)            {               ortp_error("stun: problem parsing SA_UNKNOWNATTRIBUTE");               return FALSE;            }      }      else if (atrType == SA_REFLECTEDFROM)      {            msg->hasReflectedFrom = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->reflectedFrom ) == FALSE )            {               ortp_error("stun: problem parsing SA_REFLECTEDFROM");               return FALSE;            }      }      else if (atrType == SA_REALM)      {            msg->hasRealm = TRUE;            if (stunParseAtrString( body, attrLen, &msg->realmName) == FALSE)            {               ortp_error("stun: problem parsing SA_REALM");               return FALSE;            }            else            {               ortp_debug("stun: SA_REALM = %s\n", msg->realmName.value );            }      }      else if (atrType == SA_NONCE)      {            msg->hasNonce = TRUE;            if (stunParseAtrString( body, attrLen, &msg->nonceName) == FALSE)            {               ortp_error("stun: problem parsing SA_NONCE");               return FALSE;            }            else            {               ortp_debug("stun: SA_NONCE = %s\n", msg->nonceName.value );            }      }      else if (atrType == SA_XORMAPPEDADDRESS || atrType == SA_XORMAPPEDADDRESS2)      {            msg->hasXorMappedAddress = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->xorMappedAddress ) == FALSE )            {               ortp_error("stun: problem parsing SA_XORMAPPEDADDRESS");               return FALSE;            }            else            {               ortp_debug("stun: SA_XORMAPPEDADDRESS = %s\n", ipaddr(&msg->xorMappedAddress.ipv4) );            }      }      else if (atrType == SA_XORONLY)      {            ortp_warning("stun: SA_XORONLY - non standard extension ignored\n" );      }      else if (atrType == SA_SECONDARYADDRESS)      {            ortp_debug("stun: SA_SECONDARYADDRESS - non standard extension ignored\n" );      }      else if (atrType == SA_SOFTWARE)      {            msg->hasSoftware = TRUE;            if (stunParseAtrString( body, attrLen, &msg->softwareName) == FALSE)            {               ortp_error("stun: problem parsing SA_SOFTWARE");               return FALSE;            }            else            {               ortp_debug("stun: SA_SOFTWARE = %s\n", msg->softwareName.value );            }      }      else if (atrType == TA_CHANNELNUMBER)      {           msg->hasChannelNumberAttributes = TRUE;            if (turnParseAtrChannelNumber(body, attrLen, &msg->channelNumberAttributes) == FALSE)            {               ortp_error("stun: problem parsing TA_CHANNELNUMBER");               return FALSE;            }					      }      else if (atrType == TA_LIFETIME)      {           msg->hasLifetimeAttributes = TRUE;            if (turnParseAtrLifetime(body, attrLen, &msg->lifetimeAttributes) == FALSE)            {               ortp_error("stun: problem parsing TA_LIFETIME");               return FALSE;            }					      }      else if (atrType == TA_DEPRECATEDBANDWIDTH)      {           ortp_warning("stun: deprecated attribute TA_DEPRECATEDBANDWIDTH");      }      else if (atrType == TA_XORPEERADDRESS)      {            msg->hasXorPeerAddress = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->xorPeerAddress )== FALSE )            {               ortp_error("stun: problem parsing SA_XORPEERADDRESS\n");               return FALSE;            }            else            {               ortp_debug("stun: SA_XORPEERADDRESS = %s\n", ipaddr(&msg->xorPeerAddress.ipv4));            }      }      else if (atrType == TA_DATA)      {            msg->hasData = TRUE;            if (turnParseAtrData( body, attrLen, &msg->data) == FALSE)            {               ortp_error("stun: problem parsing TA_DATA");               return FALSE;            }            else            {            }      }      else if (atrType == TA_XORRELAYEDADDRESS)      {            msg->hasXorRelayedAddress = TRUE;            if ( stunParseAtrAddress(  body,  attrLen,  &msg->xorRelayedAddress )== FALSE )            {               ortp_error("stun: problem parsing TA_XORRELAYEDADDRESS\n");               return FALSE;            }            else            {               ortp_debug("stun: TA_XORRELAYEDADDRESS = %s\n", ipaddr(&msg->xorRelayedAddress.ipv4));            }      }      else if (atrType == TA_EVENPORT)      {           ortp_warning("stun: do we need this... TA_EVENPORT");      }      else if (atrType == TA_REQUESTEDTRANSPORT)      {            msg->hasRequestedTransport = TRUE;            if ( turnParseAtrRequestedTransport(  body,  attrLen,  &msg->requestedTransport )== FALSE )            {               ortp_error("stun: problem parsing TA_REQUESTEDTRANSPORT\n");               return FALSE;            }      }      else if (atrType == TA_DONTFRAGMENT)      {            msg->hasDontFragment = TRUE;      }      else if (atrType == TA_DEPRECATEDTIMERVAL)      {           ortp_warning("stun: deprecated attribute TA_DEPRECATEDTIMERVAL");      }      else if (atrType == TA_RESERVATIONTOKEN)      {            msg->hasReservationToken = TRUE;            if ( turnParseAtrReservationToken(  body,  attrLen,  &msg->reservationToken)== FALSE )            {               ortp_error("stun: problem parsing TA_RESERVATIONTOKEN\n");               return FALSE;            }      }      else if (atrType == SA_FINGERPRINT)      {            msg->hasFingerprint = TRUE;            if ( turnParseAtrFingerprint(  body,  attrLen,  &msg->fingerprint)== FALSE )            {               ortp_error("stun: problem parsing SA_FINGERPRINT\n");               return FALSE;            }      }      else if (atrType == ICEA_PRIORITY)      {            msg->hasLifetimeAttributes = TRUE;            if (iceParseAtrPriority(body, attrLen, &msg->priority) == FALSE)            {               ortp_error("stun: problem parsing ICEA_PRIORITY");               return FALSE;            }					      }      else if (atrType == ICEA_USECANDIDATE)      {            msg->hasUseCandidate = TRUE;      }      else if (atrType == ICEA_ICECONTROLLED)      {           msg->hasIceControlled = TRUE;           if (iceParseAtrIceControll(body, attrLen, &msg->iceControlled) == FALSE)            {               ortp_error("stun: problem parsing ICEA_ICECONTROLLED");               return FALSE;            }      }      else if (atrType == ICEA_ICECONTROLLING)      {           msg->hasIceControlling = TRUE;           if (iceParseAtrIceControll(body, attrLen, &msg->iceControlling) == FALSE)            {               ortp_error("stun: problem parsing ICEA_ICECONTROLLING");               return FALSE;            }      }      else      {            if ( atrType <= 0x7FFF )             {              ortp_error("stun: Unknown Comprehension-Required attribute: %04x\n", atrType );              return FALSE;            }            else              ortp_warning("stun: Unknown attribute: %04x\n", atrType );      }		      if (attrLen%4>0)        attrLen += (4-(attrLen%4));            body += attrLen;      size -= attrLen;   }       return TRUE;}static char* encode16(char* buf, UInt16 data){   UInt16 ndata = htons(data);   memcpy(buf, &ndata, sizeof(UInt16));   return buf + sizeof(UInt16);}static char* encode32(char* buf, UInt32 data){   UInt32 ndata = htonl(data);   memcpy(buf, &ndata, sizeof(UInt32));   return buf + sizeof(UInt32);}static char* encode(char* buf, const char* data, unsigned int length){   memcpy(buf, data, length);   return buf + length;}static char* encodeAtrAddress4(char* ptr, UInt16 type, const StunAtrAddress4 *atr){   ptr = encode16(ptr, type);   ptr = encode16(ptr, 8);   *ptr++ = atr->pad;   *ptr++ = IPv4Family;   ptr = encode16(ptr, atr->ipv4.port);   ptr = encode32(ptr, atr->ipv4.addr);	   return ptr;}static char* encodeAtrChangeRequest(char* ptr, const StunAtrChangeRequest *atr){   ptr = encode16(ptr, SA_CHANGEREQUEST);   ptr = encode16(ptr, 4);   ptr = encode32(ptr, atr->value);   return ptr;}static char* encodeAtrError(char* ptr, const StunAtrError *atr){   ptr = encode16(ptr, SA_ERRORCODE);   ptr = encode16(ptr, 6 + atr->sizeReason);   ptr = encode16(ptr, atr->pad);   *ptr++ = atr->errorClass;   *ptr++ = atr->number;   ptr = encode(ptr, atr->reason, atr->sizeReason);   return ptr;}static char* encodeAtrUnknown(char* ptr, const StunAtrUnknown *atr){   int i;   ptr = encode16(ptr, SA_UNKNOWNATTRIBUTE);   ptr = encode16(ptr, 2+2*atr->numAttributes);   for (i=0; i<atr->numAttributes; i++)   {      ptr = encode16(ptr, atr->attrType[i]);   }   return ptr;}static char* encodeAtrString(char* ptr, UInt16 type, const StunAtrString *atr){   int padding;   int i;	   ptr = encode16(ptr, type);   ptr = encode16(ptr, atr->sizeValue);   ptr = encode(ptr, atr->value, atr->sizeValue);   padding = atr->sizeValue % 4;   if (padding>0)   {     for (i=0;i<4-padding;i++)     {       *ptr++ = 0;     }   }   return ptr;}static char* encodeAtrIntegrity(char* ptr, const StunAtrIntegrity *atr){   ptr = encode16(ptr, SA_MESSAGEINTEGRITY);   ptr = encode16(ptr, 20);   ptr = encode(ptr, atr->hash, sizeof(atr->hash));   return ptr;}static char* encodeAtrRequestedTransport(char* ptr, const TurnAtrRequestedTransport *atr){   ptr = encode16(ptr, TA_REQUESTEDTRANSPORT);   ptr = encode16(ptr, 4);   *ptr++ = atr->proto;   *ptr++ = atr->pad1;   *ptr++ = atr->pad2;   *ptr++ = atr->pad3;   return ptr;}static char* encodeAtrLifeTime(char* ptr, const TurnAtrLifetime *atr){   ptr = encode16(ptr, TA_LIFETIME);   ptr = encode16(ptr, 4);   ptr = encode32(ptr, atr->lifetime);   return ptr;}static char* encodeAtrDontFragment(char* ptr){   ptr = encode16(ptr, TA_DONTFRAGMENT);   ptr = encode16(ptr, 0);   return ptr;}unsigned intstunEncodeMessage( const StunMessage *msg,                    char* buf,                    unsigned int bufLen,                    const StunAtrString *password){   char* ptr = buf;   char* lengthp;   ptr = encode16(ptr, msg->msgHdr.msgType);   lengthp = ptr;   ptr = encode16(ptr, 0);   ptr = encode32(ptr, msg->msgHdr.magic_cookie);   ptr = encode(ptr, (const char*)msg->msgHdr.tr_id.octet, sizeof(msg->msgHdr.tr_id));	   ortp_debug("stun: Encoding stun message: ");   if (msg->hasRequestedTransport)   {      ortp_debug("stun: Encoding TA_REQUESTEDTRANSPORT: %i\n", msg->requestedTransport.proto );

⌨️ 快捷键说明

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