mavtables  0.2.1
MAVLink router and firewall.
UnixSyscalls.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <fcntl.h> // open, fnctl
19 #include <netinet/in.h> // sockaddr_in
20 #include <sys/ioctl.h> // ioctl
21 #include <sys/poll.h> // poll
22 #include <sys/socket.h> // socket, bind, sendto, recvfrom
23 #include <sys/stat.h> // fstat
24 #include <sys/types.h> // socklen_t type on old BSD systems
25 #include <termios.h> // terminal control
26 #include <unistd.h> // read, write, close
27 
28 #include "UnixSyscalls.hpp"
29 
30 
31 /** Bind a name to a socket.
32  *
33  * See [man 2 bind](http://man7.org/linux/man-pages/man2/bind.2.html) for
34  * documentation.
35  *
36  * \param sockfd Socket file descriptor.
37  * \param addr Address to assign to socket.
38  * \param addrlen Size of address structure in bytes.
39  */
41  int sockfd, const struct sockaddr *addr, socklen_t addrlen)
42 {
43  return ::bind(sockfd, addr, addrlen);
44 }
45 
46 
47 /** Close a file descriptor.
48  *
49  * See [man 2 close](http://man7.org/linux/man-pages/man2/close.2.html) for
50  * documentation.
51  *
52  * \param fd The file descriptor to close.
53  */
55 {
56  return ::close(fd);
57 }
58 
59 
60 /** Control device.
61  *
62  * See [man 2 ioctl](http://man7.org/linux/man-pages/man2/ioctl.2.html) for
63  * documentation.
64  *
65  * \param fd The file descriptor to control.
66  * \param request Request code, defined in <sys/ioctl.h>
67  * \param argp Pointer to input/output, dependent on request code.
68  */
69 int UnixSyscalls::ioctl(int fd, unsigned long request, void *argp)
70 {
71  return ::ioctl(fd, request, argp);
72 }
73 
74 
75 /** Open and possibly create a file.
76  *
77  * See [man 2 open](http://man7.org/linux/man-pages/man2/open.2.html) for
78  * documentation.
79  */
80 int UnixSyscalls::open(const char *pathname, int flags)
81 {
82  return ::open(pathname, flags);
83 }
84 
85 
86 /** Wait for some event on a file descriptor.
87  *
88  * See [man 2 poll](http://man7.org/linux/man-pages/man2/poll.2.html) for
89  * documentation.
90  *
91  * \param fds File descriptor event structures.
92  * \param nfds Number of file descriptor event structures.
93  * \param timeout The timeout in milliseconds.
94  */
95 int UnixSyscalls::poll(struct pollfd *fds, nfds_t nfds, int timeout)
96 {
97  return ::poll(fds, nfds, timeout);
98 }
99 
100 
101 /** Read from a file descriptor.
102  *
103  * See [man 2 read](http://man7.org/linux/man-pages/man2/read.2.html) for
104  * documentation.
105  */
106 ssize_t UnixSyscalls::read(int fd, void *buf, size_t count)
107 {
108  return ::read(fd, buf, count);
109 }
110 
111 
112 /** Receive a message from a socket.
113  *
114  * See [man 2 recvfrom](http://man7.org/linux/man-pages/man2/recv.2.html) for
115  * documentation.
116  *
117  * \param sockfd Socket file descriptor to receive data on.
118  * \param buf The buffer to write the data into.
119  * \param len The length of the buffer.
120  * \param flags Option flags.
121  * \param src_addr Source address buffer.
122  * \param addrlen Before call, length of source address buffer. After call,
123  * actual length of address data.
124  * \returns The number of bytes written to \p or -1 if an error occurred.
125  */
127  int sockfd, void *buf, size_t len, int flags,
128  struct sockaddr *src_addr, socklen_t *addrlen)
129 {
130  return ::recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
131 }
132 
133 
134 /** Send a message on a socket.
135  *
136  * See [man 2 sendto](http://man7.org/linux/man-pages/man2/send.2.html) for
137  * documentation.
138  */
140  int sockfd, const void *buf, size_t len, int flags,
141  const struct sockaddr *dest_addr, socklen_t addrlen)
142 {
143  return ::sendto(sockfd, buf, len, flags, dest_addr, addrlen);
144 }
145 
146 
147 /** Create an endpoint for communication.
148  *
149  * See [man 2 socket](http://man7.org/linux/man-pages/man2/socket.2.html) for
150  * documentation.
151  *
152  * \param domain Protocol family to use for communication.
153  * \param type Socket type.
154  * \param protocol Protocol to use for socket.
155  */
156 int UnixSyscalls::socket(int domain, int type, int protocol)
157 {
158  return ::socket(domain, type, protocol);
159 }
160 
161 
162 /** Get serial port parameters associated with the given file descriptor.
163  *
164  * See [man 2 termios](http://man7.org/linux/man-pages/man3/termios.3.html) for
165  * documentation.
166  */
167 int UnixSyscalls::tcgetattr(int fd, struct termios *termios_p)
168 {
169  return ::tcgetattr(fd, termios_p);
170 }
171 
172 
173 /** Set serial port parameters for given file descriptor.
174  *
175  * See [man 2 termios](http://man7.org/linux/man-pages/man3/termios.3.html) for
176  * documentation.
177  */
179  int fd, int optional_actions, const struct termios *termios_p)
180 {
181  return ::tcsetattr(fd, optional_actions, termios_p);
182 }
183 
184 
185 /** Write to a file descriptor.
186  *
187  * See [man 2 write](http://man7.org/linux/man-pages/man2/write.2.html) for
188  * documentation.
189  */
190 ssize_t UnixSyscalls::write(int fd, const void *buf, size_t count)
191 {
192  return ::write(fd, buf, count);
193 }
TEST_VIRTUAL ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
TEST_VIRTUAL ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
TEST_VIRTUAL ssize_t write(int fd, const void *buf, size_t count)
TEST_VIRTUAL int open(const char *pathname, int flags)
TEST_VIRTUAL int socket(int domain, int type, int protocol)
TEST_VIRTUAL int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
TEST_VIRTUAL int close(int fd)
TEST_VIRTUAL int poll(struct pollfd *fds, nfds_t nfds, int timeout)
TEST_VIRTUAL ssize_t read(int fd, void *buf, size_t count)
TEST_VIRTUAL int tcgetattr(int fd, struct termios *termios_p)
TEST_VIRTUAL int ioctl(int fd, unsigned long request, void *argp)
TEST_VIRTUAL int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)