Navigation


Changeset 24:bd83ce9328ed in freeDiameter for freeDiameter/cnxctx.c


Ignore:
Timestamp:
Oct 21, 2009, 6:42:45 PM (15 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Cleanups and completed sctp code (not finished)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameter/cnxctx.c

    r23 r24  
    174174        return NULL;
    175175}
    176 #ifndef DISABLE_SCTP
     176
     177/* Same function for SCTP, with a list of local endpoints to bind to */
    177178struct cnxctx * fd_cnx_serv_sctp(uint16_t port, struct fd_list * ep_list)
    178179{
     180#ifdef DISABLE_SCTP
     181        TRACE_DEBUG(INFO, "This function should never been called when SCTP is disabled...");
     182        ASSERT(0);
     183        CHECK_FCT_DO( ENOTSUP, return NULL);
     184#else /* DISABLE_SCTP */
    179185        struct cnxctx * cnx = NULL;
    180186        sSS dummy;
     
    201207        fd_cnx_destroy(cnx);
    202208        return NULL;
    203 }
    204209#endif /* DISABLE_SCTP */
     210}
    205211
    206212/* Allow clients to connect on the server socket */
     
    239245        CHECK_PARAMS_DO(serv, return NULL);
    240246       
     247        /* Accept the new connection -- this is blocking until new client enters or cancellation */
    241248        CHECK_SYS_DO( cli_sock = accept(serv->cc_socket, (sSA *)&ss, &ss_len), return NULL );
    242249       
    243250        if (TRACE_BOOL(INFO)) {
    244                 fd_log_debug("%s - new client [", fd_cnx_getid(serv));
    245                 sSA_DUMP_NODE( &ss, AI_NUMERICHOST );
    246                 fd_log_debug("] connected.\n");
     251                fd_log_debug("%s : accepted new client [", fd_cnx_getid(serv));
     252                sSA_DUMP_NODE( &ss, NI_NUMERICHOST );
     253                fd_log_debug("].\n");
    247254        }
    248255       
     
    259266                /* Numeric values for debug */
    260267                rc = getnameinfo((sSA *)&ss, sizeof(sSS), addrbuf, sizeof(addrbuf), portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
    261                 if (rc)
     268                if (rc) {
    262269                        snprintf(addrbuf, sizeof(addrbuf), "[err:%s]", gai_strerror(rc));
    263                
    264                 snprintf(cli->cc_id, sizeof(cli->cc_id), "Client %s [%s]:%s (%d) / serv (%d)",
     270                        portbuf[0] = '\0';
     271                }
     272               
     273                snprintf(cli->cc_id, sizeof(cli->cc_id), "Incoming %s [%s]:%s (%d) @ serv (%d)",
    265274                                IPPROTO_NAME(cli->cc_proto),
    266275                                addrbuf, portbuf,
    267276                                cli->cc_socket, serv->cc_socket);
    268277               
    269                 /* Textual value for log messages */
    270                 rc = getnameinfo((sSA *)&ss, sizeof(sSS), cli->cc_remid, sizeof(cli->cc_remid), NULL, 0, NI_NUMERICHOST);
     278                /* Name for log messages */
     279                rc = getnameinfo((sSA *)&ss, sizeof(sSS), cli->cc_remid, sizeof(cli->cc_remid), NULL, 0, 0);
    271280                if (rc)
    272281                        snprintf(cli->cc_remid, sizeof(cli->cc_remid), "[err:%s]", gai_strerror(rc));
    273282        }
    274283
     284#ifndef DISABLE_SCTP
    275285        /* SCTP-specific handlings */
    276 #ifndef DISABLE_SCTP
    277286        if (cli->cc_proto == IPPROTO_SCTP) {
    278287                /* Retrieve the number of streams */
    279                 CHECK_FCT_DO( fd_sctp_get_str_info( cli->cc_socket, &cli->cc_sctp_para.str_in, &cli->cc_sctp_para.str_out ), goto error );
     288                CHECK_FCT_DO( fd_sctp_get_str_info( cli->cc_socket, &cli->cc_sctp_para.str_in, &cli->cc_sctp_para.str_out, NULL ), goto error );
    280289                if (cli->cc_sctp_para.str_out > cli->cc_sctp_para.str_in)
    281290                        cli->cc_sctp_para.pairs = cli->cc_sctp_para.str_out;
     
    291300}
    292301
    293 /* Client side: connect to a remote server */
    294 struct cnxctx * fd_cnx_cli_connect(int proto, const sSA * sa,  socklen_t addrlen)
    295 {
    296 
    297         TODO("...");
     302/* Client side: connect to a remote server -- cancelable */
     303struct cnxctx * fd_cnx_cli_connect_tcp(sSA * sa /* contains the port already */, socklen_t addrlen)
     304{
     305        int sock;
     306        struct cnxctx * cnx = NULL;
     307       
     308        TRACE_ENTRY("%p %d", sa, addrlen);
     309        CHECK_PARAMS_DO( sa && addrlen, return NULL );
     310       
     311        /* Create the socket and connect, which can take some time and/or fail */
     312        CHECK_FCT_DO( fd_tcp_client( &sock, sa, addrlen ), return NULL );
     313       
     314        if (TRACE_BOOL(INFO)) {
     315                fd_log_debug("Connection established to server '");
     316                sSA_DUMP_NODE_SERV( sa, NI_NUMERICSERV);
     317                fd_log_debug("' (TCP:%d).\n", sock);
     318        }
     319       
     320        /* Once the socket is created successfuly, prepare the remaining of the cnx */
     321        CHECK_MALLOC_DO( cnx = fd_cnx_init(1), { shutdown(sock, SHUT_RDWR); return NULL; } );
     322       
     323        cnx->cc_socket = sock;
     324        cnx->cc_proto  = IPPROTO_TCP;
     325       
     326        /* Generate the names for the object */
     327        {
     328                char addrbuf[INET6_ADDRSTRLEN];
     329                char portbuf[10];
     330                int  rc;
     331               
     332                /* Numeric values for debug */
     333                rc = getnameinfo(sa, addrlen, addrbuf, sizeof(addrbuf), portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
     334                if (rc) {
     335                        snprintf(addrbuf, sizeof(addrbuf), "[err:%s]", gai_strerror(rc));
     336                        portbuf[0] = '\0';
     337                }
     338               
     339                snprintf(cnx->cc_id, sizeof(cnx->cc_id), "Client of TCP server [%s]:%s (%d)", addrbuf, portbuf, cnx->cc_socket);
     340               
     341                /* Name for log messages */
     342                rc = getnameinfo(sa, addrlen, cnx->cc_remid, sizeof(cnx->cc_remid), NULL, 0, 0);
     343                if (rc)
     344                        snprintf(cnx->cc_remid, sizeof(cnx->cc_remid), "[err:%s]", gai_strerror(rc));
     345        }
     346       
     347        return cnx;
     348
     349error:
     350        fd_cnx_destroy(cnx);
    298351        return NULL;
     352}
     353
     354/* Same for SCTP, accepts a list of remote addresses to connect to (see sctp_connectx) */
     355struct cnxctx * fd_cnx_cli_connect_sctp(int no_ip6, uint16_t port, struct fd_list * list)
     356{
     357#ifdef DISABLE_SCTP
     358        TRACE_DEBUG(INFO, "This function should never been called when SCTP is disabled...");
     359        ASSERT(0);
     360        CHECK_FCT_DO( ENOTSUP, return NULL);
     361#else /* DISABLE_SCTP */
     362        int sock;
     363        struct cnxctx * cnx = NULL;
     364        sSS primary;
     365       
     366        TRACE_ENTRY("%p", list);
     367        CHECK_PARAMS_DO( list && !FD_IS_LIST_EMPTY(list), return NULL );
     368       
     369        CHECK_FCT_DO( fd_sctp_client( &sock, no_ip6, port, list ), return NULL );
     370       
     371        /* Once the socket is created successfuly, prepare the remaining of the cnx */
     372        CHECK_MALLOC_DO( cnx = fd_cnx_init(1), { shutdown(sock, SHUT_RDWR); return NULL; } );
     373       
     374        cnx->cc_socket = sock;
     375        cnx->cc_proto  = IPPROTO_SCTP;
     376       
     377        /* Retrieve the number of streams and primary address */
     378        CHECK_FCT_DO( fd_sctp_get_str_info( sock, &cnx->cc_sctp_para.str_in, &cnx->cc_sctp_para.str_out, &primary ), goto error );
     379        if (cnx->cc_sctp_para.str_out > cnx->cc_sctp_para.str_in)
     380                cnx->cc_sctp_para.pairs = cnx->cc_sctp_para.str_out;
     381        else
     382                cnx->cc_sctp_para.pairs = cnx->cc_sctp_para.str_in;
     383       
     384        /* Generate the names for the object */
     385        {
     386                char addrbuf[INET6_ADDRSTRLEN];
     387                char portbuf[10];
     388                int  rc;
     389               
     390                /* Numeric values for debug */
     391                rc = getnameinfo((sSA *)&primary, sizeof(sSS), addrbuf, sizeof(addrbuf), portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
     392                if (rc) {
     393                        snprintf(addrbuf, sizeof(addrbuf), "[err:%s]", gai_strerror(rc));
     394                        portbuf[0] = '\0';
     395                }
     396               
     397                snprintf(cnx->cc_id, sizeof(cnx->cc_id), "Client of SCTP server [%s]:%s (%d)", addrbuf, portbuf, cnx->cc_socket);
     398               
     399                /* Name for log messages */
     400                rc = getnameinfo((sSA *)&primary, sizeof(sSS), cnx->cc_remid, sizeof(cnx->cc_remid), NULL, 0, 0);
     401                if (rc)
     402                        snprintf(cnx->cc_remid, sizeof(cnx->cc_remid), "[err:%s]", gai_strerror(rc));
     403        }
     404       
     405        if (TRACE_BOOL(INFO)) {
     406                fd_log_debug("Connection established to server '");
     407                sSA_DUMP_NODE_SERV( &primary, NI_NUMERICSERV);
     408                fd_log_debug("' (SCTP:%d).\n", sock);
     409        }
     410       
     411        return cnx;
     412
     413error:
     414        fd_cnx_destroy(cnx);
     415        return NULL;
     416#endif /* DISABLE_SCTP */
    299417}
    300418
     
    415533                                socklen_t sl;
    416534                                CHECK_FCT(fd_tcp_get_local_ep(conn->cc_socket, &ss, &sl));
    417                                 CHECK_FCT(fd_ep_add_merge( local, (sSA *)&ss, sl, 0, 0, 0, 1 ));
     535                                CHECK_FCT(fd_ep_add_merge( local, (sSA *)&ss, sl, EP_FL_LL | EP_FL_PRIMARY));
    418536                        }
    419537                        break;
     
    441559                                socklen_t sl;
    442560                                CHECK_FCT(fd_tcp_get_remote_ep(conn->cc_socket, &ss, &sl));
    443                                 CHECK_FCT(fd_ep_add_merge( remote, (sSA *)&ss, sl, 0, 0, 0, 1 ));
     561                                CHECK_FCT(fd_ep_add_merge( remote, (sSA *)&ss, sl, EP_FL_LL | EP_FL_PRIMARY ));
    444562                        }
    445563                        break;
     
    528646        return;
    529647}
    530 
    531 
    532 
    533 
Note: See TracChangeset for help on using the changeset viewer.