Changeset 2f19103 in mainline for uspace/srv/net/tcp/conn.c


Ignore:
Timestamp:
2015-05-22T07:21:37Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
58e9dec
Parents:
bf7587b0
Message:

TCP and UDP servers can make use of inet/endpoint.h types internally.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/tcp/conn.c

    rbf7587b0 r2f19103  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3838#include <stdbool.h>
    3939#include <errno.h>
     40#include <inet/endpoint.h>
    4041#include <io/log.h>
    4142#include <macros.h>
     
    6465/** Create new connection structure.
    6566 *
    66  * @param lsock         Local socket (will be deeply copied)
    67  * @param fsock         Foreign socket (will be deeply copied)
     67 * @param epp           Endpoint pair (will be deeply copied)
    6868 * @return              New connection or NULL
    6969 */
    70 tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
     70tcp_conn_t *tcp_conn_new(inet_ep2_t *epp)
    7171{
    7272        tcp_conn_t *conn = NULL;
     
    128128        conn->ap = ap_passive;
    129129        conn->fin_is_acked = false;
    130         conn->ident.local = *lsock;
    131         if (fsock != NULL)
    132                 conn->ident.foreign = *fsock;
     130        if (epp != NULL)
     131                conn->ident = *epp;
    133132
    134133        return conn;
     
    335334}
    336335
    337 /** Match socket with pattern. */
    338 static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
     336/** Match endpoint with pattern. */
     337static bool tcp_ep_match(inet_ep_t *ep, inet_ep_t *patt)
    339338{
    340339        log_msg(LOG_DEFAULT, LVL_DEBUG2,
    341             "tcp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port);
    342        
     340            "tcp_ep_match(ep=(%u), pat=(%u))", ep->port, patt->port);
     341
    343342        if ((!inet_addr_is_any(&patt->addr)) &&
    344             (!inet_addr_compare(&patt->addr, &sock->addr)))
     343            (!inet_addr_compare(&patt->addr, &ep->addr)))
    345344                return false;
    346345
    347346        if ((patt->port != TCP_PORT_ANY) &&
    348             (patt->port != sock->port))
     347            (patt->port != ep->port))
    349348                return false;
    350349
     
    354353}
    355354
    356 /** Match socket pair with pattern. */
    357 static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
    358 {
    359         log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_sockpair_match(%p, %p)", sp, pattern);
    360 
    361         if (!tcp_socket_match(&sp->local, &pattern->local))
     355/** Match endpoint pair with pattern. */
     356static bool tcp_ep2_match(inet_ep2_t *epp, inet_ep2_t *pattern)
     357{
     358        log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_ep2_match(%p, %p)", epp, pattern);
     359
     360        if (!tcp_ep_match(&epp->local, &pattern->local))
    362361                return false;
    363362
    364         if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
     363        if (!tcp_ep_match(&epp->remote, &pattern->remote))
    365364                return false;
    366365
     
    368367}
    369368
    370 /** Find connection structure for specified socket pair.
    371  *
    372  * A connection is uniquely identified by a socket pair. Look up our
    373  * connection map and return connection structure based on socket pair.
     369/** Find connection structure for specified endpoint pair.
     370 *
     371 * A connection is uniquely identified by a endpoint pair. Look up our
     372 * connection map and return connection structure based on endpoint pair.
    374373 * The connection reference count is bumped by one.
    375374 *
    376  * @param sp    Socket pair
     375 * @param epp   Endpoint pair
    377376 * @return      Connection structure or NULL if not found.
    378377 */
    379 tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp)
    380 {
    381         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
    382        
     378tcp_conn_t *tcp_conn_find_ref(inet_ep2_t *epp)
     379{
     380        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", epp);
     381
    383382        log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%u), l:(%u))",
    384             sp->foreign.port, sp->local.port);
    385        
     383            epp->remote.port, epp->local.port);
     384
    386385        fibril_mutex_lock(&conn_list_lock);
    387        
     386
    388387        list_foreach(conn_list, link, tcp_conn_t, conn) {
    389                 tcp_sockpair_t *csp = &conn->ident;
    390                
     388                inet_ep2_t *cepp = &conn->ident;
     389
    391390                log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%u), l:(%u))",
    392                     csp->foreign.port, csp->local.port);
    393                
    394                 if (tcp_sockpair_match(sp, csp)) {
     391                    cepp->remote.port, cepp->local.port);
     392
     393                if (tcp_ep2_match(epp, cepp)) {
    395394                        tcp_conn_addref(conn);
    396395                        fibril_mutex_unlock(&conn_list_lock);
     
    398397                }
    399398        }
    400        
     399
    401400        fibril_mutex_unlock(&conn_list_lock);
    402401        return NULL;
     
    12701269}
    12711270
    1272 /** Handle unexpected segment received on a socket pair.
     1271/** Handle unexpected segment received on an endpoint pair.
    12731272 *
    12741273 * We reply with an RST unless the received segment has RST.
    12751274 *
    1276  * @param sp            Socket pair which received the segment
     1275 * @param sp            Endpoint pair which received the segment
    12771276 * @param seg           Unexpected segment
    12781277 */
    1279 void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
    1280 {
    1281         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
     1278void tcp_unexpected_segment(inet_ep2_t *epp, tcp_segment_t *seg)
     1279{
     1280        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", epp,
     1281            seg);
    12821282
    12831283        if ((seg->ctrl & CTL_RST) == 0)
    1284                 tcp_reply_rst(sp, seg);
    1285 }
    1286 
    1287 /** Compute flipped socket pair for response.
    1288  *
    1289  * Flipped socket pair has local and foreign sockets exchanged.
    1290  *
    1291  * @param sp            Socket pair
    1292  * @param fsp           Place to store flipped socket pair
    1293  */
    1294 void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
    1295 {
    1296         fsp->local = sp->foreign;
    1297         fsp->foreign = sp->local;
     1284                tcp_reply_rst(epp, seg);
     1285}
     1286
     1287/** Compute flipped endpoint pair for response.
     1288 *
     1289 * Flipped endpoint pair has local and remote endpoints exchanged.
     1290 *
     1291 * @param epp           Endpoint pair
     1292 * @param fepp          Place to store flipped endpoint pair
     1293 */
     1294void tcp_ep2_flipped(inet_ep2_t *epp, inet_ep2_t *fepp)
     1295{
     1296        fepp->local = epp->remote;
     1297        fepp->remote = epp->local;
    12981298}
    12991299
    13001300/** Send RST in response to an incoming segment.
    13011301 *
    1302  * @param sp            Socket pair which received the segment
     1302 * @param epp           Endpoint pair which received the segment
    13031303 * @param seg           Incoming segment
    13041304 */
    1305 void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
     1305void tcp_reply_rst(inet_ep2_t *epp, tcp_segment_t *seg)
    13061306{
    13071307        tcp_segment_t *rseg;
    13081308
    1309         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
     1309        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", epp, seg);
    13101310
    13111311        rseg = tcp_segment_make_rst(seg);
    1312         tcp_transmit_segment(sp, rseg);
     1312        tcp_transmit_segment(epp, rseg);
    13131313}
    13141314
Note: See TracChangeset for help on using the changeset viewer.