Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python AF_UNIX client C server

Tags:

python

c

sockets

I am beginner in python written first program two days ago. I am having connection problem in python client and C server for AF_UNIX. I have C socket Server with AF_LOCAL.

#define NAME "#/tmp/kvsd"

int
main()
{
    int sock, msgsock, rval;
    struct sockaddr_un server;
        char buf[1024];


        unlink(NAME);
        printf("before socket \n");
        sock = socket(AF_LOCAL, SOCK_STREAM, 0);
        if (sock < 0) {
                perror("opening stream socket");
                exit(1);
        }
        memset(&server, 0, (sizeof (server)));
        server.sun_family = AF_LOCAL;
        memcpy(server.sun_path, NAME, strlen(NAME));
        server.sun_path[0] = 0;
        printf("before bind \n");

        int len = strlen(server.sun_path) + sizeof(server.sun_family);
        if (bind(sock, (struct sockaddr *) &server, len)) {
                perror("binding stream socket");
                exit(1);
        }

        printf("before listen \n");
        if (listen(sock, 5) == -1) {
                perror("listen");
                exit(1);
        }
        printf("before accept \n");
        msgsock = accept(sock, 0, 0);
        printf("accepted \n");
        if (msgsock == -1)
                perror("accept");
        else do {
                bzero(buf, sizeof(buf));

                printf("before read  \n");
                if ((rval = read(msgsock, buf, 1024)) < 0)
                        perror("reading stream message");
                else if (rval == 0)
                        printf("Ending connection\n");
                else
                        printf("-->%s\n", buf);
        } while (rval > 0);
        close(msgsock);
        close(sock);
        unlink(NAME);
}

And Python AF_UNIX client.py:-

####### CLIENT CODE #######

from socket import *

# Create an unbond and not-connected socket.
sock = socket(AF_UNIX, SOCK_STREAM)

# Connect to the peer registered as "MyBindName" in the abstract namespace. Note the '\0'.
str = "\0/tmp/kvsd\0"

print "len ", len (str)
sock.connect("\0/tmp/kvsd")

# Wait for message
msg = sock.recv(100)
print msg

# Send reply
sock.send("Hi there!\n")

# Block until new message arrives
msg = sock.recv(100)

# When the socket is closed cleanly, recv unblocks and returns ""
if not msg:
    print "It seems the other side has closed its connection"

# Close it
sock.close()

But When I run the client I'm getting following error:

[root@mat afunix]# python ./client.py len 11 Traceback (most recent call last): File "./client.py", line 13, in sock.connect("\0/tmp/kvsd") File "", line 1, in connect socket.error: [Errno 111] Connection refused [root@mat afunix]#

I am trying to use the abstract namespaces for UNIX socket but my python client is not able to connect to c server.

I tried without abstract namespaces it works. (changed NAME macro in server.c to "/tmp/kvsd" and argument to sock.connect to "/tmp/kvsd").

Can someone help me to figure out what may be the exact issue ?

Thanks in advance.

like image 915
Royy Walls Avatar asked Oct 03 '22 20:10

Royy Walls


1 Answers

Following line has a problem.

    int len = strlen(server.sun_path) + sizeof(server.sun_family);

server.sun_path has now leading null character. So strlen(server.sun_path) is 0. You need change above line as follow:

    #include <stddef.h>

    ....

    int len = offsetof(struct sockaddr_un, sun_path) + strlen(NAME);

Then, it will work.

EDIT: updated the code to use offsetof to avoid padding issue. (Thank you, alk)

PS: I assume that both server, client use name without trailing null byte. If you use name with trailing null byte, add 1 to len.

like image 157
falsetru Avatar answered Oct 11 '22 15:10

falsetru