PatchworkOS
Loading...
Searching...
No Matches
socket_family.c
Go to the documentation of this file.
2
3#include <kernel/fs/sysfs.h>
4#include <kernel/fs/vfs.h>
5#include <kernel/log/log.h>
6#include <kernel/net/net.h>
7#include <kernel/net/socket.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, file->flags);
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 DEREF(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->root, family->name, NULL, family);
88 DEREF(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(&family->factories, &factory->entry);
120 }
121
123 list_push(&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 DEREF(factory->file);
136 free(factory);
137 }
138
139 DEREF(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 DEREF(factory->file);
186 free(factory);
187 }
188
189 DEREF(family->dir);
190 free(family);
191 LOG_INFO("unregistered family %s\n", family->name);
192 return;
193}
194
196{
197 if (family == NULL || outPath == NULL)
198 {
199 errno = EINVAL;
200 return ERR;
201 }
202
204 if (mount == NULL)
205 {
206 return ERR;
207 }
208
209 path_set(outPath, mount, family->dir);
210 DEREF(mount);
211
212 return 0;
213}
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
static mount_t * mount
Definition acpi.c:15
void path_set(path_t *path, mount_t *mount, dentry_t *dentry)
Set a path.
Definition path.c:207
#define BUFFER_READ(buffer, count, offset, src, size)
Helper macros for implementing file operations dealing with simple buffers.
Definition vfs.h:372
#define LOG_WARN(format,...)
Definition log.h:88
#define LOG_INFO(format,...)
Definition log.h:87
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.
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, path_flags_t flags)
Create a new socket.
Definition socket.c:359
mount_t * net_get_mount(void)
Retrieve the mount for the networking subsystem.
Definition net.c:25
#define LOCK_CREATE
Create a lock initializer. @macro LOCK_CREATE.
Definition lock.h:66
#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:140
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
Definition lock.h:97
#define DEREF(ptr)
Decrement reference count.
Definition ref.h:80
#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:65
#define LIST_CREATE(name)
Creates a list initializer.
Definition list.h:176
#define LIST_FOR_EACH_SAFE(elem, temp, list, member)
Safely iterates over a list, allowing for element removal during iteration.
Definition list.h:81
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:317
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:345
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:184
static void list_init(list_t *list)
Initializes a list.
Definition list.h:198
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static dentry_t * file
Definition log_file.c:17
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:9
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
Definition dentry.h:87
void * private
Definition dentry.h:94
File operations structure.
Definition file.h:57
uint64_t(* read)(file_t *file, void *buffer, uint64_t count, uint64_t *offset)
Definition file.h:61
File structure.
Definition file.h:37
void * private
Definition inode.h:66
A doubly linked list.
Definition list.h:51
A simple ticket lock implementation.
Definition lock.h:43
Mount structure.
Definition mount.h:36
dentry_t * root
The root dentry of the mounted filesystem.
Definition mount.h:42
Path structure.
Definition path.h:110
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:81
char id[MAX_NAME]
Definition socket.h:83
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:174
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