iOS bind udp socket in c++ always says 'deny(1) network-bind*:0'?

I am developing a UE4 game that requires to bind a socket(created from posix api) to cellular interface in c++(which is UE4 only capable of). But no matter what i tried, i always got 'deny(1) network-bind*:0'.

Here is how i bind that:

void FSocketSubsystemIOS::BindSocketToCellular(int fd)
{
        ifaddrs* Interfaces = NULL;
        if (getifaddrs(&Interfaces) != 0) return;

        for (ifaddrs* Travel = Interfaces; Travel != NULL; Travel = Travel->ifa_next)
        {
                if (Travel->ifa_addr->sa_family == AF_INET)
                {
                        if (strcmp(Travel->ifa_name, "pdp_ip0") == 0)
                        {
                                if (int err = bind(fd, Travel->ifa_addr, sizeof(*Travel->ifa_addr)))
                                {
                                        CellularBondMap.Add(fd, false);
                                        UE_LOG(LogIOS, Error, TEXT("[socket binding] cannot bind to pdp_ip0, errno: %d"), errno);
                                }
                                else 
                                {
                                        CellularBondMap.Add(fd, true);
                                        UE_LOG(LogIOS, Error, TEXT("[socket binding] bind to pdp_ip0"));
                                }
                        }
                }
        }
        freeifaddrs(Interfaces);
}

I have tried to add entitlements

<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>

but never worked.

However, I also tried this in a small iOS app that i build from scratch, and do this in c++, it works.

Can anybody helps me?

I have tried to add entitlements

These are App Sandbox entitlements and the App Sandbox is a macOS feature. iOS apps always run in a sandbox and you can’t configure it with App Sandbox entitlements.

if (Travel->ifa_addr->sa_family == AF_INET)

Do not hard code IPv4. Many iOS devices in the real world have no IPv4 address on their WWAN interface.

if (strcmp(Travel->ifa_name, "pdp_ip0") == 0)

Do not hard code the name of the WWAN interface. It is not considered API. The value you’re using there will fail on certain iOS devices.

Also, using bind for this might work but it’s not a great option. Rather, use IP_BOUND_IF.

However, I also tried this in a small iOS app that i build from scratch, and do this in c++, it works.

That suggests that the actual problem lies with your third-party tooling, which isn’t something I can help you with.

Finally, our preferred API for low-level TCP (and UDP) connections is Network framework, and it supports this out of the box via the requiredInterfaceType property.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

hi, I have tried IP_BOUND_IF, but i got errno: 22 (Invalid arguments). (Im sure I got the code right, becase i tried the same code in my test app).

That suggests that the actual problem lies with your third-party tooling

I know this error could be caused by third-party tool error, but could you please tell me in what case would an ios app report errno 22 while IP_BOUND_IF, or deny(1) while bind? Is that permission issue?

The UE4 forum can barely helps me...

I have tried IP_BOUND_IF, but i got errno 22 (Invalid arguments). (Im sure I got the code right, becase i tried the same code in my test app).

Lemme see if I understand you correctly:

  • You call IP_BOUND_IF in your test app and it worked there.

  • You take the same code and put it in your real app and it failed there.

Is that right?

In that case, sheesh, I’ve no idea. I can’t think of anything on the Apple side that would cause that.

What I would do in your situation is set an breakpoint on the setsockopt system call (well, the libSystem function that fronts that system call), dump the actual parameter values being passed in, and compare the results you get in your test and real apps.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Do not hard code IPv4. Many iOS devices in the real world have no IPv4 address on their WWAN interface.

Turns out you are right, i forgot to check the socket protocol. When i try IPV6_BOUND_IF it succeded. Thank very much for your patient reply again.

iOS bind udp socket in c&#43;&#43; always says 'deny(1) network-bind*:0'?
 
 
Q