Changeset b10460a in mainline for uspace/lib/c/generic/inet/tcp.c
- Timestamp:
- 2015-08-07T21:39:00Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b688fd8
- Parents:
- 6accc5cf
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/inet/tcp.c
r6accc5cf rb10460a 44 44 static int tcp_conn_fibril(void *); 45 45 46 /** Incoming TCP connection info */ 46 /** Incoming TCP connection info 47 * 48 * Used to pass information about incoming TCP connection to the connection 49 * fibril 50 */ 47 51 typedef struct { 52 /** Listener who received the connection */ 48 53 tcp_listener_t *lst; 54 /** Incoming connection */ 49 55 tcp_conn_t *conn; 50 56 } tcp_in_conn_t; 51 57 58 /** Create callback connection from TCP service. 59 * 60 * @param tcp TCP service 61 * @return EOK on success or negative error code 62 */ 52 63 static int tcp_callback_create(tcp_t *tcp) 53 64 { … … 67 78 } 68 79 80 /** Create TCP client instance. 81 * 82 * @param rtcp Place to store pointer to new TCP client 83 * @return EOK on success, ENOMEM if out of memory, EIO if service 84 * cannot be contacted 85 */ 69 86 int tcp_create(tcp_t **rtcp) 70 87 { … … 111 128 } 112 129 130 /** Destroy TCP client instance. 131 * 132 * @param tcp TCP client 133 */ 113 134 void tcp_destroy(tcp_t *tcp) 114 135 { … … 126 147 } 127 148 149 /** Create new TCP connection 150 * 151 * @param tcp TCP client instance 152 * @param id Connection ID 153 * @param cb Callbacks 154 * @param arg Callback argument 155 * @param rconn Place to store pointer to new connection 156 * 157 * @return EOK on success, ENOMEM if out of memory 158 */ 128 159 static int tcp_conn_new(tcp_t *tcp, sysarg_t id, tcp_cb_t *cb, void *arg, 129 160 tcp_conn_t **rconn) … … 150 181 } 151 182 183 /** Create new TCP connection. 184 * 185 * Open a connection to the specified destination. This function returns 186 * even before the connection is established (or not). When the connection 187 * is established, @a cb->connected is called. If the connection fails, 188 * @a cb->conn_failed is called. Alternatively, the caller can call 189 * @c tcp_conn_wait_connected() to wait for connection to complete or fail. 190 * Other callbacks are available to monitor the changes in connection state. 191 * 192 * @a epp must specify the remote address and port. Both local address and 193 * port are optional. If local address is not specified, address selection 194 * will take place. If local port number is not specified, a suitable 195 * free dynamic port number will be allocated. 196 * 197 * @param tcp TCP client 198 * @param epp Internet endpoint pair 199 * @param cb Callbacks 200 * @param arg Argument to callbacks 201 * @param rconn Place to store pointer to new connection 202 * 203 * @return EOK on success or negative error code. 204 */ 152 205 int tcp_conn_create(tcp_t *tcp, inet_ep2_t *epp, tcp_cb_t *cb, void *arg, 153 206 tcp_conn_t **rconn) … … 186 239 } 187 240 241 /** Destroy TCP connection. 242 * 243 * Destroy TCP connection. The caller should destroy all connections 244 * he created before destroying the TCP client and before terminating. 245 * 246 * @param conn TCP connection 247 */ 188 248 void tcp_conn_destroy(tcp_conn_t *conn) 189 249 { … … 203 263 } 204 264 265 /** Get connection based on its ID. 266 * 267 * @param tcp TCP client 268 * @param id Connection ID 269 * @param rconn Place to store pointer to connection 270 * 271 * @return EOK on success, EINVAL if no connection with the given ID exists 272 */ 205 273 static int tcp_conn_get(tcp_t *tcp, sysarg_t id, tcp_conn_t **rconn) 206 274 { … … 215 283 } 216 284 285 /** Get the user/callback argument for a connection. 286 * 287 * @param conn TCP connection 288 * @return User argument associated with connection 289 */ 217 290 void *tcp_conn_userptr(tcp_conn_t *conn) 218 291 { … … 220 293 } 221 294 295 /** Create a TCP connection listener. 296 * 297 * A listener listens for connections on the set of endpoints specified 298 * by @a ep. Each time a new incoming connection is established, 299 * @a lcb->new_conn is called (and passed @a larg). Also, the new connection 300 * will have callbacks set to @a cb and argument to @a arg. 301 * 302 * @a ep must specify a valid port number. @a ep may specify an address 303 * or link to listen on. If it does not, the listener will listen on 304 * all links/addresses. 305 * 306 * @param tcp TCP client 307 * @param ep Internet endpoint 308 * @param lcb Listener callbacks 309 * @param larg Listener callback argument 310 * @param cb Connection callbacks for every new connection 311 * @param arg Connection argument for every new connection 312 * @param rlst Place to store pointer to new listener 313 * 314 * @return EOK on success or negative error code 315 */ 222 316 int tcp_listener_create(tcp_t *tcp, inet_ep_t *ep, tcp_listen_cb_t *lcb, 223 317 void *larg, tcp_cb_t *cb, void *arg, tcp_listener_t **rlst) … … 265 359 } 266 360 361 /** Destroy TCP connection listener. 362 * 363 * @param lst Listener 364 */ 267 365 void tcp_listener_destroy(tcp_listener_t *lst) 268 366 { … … 282 380 } 283 381 382 /** Get TCP connection listener based on its ID. 383 * 384 * @param tcp TCP client 385 * @param id Listener ID 386 * @param rlst Place to store pointer to listener 387 * 388 * @return EOK on success, EINVAL if no listener with the given ID is found 389 */ 284 390 static int tcp_listener_get(tcp_t *tcp, sysarg_t id, tcp_listener_t **rlst) 285 391 { … … 294 400 } 295 401 402 /** Get callback/user argument associated with listener. 403 * 404 * @param lst Listener 405 * @return Callback/user argument 406 */ 296 407 void *tcp_listener_userptr(tcp_listener_t *lst) 297 408 { … … 299 410 } 300 411 412 /** Wait until connection is either established or connection fails. 413 * 414 * Can be called after calling tcp_conn_create() to block until connection 415 * either completes or fails. If the connection fails, EIO is returned. 416 * In this case the connection still exists, but is in a failed 417 * state. 418 * 419 * @param conn Connection 420 * @return EOK if connection is established, EIO otherwise 421 */ 301 422 int tcp_conn_wait_connected(tcp_conn_t *conn) 302 423 { … … 315 436 } 316 437 438 /** Send data over TCP connection. 439 * 440 * @param conn Connection 441 * @param data Data 442 * @param bytes Data size in bytes 443 * 444 * @return EOK on success or negative error code 445 */ 317 446 int tcp_conn_send(tcp_conn_t *conn, const void *data, size_t bytes) 318 447 { … … 340 469 } 341 470 342 471 /** Send FIN. 472 * 473 * Send FIN, indicating no more data will be send over the connection. 474 * 475 * @param conn Connection 476 * @return EOK on success or negative error code 477 */ 343 478 int tcp_conn_send_fin(tcp_conn_t *conn) 344 479 { … … 352 487 } 353 488 489 /** Push connection. 490 * 491 * @param conn Connection 492 * @return EOK on success or negative error code 493 */ 354 494 int tcp_conn_push(tcp_conn_t *conn) 355 495 { … … 363 503 } 364 504 505 /** Reset connection. 506 * 507 * @param conn Connection 508 * @return EOK on success or negative error code 509 */ 365 510 int tcp_conn_reset(tcp_conn_t *conn) 366 511 { … … 374 519 } 375 520 521 /** Read received data from connection without blocking. 522 * 523 * If any received data is pending on the connection, up to @a bsize bytes 524 * are copied to @a buf and the acutal number is stored in @a *nrecv. 525 * The entire buffer of @a bsize bytes is filled except when less data 526 * is currently available or FIN is received. EOK is returned. 527 * 528 * If no received data is pending, returns EAGAIN. 529 * 530 * @param conn Connection 531 * @param buf Buffer 532 * @param bsize Buffer size 533 * @param nrecv Place to store actual number of received bytes 534 * 535 * @return EOK on success, EAGAIN if no received data is pending, or other 536 * negative error code in case of other error 537 */ 376 538 int tcp_conn_recv(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv) 377 539 { … … 408 570 } 409 571 410 int tcp_conn_recv_wait(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv) 572 /** Read received data from connection with blocking. 573 * 574 * Wait for @a bsize bytes of data to be received and copy them to 575 * @a buf. Less data may be returned if FIN is received on the connection. 576 * The actual If any received data is written to @a *nrecv and EOK 577 * is returned on success. 578 * 579 * @param conn Connection 580 * @param buf Buffer 581 * @param bsize Buffer size 582 * @param nrecv Place to store actual number of received bytes 583 * 584 * @return EOK on success or negative error code 585 */ 586 int tcp_conn_recv_wait(tcp_conn_t *conn, void *buf, size_t bsize, 587 size_t *nrecv) 411 588 { 412 589 async_exch_t *exch; … … 450 627 } 451 628 629 /** Connection established event. 630 * 631 * @param tcp TCP client 632 * @param iid Call ID 633 * @param icall Call data 634 */ 452 635 static void tcp_ev_connected(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 453 636 { … … 472 655 } 473 656 657 /** Connection failed event. 658 * 659 * @param tcp TCP client 660 * @param iid Call ID 661 * @param icall Call data 662 */ 474 663 static void tcp_ev_conn_failed(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 475 664 { … … 494 683 } 495 684 685 /** Connection reset event. 686 * 687 * @param tcp TCP client 688 * @param iid Call ID 689 * @param icall Call data 690 */ 496 691 static void tcp_ev_conn_reset(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 497 692 { … … 516 711 } 517 712 713 /** Data available event. 714 * 715 * @param tcp TCP client 716 * @param iid Call ID 717 * @param icall Call data 718 */ 518 719 static void tcp_ev_data(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 519 720 { … … 539 740 } 540 741 742 /** Urgent data event. 743 * 744 * @param tcp TCP client 745 * @param iid Call ID 746 * @param icall Call data 747 */ 541 748 static void tcp_ev_urg_data(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 542 749 { … … 544 751 } 545 752 753 /** New connection event. 754 * 755 * @param tcp TCP client 756 * @param iid Call ID 757 * @param icall Call data 758 */ 546 759 static void tcp_ev_new_conn(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall) 547 760 { … … 590 803 } 591 804 805 /** Callback connection handler. 806 * 807 * @param iid Connect call ID 808 * @param icall Connect call data 809 * @param arg Argument, TCP client 810 */ 592 811 static void tcp_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg) 593 812 { … … 636 855 } 637 856 638 /** Fibril for handling incoming TCP connection in background */ 857 /** Fibril for handling incoming TCP connection in background. 858 * 859 * @param arg Argument, incoming connection information (@c tcp_in_conn_t) 860 */ 639 861 static int tcp_conn_fibril(void *arg) 640 862 {
Note:
See TracChangeset
for help on using the changeset viewer.