1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
|
/*
HawkNL cross platform network library
Copyright (C) 2000-2003 Phil Frisbie, Jr. (phil@hawksoft.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include <string.h>
#include <stdio.h>
#if defined (_WIN32_WCE)
#define errno GetLastError()
#else
#include <errno.h>
#endif
#include "nlinternal.h"
#ifdef WINDOWS_APP
/* Windows systems */
#include "wsock.h"
#else
/* Unix-style systems or macs with posix support */
#include <netinet/in.h> /* for ntohs and htons */
#endif
#ifdef NL_INCLUDE_LOOPBACK
#include "loopback.h"
static NLaddress loopback_ouraddress;
static NLint loopgroup;
static volatile NLboolean reuseaddress = NL_FALSE;
static NLmutex portlock; /* In memory of my step-father, Don Portlock,
who passed away Jan 12, 2001 - Phil */
static volatile NLushort newport = 1024;
static NLushort loopback_getNextPort(void)
{
(void)nlMutexLock(&portlock);
if(++newport > 65535)
{
newport = 1024;
}
(void)nlMutexUnlock(&portlock);
return newport;
}
static NLboolean loopback_ScanPort(NLushort port, NLenum type)
{
NLint numsockets = NL_MAX_GROUP_SOCKETS;
NLsocket temp[NL_MAX_GROUP_SOCKETS];
if(reuseaddress == NL_TRUE)
{
return NL_TRUE;
}
if(nlGroupGetSockets(loopgroup, (NLsocket *)&temp, &numsockets) == NL_FALSE)
{
return NL_FALSE;
}
if(numsockets == 0)
{
return NL_TRUE;
}
while(numsockets-- > 0)
{
nl_socket_t *sock = nlSockets[temp[numsockets]];
if(sock->type == type && sock->localport == port)
{
return NL_FALSE;
}
}
return NL_TRUE;
}
static NLushort loopback_TryPort(NLushort port, NLenum type)
{
NLint ntries = 500; /* this is to prevent an infinite loop */
if(port > 0)
{
if(loopback_ScanPort(port, type) == NL_TRUE)
{
return port;
}
else
{
return 0;
}
}
/* let's find our own port number */
while(ntries-- > 0)
{
port = loopback_getNextPort();
if(loopback_ScanPort(port, type) == NL_TRUE)
{
return port;
}
}
return 0;
}
NLboolean loopback_Init(void)
{
loopgroup = nlGroupCreate();
if(loopgroup == NL_INVALID)
{
return NL_FALSE;
}
return NL_TRUE;
}
void loopback_Shutdown(void)
{
(void)nlGroupDestroy(loopgroup);
}
NLboolean loopback_Listen(NLsocket socket)
{
nl_socket_t *sock = nlSockets[socket];
NLint i = NL_MAX_ACCEPT;
if(sock->type == NL_BROADCAST)
{
nlSetError(NL_WRONG_TYPE);
return NL_FALSE;
}
sock->listen = NL_TRUE;
while(i-- > 0)
{
sock->ext->accept[i] = NL_INVALID;
}
return NL_TRUE;
}
NLsocket loopback_AcceptConnection(NLsocket socket)
{
nl_socket_t *sock = nlSockets[socket];
if(sock->listen == NL_FALSE)
{
nlSetError(NL_NOT_LISTEN);
return NL_INVALID;
}
if(sock->inuse == NL_FALSE)
{
/* socket was closed by nlShutdown */
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
if(sock->ext == NULL)
{
/* socket was closed on another thread */
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
if(sock->ext->accept[0] != NL_INVALID)
{
NLsocket newsocket;
NLsocket osock = sock->ext->accept[0];
nl_socket_t *othersock = nlSockets[osock];
/* make sure the other socket is valid */
if(nlIsValidSocket(osock) == NL_FALSE)
{
NLint i;
for(i=1;i<NL_MAX_ACCEPT;i++)
{
sock->ext->accept[i-1] = sock->ext->accept[i];
}
sock->ext->accept[NL_MAX_ACCEPT-1] = NL_INVALID;
return loopback_AcceptConnection(socket);
}
newsocket = loopback_Open(0, sock->type);
if(newsocket != NL_INVALID)
{
NLint i;
nl_socket_t *newsock = nlSockets[newsocket];
/* we must unlock the socket briefly or else nlConnect will deadlock */
nlUnlockSocket(socket, NL_BOTH);
if(nlLockSocket(osock, NL_READ) == NL_FALSE)
{
return NL_INVALID;
}
(void)nlLockSocket(socket, NL_BOTH);
/* do the connecting */
newsock->ext->consock = osock;
newsock->remoteport = othersock->localport;
othersock->ext->consock = newsocket;
othersock->remoteport = newsock->localport;
newsock->connected = NL_TRUE;
loopback_SetAddrPort(&othersock->addressin, othersock->remoteport);
loopback_SetAddrPort(&newsock->addressin, newsock->remoteport);
othersock->connected = NL_TRUE;
othersock->connecting = NL_FALSE;
/* move the accept que down one */
for(i=1;i<NL_MAX_ACCEPT;i++)
{
sock->ext->accept[i-1] = sock->ext->accept[i];
}
sock->ext->accept[NL_MAX_ACCEPT-1] = NL_INVALID;
nlUnlockSocket(socket, NL_BOTH);
nlUnlockSocket(osock, NL_READ);
(void)nlLockSocket(socket, NL_BOTH);
return newsocket;
}
}
nlSetError(NL_NO_PENDING);
return NL_INVALID;
}
NLsocket loopback_Open(NLushort port, NLenum type)
{
nl_socket_t *newsock;
NLsocket newsocket;
NLint i;
NLushort lport;
switch (type) {
case NL_RELIABLE:
case NL_UNRELIABLE:
case NL_RELIABLE_PACKETS:
case NL_BROADCAST:
break;
default:
nlSetError(NL_INVALID_ENUM);
return NL_INVALID;
}
lport = loopback_TryPort(port, type);
if(lport == 0)
{
nlSetError(NL_INVALID_PORT);
return NL_INVALID;
}
newsocket = nlGetNewSocket();
if(newsocket == NL_INVALID)
{
return NL_INVALID;
}
if(nlLockSocket(newsocket, NL_BOTH) == NL_FALSE)
{
return NL_INVALID;
}
newsock = nlSockets[newsocket];
newsock->type = type;
newsock->localport = lport;
if(type == NL_BROADCAST)
{
newsock->remoteport = lport;
}
if((newsock->ext = (nl_extra_t *)malloc(sizeof(nl_extra_t))) == NULL)
{
nlSetError(NL_OUT_OF_MEMORY);
nlUnlockSocket(newsocket, NL_BOTH);
loopback_Close(newsocket);
return NL_INVALID;
}
/* clear out the structure */
memset(newsock->ext, 0, sizeof(nl_extra_t));
for(i=0;i<NL_NUM_PACKETS;i++)
{
NLboolean err = NL_FALSE;
/* malloc the max packet length plus two bytes for the port number */
if((newsock->ext->inpacket[i] = (NLbyte *)malloc((size_t)(NL_MAX_PACKET_LENGTH + 2))) == NULL)
{
nlSetError(NL_OUT_OF_MEMORY);
err = NL_TRUE;
}
if(err == NL_TRUE)
{
while(i-- > 0)
{
free(newsock->ext->inpacket[i]);
}
nlUnlockSocket(newsocket, NL_BOTH);
loopback_Close(newsocket);
return NL_INVALID;
}
}
(void)nlGroupAddSocket(loopgroup, newsocket);
nlUnlockSocket(newsocket, NL_BOTH);
return newsocket;
}
NLboolean loopback_Connect(NLsocket socket, const NLaddress *address)
{
nl_socket_t *sock = nlSockets[socket];
NLushort port;
NLint numsockets = NL_MAX_GROUP_SOCKETS;
NLsocket temp[NL_MAX_GROUP_SOCKETS];
/* no need to connect a broadcast socket */
if(sock->type == NL_BROADCAST)
{
nlSetError(NL_WRONG_TYPE);
}
port = loopback_GetPortFromAddr(address);
/* make sure socket is not already connected */
if(sock->connected == NL_TRUE || sock->connecting == NL_TRUE)
{
nlSetError(NL_CON_REFUSED);
return NL_FALSE;
}
if(nlGroupGetSockets(loopgroup, (NLsocket *)&temp, &numsockets) == NL_FALSE)
{
return NL_FALSE;
}
if(numsockets == 0)
{
return NL_FALSE;
}
while(numsockets-- > 0)
{
NLsocket s = temp[numsockets];
nl_socket_t *othersock = nlSockets[s];
if(sock->type == othersock->type && port == othersock->localport
&& othersock->listen == NL_TRUE && othersock->connected == NL_FALSE
&& othersock->connecting == NL_FALSE)
{
/* we found the right socket, so connect */
NLint i;
if(nlLockSocket(s, NL_BOTH) == NL_FALSE)
{
return NL_FALSE;
}
for(i=0;i<NL_MAX_ACCEPT;i++)
{
if(othersock->ext->accept[i] == NL_INVALID)
{
othersock->ext->accept[i] = socket;
sock->connecting = NL_TRUE;
sock->ext->consock = s;
nlUnlockSocket(s, NL_BOTH);
if(sock->blocking == NL_TRUE)
{
nlUnlockSocket(socket, NL_BOTH);
/* wait for nlAccept to be called */
while(sock->connecting == NL_TRUE)
{
if(sock->inuse == NL_FALSE)
{
/* nlShutdown has been called */
nlSetError(NL_INVALID_SOCKET);
return NL_FALSE;
}
nlThreadSleep(NL_CONNECT_SLEEP);
}
(void)nlLockSocket(socket, NL_BOTH);
}
return NL_TRUE;
}
}
nlUnlockSocket(s, NL_BOTH);
}
}
nlSetError(NL_CON_REFUSED);
return NL_FALSE;
}
void loopback_Close(NLsocket socket)
{
nl_socket_t *sock = nlSockets[socket];
int i;
if(sock->connected == NL_TRUE || sock->connecting == NL_TRUE)
{
/* break the connection */
nl_socket_t *othersock = nlSockets[sock->ext->consock];
if(othersock->ext != NULL)
{
othersock->ext->consock = NL_INVALID;
}
othersock->connected = NL_FALSE;
sock->connected = NL_FALSE;
sock->listen = NL_FALSE;
if(sock->type != NL_BROADCAST)
{
/* this allows nlPollGroup to report socket is readable */
/* so that the app can get the NL_SOCK_DISCONNECT message*/
if(othersock->ext != NULL)
{
othersock->ext->inlen[othersock->ext->nextinused] = -1;
}
}
}
for(i=0;i<NL_NUM_PACKETS;i++)
{
void /*@owned@*/*t = sock->ext->inpacket[i];
free(t);
sock->ext->inpacket[i] = NULL;
}
free(sock->ext);
sock->ext = NULL;
}
NLint loopback_Read(NLsocket socket, NLvoid *buffer, NLint nbytes)
{
nl_socket_t *sock = nlSockets[socket];
NLint len = sock->ext->inlen[sock->ext->nextinused];
NLint c = 0;
NLushort port;
if(sock->blocking == NL_TRUE)
{
while(len == 0)
{
nlUnlockSocket(socket, NL_READ);
nlThreadSleep(10);
if(sock->inuse == NL_FALSE)
{
/* nlShutdown has been called */
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
(void)nlLockSocket(socket, NL_READ);
len = sock->ext->inlen[sock->ext->nextinused];
}
}
if(len > 0)
{
if(len > nbytes)
{
nlSetError(NL_BUFFER_SIZE);
return NL_INVALID;
}
if(sock->connecting == NL_TRUE)
{
nlSetError(NL_CON_PENDING);
return NL_INVALID;
}
/* get the port number */
readShort(sock->ext->inpacket[sock->ext->nextinused], c, port);
loopback_SetAddrPort(&sock->addressin, port);
/* copy the packet */
memcpy(buffer, sock->ext->inpacket[sock->ext->nextinused] + 2, (size_t)len);
/* zero out length and set up for next packet */
sock->ext->inlen[sock->ext->nextinused] = 0;
sock->ext->nextinused++;
if(sock->ext->nextinused >= NL_NUM_PACKETS)
{
sock->ext->nextinused = 0;
}
}
/* check for broken connection */
else if((len == -1) || (sock->connected == NL_TRUE && sock->ext->consock == NL_INVALID)
|| (sock->connected == NL_FALSE && sock->type != NL_BROADCAST))
{
nlSetError(NL_SOCK_DISCONNECT);
return NL_INVALID;
}
return len;
}
static NLint loopback_WritePacket(NLsocket to, const NLvoid *buffer, NLint nbytes, NLushort fromport)
{
nl_socket_t *sock = nlSockets[to];
NLint i, j;
NLint c = 0;
/* check the packet size */
if(nbytes > NL_MAX_PACKET_LENGTH)
{
nlSetError(NL_PACKET_SIZE);
return NL_INVALID;
}
if(nlLockSocket(to, NL_READ) == NL_FALSE)
{
return NL_INVALID;
}
/* make sure we have an empty packet buffer */
if(sock->ext->nextinfree == NL_INVALID)
{
/* all buffers were filled by last write */
/* check to see if any were emptied by a read */
i = NL_NUM_PACKETS;
j = sock->ext->nextinused;
while(i-- > 0)
{
if(sock->ext->inlen[j] == 0)
{
/* found the first free */
sock->ext->nextinfree = j;
break;
}
j++;
if(j >= NL_NUM_PACKETS)
{
j = 0;
}
}
if(sock->ext->nextinfree == NL_INVALID)
{
nlUnlockSocket(to, NL_READ);
/* none are free */
if(sock->type == NL_RELIABLE || sock->type == NL_RELIABLE_PACKETS)
{
return 0;
}
else
{
/* silently fail */
return nbytes;
}
}
}
/* write the port number */
writeShort(sock->ext->inpacket[sock->ext->nextinfree], c, fromport);
/* copy the packet buffer */
memcpy(sock->ext->inpacket[sock->ext->nextinfree] + 2, buffer, (size_t)nbytes);
sock->ext->inlen[sock->ext->nextinfree] = nbytes;
sock->ext->nextinfree++;
if(sock->ext->nextinfree >= NL_NUM_PACKETS)
{
sock->ext->nextinfree = 0;
}
/* check for full packet buffers */
if(sock->ext->inlen[sock->ext->nextinfree] != 0)
{
sock->ext->nextinfree = NL_INVALID;
}
nlUnlockSocket(to, NL_READ);
return nbytes;
}
NLint loopback_Write(NLsocket socket, const NLvoid *buffer, NLint nbytes)
{
nl_socket_t *sock = nlSockets[socket];
nl_socket_t *othersock;
NLsocket s[NL_MAX_GROUP_SOCKETS];
NLint number = NL_MAX_GROUP_SOCKETS;
NLint i;
NLint count;
switch (sock->type) {
case NL_RELIABLE:
case NL_RELIABLE_PACKETS:
default:
{
if(sock->connected == NL_TRUE)
{
/* check for broken connection */
if(sock->ext->consock == NL_INVALID)
{
nlSetError(NL_SOCK_DISCONNECT);
return NL_INVALID;
}
count = loopback_WritePacket(sock->ext->consock, buffer, nbytes, sock->localport);
}
else if(sock->connecting == NL_TRUE)
{
nlSetError(NL_CON_PENDING);
return NL_INVALID;
}
else
{
nlSetError(NL_SOCK_DISCONNECT);
return NL_INVALID;
}
}
break;
case NL_UNRELIABLE:
{
if(sock->connected == NL_TRUE)
{
/* check for broken connection */
if(sock->ext->consock == NL_INVALID)
{
nlSetError(NL_SOCK_DISCONNECT);
return NL_INVALID;
}
count = loopback_WritePacket(sock->ext->consock, buffer, nbytes, sock->localport);
}
else if(sock->connecting == NL_TRUE)
{
nlSetError(NL_CON_PENDING);
return NL_INVALID;
}
/* unconnected UDP emulation */
count = nbytes;
(void)nlGroupGetSockets(loopgroup, (NLsocket *)s, &number);
for(i=0;i<number;i++)
{
if(nlIsValidSocket(s[i]) == NL_TRUE)
{
othersock = nlSockets[s[i]];
if(sock->remoteport == othersock->localport &&
othersock->connected == NL_FALSE &&
sock->type == othersock->type)
{
(void)loopback_WritePacket(s[i], buffer, nbytes, sock->localport);
}
}
}
}
break;
case NL_BROADCAST:
{
count = nbytes;
(void)nlGroupGetSockets(loopgroup, (NLsocket *)s, &number);
for(i=0;i<number;i++)
{
if(nlIsValidSocket(s[i]) == NL_TRUE)
{
othersock = nlSockets[s[i]];
if(sock->localport == othersock->localport &&
sock->type == othersock->type)
{
(void)loopback_WritePacket(s[i], buffer, nbytes, sock->localport);
}
}
}
}
}
return count;
}
NLchar *loopback_AddrToString(const NLaddress *address, NLchar *string)
{
_stprintf(string, TEXT("127.0.0.1:%u"), loopback_GetPortFromAddr(address));
return string;
}
NLboolean loopback_StringToAddr(const NLchar *string, NLaddress *address)
{
NLchar *st;
NLint port;
memset(address, 0, sizeof(NLaddress));
address->valid = NL_TRUE;
/* check for a port number */
st = _tcschr(string, TEXT(':'));
if(st != NULL)
{
st++;
port = _ttoi(st);
if(port < 0 || port > 65535)
{
nlSetError(NL_BAD_ADDR);
address->valid = NL_FALSE;
return NL_FALSE;
}
loopback_SetAddrPort(address, (NLushort)port);
}
return NL_TRUE;
}
NLboolean loopback_GetLocalAddr(NLsocket socket, NLaddress *address)
{
nl_socket_t *sock = nlSockets[socket];
memset(address, 0, sizeof(NLaddress));
loopback_SetAddrPort(address, sock->localport);
address->valid = NL_TRUE;
return NL_TRUE;
}
NLaddress *loopback_GetAllLocalAddr(NLint *count)
{
*count = 1;
memset(&loopback_ouraddress, 0, sizeof(NLaddress));
loopback_ouraddress.valid = NL_TRUE;
return &loopback_ouraddress;
}
NLboolean loopback_SetLocalAddr(const NLaddress *address)
{
/* this is just to keep compilers happy */
if(address == NULL)
{
return NL_FALSE;
}
return NL_TRUE;
}
NLchar *loopback_GetNameFromAddr(const NLaddress *address, NLchar *name)
{
_stprintf(name, TEXT("%s:%u"), TEXT("localhost"), loopback_GetPortFromAddr(address));
return name;
}
NLboolean loopback_GetNameFromAddrAsync(const NLaddress *address, NLchar *name)
{
(void)loopback_GetNameFromAddr(address, name);
return NL_TRUE;
}
NLboolean loopback_GetAddrFromName(const NLchar *name, NLaddress *address)
{
return loopback_StringToAddr(name, address);
}
NLboolean loopback_GetAddrFromNameAsync(const NLchar *name, NLaddress *address)
{
return loopback_GetAddrFromName(name, address);
}
NLboolean loopback_AddrCompare(const NLaddress *address1, const NLaddress *address2)
{
if(*(NLushort *)(&address1->addr[0]) == *(NLushort *)(&address2->addr[0]))
{
return NL_TRUE;
}
return NL_FALSE;
}
NLushort loopback_GetPortFromAddr(const NLaddress *address)
{
return *(NLushort *)(&address->addr[0]);
}
void loopback_SetAddrPort(NLaddress *address, NLushort port)
{
*(NLushort *)(&address->addr[0]) = port;
}
NLint loopback_GetSystemError(void)
{
return errno;
}
NLint loopback_PollGroup(NLint group, NLenum name, NLsocket *sockets, NLint number, NLint timeout)
{
NLint count = 0;
NLint numsockets = NL_MAX_GROUP_SOCKETS;
NLsocket temp[NL_MAX_GROUP_SOCKETS];
NLtime end, now;
nlGroupLock();
if(nlGroupGetSocketsINT(group, (NLsocket *)&temp, &numsockets) == NL_FALSE)
{
/* any error is set by nlGroupGetSockets */
nlGroupUnlock();
return NL_INVALID;
}
nlGroupUnlock();
if(numsockets == 0)
{
return 0;
}
(void)nlTime(&now);
end.seconds = now.seconds;
end.mseconds = now.mseconds;
if(timeout > 0)
{
end.mseconds += timeout;
while(end.mseconds > 999)
{
end.mseconds -= 1000;
end.seconds++;
}
}
while(count == 0)
{
switch(name) {
case NL_READ_STATUS:
{
NLint i = 0;
NLint j = numsockets;
while(j-- > 0)
{
/* check for a packet */
nl_socket_t *sock;
if(nlIsValidSocket(temp[i]) != NL_TRUE)
{
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
sock = nlSockets[temp[i]];
if(sock->ext->inlen[sock->ext->nextinused] != 0)
{
*sockets = temp[i];
sockets++;
count++;
if(count > number)
{
nlSetError(NL_BUFFER_SIZE);
return NL_INVALID;
}
}
i++;
}
}
break;
case NL_WRITE_STATUS:
{
NLint i = 0;
NLint j = numsockets;
while(j-- > 0)
{
nl_socket_t *sock;
if(nlIsValidSocket(temp[i]) != NL_TRUE)
{
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
sock = nlSockets[temp[i]];
/* check for a free packet if reliable and connected */
if((sock->type == NL_TCP || sock->type == NL_TCP_PACKETS)
&& (sock->connecting == NL_TRUE || sock->connected == NL_TRUE))
{
nl_socket_t *othersock = nlSockets[sock->ext->consock];
if(othersock->ext->nextinfree == NL_INVALID)
{
continue;
}
}
/* add the socket to the list */
*sockets = temp[i];
sockets++;
count++;
if(count > number)
{
nlSetError(NL_BUFFER_SIZE);
return NL_INVALID;
}
i++;
}
}
break;
case NL_ERROR_STATUS:
{
NLint i = 0;
NLint j = numsockets;
while(j-- > 0)
{
nl_socket_t *sock;
if(nlIsValidSocket(temp[i]) != NL_TRUE)
{
nlSetError(NL_INVALID_SOCKET);
return NL_INVALID;
}
sock = nlSockets[temp[i]];
if(sock->connected == NL_FALSE && sock->type != NL_UDP_BROADCAST)
{
/* add the socket to the list */
*sockets = temp[i];
sockets++;
count++;
if(count > number)
{
nlSetError(NL_BUFFER_SIZE);
return NL_INVALID;
}
i++;
}
}
}
break;
default:
nlSetError(NL_INVALID_ENUM);
return NL_INVALID;
}
if(timeout != 0)
{
nlThreadSleep(1);
(void)nlTime(&now);
if(timeout > 0 && (now.seconds > end.seconds || (now.seconds == end.seconds && now.mseconds > end.mseconds)))
break;
}
else
{
break;
}
}
return count;
}
NLboolean loopback_Hint(NLenum name, NLint arg)
{
switch (name) {
case NL_REUSE_ADDRESS:
reuseaddress = (NLboolean)(arg != 0 ? NL_TRUE : NL_FALSE);
break;
default:
nlSetError(NL_INVALID_ENUM);
return NL_FALSE;
}
return NL_TRUE;
}
#endif /* NL_INCLUDE_LOOPBACK */
|