summaryrefslogtreecommitdiff
path: root/components/net/af_unix/src/af_unix_namespace.c
blob: 386dfdc05f947b369854cd25e11f99417caeb3cd (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (c) 2006-2026, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stddef.h>
#include <string.h>

#include "af_unix_internal.h"

struct af_unix_namespace_entry
{
    rt_list_t node;
    char path[AF_UNIX_PATH_MAX];
    struct af_unix_socket *sock;
};

static rt_list_t af_unix_namespace;

static struct af_unix_namespace_entry *af_unix_namespace_find_locked(
    const char *path)
{
    struct af_unix_namespace_entry *entry;

    rt_list_for_each_entry(entry, &af_unix_namespace, node)
    {
        if (rt_strcmp(entry->path, path) == 0)
        {
            return entry;
        }
    }
    return RT_NULL;
}

int af_unix_address_parse(const struct sockaddr *address, socklen_t length,
                          char path[AF_UNIX_PATH_MAX])
{
    int index;
    int path_length;
    char input_path[AF_UNIX_PATH_MAX];
    char *normalized;
    const struct sockaddr_un *unix_address;
    const int path_offset = (int)offsetof(struct sockaddr_un, sun_path);

    if (address == RT_NULL || length <= (socklen_t)path_offset ||
        length > sizeof(struct sockaddr_un))
    {
        return af_unix_error(EINVAL);
    }

    unix_address = (const struct sockaddr_un *)address;
    if (unix_address->sa_family != AF_UNIX)
    {
        return af_unix_error(EAFNOSUPPORT);
    }

    path_length = (int)length - path_offset;
    if (path_length > AF_UNIX_PATH_MAX)
    {
        path_length = AF_UNIX_PATH_MAX;
    }
    for (index = 0; index < path_length; index++)
    {
        if (unix_address->sun_path[index] == '\0')
        {
            break;
        }
    }
    if (index == 0)
    {
        return af_unix_error(EINVAL);
    }
    if (index == path_length || index >= AF_UNIX_PATH_MAX)
    {
        return af_unix_error(ENAMETOOLONG);
    }

    rt_memcpy(input_path, unix_address->sun_path, index);
    input_path[index] = '\0';
    normalized = dfs_normalize_path(RT_NULL, input_path);
    if (normalized == RT_NULL)
    {
        return af_unix_error(ENOMEM);
    }
    if (rt_strlen(normalized) >= AF_UNIX_PATH_MAX)
    {
        rt_free(normalized);
        return af_unix_error(ENAMETOOLONG);
    }
    rt_strncpy(path, normalized, AF_UNIX_PATH_MAX);
    path[AF_UNIX_PATH_MAX - 1] = '\0';
    rt_free(normalized);
    return 0;
}

void af_unix_address_set(struct sockaddr_un *address, socklen_t *length,
                         const char *path)
{
    size_t path_length = 0;

    rt_memset(address, 0, sizeof(*address));
    address->sa_family = AF_UNIX;
    if (path != RT_NULL)
    {
        path_length = rt_strlen(path);
        if (path_length >= sizeof(address->sun_path))
        {
            path_length = sizeof(address->sun_path) - 1;
        }
        rt_memcpy(address->sun_path, path, path_length);
    }
    *length = (socklen_t)(offsetof(struct sockaddr_un, sun_path) +
                          path_length + (path != RT_NULL ? 1 : 0));
}

int af_unix_address_copy(struct sockaddr *address, socklen_t *length,
                         const struct sockaddr_un *source,
                         socklen_t source_length)
{
    socklen_t copy_length;

    if (address == RT_NULL)
    {
        return 0;
    }
    if (length == RT_NULL)
    {
        return af_unix_error(EINVAL);
    }

    copy_length = *length;
    if (copy_length > source_length)
    {
        copy_length = source_length;
    }
    if (copy_length > 0)
    {
        rt_memcpy(address, source, copy_length);
    }
    *length = source_length;
    return 0;
}

int af_unix_namespace_bind(struct af_unix_socket *sock,
                           const struct sockaddr *address, socklen_t length)
{
    int result;
    char path[AF_UNIX_PATH_MAX];
    struct af_unix_namespace_entry *entry;
    struct af_unix_namespace_entry *new_entry;

    result = af_unix_address_parse(address, length, path);
    if (result < 0)
    {
        return result;
    }

    af_unix_lock();
    if (sock->closed)
    {
        af_unix_unlock();
        return af_unix_error(EBADF);
    }
    if (sock->bound)
    {
        af_unix_unlock();
        return af_unix_error(EINVAL);
    }
    af_unix_unlock();

    result = dfs_file_mknod(path, FT_SOCKET,
                            S_IRWXU | S_IRWXG | S_IRWXO);
    if (result < 0)
    {
        return af_unix_error(result == -EEXIST ? EADDRINUSE : -result);
    }

    new_entry = (struct af_unix_namespace_entry *)rt_calloc(1,
                                                            sizeof(*new_entry));
    if (new_entry == RT_NULL)
    {
        (void)dfs_file_unlink(path);
        return af_unix_error(ENOMEM);
    }
    rt_strncpy(new_entry->path, path, sizeof(new_entry->path));
    new_entry->path[sizeof(new_entry->path) - 1] = '\0';
    rt_list_init(&new_entry->node);

    af_unix_lock();
    if (sock->closed || sock->bound)
    {
        af_unix_unlock();
        rt_free(new_entry);
        (void)dfs_file_unlink(path);
        return af_unix_error(sock->closed ? EBADF : EINVAL);
    }

    entry = af_unix_namespace_find_locked(path);
    if (entry == RT_NULL)
    {
        entry = new_entry;
        new_entry = RT_NULL;
        rt_list_insert_before(&af_unix_namespace, &entry->node);
    }
    entry->sock = sock;
    sock->namespace_entry = entry;
    sock->bound = 1;
    af_unix_address_set(&sock->local_address, &sock->local_length, path);
    af_unix_unlock();

    if (new_entry != RT_NULL)
    {
        rt_free(new_entry);
    }
    return 0;
}

struct af_unix_socket *af_unix_namespace_lookup(
    const struct sockaddr *address, socklen_t length, int type,
    struct sockaddr_un *normalized_address, socklen_t *normalized_length)
{
    int result;
    char path[AF_UNIX_PATH_MAX];
    struct stat file_stat;
    struct af_unix_socket *sock = RT_NULL;
    struct af_unix_namespace_entry *entry;

    result = af_unix_address_parse(address, length, path);
    if (result < 0)
    {
        return RT_NULL;
    }
    result = dfs_file_stat(path, &file_stat);
    if (result < 0)
    {
        af_unix_error(ENOENT);
        return RT_NULL;
    }
    if (!S_ISSOCK(file_stat.st_mode))
    {
        af_unix_error(EPROTOTYPE);
        return RT_NULL;
    }

    af_unix_lock();
    entry = af_unix_namespace_find_locked(path);
    if (entry != RT_NULL && entry->sock != RT_NULL && !entry->sock->closed)
    {
        if (entry->sock->type == type)
        {
            sock = entry->sock;
            af_unix_socket_ref_locked(sock);
        }
        else
        {
            af_unix_error(EPROTOTYPE);
        }
    }
    else
    {
        af_unix_error(ECONNREFUSED);
    }
    af_unix_unlock();

    if (sock != RT_NULL && normalized_address != RT_NULL &&
        normalized_length != RT_NULL)
    {
        af_unix_address_set(normalized_address, normalized_length, path);
    }
    return sock;
}

void af_unix_namespace_detach_locked(struct af_unix_socket *sock)
{
    struct af_unix_namespace_entry *entry;

    entry = (struct af_unix_namespace_entry *)sock->namespace_entry;
    if (entry != RT_NULL && entry->sock == sock)
    {
        entry->sock = RT_NULL;
        rt_list_remove(&entry->node);
        rt_free(entry);
    }
    sock->namespace_entry = RT_NULL;
}

int af_unix_namespace_init(void)
{
    rt_list_init(&af_unix_namespace);
    return RT_EOK;
}