anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd. |
| 3 | * Copyright (c) 2017 Intel Corporation |
| 4 | * Copyright (c) 2019, Infosys Ltd. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <freeDiameter/freeDiameter-host.h> |
| 25 | #include <freeDiameter/libfdcore.h> |
| 26 | #include <freeDiameter/libfdproto.h> |
| 27 | #include "s6a_config.h" |
| 28 | #include "s6a.h" |
| 29 | #include "s6a_fd.h" |
| 30 | #include "err_codes.h" |
| 31 | #include "message_queues.h" |
| 32 | #include "ipc_api.h" |
| 33 | #include "hss_message.h" |
| 34 | #include "thread_pool.h" |
| 35 | #include <sys/types.h> |
| 36 | |
| 37 | /**Globals and externs**/ |
| 38 | struct fd_dict_objects g_fd_dict_objs; |
| 39 | struct fd_dict_data g_fd_dict_data; |
| 40 | int g_Q_mme_S6a_fd; |
| 41 | |
| 42 | int g_our_hss_fd; |
| 43 | struct thread_pool *g_tpool; |
| 44 | extern s6a_config g_s6a_cfg; |
| 45 | |
| 46 | pthread_t g_AIR_handler_tid, g_ULR_handler_tid; |
| 47 | pthread_t g_detach_handler_tid; |
| 48 | pthread_t g_our_hss_tid; |
| 49 | |
| 50 | extern char processName[255]; |
| 51 | extern int pid; |
| 52 | |
| 53 | int ipc_reader_tipc_s6; |
| 54 | |
| 55 | extern void* |
| 56 | S6Req_handler(void *data); |
| 57 | |
| 58 | /**Globals and externs**/ |
| 59 | |
| 60 | /** |
| 61 | * @brief Initialize communication channel IPC or IPC to non freediameter HSS |
| 62 | * ready |
| 63 | * @param None |
| 64 | * @return void |
| 65 | */ |
| 66 | void |
| 67 | init_hss_rpc() |
| 68 | { |
| 69 | struct sockaddr_un hss_serv; |
| 70 | |
| 71 | g_our_hss_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 72 | if (g_our_hss_fd < 0) { |
| 73 | log_msg(LOG_ERROR, "HSS socket creation failed.\n"); |
| 74 | perror("Error opening HSS socket"); |
| 75 | exit(-1); |
| 76 | } |
| 77 | |
| 78 | hss_serv.sun_family = AF_UNIX; |
| 79 | strcpy(hss_serv.sun_path, g_s6a_cfg.hss_ipc_endpt); |
| 80 | |
| 81 | if (connect(g_our_hss_fd, (struct sockaddr *)&hss_serv, |
| 82 | sizeof(struct sockaddr_un)) < 0) { |
| 83 | log_msg(LOG_ERROR, "HSS connect failed.\n"); |
| 84 | perror("connecting HSS socket"); |
| 85 | close(g_our_hss_fd); |
| 86 | exit(-1); |
| 87 | } |
| 88 | log_msg(LOG_INFO, "Connected to HSS\n"); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Initialize freediameter library, dictionary and data elements |
| 93 | * ready |
| 94 | * @param None |
| 95 | * @return int SUCCESS or S6A_FD_ERROR |
| 96 | */ |
| 97 | static int |
| 98 | init_fd() |
| 99 | { |
| 100 | log_msg(LOG_INFO, "INIT FD .. .\n"); |
| 101 | |
| 102 | /* Initialize the core freeDiameter library */ |
| 103 | CHECK_FCT_DO(fd_core_initialize(), return S6A_FD_ERROR); |
| 104 | |
| 105 | /* Parse the configuration file */ |
| 106 | CHECK_FCT_DO(fd_core_parseconf(S6A_FD_CONF), return S6A_FD_ERROR); |
| 107 | |
| 108 | if(SUCCESS != s6a_fd_init()) exit(S6A_FD_ERROR); |
| 109 | |
| 110 | if(SUCCESS != s6a_fd_objs_init()) exit(S6A_FD_ERROR); |
| 111 | |
| 112 | if(SUCCESS != s6a_fd_data_init()) exit(S6A_FD_ERROR); |
| 113 | |
| 114 | if(SUCCESS != s6a_fd_cb_reg()) exit(S6A_FD_ERROR); |
| 115 | |
| 116 | CHECK_FCT_DO( fd_core_start(), return S6A_FD_ERROR); |
| 117 | |
| 118 | return SUCCESS; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @brief Unused |
| 123 | * ready |
| 124 | * @param |
| 125 | * @return i |
| 126 | */ |
| 127 | static void |
| 128 | check_args(int argc, char **argv) |
| 129 | { |
| 130 | /*Parsse arguments to extract file path*/ |
| 131 | /*If no file path mentioned then use default*/ |
| 132 | /*For wrong arguments print help*/ |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | void * AIR_handler(void * data) |
| 137 | { |
| 138 | int bytesRead = 0; |
| 139 | while (1) |
| 140 | { |
| 141 | unsigned char buffer[255] = {0}; |
| 142 | if ((bytesRead = read_tipc_msg(ipc_reader_tipc_s6, buffer, 255)) > 0) |
| 143 | { |
| 144 | unsigned char *tmpBuf = (unsigned char *) malloc(sizeof(char) * bytesRead); |
| 145 | memcpy(tmpBuf, buffer, bytesRead); |
| 146 | log_msg(LOG_INFO, "S6 message received from mme-app"); |
| 147 | S6Req_handler(tmpBuf); |
| 148 | free(tmpBuf); |
| 149 | memset(buffer, 0, 255); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @brief Initialize s6a application message handlers |
| 156 | * ready |
| 157 | * @param None |
| 158 | * @return int SUCCESS or FAIL |
| 159 | */ |
| 160 | static int |
| 161 | init_handlers() |
| 162 | { |
| 163 | if ((ipc_reader_tipc_s6 = create_tipc_socket()) <= 0) |
| 164 | { |
| 165 | log_msg(LOG_ERROR, "Failed to create IPC Reader tipc socket \n"); |
| 166 | return -E_FAIL; |
| 167 | } |
| 168 | if ( bind_tipc_socket(ipc_reader_tipc_s6, s6AppInstanceNum_c) != 1) |
| 169 | { |
| 170 | log_msg(LOG_ERROR, "failed to bind port name %s\n", strerror(errno)); |
| 171 | return -E_FAIL; |
| 172 | } |
| 173 | |
| 174 | pthread_attr_t attr; |
| 175 | |
| 176 | pthread_attr_init(&attr); |
| 177 | /* set the thread detach state */ |
| 178 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 179 | |
| 180 | pthread_create(&g_AIR_handler_tid, &attr, &AIR_handler, NULL); |
| 181 | |
| 182 | pthread_attr_destroy(&attr); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @brief initialize s6a application IPC mechanism, queues |
| 188 | * ready |
| 189 | * @param None |
| 190 | * @return int as SUCCESS or FAIL. exit() in case of error. |
| 191 | */ |
| 192 | static int |
| 193 | init_s6a_ipc() |
| 194 | { |
| 195 | g_Q_mme_S6a_fd = create_tipc_socket(); |
| 196 | if (g_Q_mme_S6a_fd == -1) { |
| 197 | log_msg(LOG_ERROR, "Error in opening writer IPC channel\n"); |
| 198 | pthread_exit(NULL); |
| 199 | } |
| 200 | log_msg(LOG_INFO, "S6a response - mme-app TIPC: Connected.\n"); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @brief HSS message listener. Listen hss response and delegate to thread pool |
| 207 | * case of dummy hss |
| 208 | * ready |
| 209 | * @param None |
| 210 | * @return void |
| 211 | */ |
| 212 | void |
| 213 | s6a_run() |
| 214 | { |
| 215 | unsigned char buf[HSS_RCV_BUF_SIZE]; |
| 216 | int len; |
| 217 | |
| 218 | /*If using in build perf-hss then start thread to handle it's responses*/ |
| 219 | if(HSS_FD == g_s6a_cfg.hss_type) { |
| 220 | /*Use main thread for ULR or stats etc.*/ |
| 221 | while(1) { |
| 222 | sleep(10); |
| 223 | } |
| 224 | } else { |
| 225 | |
| 226 | while(1) { |
| 227 | bzero(buf, sizeof(buf)); |
| 228 | |
| 229 | if ((len = read(g_our_hss_fd, buf, |
| 230 | sizeof(struct hss_resp_msg))) < 0) { |
| 231 | log_msg(LOG_ERROR, "Error reading hss buff\n"); |
| 232 | perror("reading stream message"); |
| 233 | exit(-1); |
| 234 | } else if (len == 0) { |
| 235 | log_msg(LOG_ERROR, "Error reading hss buff\n"); |
| 236 | perror("reading stream message"); |
| 237 | exit(-1); |
| 238 | }else { |
| 239 | unsigned char *tmp_buf = (unsigned char *) |
| 240 | calloc(sizeof(char), len); |
| 241 | memcpy(tmp_buf, buf, len); |
| 242 | log_msg(LOG_INFO, "HSS Received msg len : %d \n",len); |
| 243 | insert_job(g_tpool, hss_resp_handler, tmp_buf); |
| 244 | } |
| 245 | } |
| 246 | } /*else - HSS_PERF*/ |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * brief main for s6a application |
| 251 | * @param argc and argv |
| 252 | * @return int - program's exit code |
| 253 | */ |
| 254 | int |
| 255 | main(int argc, char **argv) |
| 256 | { |
| 257 | |
| 258 | memcpy (processName, argv[0], strlen(argv[0])); |
| 259 | pid = getpid(); |
| 260 | |
| 261 | /*Check cmd line arguments for config file path*/ |
| 262 | check_args(argc, argv); |
| 263 | |
| 264 | init_parser("conf/s6a.json"); |
| 265 | parse_s6a_conf(); |
| 266 | |
| 267 | if(HSS_FD == g_s6a_cfg.hss_type){ |
| 268 | /*Initialize free diameter*/ |
| 269 | init_fd(); |
| 270 | } else { |
| 271 | init_hss_rpc(); |
| 272 | /* Initialize thread pool for handling HSS resp*/ |
| 273 | g_tpool = thread_pool_new(HSS_RESP_THREADPOOL_SIZE); |
| 274 | if (g_tpool == NULL) { |
| 275 | log_msg(LOG_ERROR, "Error in creating thread pool. \n"); |
| 276 | return -1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | init_s6a_ipc(); |
| 281 | |
| 282 | /*Initialize listner for AIR and ULR from mme-app*/ |
| 283 | init_handlers(); |
| 284 | |
| 285 | s6a_run(); |
| 286 | |
| 287 | return 0; |
| 288 | } |