1 | /* |
---|
2 | * socket.c - User level <socket> library implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <socket.h> |
---|
25 | #include <hal_user.h> |
---|
26 | #include <hal_shared_types.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <syscalls_numbers.h> |
---|
29 | #include <shared_socket.h> |
---|
30 | |
---|
31 | /////////////////////// |
---|
32 | int socket( int domain, |
---|
33 | int type, |
---|
34 | int protocol ) |
---|
35 | { |
---|
36 | if( protocol != 0 ) |
---|
37 | { |
---|
38 | printf("\[ERROR] in %s : <protocol> argument must be 0\n", __FUNCTION__ ); |
---|
39 | return -1; |
---|
40 | } |
---|
41 | |
---|
42 | return hal_user_syscall( SYS_SOCKET, |
---|
43 | SOCK_CREATE, |
---|
44 | (reg_t)domain, |
---|
45 | (reg_t)type, |
---|
46 | 0 ); |
---|
47 | } // end socket() |
---|
48 | |
---|
49 | //////////////////////////// |
---|
50 | int bind( int fdid, |
---|
51 | sockaddr_t * local_addr, |
---|
52 | int addr_length ) |
---|
53 | { |
---|
54 | if( addr_length != sizeof( sockaddr_t) ) |
---|
55 | { |
---|
56 | printf("\[ERROR] in %s : illegal <addr_length> argument\n", __FUNCTION__ ); |
---|
57 | return -1; |
---|
58 | } |
---|
59 | |
---|
60 | return hal_user_syscall( SYS_SOCKET, |
---|
61 | SOCK_BIND, |
---|
62 | (reg_t)fdid, |
---|
63 | (reg_t)local_addr, |
---|
64 | 0 ); |
---|
65 | } // end bind() |
---|
66 | |
---|
67 | /////////////////////// |
---|
68 | int listen( int fdid, |
---|
69 | int backlog ) |
---|
70 | { |
---|
71 | return hal_user_syscall( SYS_SOCKET, |
---|
72 | SOCK_LISTEN, |
---|
73 | (reg_t)fdid, |
---|
74 | (reg_t)backlog, |
---|
75 | 0 ); |
---|
76 | } // end listen() |
---|
77 | |
---|
78 | /////////////////////////////// |
---|
79 | int connect( int fdid, |
---|
80 | sockaddr_t * remote_addr, |
---|
81 | int addr_length ) |
---|
82 | { |
---|
83 | if( addr_length != sizeof( sockaddr_t) ) |
---|
84 | { |
---|
85 | printf("\[ERROR] in %s : illegal <addr_length> argument\n", __FUNCTION__ ); |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | return hal_user_syscall( SYS_SOCKET, |
---|
90 | SOCK_CONNECT, |
---|
91 | (reg_t)fdid, |
---|
92 | (reg_t)remote_addr, |
---|
93 | 0 ); |
---|
94 | } // end connect() |
---|
95 | |
---|
96 | ////////////////////////////// |
---|
97 | int accept( int fdid, |
---|
98 | sockaddr_t * remote_addr, |
---|
99 | int * addr_length ) |
---|
100 | { |
---|
101 | if( *addr_length != sizeof( sockaddr_t) ) |
---|
102 | { |
---|
103 | printf("\[ERROR] in %s : illegal <addr_length> argument\n", __FUNCTION__ ); |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | return hal_user_syscall( SYS_SOCKET, |
---|
108 | SOCK_ACCEPT, |
---|
109 | (reg_t)fdid, |
---|
110 | (reg_t)remote_addr, |
---|
111 | 0 ); |
---|
112 | } // end accept() |
---|
113 | |
---|
114 | //////////////////////////// |
---|
115 | int send( int fdid, |
---|
116 | void * buffer, |
---|
117 | int length, |
---|
118 | int flags ) |
---|
119 | { |
---|
120 | if( flags != 0 ) |
---|
121 | { |
---|
122 | printf("\[ERROR] in %s : the <flags> argument must be 0\n", __FUNCTION__ ); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | return hal_user_syscall( SYS_SOCKET, |
---|
127 | SOCK_SEND, |
---|
128 | (reg_t)fdid, |
---|
129 | (reg_t)buffer, |
---|
130 | (reg_t)length ); |
---|
131 | } // end send() |
---|
132 | |
---|
133 | //////////////////////////// |
---|
134 | int recv( int fdid, |
---|
135 | void * buffer, |
---|
136 | int length, |
---|
137 | int flags ) |
---|
138 | { |
---|
139 | if( flags != 0 ) |
---|
140 | { |
---|
141 | printf("\[ERROR] in %s : the <flags> argument must be 0\n", __FUNCTION__ ); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | return hal_user_syscall( SYS_SOCKET, |
---|
146 | SOCK_RECV, |
---|
147 | (reg_t)fdid, |
---|
148 | (reg_t)buffer, |
---|
149 | (reg_t)length ); |
---|
150 | } // end recv() |
---|
151 | |
---|
152 | ////////////////////////////// |
---|
153 | int sendto( int fdid, |
---|
154 | void * buffer, |
---|
155 | int length, |
---|
156 | int flags, |
---|
157 | sockaddr_t * remote_addr, |
---|
158 | int addr_length ) |
---|
159 | { |
---|
160 | if( flags != 0 ) |
---|
161 | { |
---|
162 | printf("\[ERROR] in %s : the <flags> argument must be 0\n", __FUNCTION__ ); |
---|
163 | return -1; |
---|
164 | } |
---|
165 | |
---|
166 | if( remote_addr == NULL ) |
---|
167 | { |
---|
168 | printf("\[ERROR] in %s : <remote_addr> argument cannot be NULL\n", __FUNCTION__ ); |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | if( addr_length != sizeof( sockaddr_t) ) |
---|
173 | { |
---|
174 | printf("\[ERROR] in %s : illegal <addr_length> argument\n", __FUNCTION__ ); |
---|
175 | return -1; |
---|
176 | } |
---|
177 | |
---|
178 | if( (length & 0x0000FFFF) != length ) |
---|
179 | { |
---|
180 | printf("\[ERROR] in %s : illegal <length> argument\n", __FUNCTION__ ); |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | if( (fdid & 0x0000FFFF) != fdid ) |
---|
185 | { |
---|
186 | printf("\[ERROR] in %s : illegal <fdid> argument\n", __FUNCTION__ ); |
---|
187 | return -1; |
---|
188 | } |
---|
189 | |
---|
190 | return hal_user_syscall( SYS_SOCKET, |
---|
191 | SOCK_SENDTO, |
---|
192 | (reg_t)((fdid & 0xFFFF) | (length << 16)), |
---|
193 | (reg_t)buffer, |
---|
194 | (reg_t)remote_addr ); |
---|
195 | } // end sendto() |
---|
196 | |
---|
197 | //////////////////////////////// |
---|
198 | int recvfrom( int fdid, |
---|
199 | void * buffer, |
---|
200 | int length, |
---|
201 | int flags, |
---|
202 | sockaddr_t * remote_addr, |
---|
203 | int * addr_length ) |
---|
204 | { |
---|
205 | if( flags != 0 ) |
---|
206 | { |
---|
207 | printf("\[ERROR] in %s : the <flags> argument must be 0\n", __FUNCTION__ ); |
---|
208 | return -1; |
---|
209 | } |
---|
210 | |
---|
211 | if( remote_addr == NULL ) |
---|
212 | { |
---|
213 | printf("\[ERROR] in %s : <remote_addr> argument cannot be NULL\n", __FUNCTION__ ); |
---|
214 | return -1; |
---|
215 | } |
---|
216 | |
---|
217 | if( *addr_length != sizeof( sockaddr_t) ) |
---|
218 | { |
---|
219 | printf("\[ERROR] in %s : illegal <addr_length> argument\n", __FUNCTION__ ); |
---|
220 | return -1; |
---|
221 | } |
---|
222 | |
---|
223 | if( (length & 0x0000FFFF) != length ) |
---|
224 | { |
---|
225 | printf("\[ERROR] in %s : illegal <length> argument\n", __FUNCTION__ ); |
---|
226 | return -1; |
---|
227 | } |
---|
228 | |
---|
229 | if( (fdid & 0x0000FFFF) != fdid ) |
---|
230 | { |
---|
231 | printf("\[ERROR] in %s : illegal <fdid> argument\n", __FUNCTION__ ); |
---|
232 | return -1; |
---|
233 | } |
---|
234 | |
---|
235 | return hal_user_syscall( SYS_SOCKET, |
---|
236 | SOCK_RECVFROM, |
---|
237 | (reg_t)((fdid & 0xFFFF) | (length << 16)), |
---|
238 | (reg_t)buffer, |
---|
239 | (reg_t)remote_addr ); |
---|
240 | } // end recvfrom() |
---|
241 | |
---|
242 | |
---|
243 | |
---|