source: mainline/uspace/srv/net/socket/socket_client.c@ 936835e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 936835e was 936835e, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago

Merge mainline changes, revision 311

  • Property mode set to 100644
File size: 27.3 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
424 // find the appropriate service
425 switch(domain){
426 case PF_INET:
427 switch(type){
428 case SOCK_STREAM:
429 if(! protocol){
430 protocol = IPPROTO_TCP;
431 }
432 switch(protocol){
433 case IPPROTO_TCP:
434 phone = socket_get_tcp_phone();
435 service = SERVICE_TCP;
436 break;
437 default:
438 return EPROTONOSUPPORT;
439 }
440 break;
441 case SOCK_DGRAM:
442 if(! protocol){
443 protocol = IPPROTO_UDP;
444 }
445 switch(protocol){
446 case IPPROTO_UDP:
447 phone = socket_get_udp_phone();
448 service = SERVICE_UDP;
449 break;
450 default:
451 return EPROTONOSUPPORT;
452 }
453 break;
454 case SOCK_RAW:
455 default:
456 return ESOCKTNOSUPPORT;
457 }
458 break;
459 // TODO IPv6
460 default:
461 return EPFNOSUPPORT;
462 }
463 if(phone < 0){
464 return phone;
465 }
466 // create a new socket structure
467 socket = (socket_ref) malloc(sizeof(socket_t));
468 if(! socket){
469 return ENOMEM;
470 }
471 bzero(socket, sizeof(*socket));
472 fibril_rwlock_write_lock(&socket_globals.lock);
473 // request a new socket
474 socket_id = socket_generate_new_id();
475 if(socket_id <= 0){
476 fibril_rwlock_write_unlock(&socket_globals.lock);
477 free(socket);
478 return socket_id;
479 }
480 if(ERROR_OCCURRED((int) async_req_3_3(phone, NET_SOCKET, socket_id, 0, service, NULL, (ipcarg_t *) &socket->data_fragment_size, (ipcarg_t *) &socket->header_size))){
481 fibril_rwlock_write_unlock(&socket_globals.lock);
482 free(socket);
483 return ERROR_CODE;
484 }
485 // finish the new socket initialization
486 socket_initialize(socket, socket_id, phone, service);
487 // store the new socket
488 ERROR_CODE = sockets_add(socket_get_sockets(), socket_id, socket);
489 fibril_rwlock_write_unlock(&socket_globals.lock);
490 if(ERROR_CODE < 0){
491 dyn_fifo_destroy(&socket->received);
492 dyn_fifo_destroy(&socket->accepted);
493 free(socket);
494 async_msg_3(phone, NET_SOCKET_CLOSE, (ipcarg_t) socket_id, 0, service);
495 return ERROR_CODE;
496 }
497
498 return socket_id;
499}
500
501int socket_send_data(int socket_id, ipcarg_t message, ipcarg_t arg2, const void * data, size_t datalength){
502 socket_ref socket;
503 aid_t message_id;
504 ipcarg_t result;
505
506 if(! data){
507 return EBADMEM;
508 }
509 if(! datalength){
510 return NO_DATA;
511 }
512
513 fibril_rwlock_read_lock(&socket_globals.lock);
514 // find the socket
515 socket = sockets_find(socket_get_sockets(), socket_id);
516 if(! socket){
517 fibril_rwlock_read_unlock(&socket_globals.lock);
518 return ENOTSOCK;
519 }
520 // request the message
521 message_id = async_send_3(socket->phone, message, (ipcarg_t) socket->socket_id, arg2, socket->service, NULL);
522 // send the address
523 async_data_write_start(socket->phone, data, datalength);
524 fibril_rwlock_read_unlock(&socket_globals.lock);
525 async_wait_for(message_id, &result);
526 return (int) result;
527}
528
529int bind(int socket_id, const struct sockaddr * my_addr, socklen_t addrlen){
530 if(addrlen <= 0){
531 return EINVAL;
532 }
533 // send the address
534 return socket_send_data(socket_id, NET_SOCKET_BIND, 0, my_addr, (size_t) addrlen);
535}
536
537int listen(int socket_id, int backlog){
538 socket_ref socket;
539 int result;
540
541 if(backlog <= 0){
542 return EINVAL;
543 }
544 fibril_rwlock_read_lock(&socket_globals.lock);
545 // find the socket
546 socket = sockets_find(socket_get_sockets(), socket_id);
547 if(! socket){
548 fibril_rwlock_read_unlock(&socket_globals.lock);
549 return ENOTSOCK;
550 }
551 // request listen backlog change
552 result = (int) async_req_3_0(socket->phone, NET_SOCKET_LISTEN, (ipcarg_t) socket->socket_id, (ipcarg_t) backlog, socket->service);
553 fibril_rwlock_read_unlock(&socket_globals.lock);
554 return result;
555}
556
557int accept(int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen){
558 socket_ref socket;
559 socket_ref new_socket;
560 aid_t message_id;
561 ipcarg_t ipc_result;
562 int result;
563 ipc_call_t answer;
564
565 if((! cliaddr) || (! addrlen)){
566 return EBADMEM;
567 }
568
569 fibril_rwlock_write_lock(&socket_globals.lock);
570 // find the socket
571 socket = sockets_find(socket_get_sockets(), socket_id);
572 if(! socket){
573 fibril_rwlock_write_unlock(&socket_globals.lock);
574 return ENOTSOCK;
575 }
576 fibril_mutex_lock(&socket->accept_lock);
577 // wait for an accepted socket
578 ++ socket->blocked;
579 while(dyn_fifo_value(&socket->accepted) <= 0){
580 fibril_rwlock_write_unlock(&socket_globals.lock);
581 fibril_condvar_wait(&socket->accept_signal, &socket->accept_lock);
582 // drop the accept lock to avoid deadlock
583 fibril_mutex_unlock(&socket->accept_lock);
584 fibril_rwlock_write_lock(&socket_globals.lock);
585 fibril_mutex_lock(&socket->accept_lock);
586 }
587 -- socket->blocked;
588
589 // create a new scoket
590 new_socket = (socket_ref) malloc(sizeof(socket_t));
591 if(! new_socket){
592 fibril_mutex_unlock(&socket->accept_lock);
593 fibril_rwlock_write_unlock(&socket_globals.lock);
594 return ENOMEM;
595 }
596 bzero(new_socket, sizeof(*new_socket));
597 socket_id = socket_generate_new_id();
598 if(socket_id <= 0){
599 fibril_mutex_unlock(&socket->accept_lock);
600 fibril_rwlock_write_unlock(&socket_globals.lock);
601 free(new_socket);
602 return socket_id;
603 }
604 socket_initialize(new_socket, socket_id, socket->phone, socket->service);
605 result = sockets_add(socket_get_sockets(), new_socket->socket_id, new_socket);
606 if(result < 0){
607 fibril_mutex_unlock(&socket->accept_lock);
608 fibril_rwlock_write_unlock(&socket_globals.lock);
609 free(new_socket);
610 return result;
611 }
612
613 // request accept
614 message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT, (ipcarg_t) socket->socket_id, 0, socket->service, 0, new_socket->socket_id, &answer);
615 // read address
616 ipc_data_read_start(socket->phone, cliaddr, * addrlen);
617 fibril_rwlock_write_unlock(&socket_globals.lock);
618 async_wait_for(message_id, &ipc_result);
619 result = (int) ipc_result;
620 if(result > 0){
621 if(result != socket_id){
622 result = EINVAL;
623 }
624 // dequeue the accepted socket if successful
625 dyn_fifo_pop(&socket->accepted);
626 // set address length
627 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
628 new_socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
629 }else if(result == ENOTSOCK){
630 // empty the queue if no accepted sockets
631 while(dyn_fifo_pop(&socket->accepted) > 0);
632 }
633 fibril_mutex_unlock(&socket->accept_lock);
634 return result;
635}
636
637int connect(int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen){
638 if(! serv_addr){
639 return EDESTADDRREQ;
640 }
641 if(! addrlen){
642 return EDESTADDRREQ;
643 }
644 // send the address
645 return socket_send_data(socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen);
646}
647
648int closesocket(int socket_id){
649 ERROR_DECLARE;
650
651 socket_ref socket;
652
653 fibril_rwlock_write_lock(&socket_globals.lock);
654 socket = sockets_find(socket_get_sockets(), socket_id);
655 if(! socket){
656 fibril_rwlock_write_unlock(&socket_globals.lock);
657 return ENOTSOCK;
658 }
659 if(socket->blocked){
660 fibril_rwlock_write_unlock(&socket_globals.lock);
661 return EINPROGRESS;
662 }
663 // request close
664 ERROR_PROPAGATE((int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE, (ipcarg_t) socket->socket_id, 0, socket->service));
665 // free the socket structure
666 socket_destroy(socket);
667 fibril_rwlock_write_unlock(&socket_globals.lock);
668 return EOK;
669}
670
671void socket_destroy(socket_ref socket){
672 int accepted_id;
673
674 // destroy all accepted sockets
675 while((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0){
676 socket_destroy(sockets_find(socket_get_sockets(), accepted_id));
677 }
678 dyn_fifo_destroy(&socket->received);
679 dyn_fifo_destroy(&socket->accepted);
680 sockets_exclude(socket_get_sockets(), socket->socket_id);
681}
682
683int send(int socket_id, void * data, size_t datalength, int flags){
684 // without the address
685 return sendto_core(NET_SOCKET_SEND, socket_id, data, datalength, flags, NULL, 0);
686}
687
688int sendto(int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
689 if(! toaddr){
690 return EDESTADDRREQ;
691 }
692 if(! addrlen){
693 return EDESTADDRREQ;
694 }
695 // with the address
696 return sendto_core(NET_SOCKET_SENDTO, socket_id, data, datalength, flags, toaddr, addrlen);
697}
698
699int sendto_core(ipcarg_t message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
700 socket_ref socket;
701 aid_t message_id;
702 ipcarg_t result;
703 size_t fragments;
704 ipc_call_t answer;
705
706 if(! data){
707 return EBADMEM;
708 }
709 if(! datalength){
710 return NO_DATA;
711 }
712 fibril_rwlock_read_lock(&socket_globals.lock);
713 // find socket
714 socket = sockets_find(socket_get_sockets(), socket_id);
715 if(! socket){
716 fibril_rwlock_read_unlock(&socket_globals.lock);
717 return ENOTSOCK;
718 }
719 fibril_rwlock_read_lock(&socket->sending_lock);
720 // compute data fragment count
721 if(socket->data_fragment_size > 0){
722 fragments = (datalength + socket->header_size) / socket->data_fragment_size;
723 if((datalength + socket->header_size) % socket->data_fragment_size) ++ fragments;
724 }else{
725 fragments = 1;
726 }
727 // request send
728 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);
729 // send the address if given
730 if((! toaddr) || (async_data_write_start(socket->phone, toaddr, addrlen) == EOK)){
731 if(fragments == 1){
732 // send all if only one fragment
733 async_data_write_start(socket->phone, data, datalength);
734 }else{
735 // send the first fragment
736 async_data_write_start(socket->phone, data, socket->data_fragment_size - socket->header_size);
737 data = ((const uint8_t *) data) + socket->data_fragment_size - socket->header_size;
738 // send the middle fragments
739 while((-- fragments) > 1){
740 async_data_write_start(socket->phone, data, socket->data_fragment_size);
741 data = ((const uint8_t *) data) + socket->data_fragment_size;
742 }
743 // send the last fragment
744 async_data_write_start(socket->phone, data, (datalength + socket->header_size) % socket->data_fragment_size);
745 }
746 }
747 async_wait_for(message_id, &result);
748 if((SOCKET_GET_DATA_FRAGMENT_SIZE(answer) > 0)
749 && (SOCKET_GET_DATA_FRAGMENT_SIZE(answer) != socket->data_fragment_size)){
750 // set the data fragment size
751 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
752 }
753 fibril_rwlock_read_unlock(&socket->sending_lock);
754 fibril_rwlock_read_unlock(&socket_globals.lock);
755 return (int) result;
756}
757
758int recv(int socket_id, void * data, size_t datalength, int flags){
759 // without the address
760 return recvfrom_core(NET_SOCKET_RECV, socket_id, data, datalength, flags, NULL, NULL);
761}
762
763int recvfrom(int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
764 if(! fromaddr){
765 return EBADMEM;
766 }
767 if(! addrlen){
768 return NO_DATA;
769 }
770 // with the address
771 return recvfrom_core(NET_SOCKET_RECVFROM, socket_id, data, datalength, flags, fromaddr, addrlen);
772}
773
774int recvfrom_core(ipcarg_t message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
775 socket_ref socket;
776 aid_t message_id;
777 ipcarg_t ipc_result;
778 int result;
779 size_t fragments;
780 size_t * lengths;
781 size_t index;
782 ipc_call_t answer;
783
784 if(! data){
785 return EBADMEM;
786 }
787 if(! datalength){
788 return NO_DATA;
789 }
790 if(fromaddr && (! addrlen)){
791 return EINVAL;
792 }
793 fibril_rwlock_read_lock(&socket_globals.lock);
794 // find the socket
795 socket = sockets_find(socket_get_sockets(), socket_id);
796 if(! socket){
797 fibril_rwlock_read_unlock(&socket_globals.lock);
798 return ENOTSOCK;
799 }
800 fibril_mutex_lock(&socket->receive_lock);
801 // wait for a received packet
802 ++ socket->blocked;
803 while((result = dyn_fifo_value(&socket->received)) <= 0){
804 fibril_rwlock_read_unlock(&socket_globals.lock);
805 fibril_condvar_wait(&socket->receive_signal, &socket->receive_lock);
806 // drop the receive lock to avoid deadlock
807 fibril_mutex_unlock(&socket->receive_lock);
808 fibril_rwlock_read_lock(&socket_globals.lock);
809 fibril_mutex_lock(&socket->receive_lock);
810 }
811 -- socket->blocked;
812 fragments = (size_t) result;
813 // prepare lengths if more fragments
814 if(fragments > 1){
815 lengths = (size_t *) malloc(sizeof(size_t) * fragments + sizeof(size_t));
816 if(! lengths){
817 fibril_mutex_unlock(&socket->receive_lock);
818 fibril_rwlock_read_unlock(&socket_globals.lock);
819 return ENOMEM;
820 }
821 // request packet data
822 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
823 // read the address if desired
824 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
825 // read the fragment lengths
826 if(async_data_read_start(socket->phone, lengths, sizeof(int) * (fragments + 1)) == EOK){
827 if(lengths[fragments] <= datalength){
828 // read all fragments if long enough
829 for(index = 0; index < fragments; ++ index){
830 async_data_read_start(socket->phone, data, lengths[index]);
831 data = ((uint8_t *) data) + lengths[index];
832 }
833 }
834 }
835 }
836 free(lengths);
837 }else{
838 // request packet data
839 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
840 // read the address if desired
841 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
842 // read all if only one fragment
843 async_data_read_start(socket->phone, data, datalength);
844 }
845 }
846 async_wait_for(message_id, &ipc_result);
847 result = (int) ipc_result;
848 // if successful
849 if(result == EOK){
850 // dequeue the received packet
851 dyn_fifo_pop(&socket->received);
852 // return read data length
853 result = SOCKET_GET_READ_DATA_LENGTH(answer);
854 // set address length
855 if(fromaddr && addrlen){
856 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
857 }
858 }
859 fibril_mutex_unlock(&socket->receive_lock);
860 fibril_rwlock_read_unlock(&socket_globals.lock);
861 return result;
862}
863
864int getsockopt(int socket_id, int level, int optname, void * value, size_t * optlen){
865 socket_ref socket;
866 aid_t message_id;
867 ipcarg_t result;
868
869 if(!(value && optlen)){
870 return EBADMEM;
871 }
872 if(!(*optlen)){
873 return NO_DATA;
874 }
875 fibril_rwlock_read_lock(&socket_globals.lock);
876 // find the socket
877 socket = sockets_find(socket_get_sockets(), socket_id);
878 if(! socket){
879 fibril_rwlock_read_unlock(&socket_globals.lock);
880 return ENOTSOCK;
881 }
882 // request option value
883 message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT, (ipcarg_t) socket->socket_id, (ipcarg_t) optname, socket->service, NULL);
884 // read the length
885 if(async_data_read_start(socket->phone, optlen, sizeof(*optlen)) == EOK){
886 // read the value
887 async_data_read_start(socket->phone, value, * optlen);
888 }
889 fibril_rwlock_read_unlock(&socket_globals.lock);
890 async_wait_for(message_id, &result);
891 return (int) result;
892}
893
894int setsockopt(int socket_id, int level, int optname, const void * value, size_t optlen){
895 // send the value
896 return socket_send_data(socket_id, NET_SOCKET_SETSOCKOPT, (ipcarg_t) optname, value, optlen);
897
898}
899
900/** @}
901 */
Note: See TracBrowser for help on using the repository browser.