xref: /f-stack/lib/ff_api.h (revision 144c6bcd)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #ifndef _FSTACK_API_H
28 #define _FSTACK_API_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 #include <sys/socket.h>
34 #include <sys/select.h>
35 #include <sys/poll.h>
36 #include <netinet/in.h>
37 
38 #include "ff_event.h"
39 #include "ff_errno.h"
40 
41 struct linux_sockaddr {
42     short sa_family;
43     char sa_data[14];
44 };
45 
46 typedef int (*loop_func_t)(void *arg);
47 
48 int ff_init(const char *conf, int argc, char * const argv[]);
49 
50 void ff_run(loop_func_t loop, void *arg);
51 
52 int ff_fcntl(int fd, int cmd, ...);
53 
54 int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
55     const void *newp, size_t newlen);
56 
57 int ff_ioctl(int fd, unsigned long request, ...);
58 
59 int ff_socket(int domain, int type, int protocol);
60 
61 int ff_setsockopt(int s, int level, int optname, const void *optval,
62     socklen_t optlen);
63 
64 int ff_getsockopt(int s, int level, int optname, void *optval,
65     socklen_t *optlen);
66 
67 int ff_socketpair(int domain, int type, int protocol, int *sv);
68 
69 int ff_listen(int s, int backlog);
70 int ff_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen);
71 int ff_accept(int s, struct linux_sockaddr *addr, socklen_t *addrlen);
72 int ff_connect(int s, const struct linux_sockaddr *name, socklen_t namelen);
73 int ff_close(int fd);
74 int ff_shutdown(int s, int how);
75 
76 int ff_getpeername(int s, struct linux_sockaddr *name,
77     socklen_t *namelen);
78 int ff_getsockname(int s, struct linux_sockaddr *name,
79     socklen_t *namelen);
80 
81 ssize_t ff_read(int d, void *buf, size_t nbytes);
82 ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt);
83 
84 ssize_t ff_write(int fd, const void *buf, size_t nbytes);
85 ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt);
86 
87 ssize_t ff_send(int s, const void *buf, size_t len, int flags);
88 ssize_t ff_sendto(int s, const void *buf, size_t len, int flags,
89     const struct linux_sockaddr *to, socklen_t tolen);
90 ssize_t ff_sendmsg(int s, const struct msghdr *msg, int flags);
91 
92 ssize_t ff_recv(int s, void *buf, size_t len, int flags);
93 ssize_t ff_recvfrom(int s, void *buf, size_t len, int flags,
94     struct linux_sockaddr *from, socklen_t *fromlen);
95 ssize_t ff_recvmsg(int s, struct msghdr *msg, int flags);
96 
97 int ff_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
98     struct timeval *timeout);
99 
100 int ff_poll(struct pollfd fds[], nfds_t nfds, int timeout);
101 
102 int ff_kqueue(void);
103 int ff_kevent(int kq, const struct kevent *changelist, int nchanges,
104     struct kevent *eventlist, int nevents, const struct timespec *timeout);
105 
106 /* route api begin */
107 enum FF_ROUTE_CTL {
108     FF_ROUTE_ADD,
109     FF_ROUTE_DEL,
110     FF_ROUTE_CHANGE,
111 };
112 
113 enum FF_ROUTE_FLAG {
114     FF_RTF_HOST,
115     FF_RTF_GATEWAY,
116 };
117 
118 /*
119  * On success, 0 is returned.
120  * On error, -1 is returned, and errno is set appropriately.
121  */
122 int ff_route_ctl(enum FF_ROUTE_CTL req, enum FF_ROUTE_FLAG flag,
123     struct linux_sockaddr *dst, struct linux_sockaddr *gw,
124     struct linux_sockaddr *netmask);
125 
126 /*
127  * This is used in handling ff_msg.
128  * The data is a pointer to struct rt_msghdr.
129  */
130 int ff_rtioctl(int fib, void *data, unsigned *plen, unsigned maxlen);
131 
132 /* route api end */
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 #endif
138 
139