summaryrefslogtreecommitdiff
path: root/Kernel/LocalSocket.cpp
blob: dbbd15edf6fd92f4b0612f6bc1417f1c6f37aba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <Kernel/LocalSocket.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/Process.h>
#include <Kernel/VirtualFileSystem.h>
#include <LibC/errno_numbers.h>

Retained<LocalSocket> LocalSocket::create(int type)
{
    return adopt(*new LocalSocket(type));
}

LocalSocket::LocalSocket(int type)
    : Socket(AF_LOCAL, type, 0)
{
    kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->name().characters(), current->pid(), this, type);
}

LocalSocket::~LocalSocket()
{
}

bool LocalSocket::get_address(sockaddr* address, socklen_t* address_size)
{
    // FIXME: Look into what fallback behavior we should have here.
    if (*address_size != sizeof(sockaddr_un))
        return false;
    memcpy(address, &m_address, sizeof(sockaddr_un));
    *address_size = sizeof(sockaddr_un);
    return true;
}

bool LocalSocket::bind(const sockaddr* address, socklen_t address_size, int& error)
{
    ASSERT(!is_connected());
    if (address_size != sizeof(sockaddr_un)) {
        error = -EINVAL;
        return false;
    }
    if (address->sa_family != AF_LOCAL) {
        error = -EINVAL;
        return false;
    }

    const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
    char safe_address[sizeof(local_address.sun_path) + 1];
    memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));

    kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->name().characters(), current->pid(), this, safe_address);

    m_file = VFS::the().open(safe_address, error, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->cwd_inode());
    if (!m_file) {
        if (error == -EEXIST)
            error = -EADDRINUSE;
        return false;
    }

    ASSERT(m_file->inode());
    m_file->inode()->bind_socket(*this);

    m_address = local_address;
    m_bound = true;
    return true;
}

bool LocalSocket::connect(const sockaddr* address, socklen_t address_size, int& error)
{
    ASSERT(!m_bound);
    if (address_size != sizeof(sockaddr_un)) {
        error = -EINVAL;
        return false;
    }
    if (address->sa_family != AF_LOCAL) {
        error = -EINVAL;
        return false;
    }

    const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
    char safe_address[sizeof(local_address.sun_path) + 1];
    memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));

    kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->name().characters(), current->pid(), this, safe_address);

    m_file = VFS::the().open(safe_address, error, 0, 0, current->cwd_inode());
    if (!m_file) {
        error = -ECONNREFUSED;
        return false;
    }
    ASSERT(m_file->inode());
    if (!m_file->inode()->socket()) {
        error = -ECONNREFUSED;
        return false;
    }

    m_address = local_address;

    auto peer = m_file->inode()->socket();
    kprintf("Queueing up connection\n");
    if (!peer->queue_connection_from(*this, error))
        return false;

    kprintf("Waiting for connect...\n");
    if (!current->wait_for_connect(*this, error))
        return false;

    kprintf("CONNECTED!\n");
    return true;
}

void LocalSocket::attach_fd(SocketRole role)
{
    if (role == SocketRole::Accepted) {
        ++m_accepted_fds_open;
    } else if (role == SocketRole::Connected) {
        ++m_connected_fds_open;
    }
}

void LocalSocket::detach_fd(SocketRole role)
{
    if (role == SocketRole::Accepted) {
        ASSERT(m_accepted_fds_open);
        --m_accepted_fds_open;
    } else if (role == SocketRole::Connected) {
        ASSERT(m_connected_fds_open);
        --m_connected_fds_open;
    }
}

bool LocalSocket::can_read(SocketRole role) const
{
    if (role == SocketRole::Listener)
        return can_accept();
    if (role == SocketRole::Accepted)
        return !m_connected_fds_open || !m_for_server.is_empty();
    if (role == SocketRole::Connected)
        return !m_accepted_fds_open || !m_for_client.is_empty();
    ASSERT_NOT_REACHED();
}

ssize_t LocalSocket::read(SocketRole role, byte* buffer, ssize_t size)
{
    if (role == SocketRole::Accepted)
        return m_for_server.read(buffer, size);
    if (role == SocketRole::Connected)
        return m_for_client.read(buffer, size);
    ASSERT_NOT_REACHED();
}

ssize_t LocalSocket::write(SocketRole role, const byte* data, ssize_t size)
{
    if (role == SocketRole::Accepted) {
        if (!m_accepted_fds_open)
            return -EPIPE;
        return m_for_client.write(data, size);
    }
    if (role == SocketRole::Connected) {
        if (!m_connected_fds_open)
            return -EPIPE;
        return m_for_server.write(data, size);
    }
    ASSERT_NOT_REACHED();
}

bool LocalSocket::can_write(SocketRole role) const
{
    if (role == SocketRole::Accepted)
        return !m_connected_fds_open || m_for_client.bytes_in_write_buffer() < 4096;
    if (role == SocketRole::Connected)
        return !m_accepted_fds_open || m_for_server.bytes_in_write_buffer() < 4096;
    ASSERT_NOT_REACHED();
}