0

I wrote a C++ server and a PHP client both communicating based on UNIX domain sockets. Reading OR writing in either direction works just fine. As soon as I try to read AND then write (or vice versa), the communication does not finish: the client returns "504 Gateway Time-out" after a while and the server just keeps waiting after printing "Waiting for authentication...".

PHP client:

<?php
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
$conn = socket_connect($sock, '/tmp/some.socket');


$pass = "pass\0";

if ($sock == FALSE)
    echo "Error: <br />   $errstr ($errno)<br />\n";
else
{
    echo "Sending the password... ";

    $ret = socket_write($sock, $pass, strlen($pass));

    if ($ret == FALSE)
        echo "error! " .  socket_strerror( socket_last_error());
    else
    {
        echo "Password was sent.<br /> ";

        $auth = socket_read($sock, 256);

        if (FALSE === $auth) 
            echo "sending password failed; reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        else if ($auth != "authenticated")
            echo "Authentication failed: $auth.";
        else
            echo "Authentication was successful. <br />";
    }
}

socket_close($sock);

?>

Main server cpp file:

#include <iostream>

#include "UnixDomainSocket.hpp"


int main()
{
    try
    {
        UnixDomainSocket uds("/tmp/some.socket");

        std::cout << "Server started." << std::endl;

        while (true)
        {
            //if a new connection stablished, read and process data 
            if (uds.newConnectionEstablished())
            {
                std::cout << "Got a new connection. Waiting for authentication..." << std::endl;

                std::string command = uds.getClientMsg().getValue();

                if (command != "pass")
                {
                    std::cout << "401" << std::endl;
                    uds.sendMsg("401");
                }
                else
                {
                    std::cout << "authenticated" << std::endl;

                    auto msgRet = uds.sendMsg("authenticated");
                }

                uds.closeConnection();
            }
        }
    }
    catch (const std::string & err)
    {
        std::cout << "Error: " << err << std::endl;

        return -1;
    }
    catch (const std::exception & err)
    {
        std::cout << "Error: " << std::string(err.what()) << std::endl;

        return -1;
    }
    catch (...)
    {
        std::cout << "Unhandled error occured. Daemon stopped." << std::endl;

        return -1;
    }
    std::cout << "Server shut down." << std::endl;
}

Server header:

#ifndef UNIXDOMAINSOCKET_HPP
#define UNIXDOMAINSOCKET_HPP

#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>

#include <string>

#include "Expected.hpp"


const int BUFFSIZE = 1024;

class UnixDomainSocket
{
public:
    UnixDomainSocket (const std::string & socketPath);
    ~UnixDomainSocket();

    bool newConnectionEstablished();
    Expected<std::string> getClientMsg();
    Expected<bool> sendMsg (const std::string & msg);
    void closeConnection();
    void closeConnection (const std::string & quitMessage);

protected:
    std::string socketPath;
    unsigned long maxConnections;
    bool connectionEstablished;

    struct sockaddr_un addr;
    int serverFileDescriptor, clientFileDescriptor;
    ssize_t bytes;
    char buf[BUFFSIZE];
};

#endif

Server cpp:

#include "UnixDomainSocket.hpp"

#include <iostream>


UnixDomainSocket::UnixDomainSocket (const std::string & socketPath)
    : socketPath(socketPath), maxConnections(100)
{
    if ((serverFileDescriptor = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
        throw "socket  error";

    memset (&addr, 0, sizeof(addr));

    //ensure that all fields, including non−standard ones, are initialized to 0
    addr.sun_family = AF_UNIX;

    //we copy one byte less, ensuring a trailing 0 exists
    strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);


    if (access(addr.sun_path, F_OK) == 0)
        unlink(addr.sun_path);

    if (bind(serverFileDescriptor, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
        throw "bind error";

    if (listen(serverFileDescriptor, maxConnections) < 0)
        throw "listen error";
}

UnixDomainSocket::~UnixDomainSocket()
{
    closeConnection();
}

bool UnixDomainSocket::newConnectionEstablished()
{
    if ((clientFileDescriptor = accept(serverFileDescriptor, NULL, NULL)) < 0)
         throw "accept error";

     connectionEstablished = true;

     return true;
}

Expected<std::string> UnixDomainSocket::getClientMsg()
{
    if (!connectionEstablished)
        return Expected<std::string>::fromException(std::logic_error("No connection established yet."));

    std::string msg;

    while ((bytes = read (clientFileDescriptor, buf, BUFFSIZE)) > 0)
        msg += buf;

    // if (msg.length())
    //     throw "empty msg from client";

    if (bytes < 0)
        throw "read error";

    return msg;
}

Expected<bool> UnixDomainSocket::sendMsg (const std::string & msg)
{
    if (!connectionEstablished)
        return Expected<bool>::fromException(std::logic_error("No connection established yet."));

    if (msg.empty())
        return Expected<bool>::fromException(std::logic_error("The message must be not empty."));


    auto bytesSent = send(clientFileDescriptor, (msg + "\n").c_str(), msg.length(), MSG_CONFIRM);

    ////Also tried:
    // long bytesSent = -1;
    // while (bytesSent < 0)
    //     bytesSent = write(clientFileDescriptor, msg.c_str(), msg.length());

    if (bytesSent != msg.length())
        return Expected<bool>::fromException(std::logic_error("Error occured while sending."));

    return true;
}


void UnixDomainSocket::closeConnection (const std::string & quitMessage)
{
    sendMsg(quitMessage);
    closeConnection();
}

void UnixDomainSocket::closeConnection()
{
    if (close(clientFileDescriptor) < 0)
        throw "close error";

    connectionEstablished = false;
}
Sceptical Jule
  • 889
  • 1
  • 8
  • 26
  • 2
    `getClientMsg()` keeps reading until it gets an error (`bytes < 0`) or EOF (`bytes == 0`). But the client doesn't close the connection after sending the password, so it will never get EOF. It just blocks, waiting for more input. – Barmar Aug 11 '17 at 22:47
  • You need some way to delimit messages in the stream, and `getClientMsg()` should read one message. – Barmar Aug 11 '17 at 22:49
  • I added a loop to receive messages that are longer than the max buffer size in "socket_write()" ... – Sceptical Jule Aug 12 '17 at 18:16

1 Answers1

1

The problem is the implementation of your

getClientMsg() {
...
while ((bytes = read (clientFileDescriptor, buf, BUFFSIZE)) > 0)
    msg += buf;
...

in which, the function stocks (again) at

read()

once the "pass" is received successfully. So within the while loop you need to check the boundary of each

send()

I modified your code a little bit and it works find now:

while ((bytes = read (clientFileDescriptor, buf, BUFFSIZE)) > 0)
{
    msg += buf;

    if (buf[bytes] == 127) break; // completion of one send()
}

server screenshot

client screenshot

Another thing I noticed is that you close the socket in the end of every iteration of the main loop:

uds.closeConnection();

which will disable the future communications in between. Thus, better remove this line.

Alt Eisen
  • 598
  • 6
  • 9