summaryrefslogtreecommitdiff
path: root/kernel/src/arch/x86_64/syscalls/sockets.cpp
blob: fe2330977c80610cca6116707d98d2c45fb3eeab (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

#include <arch/x86_64/syscalls/syscalls.hpp>
#include <arch/x86_64/syscalls/sockets.hpp>

#include <generic/vfs/vfs.hpp>

#include <generic/vfs/fd.hpp>

#include <generic/locks/spinlock.hpp>

#include <etc/errno.hpp>

#include <cstdint>

locks::spinlock socket_spinlock;
socket_node_t* head = 0;

char is_socket_init = 0;

socket_node_t* find_node(struct sockaddr_un* path) {
    socket_node_t* current = head;
    while(current) {
        if(!strcmp(current->path,path->sun_path)) {
            return current;
        }
        current = current->next;
    }
    return 0;
}

socket_node_t* find_node_str(char* path) {
    socket_node_t* current = head;
    while(current) {
        if(!strcmp(current->path,path)) {
            return current;
        }
        current = current->next;
    }
    return 0;
}

char sockets::is_exists(char* path) {

    if(!is_socket_init)
        return 0;

    socket_node_t* node = find_node_str(path);
    if(!node)
        return 0;
    return 1;
}

int sockets::bind(userspace_fd_t* fd, struct sockaddr_un* path) {
    
    if(!fd || !path)
        return EINVAL;

    if(path->sun_family != AF_UNIX)
        return ENOSYS;

    memset(fd->path,0,2048);
    memcpy(fd->path,path->sun_path,strlen(path->sun_path));

    vfs::stat_t stat;
    if(vfs::vfs::stat(fd,&stat) == 0) /* Check is there vfs object with some name */
        return EEXIST; 

    socket_spinlock.lock();

    if(find_node(path)) { socket_spinlock.unlock();
        return EEXIST; }

    socket_node_t* new_node = new socket_node_t;
    memset(new_node->path,0,128);
    memcpy(new_node->path,path->sun_path,108);
    new_node->is_used = 1;
    new_node->next = head;
    head = new_node;
    socket_spinlock.unlock();

    return 0;

}

int sockets::connect(userspace_fd_t* fd, struct sockaddr_un* path) {

    if(!fd || !path)
        return EINVAL;

    if(path->sun_family != AF_UNIX)
        return ENOSYS;

    socket_spinlock.lock();

    socket_node_t* node = find_node(path);
    if(!node) { socket_spinlock.unlock();
        return ENOENT; }

    socket_pending_obj_t* pending = new socket_pending_obj_t;
    pending->son = fd;
    pending->next = node->pending_list;
    pending->is_accepted.unlock();
    node->pending_list = pending;

    while(!pending->is_accepted.test()) { socket_spinlock.unlock(); asm volatile("pause"); socket_spinlock.lock(); }

    socket_spinlock.unlock();
    return 0;
}

int sockets::accept(userspace_fd_t* fd, struct sockaddr_un* path) {
    if(!fd)
        return -EINVAL;

    arch::x86_64::process_t* proc = CURRENT_PROC;

    socket_spinlock.lock();

    socket_node_t* node = find_node_str(fd->path);
    if(!node) { socket_spinlock.unlock();
        return -ENOENT; }

    socket_pending_obj_t* pending_connections = node->pending_list;
    while(1) {
        while(pending_connections) {
            if(!pending_connections->is_accepted.test()) {
                int new_fd = vfs::fdmanager::create(proc);

                userspace_fd_t* new_fd_s = vfs::fdmanager::search(proc,new_fd);
                memset(new_fd_s->path,0,2048);
                memcpy(new_fd_s->path,fd->path,strlen(fd->path));

                new_fd_s->offset = 0;
                new_fd_s->queue = 0;
                new_fd_s->pipe = 0;
                new_fd_s->pipe_side = 0;
                new_fd_s->cycle = 0;
                new_fd_s->is_a_tty = 0;

                new_fd_s->state = USERSPACE_FD_STATE_SOCKET;
                new_fd_s->other_state = USERSPACE_FD_OTHERSTATE_MASTER; // master - writes to read pipe and reads from write pipe 

                pending_connections->son->other_state = USERSPACE_FD_OTHERSTATE_SLAVE;

                new_fd_s->read_socket_pipe = new vfs::pipe(0);
                new_fd_s->write_socket_pipe = new vfs::pipe(0);

                new_fd_s->read_socket_pipe->create(PIPE_SIDE_READ);
                new_fd_s->read_socket_pipe->create(PIPE_SIDE_WRITE);    
                new_fd_s->write_socket_pipe->create(PIPE_SIDE_READ);   
                new_fd_s->write_socket_pipe->create(PIPE_SIDE_WRITE);
                
                pending_connections->son->read_socket_pipe = new_fd_s->read_socket_pipe;
                pending_connections->son->write_socket_pipe = new_fd_s->write_socket_pipe;

                if(path)
                    memcpy(path->sun_path,node->path,strlen(node->path));

                pending_connections->is_accepted.test_and_set();
                
                socket_spinlock.unlock();
                return new_fd_s->index;

            }
            pending_connections = pending_connections->next;
        }
        socket_spinlock.unlock();
        asm volatile("pause");
        socket_spinlock.lock();
        pending_connections = node->pending_list;
    }

    return -EFAULT;
}

void sockets::init() {
    is_socket_init = 1;
    socket_spinlock.unlock();
}

syscall_ret_t sys_connect(int fd, struct sockaddr_un* path, int len) {

    arch::x86_64::process_t* proc = CURRENT_PROC;
    userspace_fd_t* fd_s = vfs::fdmanager::search(proc,fd);
    
    if(!fd_s)
        return {0,EBADF,0};

    if(!path)
        return {0,EINVAL,0};

    struct sockaddr_un spath;

    memset(&spath,0,sizeof(spath));
    copy_in_userspace(proc,&spath,path,len > sizeof(spath) ? sizeof(spath) : len);

    SYSCALL_ENABLE_PREEMPT();
    int status = sockets::connect(fd_s,&spath);
    SYSCALL_DISABLE_PREEMPT();

    return {0,status,0};
}

syscall_ret_t sys_bind(int fd, struct sockaddr_un* path, int len) {

    arch::x86_64::process_t* proc = CURRENT_PROC;
    userspace_fd_t* fd_s = vfs::fdmanager::search(proc,fd);
    
    if(!fd_s)
        return {0,EBADF,0};

    if(!path)
        return {0,EINVAL,0};

    struct sockaddr_un spath;

    memset(&spath,0,sizeof(spath));
    copy_in_userspace(proc,&spath,path,len > sizeof(spath) ? sizeof(spath) : len);

    int status = sockets::bind(fd_s,&spath);

    return {0,status,0};
}

syscall_ret_t sys_accept(int fd, struct sockaddr_un* path, int len) {

    arch::x86_64::process_t* proc = CURRENT_PROC;
    userspace_fd_t* fd_s = vfs::fdmanager::search(proc,fd);
    
    if(!fd_s)
        return {1,EBADF,0};

    struct sockaddr_un spath;

    if(path) {
        memset(&spath,0,sizeof(spath));
        copy_in_userspace(proc,&spath,path,len > sizeof(spath) ? sizeof(spath) : len);
    }

    SYSCALL_ENABLE_PREEMPT();
    int status = sockets::accept(fd_s,path != 0 ? &spath : 0);
    SYSCALL_DISABLE_PREEMPT();

    if(path)
        copy_in_userspace(proc,path,&spath,len > sizeof(spath) ? sizeof(spath) : len);

    return {1,status < 0 ? +status : 0,status};
}

syscall_ret_t sys_socket(int family, int type, int protocol) {
    arch::x86_64::process_t* proc = CURRENT_PROC;
    int new_fd = vfs::fdmanager::create(proc);

    userspace_fd_t* new_fd_s = vfs::fdmanager::search(proc,new_fd);
    memset(new_fd_s->path,0,2048);

    new_fd_s->offset = 0;
    new_fd_s->queue = 0;
    new_fd_s->pipe = 0;
    new_fd_s->pipe_side = 0;
    new_fd_s->cycle = 0;
    new_fd_s->is_a_tty = 0;

    new_fd_s->state = USERSPACE_FD_STATE_SOCKET;

    Log::SerialDisplay(LEVEL_MESSAGE_INFO,"sys_socket %d\n",new_fd);

    return {1,0,new_fd};
}

syscall_ret_t sys_listen(int fd, int backlog) {
    return {0,0,0};
}