PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
socket_family.c
Go to the documentation of this file.
1#include "socket_family.h"
2
3#include "net.h"
4#include "socket.h"
5#include <kernel/fs/sysfs.h>
6#include <kernel/fs/vfs.h>
7#include <kernel/log/log.h>
8
9#include <errno.h>
10#include <stdlib.h>
11#include <sys/list.h>
12
15
17{
18 socket_t* sock = file->private;
19
20 uint64_t length = strlen(sock->id);
21 return BUFFER_READ(buffer, count, offset, sock->id, length);
22}
23
25{
27
28 socket_t* sock = socket_new(factory->family, factory->type);
29 if (sock == NULL)
30 {
31 return ERR;
32 }
33
34 file->private = sock;
35 return 0;
36}
37
39{
40 socket_t* sock = file->private;
41 if (sock != NULL)
42 {
43 UNREF(sock);
44 }
45}
46
52
53uint64_t socket_family_register(const socket_family_ops_t* ops, const char* name, socket_type_t supportedTypes)
54{
55 if (ops == NULL || name == NULL || supportedTypes == 0)
56 {
57 errno = EINVAL;
58 return ERR;
59 }
60
61 if (ops->init == NULL || ops->deinit == NULL)
62 {
63 errno = EINVAL;
64 return ERR;
65 }
66
67 socket_family_t* family = malloc(sizeof(socket_family_t));
68 if (family == NULL)
69 {
70 return ERR;
71 }
72 list_entry_init(&family->entry);
73 strncpy(family->name, name, MAX_NAME - 1);
74 family->name[MAX_NAME - 1] = '\0';
75 family->ops = ops;
76 family->supportedTypes = supportedTypes;
77 atomic_init(&family->newId, 0);
78 list_init(&family->factories);
79
81 if (mount == NULL)
82 {
83 free(family);
84 return ERR;
85 }
86
87 family->dir = sysfs_dir_new(mount->source, family->name, NULL, family);
88 UNREF(mount);
89 if (family->dir == NULL)
90 {
91 return ERR;
92 }
93
94 for (uint64_t i = 0; i < SOCKET_TYPE_AMOUNT; i++)
95 {
96 socket_type_t type = (1 << i);
97 if (!(type & family->supportedTypes))
98 {
99 continue;
100 }
101
102 socket_factory_t* factory = malloc(sizeof(socket_factory_t));
103 if (factory == NULL)
104 {
105 goto error;
106 }
107 list_entry_init(&factory->entry);
108 factory->type = type;
109 factory->family = family;
110
111 const char* string = socket_type_to_string(type);
112 factory->file = sysfs_file_new(family->dir, string, NULL, &fileOps, factory);
113 if (factory->file == NULL)
114 {
115 free(factory);
116 goto error;
117 }
118
119 list_push_back(&family->factories, &factory->entry);
120 }
121
123 list_push_back(&families, &family->entry);
125
126 LOG_INFO("registered family %s\n", family->name);
127 return 0;
128
129error:;
130
131 socket_factory_t* temp;
132 socket_factory_t* factory;
133 LIST_FOR_EACH_SAFE(factory, temp, &family->factories, entry)
134 {
135 UNREF(factory->file);
136 free(factory);
137 }
138
139 UNREF(family->dir);
140 return ERR;
141}
142
144{
146 socket_family_t* family;
147 LIST_FOR_EACH(family, &families, entry)
148 {
149 if (strcmp(family->name, name) == 0)
150 {
151 return family;
152 }
153 }
154 return NULL;
155}
156
158{
160 socket_family_t* family;
161 LIST_FOR_EACH(family, &families, entry)
162 {
163 if (strcmp(family->name, name) == 0)
164 {
165 list_remove(&families, &family->entry);
166 return family;
167 }
168 }
169 return NULL;
170}
171
172void socket_family_unregister(const char* name)
173{
175 if (family == NULL)
176 {
177 LOG_WARN("socket family %s not found for unregistration\n", name);
178 return;
179 }
180
181 socket_factory_t* temp;
182 socket_factory_t* factory;
183 LIST_FOR_EACH_SAFE(factory, temp, &family->factories, entry)
184 {
185 UNREF(factory->file);
186 free(factory);
187 }
188
189 UNREF(family->dir);
190 free(family);
191 LOG_INFO("unregistered family %s\n", family->name);
192 return;
193}
194
196{
197 socket_family_t* temp;
198 socket_family_t* family;
199 LIST_FOR_EACH_SAFE(family, temp, &families, entry)
200 {
202 }
203}
204
206{
207 if (family == NULL || outPath == NULL)
208 {
209 errno = EINVAL;
210 return ERR;
211 }
212
214 if (mount == NULL)
215 {
216 return ERR;
217 }
218
219 path_set(outPath, mount, family->dir);
220 UNREF(mount);
221
222 return 0;
223}
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
void path_set(path_t *path, mount_t *mount, dentry_t *dentry)
Set a path.
Definition path.c:196
#define LOG_WARN(format,...)
Definition log.h:107
#define LOG_INFO(format,...)
Definition log.h:106
#define LOCK_CREATE()
Create a lock initializer.
Definition lock.h:68
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
Definition lock.h:57
static void lock_release(lock_t *lock)
Releases a lock.
Definition lock.h:146
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
Definition lock.h:103
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:80
#define BUFFER_READ(buffer, count, offset, src, size)
Helper macros for implementing file operations dealing with simple buffers.
Definition vfs.h:194
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
Definition list.h:63
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:343
#define LIST_CREATE(name)
Creates a list initializer.
Definition list.h:174
#define LIST_FOR_EACH_SAFE(elem, temp, list, member)
Safely iterates over a list, allowing for element removal during iteration.
Definition list.h:79
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:315
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:182
static void list_init(list_t *list)
Initializes a list.
Definition list.h:196
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t socket_family_register(const socket_family_ops_t *ops, const char *name, socket_type_t supportedTypes)
Register a socket family.
uint64_t socket_family_get_dir(socket_family_t *family, path_t *outPath)
Get the directory of a socket family.
void socket_family_unregister(const char *name)
Unregister a socket family.
void socket_family_unregister_all(void)
Unregister all socket families.
socket_family_t * socket_family_get(const char *name)
Get a socket family by name.
const char * socket_type_to_string(socket_type_t type)
Convert a socket type to a string.
Definition socket_type.c:3
socket_type_t
Socket type enumeration.
Definition socket_type.h:18
@ SOCKET_TYPE_AMOUNT
Definition socket_type.h:20
socket_t * socket_new(socket_family_t *family, socket_type_t type)
Create a new socket.
Definition socket.c:357
mount_t * net_get_mount(void)
Retrieve the mount for the networking subsystem.
Definition net.c:17
static dentry_t * file
Definition log_file.c:22
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static socket_family_ops_t ops
Definition local.c:505
static atomic_long count
Definition main.c:10
static mount_t * mount
Definition ramfs.c:28
static uint64_t socket_factory_read(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
static socket_family_t * socket_family_get_and_remove(const char *name)
static lock_t lock
static list_t families
static void socket_factory_close(file_t *file)
static uint64_t socket_factory_open(file_t *file)
static file_ops_t fileOps
#define atomic_init(obj, value)
Definition stdatomic.h:75
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC char * strncpy(char *_RESTRICT s1, const char *_RESTRICT s2, size_t n)
Definition strncpy.c:3
_PUBLIC size_t strlen(const char *s)
Definition strlen.c:3
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
inode_t * inode
Will be NULL if the dentry is negative, once positive it will never be NULL.
Definition dentry.h:88
void * private
Definition dentry.h:95
File operations structure.
Definition file.h:54
uint64_t(* read)(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
Definition file.h:58
File structure.
Definition file.h:39
void * private
Definition inode.h:68
A doubly linked list.
Definition list.h:49
A simple ticket lock implementation.
Definition lock.h:43
Mount structure.
Definition mount.h:44
dentry_t * source
The dentry to appear at target once mounted, usually the root dentry of the mounted filesystem.
Definition mount.h:48
Path structure.
Definition path.h:125
Socket Factory structure.
list_entry_t entry
socket_family_t * family
socket_type_t type
Socket Family operations structure.
uint64_t(* init)(socket_t *sock)
void(* deinit)(socket_t *sock)
Socket Family structure.
list_entry_t entry
const socket_family_ops_t * ops
atomic_uint64_t newId
char name[MAX_NAME]
socket_type_t supportedTypes
Socket structure.
Definition socket.h:82
char id[MAX_NAME]
Definition socket.h:84
dentry_t * sysfs_dir_new(dentry_t *parent, const char *name, const inode_ops_t *inodeOps, void *private)
Create a new directory inside a mounted SysFS instance.
Definition sysfs.c:177
dentry_t * sysfs_file_new(dentry_t *parent, const char *name, const inode_ops_t *inodeOps, const file_ops_t *fileOps, void *private)
Create a new file inside a mounted SysFS instance.
Definition sysfs.c:216