115 lines
2.0 KiB
C
115 lines
2.0 KiB
C
|
#pragma once
|
||
|
#include "fs/VFS.h"
|
||
|
#include <bits/socket.h>
|
||
|
|
||
|
class Socket : public VFS::FileInode
|
||
|
{
|
||
|
public:
|
||
|
Socket() = default;
|
||
|
|
||
|
VFS::InodeType type() const override
|
||
|
{
|
||
|
return VFS::InodeType::Socket;
|
||
|
}
|
||
|
|
||
|
void set_fs(VFS::FileSystem* fs)
|
||
|
{
|
||
|
m_fs = fs;
|
||
|
}
|
||
|
|
||
|
void set_inode_number(usize inum)
|
||
|
{
|
||
|
m_inode_number = inum;
|
||
|
}
|
||
|
|
||
|
VFS::FileSystem* fs() const override
|
||
|
{
|
||
|
return m_fs;
|
||
|
}
|
||
|
|
||
|
usize inode_number() const override
|
||
|
{
|
||
|
return m_inode_number;
|
||
|
}
|
||
|
|
||
|
Result<usize> read(u8* buf, usize, usize length) const override
|
||
|
{
|
||
|
return recv(buf, length, 0);
|
||
|
}
|
||
|
|
||
|
Result<usize> write(const u8* buf, usize, usize length) override
|
||
|
{
|
||
|
return send(buf, length, 0);
|
||
|
}
|
||
|
|
||
|
virtual Result<usize> send(const u8*, usize, int) = 0;
|
||
|
|
||
|
virtual Result<usize> recv(u8*, usize, int) const = 0;
|
||
|
|
||
|
virtual Result<void> bind(SharedPtr<Socket>, struct sockaddr*, socklen_t) = 0;
|
||
|
virtual Result<void> connect(struct sockaddr*, socklen_t) = 0;
|
||
|
|
||
|
Result<void> truncate(usize) override
|
||
|
{
|
||
|
return err(EINVAL);
|
||
|
}
|
||
|
|
||
|
usize size() const override
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
mode_t mode() const override
|
||
|
{
|
||
|
return m_mode;
|
||
|
}
|
||
|
|
||
|
u32 uid() const override
|
||
|
{
|
||
|
return m_uid;
|
||
|
}
|
||
|
|
||
|
u32 gid() const override
|
||
|
{
|
||
|
return m_gid;
|
||
|
}
|
||
|
|
||
|
nlink_t nlinks() const override
|
||
|
{
|
||
|
return (nlink_t)m_nlinks;
|
||
|
}
|
||
|
|
||
|
Result<void> chmod(mode_t mode) override
|
||
|
{
|
||
|
m_mode = mode;
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
Result<void> chown(u32 uid, u32 gid) override
|
||
|
{
|
||
|
m_uid = uid;
|
||
|
m_gid = gid;
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
void did_link() override
|
||
|
{
|
||
|
m_nlinks++;
|
||
|
}
|
||
|
|
||
|
void did_unlink() override
|
||
|
{
|
||
|
m_nlinks--;
|
||
|
}
|
||
|
|
||
|
virtual ~Socket() = default;
|
||
|
|
||
|
protected:
|
||
|
VFS::FileSystem* m_fs;
|
||
|
usize m_inode_number { 0 };
|
||
|
mode_t m_mode;
|
||
|
u32 m_uid { 0 };
|
||
|
u32 m_gid { 0 };
|
||
|
u32 m_nlinks { 0 };
|
||
|
};
|