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/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <errno.h> |
| 26 | #include <linux/tipc.h> |
| 27 | #include <stdint.h> |
| 28 | #include "log.h" |
| 29 | #include "ipc_api.h" |
| 30 | #include "err_codes.h" |
| 31 | |
| 32 | #include <sys/socket.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #define TIPC_SERVICE_ADDR 2 |
| 36 | |
| 37 | int |
| 38 | create_ipc_channel(char *name) |
| 39 | { |
| 40 | if (mkfifo (name, IPC_MODE) == -1) { |
| 41 | log_msg(LOG_ERROR, "Error in create_ipc_channel %s\n", name); |
| 42 | perror("Error:"); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | open_ipc_channel(char *name, enum ipc_access_mode access_mode) |
| 51 | { |
| 52 | int mode = O_RDONLY; |
| 53 | int fd; |
| 54 | |
| 55 | if (access_mode == IPC_WRITE) |
| 56 | mode = O_WRONLY; |
| 57 | |
| 58 | if ((fd = open(name, mode)) == -1) { |
| 59 | log_msg(LOG_ERROR, "Error in create_ipc_channel %s\n",name); |
| 60 | perror("Error:"); |
| 61 | return -E_FAIL; |
| 62 | } |
| 63 | |
| 64 | return fd; |
| 65 | } |
| 66 | |
| 67 | int |
| 68 | create_open_ipc_channel(char *name, enum ipc_access_mode access_mode) |
| 69 | { |
| 70 | if (create_ipc_channel(name) != 0) |
| 71 | return -1; |
| 72 | |
| 73 | return open_ipc_channel(name, access_mode); |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | read_ipc_channel(ipc_handle fd, char *buffer, size_t size) |
| 78 | { |
| 79 | int len = read(fd, buffer, size); |
| 80 | switch (len) { |
| 81 | case -1: |
| 82 | // case -1 means pipe is empty and errono |
| 83 | // set EAGAIN |
| 84 | if (errno == EAGAIN) { |
| 85 | log_msg(LOG_ERROR, "pipe empty \n"); |
| 86 | usleep(5); |
| 87 | return -1; |
| 88 | } |
| 89 | else { perror("read"); |
| 90 | exit(4); |
| 91 | } |
| 92 | |
| 93 | // case 0 means all bytes are read and EOF(end of conv.) |
| 94 | case 0: |
| 95 | log_msg(LOG_ERROR, "End of conversation\n"); |
| 96 | |
| 97 | // read link |
| 98 | //close(p[0]); |
| 99 | |
| 100 | exit(0); |
| 101 | default: |
| 102 | // text read |
| 103 | // by default return no. of bytes |
| 104 | // which read call read at that time |
| 105 | return len; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | int |
| 110 | write_ipc_channel(ipc_handle fd, char *buffer, size_t size) |
| 111 | { |
| 112 | return write(fd, buffer, size); |
| 113 | } |
| 114 | |
| 115 | int |
| 116 | close_ipc_channel(ipc_handle fd) |
| 117 | { |
| 118 | if (close(fd) == -1) |
| 119 | return -1; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | create_tipc_socket() |
| 126 | { |
| 127 | int sockFd = socket(AF_TIPC, SOCK_RDM, 0); |
| 128 | |
| 129 | if (sockFd <= 0) |
| 130 | { |
| 131 | log_msg(LOG_INFO, "Failed to create tipc socket error: %s", strerror(errno)); |
| 132 | } |
| 133 | |
| 134 | return sockFd; |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | bind_tipc_socket(int sockFd, uint32_t instanceNum) |
| 139 | { |
| 140 | struct sockaddr_tipc server; |
| 141 | |
| 142 | server.family = AF_TIPC; |
| 143 | server.addrtype = TIPC_SERVICE_ADDR; |
| 144 | server.scope = TIPC_CLUSTER_SCOPE; |
| 145 | server.addr.name.name.type = tipcServiceAddressType_c; |
| 146 | server.addr.name.name.instance = instanceNum; |
| 147 | |
| 148 | int rc = 1; |
| 149 | if (0 != bind(sockFd, (void *)&server, sizeof(server))) |
| 150 | { |
| 151 | log_msg(LOG_ERROR, "Server: failed to bind port name %s\n", strerror(errno)); |
| 152 | rc = -1; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | log_msg(LOG_INFO, "Server: Success %s %d\n", strerror(errno), rc); |
| 157 | } |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | send_tipc_message(int sd, uint32_t destAddr, void * buf, int len) |
| 163 | { |
| 164 | struct sockaddr_tipc server; |
| 165 | server.family = AF_TIPC; |
| 166 | server.addrtype = TIPC_SERVICE_ADDR; |
| 167 | server.scope = TIPC_CLUSTER_SCOPE; |
| 168 | server.addr.name.domain = 0; |
| 169 | server.addr.name.name.type = tipcServiceAddressType_c; |
| 170 | server.addr.name.name.instance = destAddr; |
| 171 | |
| 172 | int rc = 0; |
| 173 | if (0 > sendto(sd, buf, len, 0, (void*)&server, sizeof(server))) |
| 174 | { |
| 175 | log_msg(LOG_ERROR, "FAILED TO SENT TIPC MESSAGE %s\n", strerror(errno)); |
| 176 | } else { |
| 177 | rc = 1; |
| 178 | log_msg(LOG_INFO, "TIPC Message sent successfully to %d\n", destAddr); |
| 179 | } |
| 180 | |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | read_tipc_msg(int sockFd, void * buf, int len) |
| 186 | { |
| 187 | int bytesRead = 0; |
| 188 | |
| 189 | if ((bytesRead = recv(sockFd, buf, len, 0)) <= 0) |
| 190 | { |
| 191 | log_msg(LOG_ERROR, "FAILED TO READ TIPC MESSAGE %s\n", strerror(errno)); |
| 192 | } |
| 193 | return bytesRead; |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | close_tipc_socket(int sockFd) |
| 198 | { |
| 199 | close(sockFd); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |