0

I am trying to create my own http server without using any low-level builtin library except ctypes but things are going terribly wrong.

My configuration is correct: IP address is correct, port is available, I have root privileges, using AF_INET (IPV4 connection) tcp_protocol with sock stream.

My config file includes:

{
    "ipv4_connection" : 2,
    "available_port" : 8081,
    "host_address" : [127, 0, 0, 1],
    "sock_stream" : 1,
    "tcp_protocol" : 0
}

I have created a ctype socket object using ctypes.Structure and everything looks good:

class SocketDeclaration(ctypes.Structure):
    """
        -- load this instance when you have to bind the socket to the given address !
    """
    _fields_ = [
        ('sin_family', ctypes.c_short),
        ('sin_port', ctypes.c_ushort),
        ('sin_addr', ctypes.c_ubyte * 4)
    ]

    def __init__(self):
        super().__init__()
        self.config = ConfigurationFetcher()
        self.sin_family = ctypes.c_short(self.config.ipv4_connection)
        self.sin_port = ctypes.c_ushort((self.config.available_port >> 8) | (self.config.available_port << 8))
        self.sin_addr = (ctypes.c_ubyte * 4)(*self.config.host_address)

The issue here is that despite getting the wrong answer from this call of bind

result = self.libc_object.bind(socket_object, instance_reference, instance_size)

from a method to handle errors gracefully being called as:

# Binding Socket to the Address
socket_instance = SocketDeclaration()
_ = self.bind_socket(new_socket, ctypes.byref(socket_instance), ctypes.sizeof(socket_instance))

it doesn't set the errorno value to -1. My exception object says "unable to bind ; Code = success" - why?

For further clarification the address sin_address is a reference to the object and not values, socket file descriptor returns 3 which is a valid response. I know syntax and just unable to figure about what is wrong.

strace shows that I have some kind of invalid argument as sin_addr but after reading a lot of documentation I just end up asking for the solution from any expert.

Any kind of help would be appreciated !

I literally tried everything; from changing ports to changing address! My machine is Kali kernal 6.10.11-amd64. I studied documentation from 5 sources including Python documentation, IBM, Stackoverflow, Chatgpt, Meta ai and it doesn't work!

6

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.