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

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

Coding style (no functional change)

  • Property mode set to 100644
File size: 27.1 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 fibril_rwlock_write_lock(&socket_globals.lock);
583 }
584 -- socket->blocked;
585
586 // create a new scoket
587 new_socket = (socket_ref) malloc(sizeof(socket_t));
588 if(! new_socket){
589 fibril_mutex_unlock(&socket->accept_lock);
590 fibril_rwlock_write_unlock(&socket_globals.lock);
591 return ENOMEM;
592 }
593 bzero(new_socket, sizeof(*new_socket));
594 socket_id = socket_generate_new_id();
595 if(socket_id <= 0){
596 fibril_mutex_unlock(&socket->accept_lock);
597 fibril_rwlock_write_unlock(&socket_globals.lock);
598 free(new_socket);
599 return socket_id;
600 }
601 socket_initialize(new_socket, socket_id, socket->phone, socket->service);
602 result = sockets_add(socket_get_sockets(), new_socket->socket_id, new_socket);
603 if(result < 0){
604 fibril_mutex_unlock(&socket->accept_lock);
605 fibril_rwlock_write_unlock(&socket_globals.lock);
606 free(new_socket);
607 return result;
608 }
609
610 // request accept
611 message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT, (ipcarg_t) socket->socket_id, 0, socket->service, 0, new_socket->socket_id, &answer);
612 // read address
613 ipc_data_read_start(socket->phone, cliaddr, * addrlen);
614 fibril_rwlock_write_unlock(&socket_globals.lock);
615 async_wait_for(message_id, &ipc_result);
616 result = (int) ipc_result;
617 if(result > 0){
618 if(result != socket_id){
619 result = EINVAL;
620 }
621 // dequeue the accepted socket if successful
622 dyn_fifo_pop(&socket->accepted);
623 // set address length
624 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
625 new_socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
626 }else if(result == ENOTSOCK){
627 // empty the queue if no accepted sockets
628 while(dyn_fifo_pop(&socket->accepted) > 0);
629 }
630 fibril_mutex_unlock(&socket->accept_lock);
631 return result;
632}
633
634int connect(int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen){
635 if(! serv_addr){
636 return EDESTADDRREQ;
637 }
638 if(! addrlen){
639 return EDESTADDRREQ;
640 }
641 // send the address
642 return socket_send_data(socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen);
643}
644
645int closesocket(int socket_id){
646 ERROR_DECLARE;
647
648 socket_ref socket;
649
650 fibril_rwlock_write_lock(&socket_globals.lock);
651 socket = sockets_find(socket_get_sockets(), socket_id);
652 if(! socket){
653 fibril_rwlock_write_unlock(&socket_globals.lock);
654 return ENOTSOCK;
655 }
656 if(socket->blocked){
657 fibril_rwlock_write_unlock(&socket_globals.lock);
658 return EINPROGRESS;
659 }
660 // request close
661 ERROR_PROPAGATE((int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE, (ipcarg_t) socket->socket_id, 0, socket->service));
662 // free the socket structure
663 socket_destroy(socket);
664 fibril_rwlock_write_unlock(&socket_globals.lock);
665 return EOK;
666}
667
668void socket_destroy(socket_ref socket){
669 int accepted_id;
670
671 // destroy all accepted sockets
672 while((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0){
673 socket_destroy(sockets_find(socket_get_sockets(), accepted_id));
674 }
675 dyn_fifo_destroy(&socket->received);
676 dyn_fifo_destroy(&socket->accepted);
677 sockets_exclude(socket_get_sockets(), socket->socket_id);
678}
679
680int send(int socket_id, void * data, size_t datalength, int flags){
681 // without the address
682 return sendto_core(NET_SOCKET_SEND, socket_id, data, datalength, flags, NULL, 0);
683}
684
685int sendto(int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
686 if(! toaddr){
687 return EDESTADDRREQ;
688 }
689 if(! addrlen){
690 return EDESTADDRREQ;
691 }
692 // with the address
693 return sendto_core(NET_SOCKET_SENDTO, socket_id, data, datalength, flags, toaddr, addrlen);
694}
695
696int sendto_core(ipcarg_t message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen){
697 socket_ref socket;
698 aid_t message_id;
699 ipcarg_t result;
700 size_t fragments;
701 ipc_call_t answer;
702
703 if(! data){
704 return EBADMEM;
705 }
706 if(! datalength){
707 return NO_DATA;
708 }
709 fibril_rwlock_read_lock(&socket_globals.lock);
710 // find socket
711 socket = sockets_find(socket_get_sockets(), socket_id);
712 if(! socket){
713 fibril_rwlock_read_unlock(&socket_globals.lock);
714 return ENOTSOCK;
715 }
716 fibril_rwlock_read_lock(&socket->sending_lock);
717 // compute data fragment count
718 if(socket->data_fragment_size > 0){
719 fragments = (datalength + socket->header_size) / socket->data_fragment_size;
720 if((datalength + socket->header_size) % socket->data_fragment_size) ++ fragments;
721 }else{
722 fragments = 1;
723 }
724 // request send
725 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);
726 // send the address if given
727 if((! toaddr) || (async_data_write_start(socket->phone, toaddr, addrlen) == EOK)){
728 if(fragments == 1){
729 // send all if only one fragment
730 async_data_write_start(socket->phone, data, datalength);
731 }else{
732 // send the first fragment
733 async_data_write_start(socket->phone, data, socket->data_fragment_size - socket->header_size);
734 data = ((const uint8_t *) data) + socket->data_fragment_size - socket->header_size;
735 // send the middle fragments
736 while((-- fragments) > 1){
737 async_data_write_start(socket->phone, data, socket->data_fragment_size);
738 data = ((const uint8_t *) data) + socket->data_fragment_size;
739 }
740 // send the last fragment
741 async_data_write_start(socket->phone, data, (datalength + socket->header_size) % socket->data_fragment_size);
742 }
743 }
744 async_wait_for(message_id, &result);
745 if((SOCKET_GET_DATA_FRAGMENT_SIZE(answer) > 0)
746 && (SOCKET_GET_DATA_FRAGMENT_SIZE(answer) != socket->data_fragment_size)){
747 // set the data fragment size
748 socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
749 }
750 fibril_rwlock_read_unlock(&socket->sending_lock);
751 fibril_rwlock_read_unlock(&socket_globals.lock);
752 return (int) result;
753}
754
755int recv(int socket_id, void * data, size_t datalength, int flags){
756 // without the address
757 return recvfrom_core(NET_SOCKET_RECV, socket_id, data, datalength, flags, NULL, NULL);
758}
759
760int recvfrom(int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
761 if(! fromaddr){
762 return EBADMEM;
763 }
764 if(! addrlen){
765 return NO_DATA;
766 }
767 // with the address
768 return recvfrom_core(NET_SOCKET_RECVFROM, socket_id, data, datalength, flags, fromaddr, addrlen);
769}
770
771int recvfrom_core(ipcarg_t message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen){
772 socket_ref socket;
773 aid_t message_id;
774 ipcarg_t ipc_result;
775 int result;
776 size_t fragments;
777 size_t * lengths;
778 size_t index;
779 ipc_call_t answer;
780
781 if(! data){
782 return EBADMEM;
783 }
784 if(! datalength){
785 return NO_DATA;
786 }
787 if(fromaddr && (! addrlen)){
788 return EINVAL;
789 }
790 fibril_rwlock_read_lock(&socket_globals.lock);
791 // find the socket
792 socket = sockets_find(socket_get_sockets(), socket_id);
793 if(! socket){
794 fibril_rwlock_read_unlock(&socket_globals.lock);
795 return ENOTSOCK;
796 }
797 fibril_mutex_lock(&socket->receive_lock);
798 // wait for a received packet
799 ++ socket->blocked;
800 while((result = dyn_fifo_value(&socket->received)) <= 0){
801 fibril_rwlock_read_unlock(&socket_globals.lock);
802 fibril_condvar_wait(&socket->receive_signal, &socket->receive_lock);
803 fibril_rwlock_read_lock(&socket_globals.lock);
804 }
805 -- socket->blocked;
806 fragments = (size_t) result;
807 // prepare lengths if more fragments
808 if(fragments > 1){
809 lengths = (size_t *) malloc(sizeof(size_t) * fragments + sizeof(size_t));
810 if(! lengths){
811 fibril_mutex_unlock(&socket->receive_lock);
812 fibril_rwlock_read_unlock(&socket_globals.lock);
813 return ENOMEM;
814 }
815 // request packet data
816 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
817 // read the address if desired
818 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
819 // read the fragment lengths
820 if(async_data_read_start(socket->phone, lengths, sizeof(int) * (fragments + 1)) == EOK){
821 if(lengths[fragments] <= datalength){
822 // read all fragments if long enough
823 for(index = 0; index < fragments; ++ index){
824 async_data_read_start(socket->phone, data, lengths[index]);
825 data = ((uint8_t *) data) + lengths[index];
826 }
827 }
828 }
829 }
830 free(lengths);
831 }else{
832 // request packet data
833 message_id = async_send_4(socket->phone, message, (ipcarg_t) socket->socket_id, 0, socket->service, (ipcarg_t) flags, &answer);
834 // read the address if desired
835 if((! fromaddr) || (async_data_read_start(socket->phone, fromaddr, * addrlen) == EOK)){
836 // read all if only one fragment
837 async_data_read_start(socket->phone, data, datalength);
838 }
839 }
840 async_wait_for(message_id, &ipc_result);
841 result = (int) ipc_result;
842 // if successful
843 if(result == EOK){
844 // dequeue the received packet
845 dyn_fifo_pop(&socket->received);
846 // return read data length
847 result = SOCKET_GET_READ_DATA_LENGTH(answer);
848 // set address length
849 if(fromaddr && addrlen){
850 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
851 }
852 }
853 fibril_mutex_unlock(&socket->receive_lock);
854 fibril_rwlock_read_unlock(&socket_globals.lock);
855 return result;
856}
857
858int getsockopt(int socket_id, int level, int optname, void * value, size_t * optlen){
859 socket_ref socket;
860 aid_t message_id;
861 ipcarg_t result;
862
863 if(!(value && optlen)){
864 return EBADMEM;
865 }
866 if(!(*optlen)){
867 return NO_DATA;
868 }
869 fibril_rwlock_read_lock(&socket_globals.lock);
870 // find the socket
871 socket = sockets_find(socket_get_sockets(), socket_id);
872 if(! socket){
873 fibril_rwlock_read_unlock(&socket_globals.lock);
874 return ENOTSOCK;
875 }
876 // request option value
877 message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT, (ipcarg_t) socket->socket_id, (ipcarg_t) optname, socket->service, NULL);
878 // read the length
879 if(async_data_read_start(socket->phone, optlen, sizeof(*optlen)) == EOK){
880 // read the value
881 async_data_read_start(socket->phone, value, * optlen);
882 }
883 fibril_rwlock_read_unlock(&socket_globals.lock);
884 async_wait_for(message_id, &result);
885 return (int) result;
886}
887
888int setsockopt(int socket_id, int level, int optname, const void * value, size_t optlen){
889 // send the value
890 return socket_send_data(socket_id, NET_SOCKET_SETSOCKOPT, (ipcarg_t) optname, value, optlen);
891
892}
893
894/** @}
895 */
Note: See TracBrowser for help on using the repository browser.