envoy / Embassy

Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux
MIT License
592 stars 74 forks source link

Binding failed while starting the server #50

Open srijanraj opened 6 years ago

srijanraj commented 6 years ago

var address = sockaddr_in6()

if !os(Linux)

    address.sin6_len = UInt8(MemoryLayout<sockaddr_in6>.stride)
    #endif
    address.sin6_family = sa_family_t(AF_INET6)
    address.sin6_port = UInt16(port).bigEndian //This basically I think Cause error
    address.sin6_flowinfo = 0
    address.sin6_addr = try ipAddressToStruct(address: interface)
    address.sin6_scope_id = 0
    let size = socklen_t(MemoryLayout<sockaddr_in6>.size)
    // bind the address and port on socket
    guard withUnsafePointer(to: &address, { pointer in
        return pointer.withMemoryRebound(to: sockaddr.self, capacity: Int(size)) { pointer in
            return SystemLibrary.bind(fileDescriptor, pointer, size) >= 0
        }
    }) else {
        throw OSError.lastIOError()
    }

This part of the code is throwing the error. Error number 1 and error description "Operation Not Permitted". I am new to swift and trying to implement mock server on the ongoing project. Can someone have answer for this?

fangpenlin commented 6 years ago

@srijanraj What's the port number you are binding to? As far as I know, if you are binding to port numbers which are below 1024 or a certain number, say you're binding port 80, it will usually require root permission to do so. I guess that's why you are seeing Operation Not Permitted error. If it's the case, I would suggest to use port numbers like 8080 or any number you like that's higher than 1024

Krystek20 commented 9 months ago

@fangpenlin

I set port 8880 and I got the same issue, when I create severer from main app target it's okay, but when I try start server from uitests target for macOS app (for iOS it works) then I got same issue:

Embassy.OSError.ioError(number: 1, message: "Operation not permitted")

Additionally my entitlements are set:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.network.server</key>
    <true/>
</dict>
</plist>