source: mainline/uspace/srv/net/socket/socket_client.c@ 5814ef7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5814ef7 was 5814ef7, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago
  • explicit cast fix
  • Property mode set to 100644
File size: 27.4 KB
Line 
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) implementation.
35 * @see socket.h for more information.
36 * This is a part of the network application library.
37 */
38
39#include <assert.h>
40#include <async.h>
41#include <fibril_synch.h>
42#include <limits.h>
43#include <stdlib.h>
44
45#include <ipc/services.h>
46
47#include "../err.h"
48#include "../modules.h"
49
50#include "../include/in.h"
51#include "../include/socket.h"
52#include "../include/socket_errno.h"
53
54#include "../structures/dynamic_fifo.h"
55#include "../structures/int_map.h"
56
57#include "socket_messages.h"
58
59/** Initial received packet queue size.
60 */
61#define SOCKET_INITIAL_RECEIVED_SIZE 4
62
63/** Maximum received packet queue size.
64 */
65#define SOCKET_MAX_RECEIVED_SIZE 0
66
67/** Initial waiting sockets queue size.
68 */
69#define SOCKET_INITIAL_ACCEPTED_SIZE 1
70
71/** Maximum waiting sockets queue size.
72 */
73#define SOCKET_MAX_ACCEPTED_SIZE 0
74
75/** Default timeout for connections in microseconds.
76 */
77#define SOCKET_CONNECT_TIMEOUT (1 * 1000 * 1000)
78
79/** Maximum number of random attempts to find a new socket identifier before switching to the sequence.
80 */
81#define SOCKET_ID_TRIES 100
82
83/** Type definition of the socket specific data.
84 * @see socket
85 */
86typedef struct socket socket_t;
87
88/** Type definition of the socket specific data pointer.
89 * @see socket
90 */
91typedef socket_t * socket_ref;
92
93/** Socket specific data.
94 * Each socket lock locks only its structure part and any number of them may be locked simultaneously.
95 */
96struct socket{
97 /** Socket identifier.
98 */
99 int socket_id;
100 /** Parent module phone.
101 */
102 int phone;
103 /** Parent module service.
104 */
105 services_t service;
106 /** Underlying protocol header size.
107 * Sending and receiving optimalization.
108 */
109 size_t header_size;
110 /** Packet data fragment size.
111 * Sending optimalization.
112 */
113 size_t data_fragment_size;
114 /** Sending safety lock.
115 * Locks the header_size and data_fragment_size attributes.
116 */
117 fibril_rwlock_t sending_lock;
118 /** Received packets queue.
119 */
120 dyn_fifo_t received;
121 /** Received packets safety lock.
122 * Used for receiving and receive notifications.
123 * Locks the received attribute.
124 */
125 fibril_mutex_t receive_lock;
126 /** Received packets signaling.
127 * Signaled upon receive notification.
128 */
129 fibril_condvar_t receive_signal;
130 /** Waiting sockets queue.
131 */
132 dyn_fifo_t accepted;
133 /** Waiting sockets safety lock.
134 * Used for accepting and accept notifications.
135 * Locks the accepted attribute.
136 */
137 fibril_mutex_t accept_lock;
138 /** Waiting sockets signaling.
139 * Signaled upon accept notification.
140 */
141 fibril_condvar_t accept_signal;
142 /** The number of blocked functions called.
143 * Used while waiting for the received packets or accepted sockets.
144 */
145 int blocked;
146};
147
148/** Sockets map.
149 * Maps socket identifiers to the socket specific data.
150 * @see int_map.h
151 */
152INT_MAP_DECLARE(sockets, socket_t);
153
154/** Socket client library global data.
155 */
156static struct socket_client_globals {
157 /** TCP module phone.
158 */
159 int tcp_phone;
160 /** UDP module phone.
161 */
162 int udp_phone;
163// /** The last socket identifier.
164// */
165// int last_id;
166 /** Active sockets.
167 */
168 sockets_ref sockets;
169 /** Safety lock.
170 * Write lock is used only for adding or removing sockets.
171 * When locked for writing, no other socket locks need to be locked.
172 * When locked for reading, any other socket locks may be locked.
173 * No socket lock may be locked if this lock is unlocked.
174 */
175 fibril_rwlock_t lock;
176} socket_globals = {
177 .tcp_phone = -1,
178 .udp_phone = -1,
179// .last_id = 0,
180 .sockets = NULL,
181 .lock = {
182 .readers = 0,
183 .writers = 0,
184 .waiters = {
185 .prev = &socket_globals.lock.waiters,
186 .next = &socket_globals.lock.waiters
187 }
188 }
189};
190
191INT_MAP_IMPLEMENT(sockets, socket_t);
192
193/** Returns the TCP module phone.
194 * Connects to the TCP module if necessary.
195 * @returns The TCP module phone.
196 * @returns Other error codes as defined for the bind_service_timeout() function.
197 */
198static int socket_get_tcp_phone(void);
199
200/** Returns the UDP module phone.
201 * Connects to the UDP module if necessary.
202 * @returns The UDP module phone.
203 * @returns Other error codes as defined for the bind_service_timeout() function.
204 */
205static int socket_get_udp_phone(void);
206
207/** Returns the active sockets.
208 * @returns The active sockets.
209 */
210static sockets_ref socket_get_sockets(void);
211
212/** Tries to find a new free socket identifier.
213 * @returns The new socket identifier.
214 * @returns ELIMIT if there is no socket identifier available.
215 */
216static int socket_generate_new_id(void);
217
218/** Default thread for new connections.
219 * @param[in] iid The initial message identifier.
220 * @param[in] icall The initial message call structure.
221 */
222void socket_connection(ipc_callid_t iid, ipc_call_t * icall);
223
224/** Sends message to the socket parent module with specified data.
225 * @param[in] socket_id Socket identifier.
226 * @param[in] message The action message.
227 * @param[in] arg2 The second message parameter.
228 * @param[in] data The data to be sent.
229 * @param[in] datalength The data length.
230 * @returns EOK on success.
231 * @returns ENOTSOCK if the socket is not found.
232 * @returns EBADMEM if the data parameter is NULL.
233 * @returns NO_DATA if the datalength parameter is zero (0).
234 * @returns Other error codes as defined for the spcific message.
235 */
236int socket_send_data(int socket_id, ipcarg_t message, ipcarg_t arg2, const void * data, size_t datalength);
237
238/** Initializes a new socket specific data.
239 * @param[in,out] socket The socket to be initialized.
240 * @param[in] socket_id The new socket identifier.
241 * @param[in] phone The parent module phone.
242 * @param[in] service The parent module service.
243 */
244void socket_initialize(socket_ref socket, int socket_id, int phone, services_t service);
245
246/** Clears and destroys the socket.
247 * @param[in] socket The socket to be destroyed.
248 */
249void socket_destroy(socket_ref socket);
250
251/** Receives data via the socket.
252 * @param[in] message The action message.
253 * @param[in] socket_id Socket identifier.
254 * @param[out] data The data buffer to be filled.
255 * @param[in] datalength The data length.
256 * @param[in] flags Various receive flags.
257 * @param[out] fromaddr The source address. May be NULL for connected sockets.
258 * @param[in,out] addrlen The address length. The maximum address length is read. The actual address length is set. Used only if fromaddr is not NULL.
259 * @returns EOK on success.
260 * @returns ENOTSOCK if the socket is not found.
261 * @returns EBADMEM if the data parameter is NULL.
262 * @returns NO_DATA if the datalength or addrlen parameter is zero (0).
263 * @returns Other error codes as defined for the spcific message.
264 */
265int recvfrom_core(ipcarg_t message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen);
266
267/** Sends data via the socket to the remote address.
268 * Binds the socket to a free port if not already connected/bound.
269 * @param[in] message The action message.
270 * @param[in] socket_id Socket identifier.
271 * @param[in] data The data to be sent.
272 * @param[in] datalength The data length.
273 * @param[in] flags Various send flags.
274 * @param[in] toaddr The destination address. May be NULL for connected sockets.
275 * @param[in] addrlen The address length. Used only if toaddr is not NULL.
276 * @returns EOK on success.
277 * @returns ENOTSOCK if the socket is not found.
278 * @returns EBADMEM if the data or toaddr parameter is NULL.
279 * @returns NO_DATA if the datalength or the addrlen parameter is zero (0).
280 * @returns Other error codes as defined for the NET_SOCKET_SENDTO message.
281 */
282int sendto_core(ipcarg_t message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen);
283
284static int socket_get_tcp_phone(void){
285 if(socket_globals.tcp_phone < 0){
286 socket_globals.tcp_phone = bind_service_timeout(SERVICE_TCP, 0, 0, SERVICE_TCP, socket_connection, SOCKET_CONNECT_TIMEOUT);
287 }
288 return socket_globals.tcp_phone;
289}
290
291static int socket_get_udp_phone(void){
292 if(socket_globals.udp_phone < 0){
293 socket_globals.udp_phone = bind_service_timeout(SERVICE_UDP, 0, 0, SERVICE_UDP, socket_connection, SOCKET_CONNECT_TIMEOUT);
294 }
295 return socket_globals.udp_phone;
296}
297
298static sockets_ref socket_get_sockets(void){
299 if(! socket_globals.sockets){
300 socket_globals.sockets = (sockets_ref) malloc(sizeof(sockets_t));
301 if(! socket_globals.sockets){
302 return NULL;
303 }
304 if(sockets_initialize(socket_globals.sockets) != EOK){
305 free(socket_globals.sockets);
306 socket_globals.sockets = NULL;
307 }
308 srand(task_get_id());
309 }
310 return socket_globals.sockets;
311}
312
313static int socket_generate_new_id(void){
314 sockets_ref sockets;
315 int socket_id;
316 int count;
317
318 sockets = socket_get_sockets();
319 count = 0;
320// socket_id = socket_globals.last_id;
321 do{
322 if(count < SOCKET_ID_TRIES){
323 socket_id = rand() % INT_MAX;
324 ++ count;
325 }else if(count == SOCKET_ID_TRIES){
326 socket_id = 1;
327 ++ count;
328 // only this branch for last_id
329 }else{
330 if(socket_id < INT_MAX){
331 ++ socket_id;
332/* }else if(socket_globals.last_id){
333* socket_globals.last_id = 0;
334* socket_id = 1;
335*/ }else{
336 return ELIMIT;
337 }
338 }
339 }while(sockets_find(sockets, socket_id));
340// last_id = socket_id
341 return socket_id;
342}
343
344void socket_initialize(socket_ref socket, int socket_id, int phone, services_t service){
345 socket->socket_id = socket_id;
346 socket->phone = phone;
347 socket->service = service;
348 dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE);
349 dyn_fifo_initialize(&socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE);
350 fibril_mutex_initialize(&socket->receive_lock);
351 fibril_condvar_initialize(&socket->receive_signal);
352 fibril_mutex_initialize(&socket->accept_lock);
353 fibril_condvar_initialize(&socket->accept_signal);
354 fibril_rwlock_initialize(&socket->sending_lock);
355}
356
357void socket_connection(ipc_callid_t iid, ipc_call_t * icall){
358 ERROR_DECLARE;
359
360 ipc_callid_t callid;
361 ipc_call_t call;
362 socket_ref socket;
363
364 while(true){
365
366 callid = async_get_call(&call);
367 switch(IPC_GET_METHOD(call)){
368 case NET_SOCKET_RECEIVED:
369 case NET_SOCKET_ACCEPTED:
370 case NET_SOCKET_DATA_FRAGMENT_SIZE:
371 fibril_rwlock_read_lock(&socket_globals.lock);
372 // find the socket
373 socket = sockets_find(socket_get_sockets(), SOCKET_GET_SOCKET_ID(call));
374 if(! socket){
375 ERROR_CODE = ENOTSOCK;
376 }else{
377 switch(IPC_GET_METHOD(call)){
378 case NET_SOCKET_RECEIVED:
379 fibril_mutex_lock(&socket->receive_lock);
380 // push the number of received packet fragments
381 if(! ERROR_OCCURRED(dyn_fifo_push(&socket->received, SOCKET_GET_DATA_FRAGMENTS(call), SOCKET_MAX_RECEIVED_SIZE))){
382 // signal the received packet
383 fibril_condvar_signal(&socket->receive_signal);
384 }
385 fibril_mutex_unlock(&socket->receive_lock);
386 break;
387 case NET_SOCKET_ACCEPTED:
388 // push the new socket identifier
389 fibril_mutex_lock(&socket->accept_lock);
390 if(! ERROR_OCCURRED(dyn_fifo_push(&socket->accepted, 1, SOCKET_MAX_ACCEPTED_SIZE))){
391 // signal the accepted socket
392 fibril_condvar_signal(&socket->accept_signal);
393 }
394 fibril_mutex_unlock(&socket->accept_lock);
395 break;
396 default:
397 ERROR_CODE = ENOTSUP;
398 }
399 if((SOCKET_GET_DATA_FRAGMENT_SIZE(call) > 0)
400 && (SOCKET_GET_DATA_FRAGMENT_SIZE(call) != socket->data_fragment_size)){
401 fibril_rwlock_write_lock(&socket->sending_lock);
402 // set the data fragment size
403 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(call);
404 fibril_rwlock_write_unlock(&socket->sending_lock);
405 }
406 }
407 fibril_rwlock_read_unlock(&socket_globals.lock);
408 break;
409 default:
410 ERROR_CODE = ENOTSUP;
411 }
412 ipc_answer_0(callid, (ipcarg_t) ERROR_CODE);
413 }
414}
415
416int socket(int domain, int type, int protocol){
417 ERROR_DECLARE;
418
419 socket_ref socket;
420 int phone;
421 int socket_id;
422 services_t service;
423 ipcarg_t fragment_size;
424 ipcarg_t header_size;
425
426 // find the appropriate service
427 switch(domain){
428 case PF_INET:
429 switch(type){
430 case SOCK_STREAM:
431 if(! protocol){
432 protocol = IPPROTO_TCP;
433 }
434 switch(protocol){
435 case IPPROTO_TCP:
436 phone = socket_get_tcp_phone();
437 service = SERVICE_TCP;
438 break;
439 default:
440 return EPROTONOSUPPORT;
441 }
442 break;
443 case SOCK_DGRAM:
444 if(! protocol){
445 protocol = IPPROTO_UDP;
446 }
447 switch(protocol){
448 case IPPROTO_UDP:
449 phone = socket_get_udp_phone();
450 service = SERVICE_UDP;
451 break;
452 default:
453 return EPROTONOSUPPORT;
454 }
455 break;
456 case SOCK_RAW:
457 default:
458 return ESOCKTNOSUPPORT;
459 }
460 break;
461 // TODO IPv6
462 default:
463 return EPFNOSUPPORT;
464 }
465 if(phone < 0){
466 return phone;
467 }
468 // create a new socket structure
469 socket = (socket_ref) malloc(sizeof(socket_t));
470 if(! socket){
471 return ENOMEM;
472 }
473 bzero(socket, sizeof(*socket));
474 fibril_rwlock_write_lock(&socket_globals.lock);
475 // request a new socket
476 socket_id = socket_generate_new_id();
477 if(socket_id <= 0){
478 fibril_rwlock_write_unlock(&socket_globals.lock);
479 free(socket);
480 return socket_id;
481 }
482 if(ERROR_OCCURRED((int) async_req_3_3(phone, NET_SOCKET, socket_id, 0, service, NULL, &fragment_size, &header_size))){
483 fibril_rwlock_write_unlock(&socket_globals.lock);
484 free(socket);
485 return ERROR_CODE;
486 }
487 socket->data_fragment_size = (size_t) fragment_size;
488 socket->header_size = (size_t) header_size;
489 // finish the new socket initialization
490 socket_initialize(socket, socket_id, phone, service);
491 // store the new socket
492 ERROR_CODE = sockets_add(socket_get_sockets(), socket_id, socket);
493 fibril_rwlock_write_unlock(&socket_globals.lock);
494 if(ERROR_CODE < 0){
495 dyn_fifo_destroy(&socket->received);
496 dyn_fifo_destroy(&socket->accepted);
497 free(socket);
498 async_msg_3(phone, NET_SOCKET_CLOSE, (ipcarg_t) socket_id, 0, service);
499 return ERROR_CODE;
500 }
501
502 return socket_id;
503}
504
505int socket_send_data(int socket_id, ipcarg_t message, ipcarg_t arg2, const void * data, size_t datalength){
506 socket_ref socket;
507 aid_t message_id;
508 ipcarg_t result;
509
510 if(! data){
511 return EBADMEM;
512 }
513 if(! datalength){
514 return NO_DATA;
515 }
516
517 fibril_rwlock_read_lock(&socket_globals.lock);
518 // find the socket
519 socket = sockets_find(socket_get_sockets(), socket_id);
520 if(! socket){
521 fibril_rwlock_read_unlock(&socket_globals.lock);
522 return ENOTSOCK;
523 }
524 // request the message
525 message_id = async_send_3(socket->phone, message, (ipcarg_t) socket->socket_id, arg2, socket->service, NULL);
526 // send the address
527 async_data_write_start(socket->phone, data, datalength);
528 fibril_rwlock_read_unlock(&socket_globals.lock);
529 async_wait_for(message_id, &result);
530 return (int) result;
531}
532
533int bind(int socket_id, const struct sockaddr * my_addr, socklen_t addrlen){
534 if(addrlen <= 0){
535 return EINVAL;
536 }
537 // send the address
538 return socket_send_data(socket_id, NET_SOCKET_BIND, 0, my_addr, (size_t) addrlen);
539}
540
541int listen(int socket_id, int backlog){
542 socket_ref socket;
543 int result;
544
545 if(backlog <= 0){
546 return EINVAL;
547 }
548 fibril_rwlock_read_lock(&socket_globals.lock);
549 // find the socket
550 socket = sockets_find(socket_get_sockets(), socket_id);
551 if(! socket){
552 fibril_rwlock_read_unlock(&socket_globals.lock);
553 return ENOTSOCK;
554 }
555 // request listen backlog change
556 result = (int) async_req_3_0(socket->phone, NET_SOCKET_LISTEN, (ipcarg_t) socket->socket_id, (ipcarg_t) backlog, socket->service);
557 fibril_rwlock_read_unlock(&socket_globals.lock);
558 return result;
559}
560
561int accept(int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen){
562 socket_ref socket;
563 socket_ref new_socket;
564 aid_t message_id;
565 ipcarg_t ipc_result;
566 int result;
567 ipc_call_t answer;
568
569 if((! cliaddr) || (! addrlen)){
570 return EBADMEM;
571 }
572
573 fibril_rwlock_write_lock(&socket_globals.lock);
574 // find the socket
575 socket = sockets_find(socket_get_sockets(), socket_id);
576 if(! socket){
577 fibril_rwlock_write_unlock(&socket_globals.lock);
578 return ENOTSOCK;
579 }
580 fibril_mutex_lock(&socket->accept_lock);
581 // wait for an accepted socket
582 ++ socket->blocked;
583 while(dyn_fifo_value(&socket->accepted) <= 0){
584 fibril_rwlock_write_unlock(&socket_globals.lock);
585 fibril_condvar_wait(&socket->accept_signal, &socket->accept_lock);
586 // drop the accept lock to avoid deadlock
587 fibril_mutex_unlock(&socket->accept_lock);
588 fibril_rwlock_write_lock(&socket_globals.lock);
589 fibril_mutex_lock(&socket->accept_lock);
590 }
591 -- socket->blocked;
592
593 // create a new scoket
594 new_socket = (socket_ref) malloc(sizeof(socket_t));
595 if(! new_socket){
596 fibril_mutex_unlock(&socket->accept_lock);
597 fibril_rwlock_write_unlock(&socket_globals.lock);
598 return ENOMEM;
599 }
600 bzero(new_socket, sizeof(*new_socket));
601 socket_id = socket_generate_new_id();
602 if(socket_id <= 0){
603 fibril_mutex_unlock(&socket->accept_lock);
604 fibril_rwlock_write_unlock(&socket_globals.lock);
605 free(new_socket);
606 return socket_id;
607 }
608 socket_initialize(new_socket, socket_id, socket->phone, socket->service);
609 result = sockets_add(socket_get_sockets(), new_socket->socket_id, new_socket);
610 if(result < 0){
611 fibril_mutex_unlock(&socket->accept_lock);
612 fibril_rwlock_write_unlock(&socket_globals.lock);
613 free(new_socket);
614 return result;
615 }
616
617 // request accept
618 message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT, (ipcarg_t) socket->socket_id, 0, socket->service, 0, new_socket->socket_id, &answer);
619 // read address
620 ipc_data_read_start(socket->phone, cliaddr, * addrlen);
621 fibril_rwlock_write_unlock(&socket_globals.lock);
622 async_wait_for(message_id, &ipc_result);
623 result = (int) ipc_result;
624 if(result > 0){
625 if(result != socket_id){
626 result = EINVAL;
627 }
628 // dequeue the accepted socket if successful
629 dyn_fifo_pop(&socket->accepted);
630 // set address length
631 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
632 new_socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
633 }else if(result == ENOTSOCK){
634 // empty the queue if no accepted sockets
635 while(dyn_fifo_pop(&socket->accepted) > 0);
636 }
637 fibril_mutex_unlock(&socket->accept_lock);
638 return result;
639}
640
641int connect(int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen){
642 if(! serv_addr){
643 return EDESTADDRREQ;
644 }
645 if(! addrlen){
646 return EDESTADDRREQ;
647 }
648 // send the address
649 return socket_send_data(socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen);
650}
651
652int closesocket(int socket_id){
653 ERROR_DECLARE;
654
655 socket_ref socket;
656
657 fibril_rwlock_write_lock(&socket_globals.lock);
658 socket = sockets_find(socket_get_sockets(), socket_id);
659 if(! socket){
660 fibril_rwlock_write_unlock(&socket_globals.lock);
661 return ENOTSOCK;
662 }
663 if(socket->blocked){
664 fibril_rwlock_write_unlock(&socket_globals.lock);
665 return EINPROGRESS;
666 }
667 // request close
668 ERROR_PROPAGATE((int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE, (ipcarg_t) socket->socket_id, 0, socket->service));
669 // free the socket structure
670 socket_destroy(socket);
671 fibril_rwlock_write_unlock(&socket_globals.lock);
672 return EOK;
673}
674
675void socket_destroy(socket_ref socket){
676 int accepted_id;
677
678 // destroy all accepted sockets
679 while((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0){
680 socket_destroy(sockets_find(socket_get_sockets(), accepted_id));
681 }
682 dyn_fifo_destroy(&socket->received);
683 dyn_fifo_destroy(&socket->accepted);
684 sockets_exclude(socket_get_sockets(), socket->socket_id);
685}
686
687int send(int socket_id, void * data, size_t datalength, int flags){
688 // without the address
689 return sendto_core(NET_SOCKET_SEND, socket_id, data, datalength, flags, NULL, 0);
690}
691
692int sendto(int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
693 if(! toaddr){
694 return EDESTADDRREQ;
695 }
696 if(! addrlen){
697 return EDESTADDRREQ;
698 }
699 // with the address
700 return sendto_core(NET_SOCKET_SENDTO, socket_id, data, datalength, flags, toaddr, addrlen);
701}
702
703int sendto_core(ipcarg_t message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
704 socket_ref socket;
705 aid_t message_id;
706 ipcarg_t result;
707 size_t fragments;
708 ipc_call_t answer;
709
710 if(! data){
711 return EBADMEM;
712 }
713 if(! datalength){
714 return NO_DATA;
715 }
716 fibril_rwlock_read_lock(&socket_globals.lock);
717 // find socket
718 socket = sockets_find(socket_get_sockets(), socket_id);
719 if(! socket){
720 fibril_rwlock_read_unlock(&socket_globals.lock);
721 return ENOTSOCK;
722 }
723 fibril_rwlock_read_lock(&socket->sending_lock);
724 // compute data fragment count
725 if(socket->data_fragment_size > 0){
726 fragments = (datalength + socket->header_size) / socket->data_fragment_size;
727 if((datalength + socket->header_size) % socket->data_fragment_size) ++ fragments;
728 }else{
729 fragments = 1;
730 }
731 // request send
732 message_id = async_send_5(socket->phone, message, (ipcarg_t) socket->socket_id, (fragments == 1 ? datalength : socket->data_fragment_size), socket->service, (ipcarg_t) flags, fragments, &answer);
733 // send the address if given
734 if((! toaddr) || (async_data_write_start(socket->phone, toaddr, addrlen) == EOK)){
735 if(fragments == 1){
736 // send all if only one fragment
737 async_data_write_start(socket->phone, data, datalength);
738 }else{
739 // send the first fragment
740 async_data_write_start(socket->phone, data, socket->data_fragment_size - socket->header_size);
741 data = ((const uint8_t *) data) + socket->data_fragment_size - socket->header_size;
742 // send the middle fragments
743 while((-- fragments) > 1){
744 async_data_write_start(socket->phone, data, socket->data_fragment_size);
745 data = ((const uint8_t *) data) + socket->data_fragment_size;
746 }
747 // send the last fragment
748 async_data_write_start(socket->phone, data, (datalength + socket->header_size) % socket->data_fragment_size);
749 }
750 }
751 async_wait_for(message_id, &result);
752 if((SOCKET_GET_DATA_FRAGMENT_SIZE(answer) > 0)
753 && (SOCKET_GET_DATA_FRAGMENT_SIZE(answer) != socket->data_fragment_size)){
754 // set the data fragment size
755 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
756 }
757 fibril_rwlock_read_unlock(&socket->sending_lock);
758 fibril_rwlock_read_unlock(&socket_globals.lock);
759 return (int) result;
760}
761
762int recv(int socket_id, void * data, size_t datalength, int flags){
763 // without the address
764 return recvfrom_core(NET_SOCKET_RECV, socket_id, data, datalength, flags, NULL, NULL);
765}
766
767int recvfrom(int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
768 if(! fromaddr){
769 return EBADMEM;
770 }
771 if(! addrlen){
772 return NO_DATA;
773 }
774 // with the address
775 return recvfrom_core(NET_SOCKET_RECVFROM, socket_id, data, datalength, flags, fromaddr, addrlen);
776}
777
778int recvfrom_core(ipcarg_t message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
779 socket_ref socket;
780 aid_t message_id;
781 ipcarg_t ipc_result;
782 int result;
783 size_t fragments;
784 size_t * lengths;
785 size_t index;
786 ipc_call_t answer;
787
788 if(! data){
789 return EBADMEM;
790 }
791 if(! datalength){
792 return NO_DATA;
793 }
794 if(fromaddr && (! addrlen)){
795 return EINVAL;
796 }
797 fibril_rwlock_read_lock(&socket_globals.lock);
798 // find the socket
799 socket = sockets_find(socket_get_sockets(), socket_id);
800 if(! socket){
801 fibril_rwlock_read_unlock(&socket_globals.lock);
802 return ENOTSOCK;
803 }
804 fibril_mutex_lock(&socket->receive_lock);
805 // wait for a received packet
806 ++ socket->blocked;
807 while((result = dyn_fifo_value(&socket->received)) <= 0){
808 fibril_rwlock_read_unlock(&socket_globals.lock);
809 fibril_condvar_wait(&socket->receive_signal, &socket->receive_lock);
810 // drop the receive lock to avoid deadlock
811 fibril_mutex_unlock(&socket->receive_lock);
812 fibril_rwlock_read_lock(&socket_globals.lock);
813 fibril_mutex_lock(&socket->receive_lock);
814 }
815 -- socket->blocked;
816 fragments = (size_t) result;
817 // prepare lengths if more fragments
818 if(fragments > 1){
819 lengths = (size_t *) malloc(sizeof(size_t) * fragments + sizeof(size_t));
820 if(! lengths){
821 fibril_mutex_unlock(&socket->receive_lock);
822 fibril_rwlock_read_unlock(&socket_globals.lock);
823 return ENOMEM;
824 }
825 // request packet data
826 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
827 // read the address if desired
828 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
829 // read the fragment lengths
830 if(async_data_read_start(socket->phone, lengths, sizeof(int) * (fragments + 1)) == EOK){
831 if(lengths[fragments] <= datalength){
832 // read all fragments if long enough
833 for(index = 0; index < fragments; ++ index){
834 async_data_read_start(socket->phone, data, lengths[index]);
835 data = ((uint8_t *) data) + lengths[index];
836 }
837 }
838 }
839 }
840 free(lengths);
841 }else{
842 // request packet data
843 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
844 // read the address if desired
845 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
846 // read all if only one fragment
847 async_data_read_start(socket->phone, data, datalength);
848 }
849 }
850 async_wait_for(message_id, &ipc_result);
851 result = (int) ipc_result;
852 // if successful
853 if(result == EOK){
854 // dequeue the received packet
855 dyn_fifo_pop(&socket->received);
856 // return read data length
857 result = SOCKET_GET_READ_DATA_LENGTH(answer);
858 // set address length
859 if(fromaddr && addrlen){
860 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
861 }
862 }
863 fibril_mutex_unlock(&socket->receive_lock);
864 fibril_rwlock_read_unlock(&socket_globals.lock);
865 return result;
866}
867
868int getsockopt(int socket_id, int level, int optname, void * value, size_t * optlen){
869 socket_ref socket;
870 aid_t message_id;
871 ipcarg_t result;
872
873 if(!(value && optlen)){
874 return EBADMEM;
875 }
876 if(!(*optlen)){
877 return NO_DATA;
878 }
879 fibril_rwlock_read_lock(&socket_globals.lock);
880 // find the socket
881 socket = sockets_find(socket_get_sockets(), socket_id);
882 if(! socket){
883 fibril_rwlock_read_unlock(&socket_globals.lock);
884 return ENOTSOCK;
885 }
886 // request option value
887 message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT, (ipcarg_t) socket->socket_id, (ipcarg_t) optname, socket->service, NULL);
888 // read the length
889 if(async_data_read_start(socket->phone, optlen, sizeof(*optlen)) == EOK){
890 // read the value
891 async_data_read_start(socket->phone, value, * optlen);
892 }
893 fibril_rwlock_read_unlock(&socket_globals.lock);
894 async_wait_for(message_id, &result);
895 return (int) result;
896}
897
898int setsockopt(int socket_id, int level, int optname, const void * value, size_t optlen){
899 // send the value
900 return socket_send_data(socket_id, NET_SOCKET_SETSOCKOPT, (ipcarg_t) optname, value, optlen);
901
902}
903
904/** @}
905 */
Note: See TracBrowser for help on using the repository browser.