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
+<rdar://problem/5545930> BTMM: Back to My Mac not working with D-Link DGL-4100 NAT gateway
+
+Revision 1.42 2007/10/16 17:37:18 cheshire
+<rdar://problem/3557903> 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
<rdar://problem/5495568> Legacy NAT Traversal - unmap request failed with error -65549
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);
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
}
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);
"</SOAP-ENV:Envelope>\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);
"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_ */