| 1 | /* |
| 2 | * Copyright 2010 Andreas Faerber <andreas.faerber@web.de> |
| 3 | * All rights reserved. Distributed under the terms of the MIT License. |
| 4 | */ |
| 5 | |
| 6 | #include <boot/net/TCP.h> |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <KernelExport.h> |
| 10 | |
| 11 | #include <boot/net/ChainBuffer.h> |
| 12 | #include <boot/net/NetStack.h> |
| 13 | |
| 14 | |
| 15 | //#define TRACE_TCP |
| 16 | #ifdef TRACE_TCP |
| 17 | # define TRACE(x) dprintf x |
| 18 | #else |
| 19 | # define TRACE(x) ; |
| 20 | #endif |
| 21 | |
| 22 | |
| 23 | TCPPacket::TCPPacket() |
| 24 | : |
| 25 | fNext(NULL) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | |
| 30 | TCPPacket::~TCPPacket() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | status_t |
| 36 | TCPPacket::SetTo(const void* data, size_t size, ip_addr_t sourceAddress, |
| 37 | uint16 sourcePort, ip_addr_t destinationAddress, uint16 destinationPort, |
| 38 | uint32 sequenceNumber, uint32 acknowledgmentNumber, uint8 flags) |
| 39 | { |
| 40 | if (data == NULL && size > 0) |
| 41 | return B_BAD_VALUE; |
| 42 | |
| 43 | if (size > 0) { |
| 44 | fData = malloc(size); |
| 45 | if (fData == NULL) |
| 46 | return B_NO_MEMORY; |
| 47 | memcpy(fData, data, size); |
| 48 | } else { |
| 49 | fData = NULL; |
| 50 | } |
| 51 | |
| 52 | fSize = size; |
| 53 | fSourceAddress = sourceAddress; |
| 54 | fSourcePort = sourcePort; |
| 55 | fDestinationAddress = destinationAddress; |
| 56 | fDestinationPort = destinationPort; |
| 57 | fSequenceNumber = sequenceNumber; |
| 58 | fAcknowledgmentNumber = acknowledgmentNumber; |
| 59 | fFlags = flags; |
| 60 | |
| 61 | return B_OK; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | ip_addr_t |
| 66 | TCPPacket::SourceAddress() const |
| 67 | { |
| 68 | return fSourceAddress; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | ip_addr_t |
| 73 | TCPPacket::DestinationAddress() const |
| 74 | { |
| 75 | return fDestinationAddress; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | uint16 |
| 80 | TCPPacket::SourcePort() const |
| 81 | { |
| 82 | return fSourcePort; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | uint16 |
| 87 | TCPPacket::DestinationPort() const |
| 88 | { |
| 89 | return fDestinationPort; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | uint32 |
| 94 | TCPPacket::SequenceNumber() const |
| 95 | { |
| 96 | return fSequenceNumber; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | uint32 |
| 101 | TCPPacket::AcknowledgmentNumber() const |
| 102 | { |
| 103 | return fAcknowledgmentNumber; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | TCPPacket* |
| 108 | TCPPacket::Next() const |
| 109 | { |
| 110 | return fNext; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void |
| 115 | TCPPacket::SetNext(TCPPacket* packet) |
| 116 | { |
| 117 | fNext = packet; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | TCPSocket::TCPSocket() |
| 124 | : |
| 125 | fTCPService(NetStack::Default()->GetTCPService()), |
| 126 | fAddress(INADDR_ANY), |
| 127 | fPort(0), |
| 128 | fSequenceNumber(0), |
| 129 | fFirstPacket(NULL), |
| 130 | fLastPacket(NULL), |
| 131 | fFirstSentPacket(NULL), |
| 132 | fLastSentPacket(NULL), |
| 133 | fState(TCP_SOCKET_STATE_INITIAL), |
| 134 | fRemoteState(TCP_SOCKET_STATE_INITIAL) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | |
| 139 | TCPSocket::~TCPSocket() |
| 140 | { |
| 141 | if (fTCPService != NULL && fPort != 0) |
| 142 | fTCPService->UnbindSocket(this); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | status_t |
| 147 | TCPSocket::Connect(ip_addr_t address, uint16 port) |
| 148 | { |
| 149 | fRemoteAddress = address; |
| 150 | fRemotePort = port; |
| 151 | TRACE(("time %llu\n", system_time())); |
| 152 | fSequenceNumber = system_time(); |
| 153 | uint16 random = system_time(); |
| 154 | fPort = 0xC000 + (random & ~0xc000); |
| 155 | fAcknowledgeNumber = 0; |
| 156 | fNextPacket = 0; |
| 157 | |
| 158 | status_t error = fTCPService->BindSocket(this); |
| 159 | if (error != B_OK) |
| 160 | return error; |
| 161 | |
| 162 | // send SYN |
| 163 | TCPPacket* packet = new(nothrow) TCPPacket(); |
| 164 | if (packet == NULL) |
| 165 | return B_NO_MEMORY; |
| 166 | error = packet->SetTo(NULL, 0, fAddress, fPort, address, port, |
| 167 | fSequenceNumber, fAcknowledgeNumber, TCP_SYN); |
| 168 | if (error != B_OK) |
| 169 | return error; |
| 170 | error = _Send(packet); |
| 171 | if (error != B_OK) |
| 172 | return error; |
| 173 | fState = TCP_SOCKET_STATE_SYN_SENT; |
| 174 | TRACE(("SYN sent\n")); |
| 175 | |
| 176 | // receive SYN-ACK |
| 177 | error = _WaitForState(TCP_SOCKET_STATE_OPEN, 1000000LL); |
| 178 | if (error != B_OK) { |
| 179 | TRACE(("no SYN-ACK received\n")); |
| 180 | return error; |
| 181 | } |
| 182 | TRACE(("SYN-ACK received\n")); |
| 183 | |
| 184 | return B_OK; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | status_t |
| 189 | TCPSocket::Close() |
| 190 | { |
| 191 | // send FIN |
| 192 | TCPPacket* packet = new(nothrow) TCPPacket(); |
| 193 | if (packet == NULL) |
| 194 | return B_NO_MEMORY; |
| 195 | status_t error = packet->SetTo(NULL, 0, fAddress, fPort, fRemoteAddress, fRemotePort, |
| 196 | fSequenceNumber, fAcknowledgeNumber, TCP_FIN | TCP_ACK); |
| 197 | if (error != B_OK) |
| 198 | return error; |
| 199 | error = _Send(packet); |
| 200 | if (error != B_OK) |
| 201 | return error; |
| 202 | fState = TCP_SOCKET_STATE_FIN_SENT; |
| 203 | TRACE(("FIN sent\n")); |
| 204 | |
| 205 | error = _WaitForState(TCP_SOCKET_STATE_CLOSED, 1000000LL); |
| 206 | if (error != B_OK) |
| 207 | return error; |
| 208 | |
| 209 | return B_OK; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | status_t |
| 214 | TCPSocket::Receive(TCPPacket** _packet, bigtime_t timeout) |
| 215 | { |
| 216 | if (fTCPService == NULL) |
| 217 | return B_NO_INIT; |
| 218 | if (_packet == NULL) |
| 219 | return B_BAD_VALUE; |
| 220 | bigtime_t startTime = system_time(); |
| 221 | do { |
| 222 | fTCPService->ProcessIncomingPackets(); |
| 223 | TCPPacket* packet = DequeuePacket(); |
| 224 | if (packet != NULL) { |
| 225 | *_packet = packet; |
| 226 | return B_OK; |
| 227 | } |
| 228 | _ResendQueue(); |
| 229 | } while (system_time() - startTime < timeout); |
| 230 | return timeout == 0 ? B_WOULD_BLOCK : B_TIMED_OUT; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void |
| 235 | TCPSocket::Acknowledge(uint32 number) |
| 236 | { |
| 237 | TRACE(("TCPSocket::Acknowledge(): %lu\n", number)); |
| 238 | // dequeue packets |
| 239 | for (TCPPacket* packet = fFirstSentPacket; packet != NULL; |
| 240 | packet = fFirstSentPacket) { |
| 241 | if (packet->SequenceNumber() > number || |
| 242 | (packet->SequenceNumber() == number && packet->DataSize() > 0)) |
| 243 | return; |
| 244 | fFirstSentPacket = packet->Next(); |
| 245 | delete packet; |
| 246 | } |
| 247 | fLastSentPacket = NULL; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | void |
| 252 | TCPSocket::ProcessPacket(TCPPacket* packet) |
| 253 | { |
| 254 | TRACE(("TCPSocket::ProcessPacket()\n")); |
| 255 | |
| 256 | if ((packet->Flags() & TCP_FIN) != 0) { |
| 257 | fRemoteState = TCP_SOCKET_STATE_FIN_SENT; |
| 258 | TRACE(("FIN received\n")); |
| 259 | _Ack(); |
| 260 | } |
| 261 | |
| 262 | if (fState == TCP_SOCKET_STATE_SYN_SENT) { |
| 263 | if ((packet->Flags() & TCP_SYN) != 0 |
| 264 | && (packet->Flags() & TCP_ACK) != 0) { |
| 265 | fNextPacket = fAcknowledgeNumber = packet->SequenceNumber() + 1; |
| 266 | fRemoteState = TCP_SOCKET_STATE_SYN_SENT; |
| 267 | delete packet; |
| 268 | _Ack(); |
| 269 | fState = fRemoteState = TCP_SOCKET_STATE_OPEN; |
| 270 | return; |
| 271 | } |
| 272 | } else if (fState == TCP_SOCKET_STATE_OPEN) { |
| 273 | } else if (fState == TCP_SOCKET_STATE_FIN_SENT) { |
| 274 | if ((packet->Flags() & TCP_ACK) != 0) { |
| 275 | TRACE(("FIN-ACK received\n")); |
| 276 | if (fRemoteState == TCP_SOCKET_STATE_FIN_SENT) |
| 277 | fState = TCP_SOCKET_STATE_CLOSED; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /*if (fLastPacket == NULL) { |
| 282 | // no packets enqueued |
| 283 | packet->SetNext(NULL); |
| 284 | fFirstPacket = fLastPacket = packet; |
| 285 | } else if (fLastPacket->SequenceNumber() < packet->SequenceNumber()) { |
| 286 | // enqueue in back |
| 287 | fLastPacket->SetNext(packet); |
| 288 | fLastPacket = packet; |
| 289 | } else if (fFirstPacket->SequenceNumber() > packet->SequenceNumber()) { |
| 290 | // enqueue in front |
| 291 | packet->SetNext(fFirstPacket); |
| 292 | fFirstPacket = packet; |
| 293 | } else { |
| 294 | // enqueue in middle |
| 295 | for (TCPPacket* queuedPacket = fFirstPacket; queuedPacket != NULL; |
| 296 | queuedPacket = queuedPacket->Next()) { |
| 297 | if (queuedPacket->SequenceNumber() == packet->SequenceNumber()) { |
| 298 | TRACE(("TCPSocket::EnqueuePacket(): TCP packet dropped\n")); |
| 299 | // we may be waiting for a previous packet |
| 300 | return; |
| 301 | } |
| 302 | if (queuedPacket->Next()->SequenceNumber() > packet->SequenceNumber()) { |
| 303 | packet->SetNext(queuedPacket->Next()); |
| 304 | queuedPacket->SetNext(packet); |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | }*/ |
| 309 | } |
| 310 | |
| 311 | |
| 312 | TCPPacket* |
| 313 | TCPSocket::DequeuePacket() |
| 314 | { |
| 315 | //TRACE(("TCPSocket::DequeuePacket()\n")); |
| 316 | if (fFirstPacket == NULL) |
| 317 | return NULL; |
| 318 | |
| 319 | if (fFirstPacket->SequenceNumber() == fNextPacket) |
| 320 | return PopPacket(); |
| 321 | |
| 322 | for (TCPPacket* packet = fFirstPacket; |
| 323 | packet != NULL && packet->Next() != NULL; |
| 324 | packet = packet->Next()) { |
| 325 | if (packet->Next()->SequenceNumber() == fNextPacket) { |
| 326 | TCPPacket* nextPacket = packet->Next(); |
| 327 | packet->SetNext(nextPacket->Next()); |
| 328 | if (fLastPacket == nextPacket) |
| 329 | fLastPacket = packet; |
| 330 | fNextPacket += nextPacket->DataSize(); |
| 331 | return nextPacket; |
| 332 | } |
| 333 | } |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | TCPPacket* |
| 339 | TCPSocket::PopPacket() |
| 340 | { |
| 341 | //TRACE(("TCPSocket::PopPacket()\n")); |
| 342 | if (fFirstPacket == NULL) |
| 343 | return NULL; |
| 344 | |
| 345 | TCPPacket* packet = fFirstPacket; |
| 346 | fFirstPacket = packet->Next(); |
| 347 | if (fFirstPacket == NULL) |
| 348 | fLastPacket = NULL; |
| 349 | packet->SetNext(NULL); |
| 350 | return packet; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | status_t |
| 355 | TCPSocket::_Send(TCPPacket* packet) |
| 356 | { |
| 357 | ChainBuffer buffer((void*)packet->Data(), packet->DataSize()); |
| 358 | status_t error = fTCPService->Send(fPort, fRemoteAddress, fRemotePort, |
| 359 | packet->SequenceNumber(), fAcknowledgeNumber, packet->Flags(), |
| 360 | &buffer); |
| 361 | if (error != B_OK) |
| 362 | return error; |
| 363 | fSequenceNumber += packet->DataSize(); |
| 364 | _EnqueueOutgoingPacket(packet); |
| 365 | return B_OK; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | status_t |
| 370 | TCPSocket::_ResendQueue() |
| 371 | { |
| 372 | TRACE(("TCPSocket::_ResendQueue()\n")); |
| 373 | for (TCPPacket* packet = fFirstSentPacket; packet != NULL; |
| 374 | packet = packet->Next()) { |
| 375 | ChainBuffer buffer((void*)packet->Data(), packet->DataSize()); |
| 376 | status_t error = fTCPService->Send(fPort, fRemoteAddress, fRemotePort, |
| 377 | packet->SequenceNumber(), fAcknowledgeNumber, packet->Flags(), |
| 378 | &buffer); |
| 379 | if (error != B_OK) |
| 380 | return error; |
| 381 | } |
| 382 | return B_OK; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | void |
| 387 | TCPSocket::_EnqueueOutgoingPacket(TCPPacket* packet) |
| 388 | { |
| 389 | if (fLastSentPacket != NULL) { |
| 390 | fLastSentPacket->SetNext(packet); |
| 391 | fLastSentPacket = packet; |
| 392 | } else { |
| 393 | fFirstSentPacket = fLastSentPacket = packet; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | status_t |
| 399 | TCPSocket::_Ack() |
| 400 | { |
| 401 | TCPPacket* packet = new(nothrow) TCPPacket(); |
| 402 | if (packet == NULL) |
| 403 | return B_NO_MEMORY; |
| 404 | status_t error = packet->SetTo(NULL, 0, fAddress, fPort, fRemoteAddress, |
| 405 | fRemotePort, fSequenceNumber, fAcknowledgeNumber, TCP_ACK); |
| 406 | if (error != B_OK) |
| 407 | return error; |
| 408 | error = _Send(packet); |
| 409 | if (error != B_OK) |
| 410 | return error; |
| 411 | return B_OK; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | status_t |
| 416 | TCPSocket::_WaitForState(TCPSocketState state, bigtime_t timeout) |
| 417 | { |
| 418 | if (fTCPService == NULL) |
| 419 | return B_NO_INIT; |
| 420 | |
| 421 | bigtime_t startTime = system_time(); |
| 422 | do { |
| 423 | fTCPService->ProcessIncomingPackets(); |
| 424 | if (fState == state) |
| 425 | return B_OK; |
| 426 | } while (system_time() - startTime < timeout); |
| 427 | return timeout == 0 ? B_WOULD_BLOCK : B_TIMED_OUT; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | TCPService::TCPService(IPService* ipService) |
| 434 | : |
| 435 | IPSubService(kTCPServiceName), |
| 436 | fIPService(ipService) |
| 437 | { |
| 438 | } |
| 439 | |
| 440 | |
| 441 | TCPService::~TCPService() |
| 442 | { |
| 443 | if (fIPService != NULL) |
| 444 | fIPService->UnregisterIPSubService(this); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | status_t |
| 449 | TCPService::Init() |
| 450 | { |
| 451 | if (fIPService == NULL) |
| 452 | return B_BAD_VALUE; |
| 453 | if (!fIPService->RegisterIPSubService(this)) |
| 454 | return B_NO_MEMORY; |
| 455 | return B_OK; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | uint8 |
| 460 | TCPService::IPProtocol() const |
| 461 | { |
| 462 | return IPPROTO_TCP; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | void |
| 467 | TCPService::HandleIPPacket(IPService* ipService, ip_addr_t sourceIP, |
| 468 | ip_addr_t destinationIP, const void* data, size_t size) |
| 469 | { |
| 470 | TRACE(("TCPService::HandleIPPacket(): source = %08lx, destination = %08lx, " |
| 471 | "%lu - %lu bytes\n", sourceIP, destinationIP, size, sizeof(tcp_header))); |
| 472 | |
| 473 | if (data == NULL || size < sizeof(tcp_header)) |
| 474 | return; |
| 475 | |
| 476 | const tcp_header* header = (const tcp_header*)data; |
| 477 | uint16 source = ntohs(header->source); |
| 478 | uint16 destination = ntohs(header->destination); |
| 479 | uint32 sequenceNumber = ntohl(header->seqNumber); |
| 480 | uint32 ackedNumber = ntohl(header->ackNumber); |
| 481 | uint8 flags = 0; |
| 482 | if (header->syn) |
| 483 | flags |= TCP_SYN; |
| 484 | if (header->ack) |
| 485 | flags |= TCP_ACK; |
| 486 | if (header->fin) |
| 487 | flags |= TCP_FIN; |
| 488 | if (header->rst) |
| 489 | flags |= TCP_RST; |
| 490 | TRACE(("\tsource = %u, dest = %u, seq = %lu, ack = %lu, dataOffset = %u, flags %s %s %s %s\n", |
| 491 | source, destination, sequenceNumber, ackedNumber, header->dataOffset, |
| 492 | header->ack ? "ACK" : "", header->syn ? "SYN" : "", header->fin ? "FIN" : "", header->rst ? "RST" : "")); |
| 493 | if (header->dataOffset > 5) { |
| 494 | uint8* option = (uint8*)data + sizeof(tcp_header); |
| 495 | while ((uint32*)option < (uint32*)data + header->dataOffset) { |
| 496 | uint8 optionKind = option[0]; |
| 497 | uint8 optionLength = 1; |
| 498 | if (optionKind > 1) |
| 499 | optionLength = option[1]; |
| 500 | TRACE(("\tTCP option kind %u, length %u\n", optionKind, optionLength)); |
| 501 | option += optionLength; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | uint16 chksum = _ChecksumData(data, size, sourceIP, destinationIP); |
| 506 | if (chksum != 0) { |
| 507 | TRACE(("TCPService::HandleIPPacket(): invalid checksum (%04x)\n", header->checksum)); |
| 508 | //return; |
| 509 | } |
| 510 | |
| 511 | TCPSocket* socket = _FindSocket(destinationIP, destination); |
| 512 | if (socket == NULL) { |
| 513 | //TODO if SYN, answer with RST? |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | if (header->ack) { |
| 518 | socket->Acknowledge(ackedNumber); |
| 519 | } |
| 520 | |
| 521 | TCPPacket* packet = new(nothrow) TCPPacket(); |
| 522 | if (packet == NULL) |
| 523 | return; |
| 524 | status_t error = packet->SetTo((uint32*)data + header->dataOffset, |
| 525 | size - header->dataOffset * 4, sourceIP, source, destinationIP, |
| 526 | destination, sequenceNumber, ackedNumber, flags); |
| 527 | if (error == B_OK) |
| 528 | socket->ProcessPacket(packet); |
| 529 | else |
| 530 | delete packet; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | status_t |
| 535 | TCPService::Send(uint16 sourcePort, ip_addr_t destinationAddress, |
| 536 | uint16 destinationPort, uint32 sequenceNumber, |
| 537 | uint32 acknowledgmentNumber, uint8 flags, ChainBuffer* buffer) |
| 538 | { |
| 539 | TRACE(("TCPService::Send(): seq = %lu, ack = %lu\n", sequenceNumber, acknowledgmentNumber)); |
| 540 | if (fIPService == NULL) |
| 541 | return B_NO_INIT; |
| 542 | if (buffer == NULL) |
| 543 | return B_BAD_VALUE; |
| 544 | |
| 545 | tcp_header header; |
| 546 | ChainBuffer headerBuffer(&header, sizeof(header), buffer); |
| 547 | memset(&header, 0, sizeof(header)); |
| 548 | header.source = htons(sourcePort); |
| 549 | header.destination = htons(destinationPort); |
| 550 | header.seqNumber = htonl(sequenceNumber); |
| 551 | header.ackNumber = htonl(acknowledgmentNumber); |
| 552 | header.dataOffset = 5; |
| 553 | header.ack = (flags & TCP_ACK) != 0; |
| 554 | header.syn = (flags & TCP_SYN) != 0; |
| 555 | header.fin = (flags & TCP_FIN) != 0; |
| 556 | header.rst = (flags & TCP_RST) != 0; |
| 557 | header.window = 0xffff; |
| 558 | |
| 559 | header.checksum = 0; |
| 560 | header.checksum = htons(_ChecksumBuffer(&headerBuffer, |
| 561 | fIPService->IPAddress(), destinationAddress, |
| 562 | headerBuffer.TotalSize())); |
| 563 | |
| 564 | return fIPService->Send(destinationAddress, IPPROTO_TCP, &headerBuffer); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | void |
| 569 | TCPService::ProcessIncomingPackets() |
| 570 | { |
| 571 | if (fIPService != NULL) |
| 572 | fIPService->ProcessIncomingPackets(); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | status_t |
| 577 | TCPService::BindSocket(TCPSocket* socket) |
| 578 | { |
| 579 | if (socket == NULL) |
| 580 | return B_BAD_VALUE; |
| 581 | |
| 582 | if (_FindSocket(socket->Address(), socket->Port())) |
| 583 | return EADDRINUSE; |
| 584 | |
| 585 | return fSockets.Add(socket); |
| 586 | } |
| 587 | |
| 588 | |
| 589 | void |
| 590 | TCPService::UnbindSocket(TCPSocket* socket) |
| 591 | { |
| 592 | fSockets.Remove(socket); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | uint16 |
| 597 | TCPService::_ChecksumBuffer(ChainBuffer* buffer, ip_addr_t source, |
| 598 | ip_addr_t destination, uint16 length) |
| 599 | { |
| 600 | struct pseudo_header { |
| 601 | ip_addr_t source; |
| 602 | ip_addr_t destination; |
| 603 | uint8 pad; |
| 604 | uint8 protocol; |
| 605 | uint16 length; |
| 606 | } __attribute__ ((__packed__)); |
| 607 | pseudo_header header = { |
| 608 | htonl(source), |
| 609 | htonl(destination), |
| 610 | 0, |
| 611 | IPPROTO_TCP, |
| 612 | htons(length) |
| 613 | }; |
| 614 | |
| 615 | ChainBuffer headerBuffer(&header, sizeof(header), buffer); |
| 616 | uint16 checksum = ip_checksum(&headerBuffer); |
| 617 | headerBuffer.DetachNext(); |
| 618 | return checksum; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | uint16 |
| 623 | TCPService::_ChecksumData(const void* data, uint16 length, ip_addr_t source, |
| 624 | ip_addr_t destination) |
| 625 | { |
| 626 | ChainBuffer buffer((void*)data, length); |
| 627 | return _ChecksumBuffer(&buffer, source, destination, length); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | TCPSocket* |
| 632 | TCPService::_FindSocket(ip_addr_t address, uint16 port) |
| 633 | { |
| 634 | for (int i = 0; i < fSockets.Count(); i++) { |
| 635 | TCPSocket* socket = fSockets.ElementAt(i); |
| 636 | if ((address == INADDR_ANY || socket->Address() == INADDR_ANY |
| 637 | || socket->Address() == address) |
| 638 | && socket->Port() == port) { |
| 639 | return socket; |
| 640 | } |
| 641 | } |
| 642 | return NULL; |
| 643 | } |