Navigation


Changeset 934:977a5375543c in freeDiameter


Ignore:
Timestamp:
Mar 8, 2013, 12:13:50 AM (11 years ago)
Author:
Sebastien Decugis <sdecugis@freediameter.net>
Branch:
default
Phase:
public
Message:

Remove one stupid indirection level

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libfdcore/routing_dispatch.c

    r754 r934  
    424424
    425425/* The DISPATCH message processing */
    426 static int msg_dispatch(struct msg ** pmsg)
     426static int msg_dispatch(struct msg * msg)
    427427{
    428428        struct msg_hdr * hdr;
     
    432432        char * ec = NULL;
    433433        char * em = NULL;
     434        struct msg *msgptr = msg;
    434435
    435436        /* Read the message header */
    436         CHECK_FCT( fd_msg_hdr(*pmsg, &hdr) );
     437        CHECK_FCT( fd_msg_hdr(msg, &hdr) );
    437438        is_req = hdr->msg_flags & CMD_FLAG_REQUEST;
    438439       
     
    441442
    442443        /* At this point, we need to understand the message content, so parse it */
    443         CHECK_FCT_DO( ret = fd_msg_parse_or_error( pmsg ),
     444        CHECK_FCT_DO( ret = fd_msg_parse_or_error( &msgptr ),
    444445                {
    445446                        /* in case of error */
    446                         if ((ret == EBADMSG) && (*pmsg != NULL)) {
    447                                 /* msg now contains the answer message to send back */
    448                                 CHECK_FCT( fd_fifo_post(fd_g_outgoing, pmsg) );
    449                         }
    450                         if (*pmsg) {    /* another error happen'd */
    451                                 fd_msg_log( FD_MSG_LOG_DROPPED, *pmsg,  "An unexpected error occurred while parsing the message (%s)", strerror(ret));
    452                                 CHECK_FCT_DO( fd_msg_free(*pmsg), /* continue */);
    453                                 *pmsg = NULL;
     447                        if ((ret == EBADMSG) && (msgptr != NULL)) {
     448                                /* msgptr now contains the answer message to send back */
     449                                CHECK_FCT( fd_fifo_post(fd_g_outgoing, &msgptr) );
     450                        }
     451                        if (msgptr) {   /* another error happen'd */
     452                                fd_msg_log( FD_MSG_LOG_DROPPED, msgptr,  "An unexpected error occurred while parsing the message (%s)", strerror(ret));
     453                                CHECK_FCT_DO( fd_msg_free(msgptr), /* continue */);
    454454                        }
    455455                        /* We're done with this one */
     
    464464
    465465                /* Retrieve the corresponding query */
    466                 CHECK_FCT( fd_msg_answ_getq( *pmsg, &qry ) );
     466                CHECK_FCT( fd_msg_answ_getq( msgptr, &qry ) );
    467467
    468468                /* Retrieve any registered handler */
     
    473473
    474474                        TRACE_DEBUG(FULL, "Calling callback registered when query was sent (%p, %p)", anscb, data);
    475                         (*anscb)(data, pmsg);
     475                        (*anscb)(data, &msgptr);
    476476                       
    477477                        /* If the message is processed, we're done */
    478                         if (*pmsg == NULL) {
     478                        if (msgptr == NULL) {
    479479                                return 0;
    480480                        }
     481                       
     482                        /* otherwise continue the dispatching --hoping that the anscb callback did not mess with our message :) */
    481483                }
    482484        }
    483485       
    484486        /* Retrieve the session of the message */
    485         CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *pmsg, &sess, NULL) );
     487        CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, msgptr, &sess, NULL) );
    486488
    487489        /* Now, call any callback registered for the message */
    488         CHECK_FCT( fd_msg_dispatch ( pmsg, sess, &action, &ec) );
     490        CHECK_FCT( fd_msg_dispatch ( &msgptr, sess, &action, &ec) );
    489491
    490492        /* Now, act depending on msg and action and ec */
    491         if (*pmsg)
     493        if (msgptr) {
    492494                switch ( action ) {
    493495                        case DISP_ACT_CONT:
     
    495497                                if (!fd_g_config->cnf_flags.no_fwd) {
    496498                                        /* requeue to fd_g_outgoing */
    497                                         CHECK_FCT( fd_fifo_post(fd_g_outgoing, pmsg) );
     499                                        CHECK_FCT( fd_fifo_post(fd_g_outgoing, &msgptr) );
    498500                                        break;
    499501                                }
     
    509511                               
    510512                                if (!is_req) {
    511                                         fd_msg_log( FD_MSG_LOG_DROPPED, *pmsg,  "Internal error: Answer received to locally issued request, but not handled by any handler.");
    512                                         fd_msg_free(*pmsg);
    513                                         *pmsg = NULL;
     513                                        fd_msg_log( FD_MSG_LOG_DROPPED, msgptr,  "Internal error: Answer received to locally issued request, but not handled by any handler.");
     514                                        fd_msg_free(msgptr);
    514515                                        break;
    515516                                }
    516517                               
    517518                                /* Create an answer with the error code and message */
    518                                 CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, pmsg, 0 ) );
    519                                 CHECK_FCT( fd_msg_rescode_set(*pmsg, ec, em, NULL, 1 ) );
     519                                CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, &msgptr, 0 ) );
     520                                CHECK_FCT( fd_msg_rescode_set(msgptr, ec, em, NULL, 1 ) );
    520521                               
    521522                        case DISP_ACT_SEND:
    522523                                /* Now, send the message */
    523                                 CHECK_FCT( fd_fifo_post(fd_g_outgoing, pmsg) );
    524                 }
     524                                CHECK_FCT( fd_fifo_post(fd_g_outgoing, &msgptr) );
     525                }
     526        }
    525527       
    526528        /* We're done with dispatching this message */
     
    529531
    530532/* The ROUTING-IN message processing */
    531 static int msg_rt_in(struct msg ** pmsg)
     533static int msg_rt_in(struct msg * msg)
    532534{
    533535        struct msg_hdr * hdr;
     
    536538        DiamId_t qry_src = NULL;
    537539        size_t   qry_src_len = 0;
     540        struct msg *msgptr = msg;
    538541
    539542        /* Read the message header */
    540         CHECK_FCT( fd_msg_hdr(*pmsg, &hdr) );
     543        CHECK_FCT( fd_msg_hdr(msg, &hdr) );
    541544        is_req = hdr->msg_flags & CMD_FLAG_REQUEST;
    542545        is_err = hdr->msg_flags & CMD_FLAG_ERROR;
     
    544547        /* Handle incorrect bits */
    545548        if (is_req && is_err) {
    546                 CHECK_FCT( return_error( pmsg, "DIAMETER_INVALID_HDR_BITS", "R & E bits were set", NULL) );
     549                CHECK_FCT( return_error( &msgptr, "DIAMETER_INVALID_HDR_BITS", "R & E bits were set", NULL) );
    547550                return 0;
    548551        }
     
    564567                        TRACE_DEBUG(INFO, "Received a routable message with application id 0 or " _stringize(AI_RELAY) " (relay),\n"
    565568                                          " returning DIAMETER_APPLICATION_UNSUPPORTED");
    566                         CHECK_FCT( return_error( pmsg, "DIAMETER_APPLICATION_UNSUPPORTED", "Routable message with application id 0 or relay", NULL) );
     569                        CHECK_FCT( return_error( &msgptr, "DIAMETER_APPLICATION_UNSUPPORTED", "Routable message with application id 0 or relay", NULL) );
    567570                        return 0;
    568571                } else {
     
    573576
    574577                /* Parse the message for Dest-Host and Dest-Realm */
    575                 CHECK_FCT(  fd_msg_browse(*pmsg, MSG_BRW_FIRST_CHILD, &avp, NULL)  );
     578                CHECK_FCT(  fd_msg_browse(msgptr, MSG_BRW_FIRST_CHILD, &avp, NULL)  );
    576579                while (avp) {
    577580                        struct avp_hdr * ahdr;
     
    590593                                                        {
    591594                                                                if (error_info.pei_errcode) {
    592                                                                         CHECK_FCT( return_error( pmsg, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
     595                                                                        CHECK_FCT( return_error( &msgptr, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
    593596                                                                        return 0;
    594597                                                                } else {
     
    610613                                                        {
    611614                                                                if (error_info.pei_errcode) {
    612                                                                         CHECK_FCT( return_error( pmsg, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
     615                                                                        CHECK_FCT( return_error( &msgptr, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
    613616                                                                        return 0;
    614617                                                                } else {
     
    632635                                                        {
    633636                                                                if (error_info.pei_errcode) {
    634                                                                         CHECK_FCT( return_error( pmsg, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
     637                                                                        CHECK_FCT( return_error( &msgptr, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
    635638                                                                        return 0;
    636639                                                                } else {
     
    657660                /* Handle the missing routing AVPs first */
    658661                if ( is_dest_realm == UNKNOWN ) {
    659                         CHECK_FCT( return_error( pmsg, "DIAMETER_COMMAND_UNSUPPORTED", "Non-routable message not supported (invalid bit ? missing Destination-Realm ?)", NULL) );
     662                        CHECK_FCT( return_error( &msgptr, "DIAMETER_COMMAND_UNSUPPORTED", "Non-routable message not supported (invalid bit ? missing Destination-Realm ?)", NULL) );
    660663                        return 0;
    661664                }
     
    665668                        if (is_local_app == YES) {
    666669                                /* Ok, give the message to the dispatch thread */
    667                                 CHECK_FCT( fd_fifo_post(fd_g_local, pmsg) );
     670                                CHECK_FCT( fd_fifo_post(fd_g_local, &msgptr) );
    668671                        } else {
    669672                                /* We don't support the application, reply an error */
    670                                 CHECK_FCT( return_error( pmsg, "DIAMETER_APPLICATION_UNSUPPORTED", NULL, NULL) );
     673                                CHECK_FCT( return_error( &msgptr, "DIAMETER_APPLICATION_UNSUPPORTED", NULL, NULL) );
    671674                        }
    672675                        return 0;
     
    676679                if ((is_dest_host == NO) || (is_dest_realm == NO)) {
    677680                        if (fd_g_config->cnf_flags.no_fwd) {
    678                                 CHECK_FCT( return_error( pmsg, "DIAMETER_UNABLE_TO_DELIVER", "I am not a Diameter agent", NULL) );
     681                                CHECK_FCT( return_error( &msgptr, "DIAMETER_UNABLE_TO_DELIVER", "I am not a Diameter agent", NULL) );
    679682                                return 0;
    680683                        }
     
    689692                                        {
    690693                                                /* If the process failed, we assume it is because of the AVP format */
    691                                                 CHECK_FCT( return_error( pmsg, "DIAMETER_INVALID_AVP_VALUE", "Failed to process decorated NAI", un) );
     694                                                CHECK_FCT( return_error( &msgptr, "DIAMETER_INVALID_AVP_VALUE", "Failed to process decorated NAI", un) );
    692695                                                return 0;
    693696                                        } );
     
    696699                        if (is_nai) {
    697700                                /* We have transformed the AVP, now submit it again in the queue */
    698                                 CHECK_FCT(fd_fifo_post(fd_g_incoming, pmsg) );
     701                                CHECK_FCT(fd_fifo_post(fd_g_incoming, &msgptr) );
    699702                                return 0;
    700703                        }
     
    702705                        if (is_local_app == YES) {
    703706                                /* Handle localy since we are able to */
    704                                 CHECK_FCT(fd_fifo_post(fd_g_local, pmsg) );
     707                                CHECK_FCT(fd_fifo_post(fd_g_local, &msgptr) );
    705708                                return 0;
    706709                        }
     
    708711                        if (fd_g_config->cnf_flags.no_fwd) {
    709712                                /* We return an error */
    710                                 CHECK_FCT( return_error( pmsg, "DIAMETER_APPLICATION_UNSUPPORTED", NULL, NULL) );
     713                                CHECK_FCT( return_error( &msgptr, "DIAMETER_APPLICATION_UNSUPPORTED", NULL, NULL) );
    711714                                return 0;
    712715                        }
     
    720723
    721724                /* Retrieve the corresponding query and its origin */
    722                 CHECK_FCT( fd_msg_answ_getq( *pmsg, &qry ) );
     725                CHECK_FCT( fd_msg_answ_getq( msgptr, &qry ) );
    723726                CHECK_FCT( fd_msg_source_get( qry, &qry_src, &qry_src_len ) );
    724727
    725728                if ((!qry_src) && (!is_err)) {
    726729                        /* The message is a normal answer to a request issued localy, we do not call the callbacks chain on it. */
    727                         CHECK_FCT(fd_fifo_post(fd_g_local, pmsg) );
     730                        CHECK_FCT(fd_fifo_post(fd_g_local, &msgptr) );
    728731                        return 0;
    729732                }
     
    740743
    741744                /* requests: dir = 1 & 2 => in order; answers = 3 & 2 => in reverse order */
    742                 for (   li = (is_req ? rt_fwd_list.next : rt_fwd_list.prev) ; *pmsg && (li != &rt_fwd_list) ; li = (is_req ? li->next : li->prev) ) {
     745                for (   li = (is_req ? rt_fwd_list.next : rt_fwd_list.prev) ; msgptr && (li != &rt_fwd_list) ; li = (is_req ? li->next : li->prev) ) {
    743746                        struct rt_hdl * rh = (struct rt_hdl *)li;
    744747                        int ret;
     
    750753
    751754                        /* Ok, call this cb */
    752                         TRACE_DEBUG(ANNOYING, "Calling next FWD callback on %p : %p", *pmsg, rh->rt_fwd_cb);
    753                         CHECK_FCT_DO( ret = (*rh->rt_fwd_cb)(rh->cbdata, pmsg),
     755                        TRACE_DEBUG(ANNOYING, "Calling next FWD callback on %p : %p", msgptr, rh->rt_fwd_cb);
     756                        CHECK_FCT_DO( ret = (*rh->rt_fwd_cb)(rh->cbdata, &msgptr),
    754757                                {
    755                                         fd_msg_log( FD_MSG_LOG_DROPPED, *pmsg, "Internal error: a FWD routing callback returned an error (%s)", strerror(ret));
    756                                         fd_msg_free(*pmsg);
    757                                         *pmsg = NULL;
     758                                        fd_msg_log( FD_MSG_LOG_DROPPED, msgptr, "Internal error: a FWD routing callback returned an error (%s)", strerror(ret));
     759                                        fd_msg_free(msgptr);
     760                                        msgptr = NULL;
    758761                                } );
    759762                }
     
    763766
    764767                /* If a callback has handled the message, we stop now */
    765                 if (!*pmsg)
     768                if (!msgptr)
    766769                        return 0;
    767770        }
     
    769772        /* Now pass the message to the next step: either forward to another peer, or dispatch to local extensions */
    770773        if (is_req || qry_src) {
    771                 CHECK_FCT(fd_fifo_post(fd_g_outgoing, pmsg) );
     774                CHECK_FCT(fd_fifo_post(fd_g_outgoing, &msgptr) );
    772775        } else {
    773                 CHECK_FCT(fd_fifo_post(fd_g_local, pmsg) );
     776                CHECK_FCT(fd_fifo_post(fd_g_local, &msgptr) );
    774777        }
    775778
     
    780783
    781784/* The ROUTING-OUT message processing */
    782 static int msg_rt_out(struct msg ** pmsg)
     785static int msg_rt_out(struct msg * msg)
    783786{
    784787        struct rt_data * rtd = NULL;
     
    789792        struct avp * avp;
    790793        struct rtd_candidate * c;
     794        struct msg *msgptr = msg;
    791795       
    792796        /* Read the message header */
    793         CHECK_FCT( fd_msg_hdr(*pmsg, &hdr) );
     797        CHECK_FCT( fd_msg_hdr(msgptr, &hdr) );
    794798        is_req = hdr->msg_flags & CMD_FLAG_REQUEST;
    795799       
     
    803807
    804808                /* Retrieve the corresponding query and its origin */
    805                 CHECK_FCT( fd_msg_answ_getq( *pmsg, &qry ) );
     809                CHECK_FCT( fd_msg_answ_getq( msgptr, &qry ) );
    806810                CHECK_FCT( fd_msg_source_get( qry, &qry_src, &qry_src_len ) );
    807811
     
    811815                CHECK_FCT( fd_peer_getbyid( qry_src, qry_src_len, 0, (void *) &peer ) );
    812816                if (fd_peer_getstate(peer) != STATE_OPEN) {
    813                         fd_msg_log( FD_MSG_LOG_DROPPED, *pmsg, "Unable to forward answer to deleted / closed peer '%s'.", qry_src);
    814                         fd_msg_free(*pmsg);
    815                         *pmsg = NULL;
     817                        fd_msg_log( FD_MSG_LOG_DROPPED, msgptr, "Unable to forward answer to deleted / closed peer '%s'.", qry_src);
     818                        fd_msg_free(msgptr);
    816819                        return 0;
    817820                }
     
    822825
    823826                /* Push the message into this peer */
    824                 CHECK_FCT( fd_out_send(pmsg, NULL, peer, 0) );
     827                CHECK_FCT( fd_out_send(&msgptr, NULL, peer, 0) );
    825828
    826829                /* We're done with this answer */
     
    831834
    832835        /* Get the routing data out of the message if any (in case of re-transmit) */
    833         CHECK_FCT( fd_msg_rt_get ( *pmsg, &rtd ) );
     836        CHECK_FCT( fd_msg_rt_get ( msgptr, &rtd ) );
    834837
    835838        /* If there is no routing data already, let's create it */
     
    851854
    852855                /* Now let's remove all peers from the Route-Records */
    853                 CHECK_FCT(  fd_msg_browse(*pmsg, MSG_BRW_FIRST_CHILD, &avp, NULL)  );
     856                CHECK_FCT(  fd_msg_browse(msgptr, MSG_BRW_FIRST_CHILD, &avp, NULL)  );
    854857                while (avp) {
    855858                        struct avp_hdr * ahdr;
     
    862865                                        {
    863866                                                if (error_info.pei_errcode) {
    864                                                         CHECK_FCT( return_error( pmsg, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
     867                                                        CHECK_FCT( return_error( &msgptr, error_info.pei_errcode, error_info.pei_message, error_info.pei_avp) );
    865868                                                        return 0;
    866869                                                } else {
     
    883886        fd_rtd_candidate_extract(rtd, &candidates, FD_SCORE_INI);
    884887
    885         /* Pass the list to registered callbacks (even if it is empty) */
     888        /* Pass the list to registered callbacks (even if it is empty list) */
    886889        {
    887890                CHECK_FCT( pthread_rwlock_rdlock( &rt_out_lock ) );
     
    892895                        struct rt_hdl * rh = (struct rt_hdl *)li;
    893896
    894                         TRACE_DEBUG(ANNOYING, "Calling next OUT callback on %p : %p (prio %d)", *pmsg, rh->rt_out_cb, rh->prio);
    895                         CHECK_FCT_DO( ret = (*rh->rt_out_cb)(rh->cbdata, *pmsg, candidates),
     897                        TRACE_DEBUG(ANNOYING, "Calling next OUT callback on %p : %p (prio %d)", msgptr, rh->rt_out_cb, rh->prio);
     898                        CHECK_FCT_DO( ret = (*rh->rt_out_cb)(rh->cbdata, msgptr, candidates),
    896899                                {
    897                                         fd_msg_log( FD_MSG_LOG_DROPPED, *pmsg, "Internal error: an OUT routing callback returned an error (%s)", strerror(ret));
    898                                         fd_msg_free(*pmsg);
    899                                         *pmsg = NULL;
     900                                        fd_msg_log( FD_MSG_LOG_DROPPED, msgptr, "Internal error: an OUT routing callback returned an error (%s)", strerror(ret));
     901                                        fd_msg_free(msgptr);
     902                                        msgptr = NULL;
    900903                                        break;
    901904                                } );
     
    906909
    907910                /* If an error occurred, skip to the next message */
    908                 if (! *pmsg) {
     911                if (! msgptr) {
    909912                        if (rtd)
    910913                                fd_rtd_free(&rtd);
     
    917920
    918921        /* Save the routing information in the message */
    919         CHECK_FCT( fd_msg_rt_associate ( *pmsg, &rtd ) );
     922        CHECK_FCT( fd_msg_rt_associate ( msgptr, &rtd ) );
    920923
    921924        /* Now try sending the message */
     
    934937                if (fd_peer_getstate(peer) == STATE_OPEN) {
    935938                        /* Send to this one */
    936                         CHECK_FCT_DO( fd_out_send(pmsg, NULL, peer, 0), continue );
     939                        CHECK_FCT_DO( fd_out_send(&msgptr, NULL, peer, 0), continue );
    937940                       
    938941                        /* If the sending was successful */
     
    942945
    943946        /* If the message has not been sent, return an error */
    944         if (*pmsg) {
    945                 fd_msg_log( FD_MSG_LOG_NODELIVER, *pmsg, "No suitable candidate to route the message to." );
    946                 return_error( pmsg, "DIAMETER_UNABLE_TO_DELIVER", "No suitable candidate to route the message to", NULL);
     947        if (msgptr) {
     948                fd_msg_log( FD_MSG_LOG_NODELIVER, msgptr, "No suitable candidate to route the message to." );
     949                return_error( &msgptr, "DIAMETER_UNABLE_TO_DELIVER", "No suitable candidate to route the message to", NULL);
    947950        }
    948951
     
    977980
    978981/* This is the common thread code (same for routing and dispatching) */
    979 static void * process_thr(void * arg, int (*action_cb)(struct msg ** pmsg), struct fifo * queue, char * action_name)
     982static void * process_thr(void * arg, int (*action_cb)(struct msg * msg), struct fifo * queue, char * action_name)
    980983{
    981984        TRACE_ENTRY("%p %p %p %p", arg, action_cb, queue, action_name);
     
    10401043               
    10411044                /* Now process the message */
    1042                 CHECK_FCT_DO( (*action_cb)(&msg), goto fatal_error);
     1045                CHECK_FCT_DO( (*action_cb)(msg), goto fatal_error);
    10431046
    10441047                /* We're done with this message */
Note: See TracChangeset for help on using the changeset viewer.