The Pedigree Project  0.1
net-syscalls.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef NET_SYSCALLS_H
21 #define NET_SYSCALLS_H
22 
23 #include "logging.h"
24 
25 #include "pedigree/kernel/process/Mutex.h"
26 #include "pedigree/kernel/utilities/List.h"
27 #include "pedigree/kernel/utilities/String.h"
28 #include "pedigree/kernel/utilities/Tree.h"
29 
30 #include "modules/subsys/posix/UnixFilesystem.h"
31 
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 
35 // Must be after sys/types.h to avoid problems with endian.h
37 
38 extern UnixFilesystem *g_pUnixFilesystem;
39 
40 struct sockaddr;
41 struct pbuf;
42 struct netbuf;
43 struct netconn;
44 
45 class Semaphore;
46 class FileDescriptor;
47 class UnixSocket;
48 class Thread;
49 class Event;
50 
52 {
53  public:
54  NetworkSyscalls(int domain, int type, int protocol);
55  virtual ~NetworkSyscalls();
56 
59  virtual bool create();
60  virtual int connect(const struct sockaddr *address, socklen_t addrlen) = 0;
61 
62  virtual ssize_t sendto_msg(const struct msghdr *msghdr) = 0;
63  virtual ssize_t recvfrom_msg(struct msghdr *msghdr) = 0;
64 
65  virtual ssize_t sendto(
66  const void *buffer, size_t bufferlen, int flags,
67  const struct sockaddr *address, socklen_t addrlen);
68  virtual ssize_t recvfrom(
69  void *buffer, size_t bufferlen, int flags, struct sockaddr *address,
70  socklen_t *addrlen);
71 
72  virtual int listen(int backlog) = 0;
73  virtual int bind(const struct sockaddr *address, socklen_t addrlen) = 0;
74  virtual int accept(struct sockaddr *address, socklen_t *addrlen) = 0;
75 
76  virtual int shutdown(int how);
77 
78  virtual int
79  getpeername(struct sockaddr *address, socklen_t *address_len) = 0;
80  virtual int
81  getsockname(struct sockaddr *address, socklen_t *address_len) = 0;
82 
83  virtual int setsockopt(
84  int level, int optname, const void *optvalue, socklen_t optlen) = 0;
85  virtual int
86  getsockopt(int level, int optname, void *optvalue, socklen_t *optlen) = 0;
87 
88  virtual bool canPoll() const;
89  virtual bool poll(bool &read, bool &write, bool &error, Semaphore *waiter);
90  virtual void unPoll(Semaphore *waiter);
91 
92  virtual bool monitor(Thread *pThread, Event *pEvent);
93  virtual bool unmonitor(Event *pEvent);
94 
95  void associate(FileDescriptor *fd);
96 
97  int getDomain() const
98  {
99  return m_Domain;
100  }
101 
102  int getType() const
103  {
104  return m_Type;
105  }
106 
107  int getProtocol() const
108  {
109  return m_Protocol;
110  }
111 
112  FileDescriptor *getFileDescriptor() const
113  {
114  return m_Fd;
115  }
116 
117  bool isBlocking() const;
118 
119  void setBlocking(bool blocking);
120 
121  protected:
122  int m_Domain;
123  int m_Type;
124  int m_Protocol;
125 
126  bool m_Blocking;
127 
128  FileDescriptor *m_Fd;
129 };
130 
132 {
133  public:
134  LwipSocketSyscalls(int domain, int type, int protocol);
135  virtual ~LwipSocketSyscalls();
136 
138  virtual bool create();
139  virtual int connect(const struct sockaddr *address, socklen_t addrlen);
140 
141  virtual ssize_t sendto_msg(const struct msghdr *msghdr);
142  virtual ssize_t recvfrom_msg(struct msghdr *msghdr);
143 
144  virtual int listen(int backlog);
145  virtual int bind(const struct sockaddr *address, socklen_t addrlen);
146  virtual int accept(struct sockaddr *address, socklen_t *addrlen);
147 
148  virtual int shutdown(int how);
149 
150  virtual int getpeername(struct sockaddr *address, socklen_t *address_len);
151  virtual int getsockname(struct sockaddr *address, socklen_t *address_len);
152 
153  virtual int
154  setsockopt(int level, int optname, const void *optvalue, socklen_t optlen);
155  virtual int
156  getsockopt(int level, int optname, void *optvalue, socklen_t *optlen);
157 
158  virtual bool canPoll() const;
159  virtual bool poll(bool &read, bool &write, bool &error, Semaphore *waiter);
160  virtual void unPoll(Semaphore *waiter);
161 
162  private:
163  static Tree<struct netconn *, LwipSocketSyscalls *> m_SyscallObjects;
164 
165  static void
166  netconnCallback(struct netconn *conn, enum netconn_evt evt, uint16_t len);
167  static void lwipToSyscallError(err_t err);
168 
169  struct netconn *m_Socket;
170 
172  {
173  LwipMetadata();
174 
175  ssize_t recv;
176  ssize_t send;
177  bool error;
178 
179  Mutex lock;
180  List<Semaphore *> semaphores;
181 
182  size_t offset;
183  struct pbuf *pb;
184  struct netbuf *buf;
185  } m_Metadata;
186 };
187 
189 {
190  public:
191  UnixSocketSyscalls(int domain, int type, int protocol);
192  virtual ~UnixSocketSyscalls();
193 
195  virtual bool create();
196  virtual int connect(const struct sockaddr *address, socklen_t addrlen);
197 
198  virtual ssize_t sendto_msg(const struct msghdr *msghdr);
199  virtual ssize_t recvfrom_msg(struct msghdr *msghdr);
200 
201  virtual int listen(int backlog);
202  virtual int bind(const struct sockaddr *address, socklen_t addrlen);
203  virtual int accept(struct sockaddr *address, socklen_t *addrlen);
204 
205  virtual int shutdown(int how);
206 
207  virtual int getpeername(struct sockaddr *address, socklen_t *address_len);
208  virtual int getsockname(struct sockaddr *address, socklen_t *address_len);
209 
210  virtual int
211  setsockopt(int level, int optname, const void *optvalue, socklen_t optlen);
212  virtual int
213  getsockopt(int level, int optname, void *optvalue, socklen_t *optlen);
214 
215  virtual bool canPoll() const;
216  virtual bool poll(bool &read, bool &write, bool &error, Semaphore *waiter);
217  virtual void unPoll(Semaphore *waiter);
218 
219  virtual bool monitor(Thread *pThread, Event *pEvent);
220  virtual bool unmonitor(Event *pEvent);
221 
224  bool pairWith(UnixSocketSyscalls *other);
225 
226  private:
227  // Not safe to copy or assign - we assume we own m_Socket
228  NOT_COPYABLE_OR_ASSIGNABLE(UnixSocketSyscalls);
229 
230  UnixSocket *getRemote() const;
231 
232  UnixSocket::SocketType getSocketType() const;
233 
234  UnixSocket *m_Socket;
235  UnixSocket *m_Remote; // other side of the unix socket
236 
237  String m_LocalPath;
238  String m_RemotePath;
239 };
240 
242 struct netconnMetadata *getNetconnMetadata(struct netconn *conn);
243 
244 int posix_socket(int domain, int type, int protocol);
245 int posix_socketpair(int domain, int type, int protocol, int sv[2]);
246 int posix_connect(int sock, const struct sockaddr *address, socklen_t addrlen);
247 
248 ssize_t posix_send(int sock, const void *buff, size_t bufflen, int flags);
249 ssize_t posix_sendto(
250  int sock, const void *buff, size_t bufflen, int flags,
251  struct sockaddr *address, socklen_t addrlen);
252 ssize_t posix_recv(int sock, void *buff, size_t bufflen, int flags);
253 ssize_t posix_recvfrom(
254  int sock, void *buff, size_t bufflen, int flags, struct sockaddr *address,
255  socklen_t *addrlen);
256 
257 int posix_listen(int sock, int backlog);
258 int posix_bind(int sock, const struct sockaddr *address, socklen_t addrlen);
259 int posix_accept(int sock, struct sockaddr *address, socklen_t *addrlen);
260 
261 int posix_shutdown(int socket, int how);
262 
263 int posix_getpeername(
264  int socket, struct sockaddr *address, socklen_t *address_len);
265 int posix_getsockname(
266  int socket, struct sockaddr *address, socklen_t *address_len);
267 
268 int posix_setsockopt(
269  int sock, int level, int optname, const void *optvalue, socklen_t optlen);
270 int posix_getsockopt(
271  int sock, int level, int optname, void *optvalue, socklen_t *optlen);
272 
273 int posix_sethostname(const char *name, size_t len);
274 
275 ssize_t posix_sendmsg(int sockfd, const struct msghdr *msg, int flags);
276 ssize_t posix_recvmsg(int sockfd, struct msghdr *msg, int flags);
277 
278 #endif
virtual bool create()
Definition: Mutex.h:58
Definition: String.h:49
Definition: pbuf.h:161
s8_t err_t
Definition: err.h:76
Definition: Thread.h:54
Definition: Event.h:48