Scheduled maintenance on Saturday, 27 September 2025, from 07:00 AM to 4:00 PM GMT (09:00 AM to 6:00 PM CEST) - some services may be unavailable -

Skip to content
pathComp_RESTapi.c 68.7 KiB
Newer Older
////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	# 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"
#include "pathComp_sp.h"
#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…