Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/inet/tcp.c

    rf9b2cb4c r1f2b07a  
    4444static int tcp_conn_fibril(void *);
    4545
    46 /** Incoming TCP connection info
    47  *
    48  * Used to pass information about incoming TCP connection to the connection
    49  * fibril
    50  */
     46/** Incoming TCP connection info */
    5147typedef struct {
    52         /** Listener who received the connection */
    5348        tcp_listener_t *lst;
    54         /** Incoming connection */
    5549        tcp_conn_t *conn;
    5650} tcp_in_conn_t;
    5751
    58 /** Create callback connection from TCP service.
    59  *
    60  * @param tcp TCP service
    61  * @return EOK on success or negative error code
    62  */
    6352static int tcp_callback_create(tcp_t *tcp)
    6453{
     
    6655
    6756        aid_t req = async_send_0(exch, TCP_CALLBACK_CREATE, NULL);
    68        
    69         port_id_t port;
    70         int rc = async_create_callback_port(exch, INTERFACE_TCP_CB, 0, 0,
    71             tcp_cb_conn, tcp, &port);
    72        
     57        int rc = async_connect_to_me(exch, 0, 0, 0, tcp_cb_conn, tcp);
    7358        async_exchange_end(exch);
    7459
     
    8267}
    8368
    84 /** Create TCP client instance.
    85  *
    86  * @param  rtcp Place to store pointer to new TCP client
    87  * @return EOK on success, ENOMEM if out of memory, EIO if service
    88  *         cannot be contacted
    89  */
    9069int tcp_create(tcp_t **rtcp)
    9170{
     
    11291        }
    11392
    114         tcp->sess = loc_service_connect(tcp_svcid, INTERFACE_TCP,
     93        tcp->sess = loc_service_connect(EXCHANGE_SERIALIZE, tcp_svcid,
    11594            IPC_FLAG_BLOCKING);
    11695        if (tcp->sess == NULL) {
     
    132111}
    133112
    134 /** Destroy TCP client instance.
    135  *
    136  * @param tcp TCP client
    137  */
    138113void tcp_destroy(tcp_t *tcp)
    139114{
     
    151126}
    152127
    153 /** Create new TCP connection
    154  *
    155  * @param tcp   TCP client instance
    156  * @param id    Connection ID
    157  * @param cb    Callbacks
    158  * @param arg   Callback argument
    159  * @param rconn Place to store pointer to new connection
    160  *
    161  * @return EOK on success, ENOMEM if out of memory
    162  */
    163128static int tcp_conn_new(tcp_t *tcp, sysarg_t id, tcp_cb_t *cb, void *arg,
    164129    tcp_conn_t **rconn)
     
    185150}
    186151
    187 /** Create new TCP connection.
    188  *
    189  * Open a connection to the specified destination. This function returns
    190  * even before the connection is established (or not). When the connection
    191  * is established, @a cb->connected is called. If the connection fails,
    192  * @a cb->conn_failed is called. Alternatively, the caller can call
    193  * @c tcp_conn_wait_connected() to wait for connection to complete or fail.
    194  * Other callbacks are available to monitor the changes in connection state.
    195  *
    196  * @a epp must specify the remote address and port. Both local address and
    197  * port are optional. If local address is not specified, address selection
    198  * will take place. If local port number is not specified, a suitable
    199  * free dynamic port number will be allocated.
    200  *
    201  * @param tcp   TCP client
    202  * @param epp   Internet endpoint pair
    203  * @param cb    Callbacks
    204  * @param arg   Argument to callbacks
    205  * @param rconn Place to store pointer to new connection
    206  *
    207  * @return EOK on success or negative error code.
    208  */
    209152int tcp_conn_create(tcp_t *tcp, inet_ep2_t *epp, tcp_cb_t *cb, void *arg,
    210153    tcp_conn_t **rconn)
     
    243186}
    244187
    245 /** Destroy TCP connection.
    246  *
    247  * Destroy TCP connection. The caller should destroy all connections
    248  * he created before destroying the TCP client and before terminating.
    249  *
    250  * @param conn TCP connection
    251  */
    252188void tcp_conn_destroy(tcp_conn_t *conn)
    253189{
     
    267203}
    268204
    269 /** Get connection based on its ID.
    270  *
    271  * @param tcp   TCP client
    272  * @param id    Connection ID
    273  * @param rconn Place to store pointer to connection
    274  *
    275  * @return EOK on success, EINVAL if no connection with the given ID exists
    276  */
    277205static int tcp_conn_get(tcp_t *tcp, sysarg_t id, tcp_conn_t **rconn)
    278206{
     
    287215}
    288216
    289 /** Get the user/callback argument for a connection.
    290  *
    291  * @param conn TCP connection
    292  * @return User argument associated with connection
    293  */
    294217void *tcp_conn_userptr(tcp_conn_t *conn)
    295218{
     
    297220}
    298221
    299 /** Create a TCP connection listener.
    300  *
    301  * A listener listens for connections on the set of endpoints specified
    302  * by @a ep. Each time a new incoming connection is established,
    303  * @a lcb->new_conn is called (and passed @a larg). Also, the new connection
    304  * will have callbacks set to @a cb and argument to @a arg.
    305  *
    306  * @a ep must specify a valid port number. @a ep may specify an address
    307  * or link to listen on. If it does not, the listener will listen on
    308  * all links/addresses.
    309  *
    310  * @param tcp  TCP client
    311  * @param ep   Internet endpoint
    312  * @param lcb  Listener callbacks
    313  * @param larg Listener callback argument
    314  * @param cb   Connection callbacks for every new connection
    315  * @param arg  Connection argument for every new connection
    316  * @param rlst Place to store pointer to new listener
    317  *
    318  * @return EOK on success or negative error code
    319  */
    320222int tcp_listener_create(tcp_t *tcp, inet_ep_t *ep, tcp_listen_cb_t *lcb,
    321223    void *larg, tcp_cb_t *cb, void *arg, tcp_listener_t **rlst)
     
    363265}
    364266
    365 /** Destroy TCP connection listener.
    366  *
    367  * @param lst Listener
    368  */
    369267void tcp_listener_destroy(tcp_listener_t *lst)
    370268{
     
    384282}
    385283
    386 /** Get TCP connection listener based on its ID.
    387  *
    388  * @param tcp TCP client
    389  * @param id  Listener ID
    390  * @param rlst Place to store pointer to listener
    391  *
    392  * @return EOK on success, EINVAL if no listener with the given ID is found
    393  */
    394284static int tcp_listener_get(tcp_t *tcp, sysarg_t id, tcp_listener_t **rlst)
    395285{
     
    404294}
    405295
    406 /** Get callback/user argument associated with listener.
    407  *
    408  * @param lst Listener
    409  * @return Callback/user argument
    410  */
    411296void *tcp_listener_userptr(tcp_listener_t *lst)
    412297{
     
    414299}
    415300
    416 /** Wait until connection is either established or connection fails.
    417  *
    418  * Can be called after calling tcp_conn_create() to block until connection
    419  * either completes or fails. If the connection fails, EIO is returned.
    420  * In this case the connection still exists, but is in a failed
    421  * state.
    422  *
    423  * @param conn Connection
    424  * @return EOK if connection is established, EIO otherwise
    425  */
    426301int tcp_conn_wait_connected(tcp_conn_t *conn)
    427302{
     
    440315}
    441316
    442 /** Send data over TCP connection.
    443  *
    444  * @param conn  Connection
    445  * @param data  Data
    446  * @param bytes Data size in bytes
    447  *
    448  * @return EOK on success or negative error code
    449  */
    450317int tcp_conn_send(tcp_conn_t *conn, const void *data, size_t bytes)
    451318{
     
    473340}
    474341
    475 /** Send FIN.
    476  *
    477  * Send FIN, indicating no more data will be send over the connection.
    478  *
    479  * @param conn Connection
    480  * @return EOK on success or negative error code
    481  */
     342
    482343int tcp_conn_send_fin(tcp_conn_t *conn)
    483344{
     
    491352}
    492353
    493 /** Push connection.
    494  *
    495  * @param conn Connection
    496  * @return EOK on success or negative error code
    497  */
    498354int tcp_conn_push(tcp_conn_t *conn)
    499355{
     
    507363}
    508364
    509 /** Reset connection.
    510  *
    511  * @param conn Connection
    512  * @return EOK on success or negative error code
    513  */
    514365int tcp_conn_reset(tcp_conn_t *conn)
    515366{
     
    523374}
    524375
    525 /** Read received data from connection without blocking.
    526  *
    527  * If any received data is pending on the connection, up to @a bsize bytes
    528  * are copied to @a buf and the acutal number is stored in @a *nrecv.
    529  * The entire buffer of @a bsize bytes is filled except when less data
    530  * is currently available or FIN is received. EOK is returned.
    531  *
    532  * If no received data is pending, returns EAGAIN.
    533  *
    534  * @param conn Connection
    535  * @param buf  Buffer
    536  * @param bsize Buffer size
    537  * @param nrecv Place to store actual number of received bytes
    538  *
    539  * @return EOK on success, EAGAIN if no received data is pending, or other
    540  *         negative error code in case of other error
    541  */
    542376int tcp_conn_recv(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv)
    543377{
     
    574408}
    575409
    576 /** Read received data from connection with blocking.
    577  *
    578  * Wait for @a bsize bytes of data to be received and copy them to
    579  * @a buf. Less data may be returned if FIN is received on the connection.
    580  * The actual If any received data is written to @a *nrecv and EOK
    581  * is returned on success.
    582  *
    583  * @param conn Connection
    584  * @param buf  Buffer
    585  * @param bsize Buffer size
    586  * @param nrecv Place to store actual number of received bytes
    587  *
    588  * @return EOK on success or negative error code
    589  */
    590 int tcp_conn_recv_wait(tcp_conn_t *conn, void *buf, size_t bsize,
    591     size_t *nrecv)
     410int tcp_conn_recv_wait(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv)
    592411{
    593412        async_exch_t *exch;
     
    631450}
    632451
    633 /** Connection established event.
    634  *
    635  * @param tcp TCP client
    636  * @param iid Call ID
    637  * @param icall Call data
    638  */
    639452static void tcp_ev_connected(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    640453{
     
    659472}
    660473
    661 /** Connection failed event.
    662  *
    663  * @param tcp TCP client
    664  * @param iid Call ID
    665  * @param icall Call data
    666  */
    667474static void tcp_ev_conn_failed(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    668475{
     
    687494}
    688495
    689 /** Connection reset event.
    690  *
    691  * @param tcp TCP client
    692  * @param iid Call ID
    693  * @param icall Call data
    694  */
    695496static void tcp_ev_conn_reset(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    696497{
     
    715516}
    716517
    717 /** Data available event.
    718  *
    719  * @param tcp TCP client
    720  * @param iid Call ID
    721  * @param icall Call data
    722  */
    723518static void tcp_ev_data(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    724519{
     
    744539}
    745540
    746 /** Urgent data event.
    747  *
    748  * @param tcp TCP client
    749  * @param iid Call ID
    750  * @param icall Call data
    751  */
    752541static void tcp_ev_urg_data(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    753542{
     
    755544}
    756545
    757 /** New connection event.
    758  *
    759  * @param tcp TCP client
    760  * @param iid Call ID
    761  * @param icall Call data
    762  */
    763546static void tcp_ev_new_conn(tcp_t *tcp, ipc_callid_t iid, ipc_call_t *icall)
    764547{
     
    807590}
    808591
    809 /** Callback connection handler.
    810  *
    811  * @param iid Connect call ID
    812  * @param icall Connect call data
    813  * @param arg Argument, TCP client
    814  */
    815592static void tcp_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    816593{
     
    859636}
    860637
    861 /** Fibril for handling incoming TCP connection in background.
    862  *
    863  * @param arg Argument, incoming connection information (@c tcp_in_conn_t)
    864  */
     638/** Fibril for handling incoming TCP connection in background */
    865639static int tcp_conn_fibril(void *arg)
    866640{
Note: See TracChangeset for help on using the changeset viewer.