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