X-Git-Url: https://git.saurik.com/apple/mdnsresponder.git/blobdiff_plain/67c8f8a10700c05d2460d60f5927f23cb5cb9241..96f69b28cfc861f450115f910b8a01ef5ba38806:/mDNSMacOSX/LegacyNATTraversal.c?ds=sidebyside diff --git a/mDNSMacOSX/LegacyNATTraversal.c b/mDNSMacOSX/LegacyNATTraversal.c index 09db9ca..2be3dd4 100644 --- a/mDNSMacOSX/LegacyNATTraversal.c +++ b/mDNSMacOSX/LegacyNATTraversal.c @@ -17,6 +17,19 @@ Change History (most recent first): $Log: LegacyNATTraversal.c,v $ +Revision 1.44 2007/11/02 20:45:40 cheshire +Don't log "connection failed" in customer builds + +Revision 1.43 2007/10/18 20:09:47 cheshire + BTMM: Back to My Mac not working with D-Link DGL-4100 NAT gateway + +Revision 1.42 2007/10/16 17:37:18 cheshire + Performance: Core code will not work on platforms with small stacks +Cut SendSOAPMsgControlAction stack from 2144 to 96 bytes + +Revision 1.41 2007/10/15 23:02:00 cheshire +Off-by-one error: Incorrect trailing zero byte on the end of the SSDP Discovery message + Revision 1.40 2007/09/20 21:41:49 cheshire Legacy NAT Traversal - unmap request failed with error -65549 @@ -283,28 +296,25 @@ mDNSlocal void handleLNTGetExternalAddressResponse(tcpLNTInfo *tcpInfo) mDNS *m = tcpInfo->m; mDNSu16 err = NATErr_None; mDNSv4Addr ExtAddr; - char *addrPtr; char *ptr = (char *)tcpInfo->Reply; char *end = (char *)tcpInfo->Reply + tcpInfo->nread; + char *addrend; + static char tagname[20] = "NewExternalIPAddress"; // Array NOT including a terminating nul // LogOperation("handleLNTGetExternalAddressResponse: %s", ptr); - while (ptr && ptr < end) // Find "NewExternalIPAddress" - { - if (*ptr == 'N' && (strncasecmp(ptr, "NewExternalIPAddress", 20) == 0)) break; // find the first 'N'; is this NewExternalIPAddress? if not, keep looking - ptr++; - } - if (ptr == mDNSNULL || ptr >= end) return; // bad or incomplete response - ptr+=21; // skip over "NewExternalIPAddress>" - if (ptr >= end) { LogOperation("handleLNTGetExternalAddressResponse: past end of buffer!"); return; } - - // find the end of the address and terminate the string so inet_pton() can convert it - for (addrPtr = ptr; addrPtr && addrPtr < end; addrPtr++) if (*addrPtr == '<') break; // first find the next '<' and count the chars - if (addrPtr == mDNSNULL || addrPtr >= end) { LogOperation("handleLNTGetExternalAddressResponse: didn't find SOAP URL string"); return; } - *addrPtr = '\0'; + while (ptr < end && strncasecmp(ptr, tagname, sizeof(tagname))) ptr++; + ptr += sizeof(tagname); // Skip over "NewExternalIPAddress" + while (ptr < end && *ptr != '>') ptr++; + ptr += 1; // Skip over ">" + // Find the end of the address and terminate the string so inet_pton() can convert it + addrend = ptr; + while (addrend < end && (mdnsIsDigit(*addrend) || *addrend == '.')) addrend++; + if (addrend >= end) return; + *addrend = 0; if (inet_pton(AF_INET, ptr, &ExtAddr) <= 0) - { LogMsg("handleLNTGetExternalAddressResponse: Router returned bad address"); err = NATErr_NetFail; } + { LogMsg("handleLNTGetExternalAddressResponse: Router returned bad address %s", ptr); err = NATErr_NetFail; } if (!err) LogOperation("handleLNTGetExternalAddressResponse: External IP address is %.4a", &ExtAddr); natTraversalHandleAddressReply(m, err, ExtAddr); @@ -319,7 +329,7 @@ mDNSlocal void handleLNTPortMappingResponse(tcpLNTInfo *tcpInfo) NATTraversalInfo *natInfo; for (natInfo = m->NATTraversals; natInfo; natInfo=natInfo->next) { if (natInfo == tcpInfo->parentNATInfo) break; } - + if (!natInfo) { LogOperation("handleLNTPortMappingResponse: can't find matching tcpInfo in NATTraversals!"); return; } // start from the beginning of the HTTP header; find "200 OK" status message; if the first characters after the @@ -465,7 +475,8 @@ mDNSlocal mStatus MakeTCPConnection(mDNS *const m, tcpLNTInfo *info, const mDNSA } else { - LogMsg("LNT MakeTCPConnection: connection failed"); + // Don't need to log this in customer builds -- it happens quite often during sleep, wake, configuration changes, etc. + LogOperation("LNT MakeTCPConnection: connection failed"); mDNSPlatformTCPCloseConnection(info->sock); // Dispose the socket we created with mDNSPlatformTCPSocket() above info->sock = mDNSNULL; mDNSPlatformMemFree(info->Reply); @@ -521,16 +532,16 @@ mDNSlocal mStatus SendSOAPMsgControlAction(mDNS *m, tcpLNTInfo *info, char *Acti "\r\n"; mStatus err; - char body[2048]; // Typically requires 1110-1122 bytes, so 2048 allows a generous safety margin + char *body = (char *)&m->omsg; // Typically requires 1110-1122 bytes; m->omsg is 8952 bytes, which is plenty int bodyLen; if (m->UPnPSOAPURL == mDNSNULL || m->UPnPSOAPAddressString == mDNSNULL) // if no SOAP URL or address exists get out here { LogOperation("SendSOAPMsgControlAction: no SOAP URL or address string"); return mStatus_Invalid; } // Create body - bodyLen = mDNS_snprintf (body, sizeof(body), body1, Action); - bodyLen += AddSOAPArguments(body + bodyLen, sizeof(body) - bodyLen, numArgs, Arguments); - bodyLen += mDNS_snprintf (body + bodyLen, sizeof(body) - bodyLen, body2, Action); + bodyLen = mDNS_snprintf (body, sizeof(m->omsg), body1, Action); + bodyLen += AddSOAPArguments(body + bodyLen, sizeof(m->omsg) - bodyLen, numArgs, Arguments); + bodyLen += mDNS_snprintf (body + bodyLen, sizeof(m->omsg) - bodyLen, body2, Action); // Create info->Request; the header needs to contain the bodyLen in the "Content-Length" field if (!info->Request) info->Request = mDNSPlatformMemAllocate(LNT_MAXBUFSIZE); @@ -809,10 +820,10 @@ mDNSexport void LNT_SendDiscoveryMsg(mDNS *m) "Man:\"ssdp:discover\"\r\n" "MX:3\r\n\r\n"; - LogOperation("LNT_SendDiscoveryMsg %.4a %.4a", &m->Router.ip.v4, &m->ExternalAddress); + LogOperation("LNT_SendDiscoveryMsg Router %.4a Current External Address %.4a", &m->Router.ip.v4, &m->ExternalAddress); if (!mDNSIPv4AddressIsZero(m->Router.ip.v4) && mDNSIPv4AddressIsZero(m->ExternalAddress)) - mDNSPlatformSendUDP(m, msg, msg + sizeof(msg), 0, &m->Router, SSDPPort); + mDNSPlatformSendUDP(m, msg, msg + sizeof(msg) - 1, 0, &m->Router, SSDPPort); } #endif /* _LEGACY_NAT_TRAVERSAL_ */