Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
////////////////////////////////////////////////////////////////////////////////////////
/**
* # Copyright 2022 Centre Tecnològic de Telecomunicacions de Catalunya (CTTC/CERCA) www.cttc.es
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Author: CTTC/CERCA PONS RU Ricardo Martínez (ricardo.martinez@cttc.es)
*/
/////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <glib.h>
#include <sys/time.h>
#include <ctype.h>
#include <strings.h>
#include <time.h>
#include <fcntl.h>
#include <uuid/uuid.h>
#include <string.h>
#include "pathComp_log.h"
#include "pathComp_tools.h"
#include "pathComp_cjson.h"
#include "pathComp_ksp.h"
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "pathComp_RESTapi.h"
#define ISspace(x) isspace((int)(x))
#define SERVER_STRING "Server: PATHCOMP/0.1.0\r\n"
// List of Clients connected to the PATH COMP
GList *RESTapi_tcp_client_list = NULL;
// Id for CLient HTTP (REST API) Connection
guint CLIENT_ID = 0;
guint32 paId_req = 0;
// Global variables
struct linkList_t* linkList;
struct deviceList_t* deviceList;
struct serviceList_t* serviceList;
gchar algId[MAX_ALG_ID_LENGTH];
gboolean syncPath = FALSE;
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Seek a connected tcp client by its fd in the RESTapi_tcp_client_list
*
* @param data
* @param userdata
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint find_rl_client_by_fd (gconstpointer data, gconstpointer userdata)
{
/** check values */
g_assert(data != NULL);
g_assert(userdata != NULL);
struct pathComp_client *client = (struct pathComp_client*)data;
gint fd = *(gint *)userdata;
if (client->fd == fd)
return 0;
return -1;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to send a message to the corresponding channel
*
* @param source
* @param buf
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint rapi_send_message (GIOChannel *channel, char *buf) {
gsize nbytes, buffersize;
//DEBUG_PC ("Msg prepared to be sent REST API RESPONSE ");
gint len = strlen ((const char*) buf);
//DEBUG_PC ("Targeted Length of the buffer: %d", len);
buffersize = g_io_channel_get_buffer_size (channel);
//DEBUG_PC ("GIOChannel with Buffer Size: %d", (gint)buffersize);
gsize newBufferSize = MAX_GIO_CHANNEL_BUFFER_SIZE;
g_io_channel_set_buffer_size (channel, newBufferSize);
buffersize = g_io_channel_get_buffer_size (channel);
//DEBUG_PC ("GIOChannel with Buffer Size: %d", (gint)buffersize);
/** Send message. */
GError *error = NULL;
char *ptr = buf;
gint nleft = strlen ((const char *)buf);
while (nleft > 0) {
g_io_channel_write_chars (channel, (void *)ptr, nleft, &nbytes, &error);
if (error) {
DEBUG_PC ("Error sending the message to TCP Client");
return (-1);
}
//DEBUG_PC ("Sent %d bytes", (gint)nbytes);
nleft = nleft - nbytes;
//DEBUG_PC ("Remaining to be sent %d", nleft);
ptr += nbytes;
}
DEBUG_PC("RESPONSE MSG SENT");
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to return when something goes wrong when processing the REST API Command
*
* @param source
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_unimplemented (GIOChannel *source)
{
gint ret = 0;
guchar buftmp[1024];
char *buf = g_malloc0 (sizeof (char) * 2048000);
sprintf((char *)buf, "HTTP/1.1 400 Bad request\r\n");
sprintf((char *)buftmp, SERVER_STRING);
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "Content-Type: text/plain\r\n");
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "\r\n");
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "</TITLE></HEAD>\r\n");
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "<BODY><P>HTTP request method not supported.\r\n");
strcat ((char *)buf, (const char *)buftmp);
sprintf((char *)buftmp, "</BODY></HTML>\r\n");
strcat ((char *)buf, (const char *)buftmp);
ret = rapi_send_message (source, buf);
g_free (buf);
(void)ret;
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to put in the buffer the date according to RFC 1123
*
* @param date
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void rapi_add_date_header (char *date)
{
static const char *DAYS[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *MONTHS[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Loading
Loading full blame…