- auto rc = sendRmrMessage(rmrMessageBuffer, message, &lspan);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return rc;
-}
-
-/**
- * build the SCTP connection to eNodB or gNb
- * @param rmrMessageBuffer
- * @param message
- * @param epoll_fd
- * @param sctpMap
- * @param pSpan
- * @return
- */
-int connectToCUandSetUp(RmrMessagesBuffer_t &rmrMessageBuffer,
- ReportingMessages_t &message,
- int epoll_fd,
- Sctp_Map_t *sctpMap,
- otSpan *pSpan) {
-#ifdef __TRACING__
- auto lspan = opentracing::Tracer::Global()->StartSpan(
- __FUNCTION__, { opentracing::ChildOf(&pSpan->get()->context()) });
-#else
- otSpan lspan = 0;
-#endif
- struct sockaddr_in6 servaddr{};
- struct addrinfo hints{}, *result;
- auto msgData = rmrMessageBuffer.rcvMessage->payload;
- unsigned char meid[RMR_MAX_MEID]{};
- char host[256]{};
- uint16_t port = 0;
-
- message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
- rmr_mbuf_t *msg = rmrMessageBuffer.rcvMessage;
- rmr_get_meid(msg, meid);
-
- if (mdclog_level_get() >= MDCLOG_INFO) {
- mdclog_write(MDCLOG_INFO, "message %d Received for MEID :%s. SETUP/EN-DC Setup Request from xApp, Message = %s",
- msg->mtype, meid, msgData);
- }
- if (getSetupRequestMetaData(message, (char *)msgData, host, port, &lspan) < 0) {
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Error in setup parameters %s, %d", __func__, __LINE__);
- }
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- //// message asndata points to the start of the asndata of the message and not to start of payload
- // search if the same host:port but not the same enodbname
- char searchBuff[256]{};
- snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", host, port);
- auto e = (char *)sctpMap->find(searchBuff);
- if (e != nullptr) {
- // found one compare if not the same
- if (strcmp(message.message.enodbName, e) != 0) {
- mdclog_write(MDCLOG_ERR,
- "Try to connect CU %s to Host %s but %s already connected",
- message.message.enodbName, host, e);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- }
-
- // check if not alread connected. if connected send the request and return
- auto *peerInfo = (ConnectedCU_t *)sctpMap->find(message.message.enodbName);
- if (peerInfo != nullptr) {
-// snprintf(strErr,
-// 128,
-// "Device %s already connected please remove and then setup again",
-// message.message.enodbName);
- if (mdclog_level_get() >= MDCLOG_INFO) {
- mdclog_write(MDCLOG_INFO,
- "Device already connected to %s",
- message.message.enodbName);
- }
- message.message.messageType = msg->mtype;
- auto rc = sendSctpMsg(peerInfo, message, sctpMap, &lspan);
- if (rc != 0) {
- mdclog_write(MDCLOG_ERR, "failed write to SCTP %s, %d", __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- char key[MAX_ENODB_NAME_SIZE * 2];
- snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName, msg->mtype);
- int xaction_len = strlen((const char *) msg->xaction);
- auto *xaction = (unsigned char *) calloc(1, xaction_len);
- memcpy(xaction, msg->xaction, xaction_len);
- sctpMap->setkey(key, xaction);
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "set key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
- }
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return 0;
- }
-
- peerInfo = (ConnectedCU_t *) calloc(1, sizeof(ConnectedCU_t));
- memcpy(peerInfo->enodbName, message.message.enodbName, sizeof(message.message.enodbName));
-
- // new connection
- if ((peerInfo->fileDescriptor = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP)) < 0) {
- mdclog_write(MDCLOG_ERR, "Socket Error, %s %s, %d", strerror(errno), __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- auto optval = 1;
- if (setsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof optval) != 0) {
- mdclog_write(MDCLOG_ERR, "setsockopt SO_REUSEPORT Error, %s %s, %d", strerror(errno), __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- optval = 1;
- if (setsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) != 0) {
- mdclog_write(MDCLOG_ERR, "setsockopt SO_REUSEADDR Error, %s %s, %d", strerror(errno), __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- servaddr.sin6_family = AF_INET6;
-
- struct sockaddr_in6 localAddr {};
- localAddr.sin6_family = AF_INET6;
- localAddr.sin6_addr = in6addr_any;
- localAddr.sin6_port = htons(SRC_PORT);
-
- if (bind(peerInfo->fileDescriptor, (struct sockaddr*)&localAddr , sizeof(struct sockaddr_in6)) < 0) {
- mdclog_write(MDCLOG_ERR, "bind Socket Error, %s %s, %d", strerror(errno), __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }//Ends the binding.
-
- memset(&hints, 0, sizeof hints);
- hints.ai_flags = AI_NUMERICHOST;
- if (getaddrinfo(host, nullptr, &hints, &result) < 0) {
- close(peerInfo->fileDescriptor);
- mdclog_write(MDCLOG_ERR, "getaddrinfo error for %s, Error = %s", host, strerror(errno));
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- memcpy(&servaddr, result->ai_addr, sizeof(struct sockaddr_in6));
- freeaddrinfo(result);
-
- servaddr.sin6_port = htons(port); /* daytime server */
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Send Connect FD = %d host : %s port %d",
- peerInfo->fileDescriptor,
- host,
- port);
- }
-
- // Add to Epol
- if (addToEpoll(epoll_fd, peerInfo, (EPOLLOUT | EPOLLIN | EPOLLET), sctpMap, message.message.enodbName,
- msg->mtype, &lspan) != 0) {
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- char hostBuff[NI_MAXHOST];
- char portBuff[NI_MAXHOST];
-
- if (getnameinfo((SA *) &servaddr, sizeof(servaddr),
- hostBuff, sizeof(hostBuff),
- portBuff, sizeof(portBuff),
- (uint) (NI_NUMERICHOST) | (uint) (NI_NUMERICSERV)) != 0) {
- mdclog_write(MDCLOG_ERR, "getnameinfo() Error, %s %s %d", strerror(errno), __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- if (setSocketNoBlocking(peerInfo->fileDescriptor) != 0) {
- mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on sctpPort %s", hostBuff,
- portBuff);
- close(peerInfo->fileDescriptor);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- memcpy(peerInfo->hostName, hostBuff, strlen(hostBuff));
- peerInfo->hostName[strlen(hostBuff)] = 0;
- memcpy(peerInfo->portNumber, portBuff, strlen(portBuff));
- peerInfo->portNumber[strlen(portBuff)] = 0;
-
- // map by enoodb/gnb name
- sctpMap->setkey(message.message.enodbName, peerInfo);
- //map host and port to enodeb
- sctpMap->setkey(searchBuff, message.message.enodbName);
-
- // save message for the return values
- char key[MAX_ENODB_NAME_SIZE * 2];
- snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName, msg->mtype);
- int xaction_len = strlen((const char *) msg->xaction);
- auto *xaction = (unsigned char *) calloc(1, xaction_len);
- memcpy(xaction, msg->xaction, xaction_len);
- sctpMap->setkey(key, xaction);
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "End building peerinfo: %s for CU %s", key, message.message.enodbName);
- }
-
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Send connect to FD %d, %s, %d",
- peerInfo->fileDescriptor, __func__, __LINE__);
- }
- if (connect(peerInfo->fileDescriptor, (SA *) &servaddr, sizeof(servaddr)) < 0) {
- if (errno != EINPROGRESS) {
- mdclog_write(MDCLOG_ERR, "connect FD %d to host : %s port %d, %s",
- peerInfo->fileDescriptor, host, port, strerror(errno));
- close(peerInfo->fileDescriptor);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG,
- "Connect to FD %d returned with EINPROGRESS : %s",
- peerInfo->fileDescriptor, strerror(errno));
- }
- // since message.message.asndata is pointing to the asndata in the rmr message payload we copy it like this
- memcpy(peerInfo->asnData, message.message.asndata, message.message.asnLength);
- peerInfo->asnLength = message.message.asnLength;
- peerInfo->mtype = msg->mtype;
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return 0;
- }
-
- if (mdclog_level_get() >= MDCLOG_INFO) {
- mdclog_write(MDCLOG_INFO, "Connect to FD %d returned OK without EINPROGRESS", peerInfo->fileDescriptor);
- }
-
- peerInfo->isConnected = true;
-
- if (modifyToEpoll(epoll_fd, peerInfo, (EPOLLIN | EPOLLET), sctpMap, message.message.enodbName, msg->mtype,
- &lspan) != 0) {
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
-
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Connected to host : %s port %d", host, port);
- }
-
- message.message.messageType = msg->mtype;
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Send SCTP message to FD %d", peerInfo->fileDescriptor);
- }
- if (sendSctpMsg(peerInfo, message, sctpMap, &lspan) != 0) {
- mdclog_write(MDCLOG_ERR, "Error write to SCTP %s %d", __func__, __LINE__);
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return -1;
- }
- memset(peerInfo->asnData, 0, message.message.asnLength);
- peerInfo->asnLength = 0;
- peerInfo->mtype = 0;
-
- if (mdclog_level_get() >= MDCLOG_DEBUG) {
- mdclog_write(MDCLOG_DEBUG, "Sent message to SCTP for %s", message.message.enodbName);
- }
-#ifdef __TRACING__
- lspan->Finish();
-#endif
- return 0;
-}