Question

I'm doing an ios application, it starts a server and listen for incoming connections, the device running the application may be behind a router so I need to make a port forward. I'm trying to make a port forward using DNSServiceNATPortMappingCreate but its always returning error code -65540

DNSServiceRef *sdRef = NULL ;
void ( *DNSServiceNATPortMappingReply) (DNSServiceRef sdRef,
                                        DNSServiceFlags flags,
                                        uint32_t interfaceIndex,
                                        DNSServiceErrorType errorCode,
                                        uint32_t externalAddress,
                                        DNSServiceProtocol protocol,
                                        uint16_t internalPort,
                                        uint16_t externalPort,
                                        uint32_t ttl,
                                        void *context );

DNSServiceNATPortMappingReply = &DNSServiceNATPortMappingCreate_callback ;

DNSServiceErrorType error = DNSServiceNATPortMappingCreate(sdRef,
                                                           0,
                                                           0,
                                                           kDNSServiceProtocol_TCP,
                                                           htons(2000),
                                                           htons(5000),
                                                           0,
                                                          DNSServiceNATPortMappingReply,
                                                           NULL
) ;

and this is the callback

void DNSServiceNATPortMappingCreate_callback(
                                         DNSServiceRef sdRef,
                                         DNSServiceFlags flags,
                                         uint32_t interfaceIndex,
                                         DNSServiceErrorType errorCode,
                                         uint32_t externalAddress, 
                                         DNSServiceProtocol protocol,
                                         uint16_t internalPort, 
                                         uint16_t externalPort, 
                                         uint32_t ttl, 
                                         void *context )
{
    printf("in callback\n") ;
}
Was it helpful?

Solution

Based on the docs for DNSServiceDiscovery, the error code -65540 means kDNSServiceErr_BadParam.

The docs for DNSServiceNATPortMappingCreate suggest that you have to allocate storage for the DNSServiceRef passed as the first argument. i.e. You need to change

DNSServiceRef *sdRef = NULL ;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(sdRef, ...

to

DNSServiceRef sdRef;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(&sdRef, ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top