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
|
#include <errno.h>
#include <fs/devfs.h>
#include <fs/tmpfs.h>
#include <poll.h>
#include <sched/scheduler.h>
#include <socket.h>
OPEN_UNIX_SOCKET *un_sockets[100] = {0};
int uds_open(const char *path) {
// FIXME: This is super ugly
// Find the socket that path belongs to
SOCKET *s = NULL;
for (int i = 0; i < 100; i++) {
if (!un_sockets[i])
continue;
const char *p = path;
const char *e = p;
for (; *e; e++)
;
for (; e != p && *e != '/'; e--)
;
if (0 == strcmp(e, un_sockets[i]->path)) {
s = un_sockets[i]->s;
break;
}
}
if (!s) {
return -1;
}
// Create a pipe
int fd[2];
dual_pipe(fd);
char c = 'i';
raw_vfs_pwrite(s->ptr_fifo_fd, &c, 1, 0);
raw_vfs_pwrite(s->ptr_socket_fd, &c, 1, 0);
s->incoming_fd = get_current_task()->file_descriptors[fd[1]];
// vfs_close(fd[1]);
return fd[0];
}
int accept(int socket, struct sockaddr *address, socklen_t *address_len) {
(void)address;
(void)address_len;
vfs_inode_t *inode = get_current_task()->file_descriptors[socket]->inode;
SOCKET *s = (SOCKET *)inode->internal_object;
if (NULL == s->incoming_fd) {
// Wait until we have gotten a connection
struct pollfd fds[1];
fds[0].fd = s->fifo_fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
poll(fds, 1, 0);
}
int n = 0;
for (; get_current_task()->file_descriptors[n]; n++)
;
get_current_task()->file_descriptors[n] = s->incoming_fd;
get_current_task()->file_descriptors[n]->reference_count++;
s->incoming_fd = NULL;
for (char c; 0 < vfs_pread(s->fifo_fd, &c, 1, 0);)
;
inode->has_data = 0;
// s->ptr_fifo_fd->inode->has_data = 0;
return n;
}
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
(void)addrlen;
struct sockaddr_un *un = (struct sockaddr_un *)addr;
vfs_fd_t *fd = get_vfs_fd(sockfd);
if (!fd)
return -EBADF;
vfs_inode_t *inode = fd->inode;
if (!inode)
return -EBADF;
SOCKET *s = (SOCKET *)inode->internal_object;
size_t path_len = strlen(un->sun_path);
s->path = kmalloc(path_len + 1);
memcpy(s->path, un->sun_path, path_len);
s->path[path_len] = '\0';
OPEN_UNIX_SOCKET *us;
int i = 0;
for (; i < 100; i++)
if (!un_sockets[i])
break;
us = un_sockets[i] = kmalloc(sizeof(OPEN_UNIX_SOCKET));
us->path = s->path;
us->s = s;
devfs_add_file(us->path, NULL, NULL, NULL, 1, 1, FS_TYPE_UNIX_SOCKET);
return 0;
}
int socket_write(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd) {
(void)buffer;
(void)offset;
fd->inode->has_data = 1;
return len;
}
int socket_read(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd) {
(void)buffer;
(void)offset;
fd->inode->has_data = 0;
return len;
}
void socket_close(vfs_fd_t *fd) { fd->inode->is_open = 0; }
int socket(int domain, int type, int protocol) {
if (AF_UNIX != domain)
return -EINVAL;
SOCKET *new_socket = kmalloc_eternal(sizeof(SOCKET));
vfs_inode_t *inode = vfs_create_inode(
0 /*inode_num*/, FS_TYPE_UNIX_SOCKET, 0 /*has_data*/, 1 /*can_write*/,
1 /*is_open*/, new_socket /*internal_object*/, 0 /*file_size*/,
NULL /*open*/, NULL /*create_file*/, socket_read, socket_write,
socket_close, NULL /*get_vm_object*/);
vfs_fd_t *fd;
int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd);
new_socket->domain = domain;
new_socket->type = type;
new_socket->protocol = protocol;
new_socket->path = NULL;
new_socket->incoming_fd = NULL;
new_socket->fifo_fd = create_fifo();
new_socket->ptr_fifo_fd =
get_current_task()->file_descriptors[new_socket->fifo_fd];
new_socket->ptr_socket_fd = fd;
return n;
}
|