1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
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
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup socket
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Socket application program interface (API).
|
---|
35 | * This is a part of the network application library.
|
---|
36 | * Based on the BSD socket interface.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #ifndef __NET_SOCKET_H__
|
---|
40 | #define __NET_SOCKET_H__
|
---|
41 |
|
---|
42 | #include "byteorder.h"
|
---|
43 | #include "in.h"
|
---|
44 | #include "in6.h"
|
---|
45 | #include "inet.h"
|
---|
46 |
|
---|
47 | #include "socket_codes.h"
|
---|
48 | #include "socket_errno.h"
|
---|
49 |
|
---|
50 | /** @name Socket application programming interface
|
---|
51 | */
|
---|
52 | /*@{*/
|
---|
53 |
|
---|
54 | /** Creates a new socket.
|
---|
55 | * @param[in] domain The socket protocol family.
|
---|
56 | * @param[in] type Socket type.
|
---|
57 | * @param[in] protocol Socket protocol.
|
---|
58 | * @returns The socket identifier on success.
|
---|
59 | * @returns EPFNOTSUPPORT if the protocol family is not supported.
|
---|
60 | * @returns ESOCKNOTSUPPORT if the socket type is not supported.
|
---|
61 | * @returns EPROTONOSUPPORT if the protocol is not supported.
|
---|
62 | * @returns ENOMEM if there is not enough memory left.
|
---|
63 | * @returns ELIMIT if there was not a free socket identifier found this time.
|
---|
64 | * @returns Other error codes as defined for the NET_SOCKET message.
|
---|
65 | * @returns Other error codes as defined for the bind_service_timeout() function.
|
---|
66 | */
|
---|
67 | int socket(int domain, int type, int protocol);
|
---|
68 |
|
---|
69 | /** Binds the socket to a port address.
|
---|
70 | * @param[in] socket_id Socket identifier.
|
---|
71 | * @param[in] my_addr The port address.
|
---|
72 | * @param[in] addrlen The address length.
|
---|
73 | * @returns EOK on success.
|
---|
74 | * @returns ENOTSOCK if the socket is not found.
|
---|
75 | * @returns EBADMEM if the my_addr parameter is NULL.
|
---|
76 | * @returns NO_DATA if the addlen parameter is zero (0).
|
---|
77 | * @returns Other error codes as defined for the NET_SOCKET_BIND message.
|
---|
78 | */
|
---|
79 | int bind(int socket_id, const struct sockaddr * my_addr, socklen_t addrlen);
|
---|
80 |
|
---|
81 | /** Sets the number of connections waiting to be accepted.
|
---|
82 | * @param[in] socket_id Socket identifier.
|
---|
83 | * @param[in] backlog The maximum number of waiting sockets to be accepted.
|
---|
84 | * @returns EOK on success.
|
---|
85 | * @returns EINVAL if the backlog parameter is not positive (<=0).
|
---|
86 | * @returns ENOTSOCK if the socket is not found.
|
---|
87 | * @returns Other error codes as defined for the NET_SOCKET_LISTEN message.
|
---|
88 | */
|
---|
89 | int listen(int socket_id, int backlog);
|
---|
90 |
|
---|
91 | /** Accepts waiting socket.
|
---|
92 | * Blocks until such a socket exists.
|
---|
93 | * @param[in] socket_id Socket identifier.
|
---|
94 | * @param[out] cliaddr The remote client address.
|
---|
95 | * @param[in] addrlen The address length.
|
---|
96 | * @returns EOK on success.
|
---|
97 | * @returns EBADMEM if the cliaddr or addrlen parameter is NULL.
|
---|
98 | * @returns EINVAL if the backlog parameter is not positive (<=0).
|
---|
99 | * @returns ENOTSOCK if the socket is not found.
|
---|
100 | * @returns Other error codes as defined for the NET_SOCKET_ACCEPT message.
|
---|
101 | */
|
---|
102 | int accept(int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen);
|
---|
103 |
|
---|
104 | /** Connects socket to the remote server.
|
---|
105 | * @param[in] socket_id Socket identifier.
|
---|
106 | * @param[in] serv_addr The remote server address.
|
---|
107 | * @param[in] addrlen The address length.
|
---|
108 | * @returns EOK on success.
|
---|
109 | * @returns EBADMEM if the serv_addr parameter is NULL.
|
---|
110 | * @returns NO_DATA if the addlen parameter is zero (0).
|
---|
111 | * @returns ENOTSOCK if the socket is not found.
|
---|
112 | * @returns Other error codes as defined for the NET_SOCKET_CONNECT message.
|
---|
113 | */
|
---|
114 | int connect(int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen);
|
---|
115 |
|
---|
116 | /** Closes the socket.
|
---|
117 | * @param[in] socket_id Socket identifier.
|
---|
118 | * @returns EOK on success.
|
---|
119 | * @returns ENOTSOCK if the socket is not found.
|
---|
120 | * @returns EINPROGRESS if there is another blocking function in progress.
|
---|
121 | * @returns Other error codes as defined for the NET_SOCKET_CLOSE message.
|
---|
122 | */
|
---|
123 | int closesocket(int socket_id);
|
---|
124 |
|
---|
125 | /** Sends data via the socket.
|
---|
126 | * @param[in] socket_id Socket identifier.
|
---|
127 | * @param[in] data The data to be sent.
|
---|
128 | * @param[in] datalength The data length.
|
---|
129 | * @param[in] flags Various send flags.
|
---|
130 | * @returns EOK on success.
|
---|
131 | * @returns ENOTSOCK if the socket is not found.
|
---|
132 | * @returns EBADMEM if the data parameter is NULL.
|
---|
133 | * @returns NO_DATA if the datalength parameter is zero (0).
|
---|
134 | * @returns Other error codes as defined for the NET_SOCKET_SEND message.
|
---|
135 | */
|
---|
136 | int send(int socket_id, void * data, size_t datalength, int flags);
|
---|
137 |
|
---|
138 | /** Sends data via the socket to the remote address.
|
---|
139 | * Binds the socket to a free port if not already connected/bound.
|
---|
140 | * @param[in] socket_id Socket identifier.
|
---|
141 | * @param[in] data The data to be sent.
|
---|
142 | * @param[in] datalength The data length.
|
---|
143 | * @param[in] flags Various send flags.
|
---|
144 | * @param[in] toaddr The destination address.
|
---|
145 | * @param[in] addrlen The address length.
|
---|
146 | * @returns EOK on success.
|
---|
147 | * @returns ENOTSOCK if the socket is not found.
|
---|
148 | * @returns EBADMEM if the data or toaddr parameter is NULL.
|
---|
149 | * @returns NO_DATA if the datalength or the addrlen parameter is zero (0).
|
---|
150 | * @returns Other error codes as defined for the NET_SOCKET_SENDTO message.
|
---|
151 | */
|
---|
152 | int sendto(int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen);
|
---|
153 |
|
---|
154 | /** Receives data via the socket.
|
---|
155 | * @param[in] socket_id Socket identifier.
|
---|
156 | * @param[out] data The data buffer to be filled.
|
---|
157 | * @param[in] datalength The data length.
|
---|
158 | * @param[in] flags Various receive flags.
|
---|
159 | * @returns EOK on success.
|
---|
160 | * @returns ENOTSOCK if the socket is not found.
|
---|
161 | * @returns EBADMEM if the data parameter is NULL.
|
---|
162 | * @returns NO_DATA if the datalength parameter is zero (0).
|
---|
163 | * @returns Other error codes as defined for the NET_SOCKET_RECV message.
|
---|
164 | */
|
---|
165 | int recv(int socket_id, void * data, size_t datalength, int flags);
|
---|
166 |
|
---|
167 | /** Receives data via the socket.
|
---|
168 | * @param[in] socket_id Socket identifier.
|
---|
169 | * @param[out] data The data buffer to be filled.
|
---|
170 | * @param[in] datalength The data length.
|
---|
171 | * @param[in] flags Various receive flags.
|
---|
172 | * @param[out] fromaddr The source address.
|
---|
173 | * @param[in,out] addrlen The address length. The maximum address length is read. The actual address length is set.
|
---|
174 | * @returns EOK on success.
|
---|
175 | * @returns ENOTSOCK if the socket is not found.
|
---|
176 | * @returns EBADMEM if the data or fromaddr parameter is NULL.
|
---|
177 | * @returns NO_DATA if the datalength or addrlen parameter is zero (0).
|
---|
178 | * @returns Other error codes as defined for the NET_SOCKET_RECVFROM message.
|
---|
179 | */
|
---|
180 | int recvfrom(int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen);
|
---|
181 |
|
---|
182 | /** Gets socket option.
|
---|
183 | * @param[in] socket_id Socket identifier.
|
---|
184 | * @param[in] level The socket options level.
|
---|
185 | * @param[in] optname The socket option to be get.
|
---|
186 | * @param[out] value The value buffer to be filled.
|
---|
187 | * @param[in,out] optlen The value buffer length. The maximum length is read. The actual length is set.
|
---|
188 | * @returns EOK on success.
|
---|
189 | * @returns ENOTSOCK if the socket is not found.
|
---|
190 | * @returns EBADMEM if the value or optlen parameter is NULL.
|
---|
191 | * @returns NO_DATA if the optlen parameter is zero (0).
|
---|
192 | * @returns Other error codes as defined for the NET_SOCKET_GETSOCKOPT message.
|
---|
193 | */
|
---|
194 | int getsockopt(int socket_id, int level, int optname, void * value, size_t * optlen);
|
---|
195 |
|
---|
196 | /** Sets socket option.
|
---|
197 | * @param[in] socket_id Socket identifier.
|
---|
198 | * @param[in] level The socket options level.
|
---|
199 | * @param[in] optname The socket option to be set.
|
---|
200 | * @param[in] value The value to be set.
|
---|
201 | * @param[in] optlen The value length.
|
---|
202 | * @returns EOK on success.
|
---|
203 | * @returns ENOTSOCK if the socket is not found.
|
---|
204 | * @returns EBADMEM if the value parameter is NULL.
|
---|
205 | * @returns NO_DATA if the optlen parameter is zero (0).
|
---|
206 | * @returns Other error codes as defined for the NET_SOCKET_SETSOCKOPT message.
|
---|
207 | */
|
---|
208 | int setsockopt(int socket_id, int level, int optname, const void * value, size_t optlen);
|
---|
209 |
|
---|
210 | /*@}*/
|
---|
211 |
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | /** @}
|
---|
215 | */
|
---|