blob: 044dcd265785dee8428ea90544c265923b4173f3 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2019, Infosys Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../../include/cmn/tipcSocket.h"
18#include "../../include/cmn/tipcTypes.h"
19#include "../../include/common/log.h"
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26#include <unistd.h>
27#include <linux/tipc.h>
28
29#define TIPC_SERVICE_ADDR 2
30
31namespace cmn {
32namespace ipc {
33
34TipcSocket::TipcSocket():IpcChannel(IpcChannelType::tipc_c)
35{
36 initialize();
37}
38
39TipcSocket::~TipcSocket()
40{
41 if (ipcHdl_m.u32 > 0)
42 close(ipcHdl_m.u32);
43}
44
45void TipcSocket::initialize()
46{
47 ipcHdl_m.u32 = socket(AF_TIPC, SOCK_RDM, 0);
48 if (ipcHdl_m.u32 <= 0)
49 log_msg(LOG_ERROR, "Failed to create TIPC socket. error: %s", strerror(errno));
50
51 return;
52}
53
54bool TipcSocket::bindTipcSocket(IpcAddress myAddress)
55{
56 if (ipcHdl_m.u32 <= 0)
57 {
58 log_msg(LOG_ERROR, "Invalid socket fd");
59 return false;
60 }
61
62 struct sockaddr_tipc server;
63
64 server.family = AF_TIPC;
65 server.addrtype = TIPC_SERVICE_ADDR;
66 server.scope = TIPC_CLUSTER_SCOPE;
67 server.addr.name.name.type = tipcServiceAddressType_c;
68 server.addr.name.name.instance = myAddress.u32;
69
70 bool rc = true;
71
72 if (0 != bind(ipcHdl_m.u32, (sockaddr*)&server, sizeof(server)))
73 {
74 log_msg(LOG_ERROR, "Failed to bind TIPC socket. error: %s", strerror(errno));
75 rc = false;
76 }
77 return rc;
78}
79
80void TipcSocket::sendMsgTo(void * buffer, uint32_t len, IpcAddress destAddress)
81{
82 struct sockaddr_tipc dest;
83 dest.family = AF_TIPC;
84 dest.addrtype = TIPC_SERVICE_ADDR;
85 dest.scope = TIPC_CLUSTER_SCOPE;
86 dest.addr.name.domain = 0;
87 dest.addr.name.name.type = tipcServiceAddressType_c;
88 dest.addr.name.name.instance = destAddress.u32;
89
90
91 if (0 > sendto(ipcHdl_m.u32, buffer, len, 0, (sockaddr*)&dest, sizeof(dest)))
92 {
93 log_msg(LOG_ERROR, "Failed to send message via TIPC socket. error: %s", strerror(errno));
94 }
95 else
96 {
97 log_msg(LOG_INFO, "Message sent successfully");
98 }
99}
100
101
102uint32_t TipcSocket::recvMsgFrom(void * buffer, uint32_t len, IpcAddress& srcAddr)
103{
104 uint32_t bytesRead = 0;
105
106 struct sockaddr_tipc client;
107 socklen_t alen = sizeof(client);
108
109 if ((bytesRead = recvfrom(ipcHdl_m.u32, buffer, len, 0,
110 (struct sockaddr *)&client, &alen)) > 0)
111 {
112 srcAddr.u32 = client.addr.name.name.instance;
113 }
114
115 return bytesRead;
116}
117
118} /* namespace ipc */
119} /* namespace cmn */