Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /* Install the dispatch callbacks */ |
| 37 | |
| 38 | #include "test_app.h" |
| 39 | |
| 40 | static struct disp_hdl * ta_hdl_fb = NULL; /* handler for fallback cb */ |
| 41 | static struct disp_hdl * ta_hdl_tr = NULL; /* handler for Test-Request req cb */ |
| 42 | |
| 43 | /* Default callback for the application. */ |
| 44 | static int ta_fb_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act) |
| 45 | { |
| 46 | /* This CB should never be called */ |
| 47 | TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act); |
| 48 | |
| 49 | fd_log_debug("Unexpected message received!"); |
| 50 | |
| 51 | return ENOTSUP; |
| 52 | } |
| 53 | |
| 54 | /* Callback for incoming Test-Request messages */ |
| 55 | static int ta_tr_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act) |
| 56 | { |
| 57 | struct msg *ans, *qry; |
| 58 | struct avp * a; |
| 59 | |
| 60 | TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act); |
| 61 | |
| 62 | if (msg == NULL) |
| 63 | return EINVAL; |
| 64 | |
| 65 | /* Value of Origin-Host */ |
| 66 | if (! (ta_conf->mode & MODE_BENCH)) { |
| 67 | fprintf(stderr, "ECHO Test-Request received from "); |
| 68 | CHECK_FCT( fd_msg_search_avp ( *msg, ta_origin_host, &a) ); |
| 69 | if (a) { |
| 70 | struct avp_hdr * hdr; |
| 71 | CHECK_FCT( fd_msg_avp_hdr( a, &hdr ) ); |
| 72 | fprintf(stderr, "'%.*s'", (int)hdr->avp_value->os.len, hdr->avp_value->os.data); |
| 73 | } else { |
| 74 | fprintf(stderr, "no_Origin-Host"); |
| 75 | } |
| 76 | fprintf(stderr, ", replying...\n"); |
| 77 | } |
| 78 | |
| 79 | /* Create answer header */ |
| 80 | qry = *msg; |
| 81 | CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) ); |
| 82 | ans = *msg; |
| 83 | |
| 84 | /* Set the Test-AVP AVP */ |
| 85 | { |
| 86 | struct avp * src = NULL; |
| 87 | struct avp_hdr * hdr = NULL; |
| 88 | |
| 89 | CHECK_FCT( fd_msg_search_avp ( qry, ta_avp, &src) ); |
| 90 | CHECK_FCT( fd_msg_avp_hdr( src, &hdr ) ); |
| 91 | |
| 92 | CHECK_FCT( fd_msg_avp_new ( ta_avp, 0, &avp ) ); |
| 93 | CHECK_FCT( fd_msg_avp_setvalue( avp, hdr->avp_value ) ); |
| 94 | CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, avp ) ); |
| 95 | } |
| 96 | |
| 97 | /* Set the Test-Payload-AVP AVP */ |
| 98 | if (ta_conf->long_avp_id) { |
| 99 | struct avp * src = NULL; |
| 100 | struct avp_hdr * hdr = NULL; |
| 101 | |
| 102 | CHECK_FCT( fd_msg_search_avp ( qry, ta_avp_long, &src) ); |
| 103 | CHECK_FCT( fd_msg_avp_hdr( src, &hdr ) ); |
| 104 | |
| 105 | CHECK_FCT( fd_msg_avp_new ( ta_avp_long, 0, &avp ) ); |
| 106 | CHECK_FCT( fd_msg_avp_setvalue( avp, hdr->avp_value ) ); |
| 107 | CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, avp ) ); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */ |
| 112 | CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) ); |
| 113 | |
| 114 | /* Send the answer */ |
| 115 | CHECK_FCT( fd_msg_send( msg, NULL, NULL ) ); |
| 116 | |
| 117 | /* Add this value to the stats */ |
| 118 | CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), ); |
| 119 | ta_conf->stats.nb_echoed++; |
| 120 | CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), ); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int ta_serv_init(void) |
| 126 | { |
| 127 | struct disp_when data; |
| 128 | |
| 129 | TRACE_DEBUG(FULL, "Initializing dispatch callbacks for test"); |
| 130 | |
| 131 | memset(&data, 0, sizeof(data)); |
| 132 | data.app = ta_appli; |
| 133 | data.command = ta_cmd_r; |
| 134 | |
| 135 | /* fallback CB if command != Test-Request received */ |
| 136 | CHECK_FCT( fd_disp_register( ta_fb_cb, DISP_HOW_APPID, &data, NULL, &ta_hdl_fb ) ); |
| 137 | |
| 138 | /* Now specific handler for Test-Request */ |
| 139 | CHECK_FCT( fd_disp_register( ta_tr_cb, DISP_HOW_CC, &data, NULL, &ta_hdl_tr ) ); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void ta_serv_fini(void) |
| 145 | { |
| 146 | if (ta_hdl_fb) { |
| 147 | (void) fd_disp_unregister(&ta_hdl_fb, NULL); |
| 148 | } |
| 149 | if (ta_hdl_tr) { |
| 150 | (void) fd_disp_unregister(&ta_hdl_tr, NULL); |
| 151 | } |
| 152 | |
| 153 | return; |
| 154 | } |