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