xref: /haiku/src/add-ons/kernel/network/stack/net_socket.cpp (revision 37fedaf8494b34aad811abcc49e79aa32943f880)
1 /*
2  * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include "stack_private.h"
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/ioctl.h>
15 #include <sys/time.h>
16 
17 #include <new>
18 
19 #include <Drivers.h>
20 #include <KernelExport.h>
21 #include <Select.h>
22 
23 #include <AutoDeleter.h>
24 #include <team.h>
25 #include <util/AutoLock.h>
26 #include <util/list.h>
27 #include <WeakReferenceable.h>
28 
29 #include <fs/select_sync_pool.h>
30 #include <kernel.h>
31 
32 #include <net_protocol.h>
33 #include <net_stack.h>
34 #include <net_stat.h>
35 
36 #include "ancillary_data.h"
37 #include "utility.h"
38 
39 
40 //#define TRACE_SOCKET
41 #ifdef TRACE_SOCKET
42 #	define TRACE(x...) dprintf(STACK_DEBUG_PREFIX x)
43 #else
44 #	define TRACE(x...) ;
45 #endif
46 
47 
48 struct net_socket_private;
49 typedef DoublyLinkedList<net_socket_private> SocketList;
50 
51 struct net_socket_private : net_socket,
52 		DoublyLinkedListLinkImpl<net_socket_private>,
53 		BWeakReferenceable {
54 	net_socket_private();
55 	~net_socket_private();
56 
57 	void RemoveFromParent();
58 
59 	BWeakReference<net_socket_private> parent;
60 	team_id						owner;
61 	uint32						max_backlog;
62 	uint32						child_count;
63 	SocketList					pending_children;
64 	SocketList					connected_children;
65 
66 	struct select_sync_pool*	select_pool;
67 	mutex						lock;
68 
69 	bool						is_connected;
70 	bool						is_in_socket_list;
71 };
72 
73 
74 int socket_bind(net_socket* socket, const struct sockaddr* address,
75 	socklen_t addressLength);
76 int socket_setsockopt(net_socket* socket, int level, int option,
77 	const void* value, int length);
78 ssize_t socket_read_avail(net_socket* socket);
79 
80 static SocketList sSocketList;
81 static mutex sSocketLock;
82 
83 
84 net_socket_private::net_socket_private()
85 	:
86 	owner(-1),
87 	max_backlog(0),
88 	child_count(0),
89 	select_pool(NULL),
90 	is_connected(false),
91 	is_in_socket_list(false)
92 {
93 	first_protocol = NULL;
94 	first_info = NULL;
95 	options = 0;
96 	linger = 0;
97 	bound_to_device = 0;
98 	error = 0;
99 
100 	address.ss_len = 0;
101 	peer.ss_len = 0;
102 
103 	mutex_init(&lock, "socket");
104 
105 	// set defaults (may be overridden by the protocols)
106 	send.buffer_size = 65535;
107 	send.low_water_mark = 1;
108 	send.timeout = B_INFINITE_TIMEOUT;
109 	receive.buffer_size = 65535;
110 	receive.low_water_mark = 1;
111 	receive.timeout = B_INFINITE_TIMEOUT;
112 }
113 
114 
115 net_socket_private::~net_socket_private()
116 {
117 	TRACE("delete net_socket %p\n", this);
118 
119 	if (parent != NULL)
120 		panic("socket still has a parent!");
121 
122 	if (is_in_socket_list) {
123 		MutexLocker _(sSocketLock);
124 		sSocketList.Remove(this);
125 	}
126 
127 	mutex_lock(&lock);
128 
129 	// also delete all children of this socket
130 	while (net_socket_private* child = pending_children.RemoveHead()) {
131 		child->RemoveFromParent();
132 	}
133 	while (net_socket_private* child = connected_children.RemoveHead()) {
134 		child->RemoveFromParent();
135 	}
136 
137 	mutex_unlock(&lock);
138 
139 	put_domain_protocols(this);
140 
141 	mutex_destroy(&lock);
142 }
143 
144 
145 void
146 net_socket_private::RemoveFromParent()
147 {
148 	ASSERT(!is_in_socket_list && parent != NULL);
149 
150 	parent = NULL;
151 
152 	mutex_lock(&sSocketLock);
153 	sSocketList.Add(this);
154 	mutex_unlock(&sSocketLock);
155 
156 	is_in_socket_list = true;
157 
158 	ReleaseReference();
159 }
160 
161 
162 //	#pragma mark -
163 
164 
165 static size_t
166 compute_user_iovec_length(iovec* userVec, uint32 count)
167 {
168 	size_t length = 0;
169 
170 	for (uint32 i = 0; i < count; i++) {
171 		iovec vec;
172 		if (user_memcpy(&vec, userVec + i, sizeof(iovec)) < B_OK)
173 			return 0;
174 
175 		length += vec.iov_len;
176 	}
177 
178 	return length;
179 }
180 
181 
182 static status_t
183 create_socket(int family, int type, int protocol, net_socket_private** _socket)
184 {
185 	struct net_socket_private* socket = new(std::nothrow) net_socket_private;
186 	if (socket == NULL)
187 		return B_NO_MEMORY;
188 	status_t status = socket->InitCheck();
189 	if (status != B_OK) {
190 		delete socket;
191 		return status;
192 	}
193 
194 	socket->family = family;
195 	socket->type = type;
196 	socket->protocol = protocol;
197 
198 	status = get_domain_protocols(socket);
199 	if (status != B_OK) {
200 		delete socket;
201 		return status;
202 	}
203 
204 	TRACE("create net_socket %p (%u.%u.%u):\n", socket, socket->family,
205 		socket->type, socket->protocol);
206 
207 #ifdef TRACE_SOCKET
208 	net_protocol* current = socket->first_protocol;
209 	for (int i = 0; current != NULL; current = current->next, i++)
210 		TRACE("  [%d] %p  %s\n", i, current, current->module->info.name);
211 #endif
212 
213 	*_socket = socket;
214 	return B_OK;
215 }
216 
217 
218 static status_t
219 add_ancillary_data(net_socket* socket, ancillary_data_container* container,
220 	void* data, size_t dataLen)
221 {
222 	cmsghdr* header = (cmsghdr*)data;
223 
224 	while (dataLen > 0) {
225 		if (header->cmsg_len < sizeof(cmsghdr) || header->cmsg_len > dataLen)
226 			return B_BAD_VALUE;
227 
228 		if (socket->first_info->add_ancillary_data == NULL)
229 			return B_NOT_SUPPORTED;
230 
231 		status_t status = socket->first_info->add_ancillary_data(
232 			socket->first_protocol, container, header);
233 		if (status != B_OK)
234 			return status;
235 
236 		dataLen -= _ALIGN(header->cmsg_len);
237 		header = (cmsghdr*)((uint8*)header + _ALIGN(header->cmsg_len));
238 	}
239 
240 	return B_OK;
241 }
242 
243 
244 static status_t
245 process_ancillary_data(net_socket* socket, ancillary_data_container* container,
246 	msghdr* messageHeader)
247 {
248 	uint8* dataBuffer = (uint8*)messageHeader->msg_control;
249 	int dataBufferLen = messageHeader->msg_controllen;
250 
251 	if (container == NULL || dataBuffer == NULL) {
252 		messageHeader->msg_controllen = 0;
253 		return B_OK;
254 	}
255 
256 	ancillary_data_header header;
257 	void* data = NULL;
258 
259 	while ((data = next_ancillary_data(container, data, &header)) != NULL) {
260 		if (socket->first_info->process_ancillary_data == NULL)
261 			return B_NOT_SUPPORTED;
262 
263 		ssize_t bytesWritten = socket->first_info->process_ancillary_data(
264 			socket->first_protocol, &header, data, dataBuffer, dataBufferLen);
265 		if (bytesWritten < 0)
266 			return bytesWritten;
267 
268 		dataBuffer += bytesWritten;
269 		dataBufferLen -= bytesWritten;
270 	}
271 
272 	messageHeader->msg_controllen -= dataBufferLen;
273 
274 	return B_OK;
275 }
276 
277 
278 static status_t
279 process_ancillary_data(net_socket* socket,
280 	net_buffer* buffer, msghdr* messageHeader)
281 {
282 	void *dataBuffer = messageHeader->msg_control;
283 	ssize_t bytesWritten;
284 
285 	if (dataBuffer == NULL) {
286 		messageHeader->msg_controllen = 0;
287 		return B_OK;
288 	}
289 
290 	if (socket->first_info->process_ancillary_data_no_container == NULL)
291 		return B_NOT_SUPPORTED;
292 
293 	bytesWritten = socket->first_info->process_ancillary_data_no_container(
294 		socket->first_protocol, buffer, dataBuffer,
295 		messageHeader->msg_controllen);
296 	if (bytesWritten < 0)
297 		return bytesWritten;
298 	messageHeader->msg_controllen = bytesWritten;
299 
300 	return B_OK;
301 }
302 
303 
304 static ssize_t
305 socket_receive_no_buffer(net_socket* socket, msghdr* header, void* data,
306 	size_t length, int flags)
307 {
308 	iovec stackVec = { data, length };
309 	iovec* vecs = header ? header->msg_iov : &stackVec;
310 	int vecCount = header ? header->msg_iovlen : 1;
311 	sockaddr* address = header ? (sockaddr*)header->msg_name : NULL;
312 	socklen_t* addressLen = header ? &header->msg_namelen : NULL;
313 
314 	ancillary_data_container* ancillaryData = NULL;
315 	ssize_t bytesRead = socket->first_info->read_data_no_buffer(
316 		socket->first_protocol, vecs, vecCount, &ancillaryData, address,
317 		addressLen);
318 	if (bytesRead < 0)
319 		return bytesRead;
320 
321 	CObjectDeleter<ancillary_data_container> ancillaryDataDeleter(ancillaryData,
322 		&delete_ancillary_data_container);
323 
324 	// process ancillary data
325 	if (header != NULL) {
326 		status_t status = process_ancillary_data(socket, ancillaryData, header);
327 		if (status != B_OK)
328 			return status;
329 
330 		header->msg_flags = 0;
331 	}
332 
333 	return bytesRead;
334 }
335 
336 
337 #if ENABLE_DEBUGGER_COMMANDS
338 
339 
340 static void
341 print_socket_line(net_socket_private* socket, const char* prefix)
342 {
343 	BReference<net_socket_private> parent = socket->parent.GetReference();
344 	kprintf("%s%p %2d.%2d.%2d %6" B_PRId32 " %p %p  %p%s\n", prefix, socket,
345 		socket->family, socket->type, socket->protocol, socket->owner,
346 		socket->first_protocol, socket->first_info, parent.Get(),
347 		parent.Get() != NULL ? socket->is_connected ? " (c)" : " (p)" : "");
348 }
349 
350 
351 static int
352 dump_socket(int argc, char** argv)
353 {
354 	if (argc < 2) {
355 		kprintf("usage: %s [address]\n", argv[0]);
356 		return 0;
357 	}
358 
359 	net_socket_private* socket = (net_socket_private*)parse_expression(argv[1]);
360 
361 	kprintf("SOCKET %p\n", socket);
362 	kprintf("  family.type.protocol: %d.%d.%d\n",
363 		socket->family, socket->type, socket->protocol);
364 	BReference<net_socket_private> parent = socket->parent.GetReference();
365 	kprintf("  parent:               %p\n", parent.Get());
366 	kprintf("  first protocol:       %p\n", socket->first_protocol);
367 	kprintf("  first module_info:    %p\n", socket->first_info);
368 	kprintf("  options:              %x\n", socket->options);
369 	kprintf("  linger:               %d\n", socket->linger);
370 	kprintf("  bound to device:      %" B_PRIu32 "\n", socket->bound_to_device);
371 	kprintf("  owner:                %" B_PRId32 "\n", socket->owner);
372 	kprintf("  max backlog:          %" B_PRId32 "\n", socket->max_backlog);
373 	kprintf("  is connected:         %d\n", socket->is_connected);
374 	kprintf("  child_count:          %" B_PRIu32 "\n", socket->child_count);
375 
376 	if (socket->child_count == 0)
377 		return 0;
378 
379 	kprintf("    pending children:\n");
380 	SocketList::Iterator iterator = socket->pending_children.GetIterator();
381 	while (net_socket_private* child = iterator.Next()) {
382 		print_socket_line(child, "      ");
383 	}
384 
385 	kprintf("    connected children:\n");
386 	iterator = socket->connected_children.GetIterator();
387 	while (net_socket_private* child = iterator.Next()) {
388 		print_socket_line(child, "      ");
389 	}
390 
391 	return 0;
392 }
393 
394 
395 static int
396 dump_sockets(int argc, char** argv)
397 {
398 	kprintf("address        kind  owner protocol   module_info parent\n");
399 
400 	SocketList::Iterator iterator = sSocketList.GetIterator();
401 	while (net_socket_private* socket = iterator.Next()) {
402 		print_socket_line(socket, "");
403 
404 		SocketList::Iterator childIterator
405 			= socket->pending_children.GetIterator();
406 		while (net_socket_private* child = childIterator.Next()) {
407 			print_socket_line(child, " ");
408 		}
409 
410 		childIterator = socket->connected_children.GetIterator();
411 		while (net_socket_private* child = childIterator.Next()) {
412 			print_socket_line(child, " ");
413 		}
414 	}
415 
416 	return 0;
417 }
418 
419 
420 #endif	// ENABLE_DEBUGGER_COMMANDS
421 
422 
423 //	#pragma mark -
424 
425 
426 status_t
427 socket_open(int family, int type, int protocol, net_socket** _socket)
428 {
429 	net_socket_private* socket;
430 	status_t status = create_socket(family, type, protocol, &socket);
431 	if (status != B_OK)
432 		return status;
433 
434 	status = socket->first_info->open(socket->first_protocol);
435 	if (status != B_OK) {
436 		delete socket;
437 		return status;
438 	}
439 
440 	socket->owner = team_get_current_team_id();
441 	socket->is_in_socket_list = true;
442 
443 	mutex_lock(&sSocketLock);
444 	sSocketList.Add(socket);
445 	mutex_unlock(&sSocketLock);
446 
447 	*_socket = socket;
448 	return B_OK;
449 }
450 
451 
452 status_t
453 socket_close(net_socket* _socket)
454 {
455 	net_socket_private* socket = (net_socket_private*)_socket;
456 	return socket->first_info->close(socket->first_protocol);
457 }
458 
459 
460 void
461 socket_free(net_socket* _socket)
462 {
463 	net_socket_private* socket = (net_socket_private*)_socket;
464 	socket->first_info->free(socket->first_protocol);
465 	socket->ReleaseReference();
466 }
467 
468 
469 status_t
470 socket_readv(net_socket* socket, const iovec* vecs, size_t vecCount,
471 	size_t* _length)
472 {
473 	return -1;
474 }
475 
476 
477 status_t
478 socket_writev(net_socket* socket, const iovec* vecs, size_t vecCount,
479 	size_t* _length)
480 {
481 	if (socket->peer.ss_len == 0)
482 		return ECONNRESET;
483 
484 	if (socket->address.ss_len == 0) {
485 		// try to bind first
486 		status_t status = socket_bind(socket, NULL, 0);
487 		if (status != B_OK)
488 			return status;
489 	}
490 
491 	// TODO: useful, maybe even computed header space!
492 	net_buffer* buffer = gNetBufferModule.create(256);
493 	if (buffer == NULL)
494 		return ENOBUFS;
495 
496 	// copy data into buffer
497 
498 	for (uint32 i = 0; i < vecCount; i++) {
499 		if (gNetBufferModule.append(buffer, vecs[i].iov_base,
500 				vecs[i].iov_len) < B_OK) {
501 			gNetBufferModule.free(buffer);
502 			return ENOBUFS;
503 		}
504 	}
505 
506 	memcpy(buffer->source, &socket->address, socket->address.ss_len);
507 	memcpy(buffer->destination, &socket->peer, socket->peer.ss_len);
508 	size_t size = buffer->size;
509 
510 	ssize_t bytesWritten = socket->first_info->send_data(socket->first_protocol,
511 		buffer);
512 	if (bytesWritten < B_OK) {
513 		if (buffer->size != size) {
514 			// this appears to be a partial write
515 			*_length = size - buffer->size;
516 		}
517 		gNetBufferModule.free(buffer);
518 		return bytesWritten;
519 	}
520 
521 	*_length = bytesWritten;
522 	return B_OK;
523 }
524 
525 
526 status_t
527 socket_control(net_socket* socket, int32 op, void* data, size_t length)
528 {
529 	switch (op) {
530 		case FIONBIO:
531 		{
532 			if (data == NULL)
533 				return B_BAD_VALUE;
534 
535 			int value;
536 			if (is_syscall()) {
537 				if (!IS_USER_ADDRESS(data)
538 					|| user_memcpy(&value, data, sizeof(int)) != B_OK) {
539 					return B_BAD_ADDRESS;
540 				}
541 			} else
542 				value = *(int*)data;
543 
544 			return socket_setsockopt(socket, SOL_SOCKET, SO_NONBLOCK, &value,
545 				sizeof(int));
546 		}
547 
548 		case FIONREAD:
549 		{
550 			if (data == NULL)
551 				return B_BAD_VALUE;
552 
553 			int available = (int)socket_read_avail(socket);
554 			if (available < 0)
555 				return available;
556 
557 			if (is_syscall()) {
558 				if (!IS_USER_ADDRESS(data)
559 					|| user_memcpy(data, &available, sizeof(available))
560 						!= B_OK) {
561 					return B_BAD_ADDRESS;
562 				}
563 			} else
564 				*(int*)data = available;
565 
566 			return B_OK;
567 		}
568 
569 		case B_SET_BLOCKING_IO:
570 		case B_SET_NONBLOCKING_IO:
571 		{
572 			int value = op == B_SET_NONBLOCKING_IO;
573 			return socket_setsockopt(socket, SOL_SOCKET, SO_NONBLOCK, &value,
574 				sizeof(int));
575 		}
576 	}
577 
578 	return socket->first_info->control(socket->first_protocol,
579 		LEVEL_DRIVER_IOCTL, op, data, &length);
580 }
581 
582 
583 ssize_t
584 socket_read_avail(net_socket* socket)
585 {
586 	return socket->first_info->read_avail(socket->first_protocol);
587 }
588 
589 
590 ssize_t
591 socket_send_avail(net_socket* socket)
592 {
593 	return socket->first_info->send_avail(socket->first_protocol);
594 }
595 
596 
597 status_t
598 socket_send_data(net_socket* socket, net_buffer* buffer)
599 {
600 	return socket->first_info->send_data(socket->first_protocol,
601 		buffer);
602 }
603 
604 
605 status_t
606 socket_receive_data(net_socket* socket, size_t length, uint32 flags,
607 	net_buffer** _buffer)
608 {
609 	status_t status = socket->first_info->read_data(socket->first_protocol,
610 		length, flags, _buffer);
611 	if (status != B_OK)
612 		return status;
613 
614 	if (*_buffer && length < (*_buffer)->size) {
615 		// discard any data behind the amount requested
616 		gNetBufferModule.trim(*_buffer, length);
617 	}
618 
619 	return status;
620 }
621 
622 
623 status_t
624 socket_get_next_stat(uint32* _cookie, int family, struct net_stat* stat)
625 {
626 	MutexLocker locker(sSocketLock);
627 
628 	net_socket_private* socket = NULL;
629 	SocketList::Iterator iterator = sSocketList.GetIterator();
630 	uint32 cookie = *_cookie;
631 	uint32 count = 0;
632 
633 	while (true) {
634 		socket = iterator.Next();
635 		if (socket == NULL)
636 			return B_ENTRY_NOT_FOUND;
637 
638 		// TODO: also traverse the pending connections
639 		if (count == cookie)
640 			break;
641 
642 		if (family == -1 || family == socket->family)
643 			count++;
644 	}
645 
646 	*_cookie = count + 1;
647 
648 	stat->family = socket->family;
649 	stat->type = socket->type;
650 	stat->protocol = socket->protocol;
651 	stat->owner = socket->owner;
652 	stat->state[0] = '\0';
653 	memcpy(&stat->address, &socket->address, sizeof(struct sockaddr_storage));
654 	memcpy(&stat->peer, &socket->peer, sizeof(struct sockaddr_storage));
655 	stat->receive_queue_size = 0;
656 	stat->send_queue_size = 0;
657 
658 	// fill in protocol specific data (if supported by the protocol)
659 	size_t length = sizeof(net_stat);
660 	socket->first_info->control(socket->first_protocol, socket->protocol,
661 		NET_STAT_SOCKET, stat, &length);
662 
663 	return B_OK;
664 }
665 
666 
667 //	#pragma mark - connections
668 
669 
670 bool
671 socket_acquire(net_socket* _socket)
672 {
673 	net_socket_private* socket = (net_socket_private*)_socket;
674 
675 	// During destruction, the socket might still be accessible over its
676 	// endpoint protocol. We need to make sure the endpoint cannot acquire the
677 	// socket anymore -- while not obvious, the endpoint protocol is responsible
678 	// for the proper locking here.
679 	if (socket->CountReferences() == 0)
680 		return false;
681 
682 	socket->AcquireReference();
683 	return true;
684 }
685 
686 
687 bool
688 socket_release(net_socket* _socket)
689 {
690 	net_socket_private* socket = (net_socket_private*)_socket;
691 	return socket->ReleaseReference();
692 }
693 
694 
695 status_t
696 socket_spawn_pending(net_socket* _parent, net_socket** _socket)
697 {
698 	net_socket_private* parent = (net_socket_private*)_parent;
699 
700 	TRACE("%s(%p)\n", __FUNCTION__, parent);
701 
702 	MutexLocker locker(parent->lock);
703 
704 	// We actually accept more pending connections to compensate for those
705 	// that never complete, and also make sure at least a single connection
706 	// can always be accepted
707 	if (parent->child_count > 3 * parent->max_backlog / 2)
708 		return ENOBUFS;
709 
710 	net_socket_private* socket;
711 	status_t status = create_socket(parent->family, parent->type,
712 		parent->protocol, &socket);
713 	if (status != B_OK)
714 		return status;
715 
716 	// inherit parent's properties
717 	socket->send = parent->send;
718 	socket->receive = parent->receive;
719 	socket->options = parent->options & ~SO_ACCEPTCONN;
720 	socket->linger = parent->linger;
721 	socket->owner = parent->owner;
722 	memcpy(&socket->address, &parent->address, parent->address.ss_len);
723 	memcpy(&socket->peer, &parent->peer, parent->peer.ss_len);
724 
725 	// add to the parent's list of pending connections
726 	parent->pending_children.Add(socket);
727 	socket->parent = parent;
728 	parent->child_count++;
729 
730 	*_socket = socket;
731 	return B_OK;
732 }
733 
734 
735 /*!	Dequeues a connected child from a parent socket.
736 	It also returns a reference with the child socket.
737 */
738 status_t
739 socket_dequeue_connected(net_socket* _parent, net_socket** _socket)
740 {
741 	net_socket_private* parent = (net_socket_private*)_parent;
742 
743 	mutex_lock(&parent->lock);
744 
745 	net_socket_private* socket = parent->connected_children.RemoveHead();
746 	if (socket != NULL) {
747 		socket->AcquireReference();
748 		socket->RemoveFromParent();
749 		parent->child_count--;
750 		*_socket = socket;
751 	}
752 
753 	mutex_unlock(&parent->lock);
754 
755 	if (socket == NULL)
756 		return B_ENTRY_NOT_FOUND;
757 
758 	return B_OK;
759 }
760 
761 
762 ssize_t
763 socket_count_connected(net_socket* _parent)
764 {
765 	net_socket_private* parent = (net_socket_private*)_parent;
766 
767 	MutexLocker _(parent->lock);
768 	return parent->connected_children.Count();
769 }
770 
771 
772 status_t
773 socket_set_max_backlog(net_socket* _socket, uint32 backlog)
774 {
775 	net_socket_private* socket = (net_socket_private*)_socket;
776 
777 	// we enforce an upper limit of connections waiting to be accepted
778 	if (backlog > 256)
779 		backlog = 256;
780 
781 	MutexLocker _(socket->lock);
782 
783 	// first remove the pending connections, then the already connected
784 	// ones as needed
785 	net_socket_private* child;
786 	while (socket->child_count > backlog
787 		&& (child = socket->pending_children.RemoveTail()) != NULL) {
788 		child->RemoveFromParent();
789 		socket->child_count--;
790 	}
791 	while (socket->child_count > backlog
792 		&& (child = socket->connected_children.RemoveTail()) != NULL) {
793 		child->RemoveFromParent();
794 		socket->child_count--;
795 	}
796 
797 	socket->max_backlog = backlog;
798 	return B_OK;
799 }
800 
801 
802 /*!	Returns whether or not this socket has a parent. The parent might not be
803 	valid anymore, though.
804 */
805 bool
806 socket_has_parent(net_socket* _socket)
807 {
808 	net_socket_private* socket = (net_socket_private*)_socket;
809 	return socket->parent != NULL;
810 }
811 
812 
813 /*!	The socket has been connected. It will be moved to the connected queue
814 	of its parent socket.
815 */
816 status_t
817 socket_connected(net_socket* _socket)
818 {
819 	net_socket_private* socket = (net_socket_private*)_socket;
820 
821 	TRACE("socket_connected(%p)\n", socket);
822 
823 	BReference<net_socket_private> parent = socket->parent.GetReference();
824 	if (parent.Get() == NULL)
825 		return B_BAD_VALUE;
826 
827 	MutexLocker _(parent->lock);
828 
829 	parent->pending_children.Remove(socket);
830 	parent->connected_children.Add(socket);
831 	socket->is_connected = true;
832 
833 	// notify parent
834 	if (parent->select_pool)
835 		notify_select_event_pool(parent->select_pool, B_SELECT_READ);
836 
837 	return B_OK;
838 }
839 
840 
841 /*!	The socket has been aborted. Steals the parent's reference, and releases
842 	it.
843 */
844 status_t
845 socket_aborted(net_socket* _socket)
846 {
847 	net_socket_private* socket = (net_socket_private*)_socket;
848 
849 	TRACE("socket_aborted(%p)\n", socket);
850 
851 	BReference<net_socket_private> parent = socket->parent.GetReference();
852 	if (parent.Get() == NULL)
853 		return B_BAD_VALUE;
854 
855 	MutexLocker _(parent->lock);
856 
857 	if (socket->is_connected)
858 		parent->connected_children.Remove(socket);
859 	else
860 		parent->pending_children.Remove(socket);
861 
862 	parent->child_count--;
863 	socket->RemoveFromParent();
864 
865 	return B_OK;
866 }
867 
868 
869 //	#pragma mark - notifications
870 
871 
872 status_t
873 socket_request_notification(net_socket* _socket, uint8 event, selectsync* sync)
874 {
875 	net_socket_private* socket = (net_socket_private*)_socket;
876 
877 	mutex_lock(&socket->lock);
878 
879 	status_t status = add_select_sync_pool_entry(&socket->select_pool, sync,
880 		event);
881 
882 	mutex_unlock(&socket->lock);
883 
884 	if (status != B_OK)
885 		return status;
886 
887 	// check if the event is already present
888 	// TODO: add support for poll() types
889 
890 	switch (event) {
891 		case B_SELECT_READ:
892 		{
893 			ssize_t available = socket_read_avail(socket);
894 			if ((ssize_t)socket->receive.low_water_mark <= available
895 				|| available < B_OK)
896 				notify_select_event(sync, event);
897 			break;
898 		}
899 		case B_SELECT_WRITE:
900 		{
901 			ssize_t available = socket_send_avail(socket);
902 			if ((ssize_t)socket->send.low_water_mark <= available
903 				|| available < B_OK)
904 				notify_select_event(sync, event);
905 			break;
906 		}
907 		case B_SELECT_ERROR:
908 			// TODO: B_SELECT_ERROR condition!
909 			break;
910 	}
911 
912 	return B_OK;
913 }
914 
915 
916 status_t
917 socket_cancel_notification(net_socket* _socket, uint8 event, selectsync* sync)
918 {
919 	net_socket_private* socket = (net_socket_private*)_socket;
920 
921 	MutexLocker _(socket->lock);
922 	return remove_select_sync_pool_entry(&socket->select_pool, sync, event);
923 }
924 
925 
926 status_t
927 socket_notify(net_socket* _socket, uint8 event, int32 value)
928 {
929 	net_socket_private* socket = (net_socket_private*)_socket;
930 	bool notify = true;
931 
932 	switch (event) {
933 		case B_SELECT_READ:
934 			if ((ssize_t)socket->receive.low_water_mark > value
935 				&& value >= B_OK)
936 				notify = false;
937 			break;
938 
939 		case B_SELECT_WRITE:
940 			if ((ssize_t)socket->send.low_water_mark > value && value >= B_OK)
941 				notify = false;
942 			break;
943 
944 		case B_SELECT_ERROR:
945 			socket->error = value;
946 			break;
947 	}
948 
949 	MutexLocker _(socket->lock);
950 
951 	if (notify && socket->select_pool != NULL) {
952 		notify_select_event_pool(socket->select_pool, event);
953 
954 		if (event == B_SELECT_ERROR) {
955 			// always notify read/write on error
956 			notify_select_event_pool(socket->select_pool, B_SELECT_READ);
957 			notify_select_event_pool(socket->select_pool, B_SELECT_WRITE);
958 		}
959 	}
960 
961 	return B_OK;
962 }
963 
964 
965 //	#pragma mark - standard socket API
966 
967 
968 int
969 socket_accept(net_socket* socket, struct sockaddr* address,
970 	socklen_t* _addressLength, net_socket** _acceptedSocket)
971 {
972 	if ((socket->options & SO_ACCEPTCONN) == 0)
973 		return B_BAD_VALUE;
974 
975 	net_socket* accepted;
976 	status_t status = socket->first_info->accept(socket->first_protocol,
977 		&accepted);
978 	if (status != B_OK)
979 		return status;
980 
981 	if (address && *_addressLength > 0) {
982 		memcpy(address, &accepted->peer, min_c(*_addressLength,
983 			min_c(accepted->peer.ss_len, sizeof(sockaddr_storage))));
984 		*_addressLength = accepted->peer.ss_len;
985 	}
986 
987 	*_acceptedSocket = accepted;
988 	return B_OK;
989 }
990 
991 
992 int
993 socket_bind(net_socket* socket, const struct sockaddr* address,
994 	socklen_t addressLength)
995 {
996 	sockaddr empty;
997 	if (address == NULL) {
998 		// special - try to bind to an empty address, like INADDR_ANY
999 		memset(&empty, 0, sizeof(sockaddr));
1000 		empty.sa_len = sizeof(sockaddr);
1001 		empty.sa_family = socket->family;
1002 
1003 		address = &empty;
1004 		addressLength = sizeof(sockaddr);
1005 	}
1006 
1007 	if (socket->address.ss_len != 0) {
1008 		status_t status = socket->first_info->unbind(socket->first_protocol,
1009 			(sockaddr*)&socket->address);
1010 		if (status != B_OK)
1011 			return status;
1012 	}
1013 
1014 	memcpy(&socket->address, address, sizeof(sockaddr));
1015 	socket->address.ss_len = sizeof(sockaddr_storage);
1016 
1017 	status_t status = socket->first_info->bind(socket->first_protocol,
1018 		(sockaddr*)address);
1019 	if (status != B_OK) {
1020 		// clear address again, as binding failed
1021 		socket->address.ss_len = 0;
1022 	}
1023 
1024 	return status;
1025 }
1026 
1027 
1028 int
1029 socket_connect(net_socket* socket, const struct sockaddr* address,
1030 	socklen_t addressLength)
1031 {
1032 	if (address == NULL || addressLength == 0)
1033 		return ENETUNREACH;
1034 
1035 	if (socket->address.ss_len == 0) {
1036 		// try to bind first
1037 		status_t status = socket_bind(socket, NULL, 0);
1038 		if (status != B_OK)
1039 			return status;
1040 	}
1041 
1042 	return socket->first_info->connect(socket->first_protocol, address);
1043 }
1044 
1045 
1046 int
1047 socket_getpeername(net_socket* socket, struct sockaddr* address,
1048 	socklen_t* _addressLength)
1049 {
1050 	if (socket->peer.ss_len == 0)
1051 		return ENOTCONN;
1052 
1053 	memcpy(address, &socket->peer, min_c(*_addressLength, socket->peer.ss_len));
1054 	*_addressLength = socket->peer.ss_len;
1055 	return B_OK;
1056 }
1057 
1058 
1059 int
1060 socket_getsockname(net_socket* socket, struct sockaddr* address,
1061 	socklen_t* _addressLength)
1062 {
1063 	if (socket->address.ss_len == 0)
1064 		return ENOTCONN;
1065 
1066 	memcpy(address, &socket->address, min_c(*_addressLength,
1067 		socket->address.ss_len));
1068 	*_addressLength = socket->address.ss_len;
1069 	return B_OK;
1070 }
1071 
1072 
1073 status_t
1074 socket_get_option(net_socket* socket, int level, int option, void* value,
1075 	int* _length)
1076 {
1077 	if (level != SOL_SOCKET)
1078 		return ENOPROTOOPT;
1079 
1080 	switch (option) {
1081 		case SO_SNDBUF:
1082 		{
1083 			uint32* size = (uint32*)value;
1084 			*size = socket->send.buffer_size;
1085 			*_length = sizeof(uint32);
1086 			return B_OK;
1087 		}
1088 
1089 		case SO_RCVBUF:
1090 		{
1091 			uint32* size = (uint32*)value;
1092 			*size = socket->receive.buffer_size;
1093 			*_length = sizeof(uint32);
1094 			return B_OK;
1095 		}
1096 
1097 		case SO_SNDLOWAT:
1098 		{
1099 			uint32* size = (uint32*)value;
1100 			*size = socket->send.low_water_mark;
1101 			*_length = sizeof(uint32);
1102 			return B_OK;
1103 		}
1104 
1105 		case SO_RCVLOWAT:
1106 		{
1107 			uint32* size = (uint32*)value;
1108 			*size = socket->receive.low_water_mark;
1109 			*_length = sizeof(uint32);
1110 			return B_OK;
1111 		}
1112 
1113 		case SO_RCVTIMEO:
1114 		case SO_SNDTIMEO:
1115 		{
1116 			if (*_length < (int)sizeof(struct timeval))
1117 				return B_BAD_VALUE;
1118 
1119 			bigtime_t timeout;
1120 			if (option == SO_SNDTIMEO)
1121 				timeout = socket->send.timeout;
1122 			else
1123 				timeout = socket->receive.timeout;
1124 			if (timeout == B_INFINITE_TIMEOUT)
1125 				timeout = 0;
1126 
1127 			struct timeval* timeval = (struct timeval*)value;
1128 			timeval->tv_sec = timeout / 1000000LL;
1129 			timeval->tv_usec = timeout % 1000000LL;
1130 
1131 			*_length = sizeof(struct timeval);
1132 			return B_OK;
1133 		}
1134 
1135 		case SO_NONBLOCK:
1136 		{
1137 			int32* _set = (int32*)value;
1138 			*_set = socket->receive.timeout == 0 && socket->send.timeout == 0;
1139 			*_length = sizeof(int32);
1140 			return B_OK;
1141 		}
1142 
1143 		case SO_ACCEPTCONN:
1144 		case SO_BROADCAST:
1145 		case SO_DEBUG:
1146 		case SO_DONTROUTE:
1147 		case SO_KEEPALIVE:
1148 		case SO_OOBINLINE:
1149 		case SO_REUSEADDR:
1150 		case SO_REUSEPORT:
1151 		case SO_USELOOPBACK:
1152 		{
1153 			int32* _set = (int32*)value;
1154 			*_set = (socket->options & option) != 0;
1155 			*_length = sizeof(int32);
1156 			return B_OK;
1157 		}
1158 
1159 		case SO_TYPE:
1160 		{
1161 			int32* _set = (int32*)value;
1162 			*_set = socket->type;
1163 			*_length = sizeof(int32);
1164 			return B_OK;
1165 		}
1166 
1167 		case SO_ERROR:
1168 		{
1169 			int32* _set = (int32*)value;
1170 			*_set = socket->error;
1171 			*_length = sizeof(int32);
1172 
1173 			socket->error = B_OK;
1174 				// clear error upon retrieval
1175 			return B_OK;
1176 		}
1177 
1178 		default:
1179 			break;
1180 	}
1181 
1182 	dprintf("socket_getsockopt: unknown option %d\n", option);
1183 	return ENOPROTOOPT;
1184 }
1185 
1186 
1187 int
1188 socket_getsockopt(net_socket* socket, int level, int option, void* value,
1189 	int* _length)
1190 {
1191 	return socket->first_protocol->module->getsockopt(socket->first_protocol,
1192 		level, option, value, _length);
1193 }
1194 
1195 
1196 int
1197 socket_listen(net_socket* socket, int backlog)
1198 {
1199 	status_t status = socket->first_info->listen(socket->first_protocol,
1200 		backlog);
1201 	if (status == B_OK)
1202 		socket->options |= SO_ACCEPTCONN;
1203 
1204 	return status;
1205 }
1206 
1207 
1208 ssize_t
1209 socket_receive(net_socket* socket, msghdr* header, void* data, size_t length,
1210 	int flags)
1211 {
1212 	// If the protocol sports read_data_no_buffer() we use it.
1213 	if (socket->first_info->read_data_no_buffer != NULL)
1214 		return socket_receive_no_buffer(socket, header, data, length, flags);
1215 
1216 	size_t totalLength = length;
1217 	net_buffer* buffer;
1218 	int i;
1219 
1220 	// the convention to this function is that have header been
1221 	// present, { data, length } would have been iovec[0] and is
1222 	// always considered like that
1223 
1224 	if (header) {
1225 		// calculate the length considering all of the extra buffers
1226 		for (i = 1; i < header->msg_iovlen; i++)
1227 			totalLength += header->msg_iov[i].iov_len;
1228 	}
1229 
1230 	status_t status = socket->first_info->read_data(
1231 		socket->first_protocol, totalLength, flags, &buffer);
1232 	if (status != B_OK)
1233 		return status;
1234 
1235 	// process ancillary data
1236 	if (header != NULL) {
1237 		if (buffer != NULL && header->msg_control != NULL) {
1238 			ancillary_data_container* container
1239 				= gNetBufferModule.get_ancillary_data(buffer);
1240 			if (container != NULL)
1241 				status = process_ancillary_data(socket, container, header);
1242 			else
1243 				status = process_ancillary_data(socket, buffer, header);
1244 			if (status != B_OK) {
1245 				gNetBufferModule.free(buffer);
1246 				return status;
1247 			}
1248 		} else
1249 			header->msg_controllen = 0;
1250 	}
1251 
1252 	// TODO: - returning a NULL buffer when received 0 bytes
1253 	//         may not make much sense as we still need the address
1254 	//       - gNetBufferModule.read() uses memcpy() instead of user_memcpy
1255 
1256 	size_t nameLen = 0;
1257 
1258 	if (header) {
1259 		// TODO: - consider the control buffer options
1260 		nameLen = header->msg_namelen;
1261 		header->msg_namelen = 0;
1262 		header->msg_flags = 0;
1263 	}
1264 
1265 	if (buffer == NULL)
1266 		return 0;
1267 
1268 	size_t bytesReceived = buffer->size, bytesCopied = 0;
1269 
1270 	length = min_c(bytesReceived, length);
1271 	if (gNetBufferModule.read(buffer, 0, data, length) < B_OK) {
1272 		gNetBufferModule.free(buffer);
1273 		return ENOBUFS;
1274 	}
1275 
1276 	// if first copy was a success, proceed to following
1277 	// copies as required
1278 	bytesCopied += length;
1279 
1280 	if (header) {
1281 		// we only start considering at iovec[1]
1282 		// as { data, length } is iovec[0]
1283 		for (i = 1; i < header->msg_iovlen && bytesCopied < bytesReceived; i++) {
1284 			iovec& vec = header->msg_iov[i];
1285 			size_t toRead = min_c(bytesReceived - bytesCopied, vec.iov_len);
1286 			if (gNetBufferModule.read(buffer, bytesCopied, vec.iov_base,
1287 					toRead) < B_OK) {
1288 				break;
1289 			}
1290 
1291 			bytesCopied += toRead;
1292 		}
1293 
1294 		if (header->msg_name != NULL) {
1295 			header->msg_namelen = min_c(nameLen, buffer->source->sa_len);
1296 			memcpy(header->msg_name, buffer->source, header->msg_namelen);
1297 		}
1298 	}
1299 
1300 	gNetBufferModule.free(buffer);
1301 
1302 	if (bytesCopied < bytesReceived) {
1303 		if (header)
1304 			header->msg_flags = MSG_TRUNC;
1305 
1306 		if (flags & MSG_TRUNC)
1307 			return bytesReceived;
1308 	}
1309 
1310 	return bytesCopied;
1311 }
1312 
1313 
1314 ssize_t
1315 socket_send(net_socket* socket, msghdr* header, const void* data, size_t length,
1316 	int flags)
1317 {
1318 	const sockaddr* address = NULL;
1319 	socklen_t addressLength = 0;
1320 	size_t bytesLeft = length;
1321 
1322 	if (length > SSIZE_MAX)
1323 		return B_BAD_VALUE;
1324 
1325 	ancillary_data_container* ancillaryData = NULL;
1326 	CObjectDeleter<ancillary_data_container> ancillaryDataDeleter(NULL,
1327 		&delete_ancillary_data_container);
1328 
1329 	if (header != NULL) {
1330 		address = (const sockaddr*)header->msg_name;
1331 		addressLength = header->msg_namelen;
1332 
1333 		// get the ancillary data
1334 		if (header->msg_control != NULL) {
1335 			ancillaryData = create_ancillary_data_container();
1336 			if (ancillaryData == NULL)
1337 				return B_NO_MEMORY;
1338 			ancillaryDataDeleter.SetTo(ancillaryData);
1339 
1340 			status_t status = add_ancillary_data(socket, ancillaryData,
1341 				(cmsghdr*)header->msg_control, header->msg_controllen);
1342 			if (status != B_OK)
1343 				return status;
1344 		}
1345 	}
1346 
1347 	if (addressLength == 0)
1348 		address = NULL;
1349 	else if (address == NULL)
1350 		return B_BAD_VALUE;
1351 
1352 	if (socket->peer.ss_len != 0) {
1353 		if (address != NULL)
1354 			return EISCONN;
1355 
1356 		// socket is connected, we use that address
1357 		address = (struct sockaddr*)&socket->peer;
1358 		addressLength = socket->peer.ss_len;
1359 	}
1360 
1361 	if (address == NULL || addressLength == 0) {
1362 		// don't know where to send to:
1363 		return EDESTADDRREQ;
1364 	}
1365 
1366 	if ((socket->first_info->flags & NET_PROTOCOL_ATOMIC_MESSAGES) != 0
1367 		&& bytesLeft > socket->send.buffer_size)
1368 		return EMSGSIZE;
1369 
1370 	if (socket->address.ss_len == 0) {
1371 		// try to bind first
1372 		status_t status = socket_bind(socket, NULL, 0);
1373 		if (status != B_OK)
1374 			return status;
1375 	}
1376 
1377 	// If the protocol has a send_data_no_buffer() hook, we use that one.
1378 	if (socket->first_info->send_data_no_buffer != NULL) {
1379 		iovec stackVec = { (void*)data, length };
1380 		iovec* vecs = header ? header->msg_iov : &stackVec;
1381 		int vecCount = header ? header->msg_iovlen : 1;
1382 
1383 		ssize_t written = socket->first_info->send_data_no_buffer(
1384 			socket->first_protocol, vecs, vecCount, ancillaryData, address,
1385 			addressLength);
1386 		if (written > 0)
1387 			ancillaryDataDeleter.Detach();
1388 		return written;
1389 	}
1390 
1391 	// By convention, if a header is given, the (data, length) equals the first
1392 	// iovec. So drop the header, if it is the only iovec. Otherwise compute
1393 	// the size of the remaining ones.
1394 	if (header != NULL) {
1395 		if (header->msg_iovlen <= 1)
1396 			header = NULL;
1397 		else {
1398 // TODO: The iovecs have already been copied to kernel space. Simplify!
1399 			bytesLeft += compute_user_iovec_length(header->msg_iov + 1,
1400 				header->msg_iovlen - 1);
1401 		}
1402 	}
1403 
1404 	ssize_t bytesSent = 0;
1405 	size_t vecOffset = 0;
1406 	uint32 vecIndex = 0;
1407 
1408 	while (bytesLeft > 0) {
1409 		// TODO: useful, maybe even computed header space!
1410 		net_buffer* buffer = gNetBufferModule.create(256);
1411 		if (buffer == NULL)
1412 			return ENOBUFS;
1413 
1414 		while (buffer->size < socket->send.buffer_size
1415 			&& buffer->size < bytesLeft) {
1416 			if (vecIndex > 0 && vecOffset == 0) {
1417 				// retrieve next iovec buffer from header
1418 				iovec vec;
1419 				if (user_memcpy(&vec, header->msg_iov + vecIndex, sizeof(iovec))
1420 						< B_OK) {
1421 					gNetBufferModule.free(buffer);
1422 					return B_BAD_ADDRESS;
1423 				}
1424 
1425 				data = vec.iov_base;
1426 				length = vec.iov_len;
1427 			}
1428 
1429 			size_t bytes = length;
1430 			if (buffer->size + bytes > socket->send.buffer_size)
1431 				bytes = socket->send.buffer_size - buffer->size;
1432 
1433 			if (gNetBufferModule.append(buffer, data, bytes) < B_OK) {
1434 				gNetBufferModule.free(buffer);
1435 				return ENOBUFS;
1436 			}
1437 
1438 			if (bytes != length) {
1439 				// partial send
1440 				vecOffset = bytes;
1441 				length -= vecOffset;
1442 				data = (uint8*)data + vecOffset;
1443 			} else if (header != NULL) {
1444 				// proceed with next buffer, if any
1445 				vecOffset = 0;
1446 				vecIndex++;
1447 
1448 				if (vecIndex >= (uint32)header->msg_iovlen)
1449 					break;
1450 			}
1451 		}
1452 
1453 		// attach ancillary data to the first buffer
1454 		status_t status = B_OK;
1455 		if (ancillaryData != NULL) {
1456 			gNetBufferModule.set_ancillary_data(buffer, ancillaryData);
1457 			ancillaryDataDeleter.Detach();
1458 			ancillaryData = NULL;
1459 		}
1460 
1461 		size_t bufferSize = buffer->size;
1462 		buffer->flags = flags;
1463 		memcpy(buffer->source, &socket->address, socket->address.ss_len);
1464 		memcpy(buffer->destination, address, addressLength);
1465 		buffer->destination->sa_len = addressLength;
1466 
1467 		if (status == B_OK) {
1468 			status = socket->first_info->send_data(socket->first_protocol,
1469 				buffer);
1470 		}
1471 		if (status != B_OK) {
1472 			size_t sizeAfterSend = buffer->size;
1473 			gNetBufferModule.free(buffer);
1474 
1475 			if ((sizeAfterSend != bufferSize || bytesSent > 0)
1476 				&& (status == B_INTERRUPTED || status == B_WOULD_BLOCK)) {
1477 				// this appears to be a partial write
1478 				return bytesSent + (bufferSize - sizeAfterSend);
1479 			}
1480 			return status;
1481 		}
1482 
1483 		bytesLeft -= bufferSize;
1484 		bytesSent += bufferSize;
1485 	}
1486 
1487 	return bytesSent;
1488 }
1489 
1490 
1491 status_t
1492 socket_set_option(net_socket* socket, int level, int option, const void* value,
1493 	int length)
1494 {
1495 	if (level != SOL_SOCKET)
1496 		return ENOPROTOOPT;
1497 
1498 	TRACE("%s(socket %p, option %d\n", __FUNCTION__, socket, option);
1499 
1500 	switch (option) {
1501 		// TODO: implement other options!
1502 		case SO_LINGER:
1503 		{
1504 			if (length < (int)sizeof(struct linger))
1505 				return B_BAD_VALUE;
1506 
1507 			struct linger* linger = (struct linger*)value;
1508 			if (linger->l_onoff) {
1509 				socket->options |= SO_LINGER;
1510 				socket->linger = linger->l_linger;
1511 			} else {
1512 				socket->options &= ~SO_LINGER;
1513 				socket->linger = 0;
1514 			}
1515 			return B_OK;
1516 		}
1517 
1518 		case SO_SNDBUF:
1519 			if (length != sizeof(uint32))
1520 				return B_BAD_VALUE;
1521 
1522 			socket->send.buffer_size = *(const uint32*)value;
1523 			return B_OK;
1524 
1525 		case SO_RCVBUF:
1526 			if (length != sizeof(uint32))
1527 				return B_BAD_VALUE;
1528 
1529 			socket->receive.buffer_size = *(const uint32*)value;
1530 			return B_OK;
1531 
1532 		case SO_SNDLOWAT:
1533 			if (length != sizeof(uint32))
1534 				return B_BAD_VALUE;
1535 
1536 			socket->send.low_water_mark = *(const uint32*)value;
1537 			return B_OK;
1538 
1539 		case SO_RCVLOWAT:
1540 			if (length != sizeof(uint32))
1541 				return B_BAD_VALUE;
1542 
1543 			socket->receive.low_water_mark = *(const uint32*)value;
1544 			return B_OK;
1545 
1546 		case SO_RCVTIMEO:
1547 		case SO_SNDTIMEO:
1548 		{
1549 			if (length != sizeof(struct timeval))
1550 				return B_BAD_VALUE;
1551 
1552 			const struct timeval* timeval = (const struct timeval*)value;
1553 			bigtime_t timeout = timeval->tv_sec * 1000000LL + timeval->tv_usec;
1554 			if (timeout == 0)
1555 				timeout = B_INFINITE_TIMEOUT;
1556 
1557 			if (option == SO_SNDTIMEO)
1558 				socket->send.timeout = timeout;
1559 			else
1560 				socket->receive.timeout = timeout;
1561 			return B_OK;
1562 		}
1563 
1564 		case SO_NONBLOCK:
1565 			if (length != sizeof(int32))
1566 				return B_BAD_VALUE;
1567 
1568 			if (*(const int32*)value) {
1569 				socket->send.timeout = 0;
1570 				socket->receive.timeout = 0;
1571 			} else {
1572 				socket->send.timeout = B_INFINITE_TIMEOUT;
1573 				socket->receive.timeout = B_INFINITE_TIMEOUT;
1574 			}
1575 			return B_OK;
1576 
1577 		case SO_BROADCAST:
1578 		case SO_DEBUG:
1579 		case SO_DONTROUTE:
1580 		case SO_KEEPALIVE:
1581 		case SO_OOBINLINE:
1582 		case SO_REUSEADDR:
1583 		case SO_REUSEPORT:
1584 		case SO_USELOOPBACK:
1585 			if (length != sizeof(int32))
1586 				return B_BAD_VALUE;
1587 
1588 			if (*(const int32*)value)
1589 				socket->options |= option;
1590 			else
1591 				socket->options &= ~option;
1592 			return B_OK;
1593 
1594 		case SO_BINDTODEVICE:
1595 		{
1596 			if (length != sizeof(uint32))
1597 				return B_BAD_VALUE;
1598 
1599 			// TODO: we might want to check if the device exists at all
1600 			// (although it doesn't really harm when we don't)
1601 			socket->bound_to_device = *(const uint32*)value;
1602 			return B_OK;
1603 		}
1604 
1605 		default:
1606 			break;
1607 	}
1608 
1609 	dprintf("socket_setsockopt: unknown option %d\n", option);
1610 	return ENOPROTOOPT;
1611 }
1612 
1613 
1614 int
1615 socket_setsockopt(net_socket* socket, int level, int option, const void* value,
1616 	int length)
1617 {
1618 	return socket->first_protocol->module->setsockopt(socket->first_protocol,
1619 		level, option, value, length);
1620 }
1621 
1622 
1623 int
1624 socket_shutdown(net_socket* socket, int direction)
1625 {
1626 	return socket->first_info->shutdown(socket->first_protocol, direction);
1627 }
1628 
1629 
1630 status_t
1631 socket_socketpair(int family, int type, int protocol, net_socket* sockets[2])
1632 {
1633 	sockets[0] = NULL;
1634 	sockets[1] = NULL;
1635 
1636 	// create sockets
1637 	status_t error = socket_open(family, type, protocol, &sockets[0]);
1638 	if (error != B_OK)
1639 		return error;
1640 
1641 	if (error == B_OK)
1642 		error = socket_open(family, type, protocol, &sockets[1]);
1643 
1644 	// bind one
1645 	if (error == B_OK)
1646 		error = socket_bind(sockets[0], NULL, 0);
1647 
1648 	// start listening
1649 	if (error == B_OK)
1650 		error = socket_listen(sockets[0], 1);
1651 
1652 	// connect them
1653 	if (error == B_OK) {
1654 		error = socket_connect(sockets[1], (sockaddr*)&sockets[0]->address,
1655 			sockets[0]->address.ss_len);
1656 	}
1657 
1658 	// accept a socket
1659 	net_socket* acceptedSocket = NULL;
1660 	if (error == B_OK)
1661 		error = socket_accept(sockets[0], NULL, NULL, &acceptedSocket);
1662 
1663 	if (error == B_OK) {
1664 		// everything worked: close the listener socket
1665 		socket_close(sockets[0]);
1666 		socket_free(sockets[0]);
1667 		sockets[0] = acceptedSocket;
1668 	} else {
1669 		// close sockets on error
1670 		for (int i = 0; i < 2; i++) {
1671 			if (sockets[i] != NULL) {
1672 				socket_close(sockets[i]);
1673 				socket_free(sockets[i]);
1674 				sockets[i] = NULL;
1675 			}
1676 		}
1677 	}
1678 
1679 	return error;
1680 }
1681 
1682 
1683 //	#pragma mark -
1684 
1685 
1686 static status_t
1687 socket_std_ops(int32 op, ...)
1688 {
1689 	switch (op) {
1690 		case B_MODULE_INIT:
1691 		{
1692 			new (&sSocketList) SocketList;
1693 			mutex_init(&sSocketLock, "socket list");
1694 
1695 #if ENABLE_DEBUGGER_COMMANDS
1696 			add_debugger_command("sockets", dump_sockets, "lists all sockets");
1697 			add_debugger_command("socket", dump_socket, "dumps a socket");
1698 #endif
1699 			return B_OK;
1700 		}
1701 		case B_MODULE_UNINIT:
1702 			ASSERT(sSocketList.IsEmpty());
1703 			mutex_destroy(&sSocketLock);
1704 
1705 #if ENABLE_DEBUGGER_COMMANDS
1706 			remove_debugger_command("socket", dump_socket);
1707 			remove_debugger_command("sockets", dump_sockets);
1708 #endif
1709 			return B_OK;
1710 
1711 		default:
1712 			return B_ERROR;
1713 	}
1714 }
1715 
1716 
1717 net_socket_module_info gNetSocketModule = {
1718 	{
1719 		NET_SOCKET_MODULE_NAME,
1720 		0,
1721 		socket_std_ops
1722 	},
1723 	socket_open,
1724 	socket_close,
1725 	socket_free,
1726 
1727 	socket_readv,
1728 	socket_writev,
1729 	socket_control,
1730 
1731 	socket_read_avail,
1732 	socket_send_avail,
1733 
1734 	socket_send_data,
1735 	socket_receive_data,
1736 
1737 	socket_get_option,
1738 	socket_set_option,
1739 
1740 	socket_get_next_stat,
1741 
1742 	// connections
1743 	socket_acquire,
1744 	socket_release,
1745 	socket_spawn_pending,
1746 	socket_dequeue_connected,
1747 	socket_count_connected,
1748 	socket_set_max_backlog,
1749 	socket_has_parent,
1750 	socket_connected,
1751 	socket_aborted,
1752 
1753 	// notifications
1754 	socket_request_notification,
1755 	socket_cancel_notification,
1756 	socket_notify,
1757 
1758 	// standard socket API
1759 	socket_accept,
1760 	socket_bind,
1761 	socket_connect,
1762 	socket_getpeername,
1763 	socket_getsockname,
1764 	socket_getsockopt,
1765 	socket_listen,
1766 	socket_receive,
1767 	socket_send,
1768 	socket_setsockopt,
1769 	socket_shutdown,
1770 	socket_socketpair
1771 };
1772 
1773