a7187e387de2a77ae9d5979dd460955d2a45269b
[o-du/l2.git] / src / o1 / o1_client / TcpClient.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020] [HCL Technologies Ltd.]                               #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains functions to connect to TCP server and send massages */
20
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30
31 #include "TcpClient.h"
32 #include "GlobalDefs.h"
33 #include "ssi.h"
34
35 static int s_sock;
36 static struct sockaddr_in s_serverName;
37 static uint16_t s_port;
38 static const char* s_hostName = NULL;
39
40
41 /********************************************************************** 
42    Description : Initilize the sockadd_in structure 
43    Params[In]  : None
44    Return      : ROK     - success
45                  RFAILED - failure
46 **********************************************************************/
47 static uint8_t initSockaddr()
48 {
49
50    struct hostent *hostInfo;
51    struct sockaddr_in *name = &s_serverName;
52    bzero(&s_serverName, sizeof(s_serverName));
53    name->sin_family = AF_INET;
54    name->sin_port = htons (s_port);
55    hostInfo = gethostbyname (s_hostName);
56    if (hostInfo == NULL)
57    {
58       O1_LOG("\nO1 TcpClient : Unknown host %s", s_hostName);
59       return RFAILED;
60    }
61    name->sin_addr = *(struct in_addr *) hostInfo->h_addr;
62    return ROK;
63 }
64
65
66 /********************************************************************** 
67    Description : Open a TCP socket 
68    Params[In]  : hostName, port
69    Return      : ROK     - success
70                  RFAILED - failure
71 **********************************************************************/
72 uint8_t openSocket(const char* hostName, const uint16_t port)
73 {
74    /* Create the socket. */
75    s_port = port;
76    s_hostName = hostName;
77    s_sock = socket (PF_INET, SOCK_STREAM, 0);
78    if (s_sock < 0)
79    {
80       O1_LOG("\nO1 TcpClient : Error opening socket");
81       return RFAILED;
82    }
83
84    /* Init the sockaddr_in structure */
85    if (initSockaddr() == RFAILED)
86    {
87       return RFAILED;
88    }
89    /* Connect to the server */
90    if (0 > connect (s_sock,
91                    (struct sockaddr_in *)&s_serverName ,
92                    sizeof (s_serverName)))
93    {
94       O1_LOG("\nO1 TcpClient : Error connecting");
95       return RFAILED;
96    }
97
98    return ROK;
99
100 }
101
102 /********************************************************************** 
103    Description : Send the message on TCP socket 
104    Params[In]  : message, size of the message
105    Return      : Number of bytes sent
106 **********************************************************************/
107 int sendData(void* data, const int size)
108 {
109    int nbytes = write (s_sock, data, size);
110    if (nbytes < 0)
111    {
112       O1_LOG("\nO1 TcpClient : Error writing. %d bytes sent", nbytes);
113    }
114    return nbytes;
115 }
116
117 /********************************************************************** 
118    Description : Close the TCP socket 
119    Params[In]  : None
120    Return      : ROK     - success
121                  RFAILED - failure
122 **********************************************************************/
123 uint8_t closeSocket()
124 {
125    if( close(s_sock) != 0 )
126       return RFAILED;
127    return ROK;
128 }
129
130 /**********************************************************************
131          End of file
132 **********************************************************************/