From 4b561628b80579b9ef634c72a67166a35d1a8309 Mon Sep 17 00:00:00 2001 From: Rahul Paknikar Date: Tue, 25 Jun 2019 09:41:53 +0530 Subject: Multiple Outputs --- src/ghdlserver/ghdlserver.c | 108 +++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 46 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 09514e1..80b40f5 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -405,62 +405,78 @@ static void Data_Send(int sockid) out = calloc(1, 1024); - for (i=0;ikey) == 0) { found=1; - break; + // break; } - } - - if(found) - { - strncat(out, s->key, strlen(s->key)); - strncat(out, &colon, 1); - strncat(out, s->val, strlen(s->val)); - strncat(out, &semicolon, 1); + // } + + if(found) + { + strncat(out, s->key, strlen(s->key)); + strncat(out, &colon, 1); + strncat(out, s->val, strlen(s->val)); + strncat(out, &semicolon, 1); + printf("\n\n Out 1: %s \n\n", out); + } + else + { + printf("\n Failed (Not Found) at i=%d \n", i); + + syslog(LOG_ERR,"The %s's value not found in the table.", + Out_Port_Array[i]); + free(out); + return; + } + } + while(1) { - if (wrt_retries > 2) // 22.Feb.2017 - Kludge - { - free(out); - return; - } - ret = can_write_to_socket(sockid); - if (ret > 0) break; - if( ret == -100) - { - syslog(LOG_ERR,"Send aborted to CLT:%d buffer:%s ret=%d", - sockid, out,ret); - free(out); - return; - } - else // select() timed out. Retry.... - { - usleep(1000); - wrt_retries++; - } - } - } - else - { - syslog(LOG_ERR,"The %s's value not found in the table.", - Out_Port_Array[i]); - free(out); - return; - } - - if ((send(sockid, out, strlen(out), 0)) == -1) - { - syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); - exit(1); - } + if (wrt_retries > 2) // 22.Feb.2017 - Kludge + { + free(out); + printf("\n Retries finished \n"); + return; + } + ret = can_write_to_socket(sockid); + if (ret > 0) break; + + if( ret == -100) + { + syslog(LOG_ERR,"Send aborted to CLT:%d buffer:%s ret=%d", + sockid, out,ret); + free(out); + printf("\n Cannot write Socket \n"); + return; + } + else // select() timed out. Retry.... + { + printf("\n Sleep \n"); + usleep(1000); + wrt_retries++; + } + } - syslog(LOG_INFO,"SNT:TRNUM:%d to CLT:%d buffer: %s", trnum++, sockid, out); - free(out); + if ((send(sockid, out, strlen(out), 0)) == -1) + { + printf("\n Failed (Socket not send) at i=%d \n", i); + syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); + exit(1); + } + + syslog(LOG_INFO,"SNT:TRNUM:%d to CLT:%d buffer: %s", trnum++, sockid, out); + free(out); + } void Vhpi_Initialize(int sock_port) -- cgit From f087e3036424fe107f4d95af7f858bffd3488ef1 Mon Sep 17 00:00:00 2001 From: Rahul Paknikar Date: Wed, 3 Jul 2019 16:20:52 +0530 Subject: Solution to Infinite Loop --- src/ghdlserver/ghdlserver.c | 121 +++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 59 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 80b40f5..2860ad8 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -77,7 +77,7 @@ static int get_ngspice_pid(void) FILE* fp = NULL; struct dirent* dir_entry; char path[1024], rd_buff[1024]; - pid_t pid = -1; + pid_t pid[32]; if ((dirp = opendir("/proc/")) == NULL) { @@ -85,39 +85,47 @@ static int get_ngspice_pid(void) exit(-1); } + for(int i=0; i<31; i++) + pid[i] = -1; + + int i=0; + while ((dir_entry = readdir(dirp)) != NULL) { - char* nptr; - int valid_num = 0; + char* nptr; + int valid_num = 0; - int tmp = strtol(dir_entry->d_name, &nptr, 10); - if ((errno == ERANGE) && (tmp == LONG_MAX || tmp == LONG_MIN)) - { - perror("strtol"); // Number out of range. - return(-1); - } - if (dir_entry->d_name == nptr) - { - continue; // No digits found. - } - if (tmp) - { - sprintf(path, "/proc/%s/comm", dir_entry->d_name); - if ((fp = fopen(path, "r")) != NULL) - { - fscanf(fp, "%s", rd_buff); - if (strcmp(rd_buff, NGSPICE) == 0) + int tmp = strtol(dir_entry->d_name, &nptr, 10); + if ((errno == ERANGE) && (tmp == LONG_MAX || tmp == LONG_MIN)) { - pid = (pid_t)tmp; - break; + perror("strtol"); // Number out of range. + return(-1); + } + if (dir_entry->d_name == nptr) + { + continue; // No digits found. + } + if (tmp) + { + sprintf(path, "/proc/%s/comm", dir_entry->d_name); + if ((fp = fopen(path, "r")) != NULL) + { + fscanf(fp, "%s", rd_buff); + if (strcmp(rd_buff, NGSPICE) == 0) + { + pid[i++] = (pid_t)tmp; + // break; + } + } } - } - } } + + for(int j=0; jkey, var, 10); - strncpy(s->val, value, 10); + strncpy(s->key, var, 64); + strncpy(s->val, value, 64); HASH_ADD_STR(users, key, s ); break; } } s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->key, "sock_id", 10); - snprintf(s->val,10, "%d", sock_id); + strncpy(s->key, "sock_id", 64); + snprintf(s->val,64, "%d", sock_id); HASH_ADD_STR(users, key, s); } @@ -289,15 +297,15 @@ static int connect_to_client(int server_fd) newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd >= 0) { - syslog(LOG_INFO, "SRV:%d New Client Connection CLT:%d", - server_fd, newsockfd); + syslog(LOG_INFO, "SRV:%d New Client Connection CLT:%d", server_fd, newsockfd); } else { syslog(LOG_ERR,"Error: failed in accept(), socket=%d", server_fd); - exit(1); + exit(1); } } + return(newsockfd); } @@ -409,16 +417,12 @@ static void Data_Send(int sockid) { found = 0; - printf("\n Server : i=%d out_port_num=%d \n", i, out_port_num); - - + HASH_FIND_STR(users,Out_Port_Array[i],s); if (strcmp(Out_Port_Array[i], s->key) == 0) { found=1; - // break; } - // } if(found) { @@ -426,12 +430,9 @@ static void Data_Send(int sockid) strncat(out, &colon, 1); strncat(out, s->val, strlen(s->val)); strncat(out, &semicolon, 1); - - printf("\n\n Out 1: %s \n\n", out); } else { - printf("\n Failed (Not Found) at i=%d \n", i); syslog(LOG_ERR,"The %s's value not found in the table.", Out_Port_Array[i]); @@ -445,7 +446,6 @@ static void Data_Send(int sockid) if (wrt_retries > 2) // 22.Feb.2017 - Kludge { free(out); - printf("\n Retries finished \n"); return; } ret = can_write_to_socket(sockid); @@ -456,7 +456,6 @@ static void Data_Send(int sockid) syslog(LOG_ERR,"Send aborted to CLT:%d buffer:%s ret=%d", sockid, out,ret); free(out); - printf("\n Cannot write Socket \n"); return; } else // select() timed out. Retry.... @@ -469,7 +468,6 @@ static void Data_Send(int sockid) if ((send(sockid, out, strlen(out), 0)) == -1) { - printf("\n Failed (Socket not send) at i=%d \n", i); syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); exit(1); } @@ -586,33 +584,34 @@ void Vhpi_Listen() while(1) { - new_sock=connect_to_client(server_socket_id); + new_sock = connect_to_client(server_socket_id); if(new_sock > 0) { char receive_buffer[MAX_BUF_SIZE]; - int n = receive_string(new_sock, receive_buffer); - if(n > 0) + int n = receive_string(new_sock, receive_buffer); + if(n > 0) { - sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge - syslog(LOG_INFO, - "Vhpi_Listen:New socket connection CLT:%d",new_sock); - if(strcmp(receive_buffer, "END")==0) + sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge + syslog(LOG_INFO, "Vhpi_Listen:New socket connection CLT:%d",new_sock); + + printf("\n\n%s\n\n", receive_buffer); + + if(strcmp(receive_buffer, "END")==0) { - syslog(LOG_INFO, - "RCVD:CLOSE REQUEST from CLT:%d", new_sock); + syslog(LOG_INFO, "RCVD:CLOSE REQUEST from CLT:%d", new_sock); Vhpi_Exit(0); } - else + else { parse_buffer(new_sock,receive_buffer); } break; } } - else - { - break; - } + else + { + break; + } } } @@ -662,9 +661,13 @@ void Vhpi_Exit(int sig) { Vhpi_Close(); + // printf("\nVHPI EXIT\n"); + // 10.Mar.2017 - RM - if (pid_file_created) - remove(pid_filename); + if (pid_file_created) { + // printf("%s\n", pid_filename); + remove(pid_filename); + } syslog(LOG_INFO, "*** Exiting ***"); -- cgit From 57ac333e92ddc237b1d107ceadc03afc3d32535f Mon Sep 17 00:00:00 2001 From: Rahul Paknikar Date: Sun, 7 Jul 2019 17:05:50 +0530 Subject: solved infinite loop, greater buffer size --- src/ghdlserver/ghdlserver.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 2860ad8..ac34a87 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -411,7 +411,7 @@ static void Data_Send(int sockid) s = NULL; int found = 0; - out = calloc(1, 1024); + out = calloc(1, 2048); for (i=0; ikey, port_name,10); - strncpy(s->val,port_value,10); + strncpy(s->key, port_name,64); + strncpy(s->val,port_value,64); HASH_ADD_STR( users, key, s ); } -- cgit From f8d3dbc8c0f1c59a0546998cb9365e5c291dca07 Mon Sep 17 00:00:00 2001 From: fossee Date: Tue, 3 Sep 2019 11:07:32 +0530 Subject: added examples and modified server --- src/ghdlserver/ghdlserver.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index ac34a87..5bf2e99 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -77,7 +77,7 @@ static int get_ngspice_pid(void) FILE* fp = NULL; struct dirent* dir_entry; char path[1024], rd_buff[1024]; - pid_t pid[32]; + pid_t pid = -1; if ((dirp = opendir("/proc/")) == NULL) { @@ -85,11 +85,6 @@ static int get_ngspice_pid(void) exit(-1); } - for(int i=0; i<31; i++) - pid[i] = -1; - - int i=0; - while ((dir_entry = readdir(dirp)) != NULL) { char* nptr; @@ -113,19 +108,15 @@ static int get_ngspice_pid(void) fscanf(fp, "%s", rd_buff); if (strcmp(rd_buff, NGSPICE) == 0) { - pid[i++] = (pid_t)tmp; - // break; + pid = (pid_t)tmp; } } } } - for(int j=0; j FOSSEE, IIT-Mumbai +/******************************************************************************** + * FOSSEE, IIT-Bombay + * 18.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to + * support multiple digital models + * - Reads the ip set by its client to map + * with the same socket with the name of file + * NGHDL_IP in the model's local directory + * - On exit, the test bench removes this file as + * well as the NGHDL_COMMON_IP file. It is created + * by its client (cfunc) and stored in /tmp + * directory. It tracks the used IPs for existing + * digital models in current simulation. + * 5.July.2019 - Rahul Paknikar - Added loop to send all port values for + * a given event. + * - Removed bug to terminate multiple testbench + * instances in ngpsice windows. + * * 24.Mar.2017 - Raj Mohan - Added signal handler for SIGUSR1, to handle an * orphan test bench process. * The test bench will now create a PID file in @@ -21,8 +36,8 @@ * Added the following functions: * o curtim() * o print_hash_table() - ************************************************************************* - */ + *********************************************************************************/ + #include #include "ghdlserver.h" #include "uthash.h" @@ -33,7 +48,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -105,11 +121,11 @@ static int get_ngspice_pid(void) sprintf(path, "/proc/%s/comm", dir_entry->d_name); if ((fp = fopen(path, "r")) != NULL) { - fscanf(fp, "%s", rd_buff); - if (strcmp(rd_buff, NGSPICE) == 0) - { - pid = (pid_t)tmp; - } + fscanf(fp, "%s", rd_buff); + if (strcmp(rd_buff, NGSPICE) == 0) + { + pid = (pid_t)tmp; // 5.July.2019 - RP - Kludge + } } } } @@ -127,24 +143,25 @@ static int get_ngspice_pid(void) static void create_pid_file(int sock_port) { pid_t my_pid = getpid(); - pid_t ngspice_pid = get_ngspice_pid(); + pid_t ngspice_pid = get_ngspice_pid(); if (ngspice_pid == -1) { - fprintf(stderr, "create_pid_file() Failed to get ngspice PID"); - syslog(LOG_ERR, "create_pid_file() Failed to get ngspice PID"); - exit(1); + fprintf(stderr, "create_pid_file() Failed to get ngspice PID"); + syslog(LOG_ERR, "create_pid_file() Failed to get ngspice PID"); + exit(1); } + sprintf(pid_filename, "/tmp/NGHDL_%d_%s_%d", ngspice_pid, __progname, sock_port); pid_file = fopen(pid_filename, "w"); if (pid_file) { - pid_file_created = 1; - fprintf(pid_file,"%d\n", my_pid); - fclose(pid_file); + pid_file_created = 1; + fprintf(pid_file,"%d\n", my_pid); + fclose(pid_file); } else { perror("fopen() - PID file"); - syslog(LOG_ERR, "create_pid_file(): Unable to open PID file in /tmp"); + syslog(LOG_ERR, "create_pid_file(): Unable to open PID file in /tmp"); exit(1); } @@ -218,7 +235,7 @@ static void parse_buffer(int sock_id, char* receive_buffer) // //Create Server and listen for client connections. // -static int create_server(int port_number,int max_connections) +static int create_server(int port_number, int max_connections) { int sockfd, reuse = 1; struct sockaddr_in serv_addr; @@ -236,12 +253,32 @@ static int create_server(int port_number,int max_connections) int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); if (ret < 0) { - syslog(LOG_ERR, "create_server:setsockopt() failed...."); + syslog(LOG_ERR, "create_server:setsockopt() failed...."); } +/* 18.Sept.2019 - RP - Read and set IP of server decided by its client */ + char ip_filename[80]; + sprintf(ip_filename, "NGHDL_IP.txt"); + + char lo_ip[20]; + + FILE* fp = fopen(ip_filename, "r"); + + if (fp) + { + if (fscanf(fp, "%s", lo_ip) != EOF) + { + fclose(fp); + } + } else { + perror("fopen() - NGHDL_IP file"); + syslog(LOG_ERR, "create_server(): Unable to open NGHDL_IP file in /tmp"); + exit(1); + } + bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_addr.s_addr = inet_addr(lo_ip); // 18.Sept.2019 - RP - Bind to specific IP only serv_addr.sin_port = htons(port_number); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) @@ -404,7 +441,7 @@ static void Data_Send(int sockid) out = calloc(1, 2048); - for (i=0; i 0) { - server_socket_id = create_server(DEFAULT_SERVER_PORT,DEFAULT_MAX_CONNECTIONS); + // 18.Sept.2019 - RP + server_socket_id = create_server(DEFAULT_SERVER_PORT, DEFAULT_MAX_CONNECTIONS); if(server_socket_id > 0) { syslog(LOG_INFO,"Started the server on port %d SRV:%d", @@ -636,8 +674,8 @@ void Vhpi_Send() if (prev_sendto_sock != sendto_sock) { - Data_Send(sendto_sock); - prev_sendto_sock = sendto_sock; + Data_Send(sendto_sock); + prev_sendto_sock = sendto_sock; } // 22.Feb.2017 End kludge @@ -653,15 +691,16 @@ void Vhpi_Exit(int sig) { Vhpi_Close(); - // printf("\nVHPI EXIT\n"); - -// 10.Mar.2017 - RM + // 10.Mar.2017 - RM if (pid_file_created) { - // printf("%s\n", pid_filename); remove(pid_filename); } + // 18.Sept.2019 - RP + remove("NGHDL_IP.txt"); + remove("/tmp/NGHDL_COMMON_IP.txt"); + syslog(LOG_INFO, "*** Exiting ***"); exit(0); -} +} \ No newline at end of file -- cgit From 7852873cf0bc8b8082ea847a868ab84930163667 Mon Sep 17 00:00:00 2001 From: rahul Date: Thu, 26 Sep 2019 15:21:18 +0530 Subject: multiple instances (static ip) --- src/ghdlserver/Vhpi_Package.vhdl | 7 +++-- src/ghdlserver/ghdlserver.c | 65 ++++++++-------------------------------- src/ghdlserver/ghdlserver.h | 2 +- 3 files changed, 18 insertions(+), 56 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/Vhpi_Package.vhdl b/src/ghdlserver/Vhpi_Package.vhdl index 0c0baac..5d3db8d 100755 --- a/src/ghdlserver/Vhpi_Package.vhdl +++ b/src/ghdlserver/Vhpi_Package.vhdl @@ -1,4 +1,7 @@ -- author: Madhav P. Desai +-- modified: Rahul Paknikar +-- (changed definition of Vhpi_Initialize) + library ieee; use ieee.std_logic_1164.all; library work; @@ -8,7 +11,7 @@ package Vhpi_Foreign is ----------------------------------------------------------------------------- -- foreign Vhpi function ----------------------------------------------------------------------------- - procedure Vhpi_Initialize(sock_port : in integer); + procedure Vhpi_Initialize(sock_port : in integer; sock_ip : in VhpiString); attribute foreign of Vhpi_Initialize : procedure is "VHPIDIRECT Vhpi_Initialize"; procedure Vhpi_Close; -- close . @@ -36,7 +39,7 @@ package body Vhpi_Foreign is ----------------------------------------------------------------------------- -- subprogram bodies for foreign vhpi routines. will never be called ----------------------------------------------------------------------------- - procedure Vhpi_Initialize(sock_port: in integer) is + procedure Vhpi_Initialize(sock_port: in integer; sock_ip : in VhpiString) is begin assert false report "fatal: this should never be called" severity failure; end Vhpi_Initialize; diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 622c246..0c1a683 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -1,6 +1,6 @@ /******************************************************************************** * FOSSEE, IIT-Bombay - * 18.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to + * 26.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to * support multiple digital models * - Reads the ip set by its client to map * with the same socket with the name of file @@ -10,6 +10,7 @@ * by its client (cfunc) and stored in /tmp * directory. It tracks the used IPs for existing * digital models in current simulation. + * - Write PID file in append mode. * 5.July.2019 - Rahul Paknikar - Added loop to send all port values for * a given event. * - Removed bug to terminate multiple testbench @@ -153,7 +154,7 @@ static void create_pid_file(int sock_port) sprintf(pid_filename, "/tmp/NGHDL_%d_%s_%d", ngspice_pid, __progname, sock_port); - pid_file = fopen(pid_filename, "w"); + pid_file = fopen(pid_filename, "a"); // 26.Sept.2019 - RP if (pid_file) { pid_file_created = 1; @@ -234,8 +235,8 @@ static void parse_buffer(int sock_id, char* receive_buffer) // //Create Server and listen for client connections. -// -static int create_server(int port_number, int max_connections) +// 26.Sept.2019 - RP - added parameter of socket ip +static int create_server(int port_number, char my_ip[], int max_connections) { int sockfd, reuse = 1; struct sockaddr_in serv_addr; @@ -256,29 +257,9 @@ static int create_server(int port_number, int max_connections) syslog(LOG_ERR, "create_server:setsockopt() failed...."); } -/* 18.Sept.2019 - RP - Read and set IP of server decided by its client */ - char ip_filename[80]; - sprintf(ip_filename, "NGHDL_IP.txt"); - - char lo_ip[20]; - - FILE* fp = fopen(ip_filename, "r"); - - if (fp) - { - if (fscanf(fp, "%s", lo_ip) != EOF) - { - fclose(fp); - } - } else { - perror("fopen() - NGHDL_IP file"); - syslog(LOG_ERR, "create_server(): Unable to open NGHDL_IP file in /tmp"); - exit(1); - } - bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = inet_addr(lo_ip); // 18.Sept.2019 - RP - Bind to specific IP only + serv_addr.sin_addr.s_addr = inet_addr(my_ip); // 26.Sept.2019 - RP - Bind to specific IP only serv_addr.sin_port = htons(port_number); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) @@ -506,7 +487,8 @@ static void Data_Send(int sockid) } -void Vhpi_Initialize(int sock_port) +// 26.Sept.2019 - RP - added parameter of socket ip +void Vhpi_Initialize(int sock_port, char sock_ip[]) { DEFAULT_SERVER_PORT = sock_port; @@ -519,8 +501,9 @@ void Vhpi_Initialize(int sock_port) while(try_limit > 0) { - // 18.Sept.2019 - RP - server_socket_id = create_server(DEFAULT_SERVER_PORT, DEFAULT_MAX_CONNECTIONS); + + // 26.Sept.2019 - RP + server_socket_id = create_server(DEFAULT_SERVER_PORT, sock_ip, DEFAULT_MAX_CONNECTIONS); if(server_socket_id > 0) { syslog(LOG_INFO,"Started the server on port %d SRV:%d", @@ -648,30 +631,7 @@ void Vhpi_Listen() void Vhpi_Send() { - int sockid; - char* out; - -// Traverse the list of finished jobs and send out the resulting port values.. // 22.Feb.2017 - RM - Kludge -// log_server=fopen("server.log","a"); -// fprintf(log_server, "%s Vhpi_Send() called\n", curtim()); - -// fprintf(log_server,"Vhpi_Send()-------------------\n"); -// print_hash_table(); -// fprintf(log_server,"----------------------------------------\n"); -// HASH_FIND_STR(users,"sock_id",s); -// if(s) -// { -// sockid=atoi(s->val); -// } -// else -// { -// fprintf(log_server,"%s Socket id not in table - key=%s val=%s\n", -// curtim(), -// users->key, users->val); -// } -// Data_Send(sockid); - if (prev_sendto_sock != sendto_sock) { Data_Send(sendto_sock); @@ -696,8 +656,7 @@ void Vhpi_Exit(int sig) remove(pid_filename); } - // 18.Sept.2019 - RP - remove("NGHDL_IP.txt"); + // 26.Sept.2019 - RP remove("/tmp/NGHDL_COMMON_IP.txt"); syslog(LOG_INFO, "*** Exiting ***"); diff --git a/src/ghdlserver/ghdlserver.h b/src/ghdlserver/ghdlserver.h index 9dc8afc..082a1af 100644 --- a/src/ghdlserver/ghdlserver.h +++ b/src/ghdlserver/ghdlserver.h @@ -21,7 +21,7 @@ int DEFAULT_SERVER_PORT; //Vhpi Functions. -void Vhpi_Initialize(int sock_port); +void Vhpi_Initialize(int sock_port, char sock_ip[]); /* 26.Sept.2019 - RP */ void Vhpi_Close(); void Vhpi_Exit(); void Vhpi_Listen(); -- cgit From d8fccdec26e2233f4d013bb6516b785892eb0ea4 Mon Sep 17 00:00:00 2001 From: rahul Date: Fri, 27 Sep 2019 15:07:17 +0530 Subject: adjusted layout --- src/ghdlserver/Vhpi_Package.vhdl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/Vhpi_Package.vhdl b/src/ghdlserver/Vhpi_Package.vhdl index 5d3db8d..77e0f3b 100755 --- a/src/ghdlserver/Vhpi_Package.vhdl +++ b/src/ghdlserver/Vhpi_Package.vhdl @@ -1,6 +1,6 @@ -- author: Madhav P. Desai --- modified: Rahul Paknikar --- (changed definition of Vhpi_Initialize) +-- modified: Rahul Paknikar (26.Sept.2019) +-- (changed definition of Vhpi_Initialize) library ieee; use ieee.std_logic_1164.all; -- cgit From 7f0123a500130711c464a63d3a06636697ecaf89 Mon Sep 17 00:00:00 2001 From: rahul Date: Thu, 3 Oct 2019 12:24:34 +0530 Subject: multiple sockets through poll() --- src/ghdlserver/ghdlserver.c | 146 +++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 75 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 0c1a683..a38541c 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -1,5 +1,7 @@ -/******************************************************************************** +/********************************************************************************** * FOSSEE, IIT-Bombay + * 2.Oct.2019 - Rahul Paknikar - Removed select() file descriptors and replaced + * with poll() to handle sockets greater than 1024. * 26.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to * support multiple digital models * - Reads the ip set by its client to map @@ -10,7 +12,7 @@ * by its client (cfunc) and stored in /tmp * directory. It tracks the used IPs for existing * digital models in current simulation. - * - Write PID file in append mode. + * - Write PID file in append mode. * 5.July.2019 - Rahul Paknikar - Added loop to send all port values for * a given event. * - Removed bug to terminate multiple testbench @@ -52,7 +54,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -267,9 +270,7 @@ static int create_server(int port_number, char my_ip[], int max_connections) fprintf(stderr,"%s- Error: could not bind socket to port %d\n", __progname, port_number); syslog(LOG_ERR, "Error: could not bind socket to port %d", port_number); - close(sockfd); - exit(1); } @@ -286,22 +287,19 @@ static int connect_to_client(int server_fd) int newsockfd = -1; socklen_t clilen; struct sockaddr_in cli_addr; - fd_set c_set; - struct timeval time_limit; - - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - clilen = sizeof(cli_addr); + /* 2.Oct.2019 - RP - Poll File Descriptor */ + struct pollfd fds[1]; - FD_ZERO(&c_set); - FD_SET(server_fd, &c_set); + clilen = sizeof(cli_addr); - select(server_fd + 1, &c_set, NULL, NULL, &time_limit); + fds[0].fd = server_fd; + fds[0].events = POLLIN; - ret_val = FD_ISSET(server_fd, &c_set); + poll(fds, 1, 1); + ret_val = (fds[0].revents & POLLIN); - if(ret_val) + if(ret_val > 0) { newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd >= 0) @@ -313,7 +311,7 @@ static int connect_to_client(int server_fd) syslog(LOG_ERR,"Error: failed in accept(), socket=%d", server_fd); exit(1); } - } + } return(newsockfd); } @@ -323,49 +321,47 @@ static int connect_to_client(int server_fd) // static int can_read_from_socket(int socket_id) { - struct timeval time_limit; - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - - fd_set c_set; - FD_ZERO(&c_set); - FD_SET(socket_id, &c_set); - - int npending = select(socket_id + 1, &c_set, NULL, NULL, &time_limit); + /* 2.Oct.2019 - RP - Poll File Descriptor */ + struct pollfd fds[1]; + + fds[0].fd = socket_id; + fds[0].events = POLLIN; + + int npending = poll(fds, 1, 1); + if (npending == -1) { npending = errno; - syslog(LOG_ERR, "can_read_from_socket:select() ERRNO=%d",npending); + syslog(LOG_ERR, "can_read_from_socket:poll() ERRNO=%d",npending); return(-100); } - return(FD_ISSET(socket_id, &c_set)); + + return (fds[0].revents & POLLIN); // 2.Oct.2019 - RP } // // Check if we can write to the socket.. // static int can_write_to_socket(int socket_id) -{ - struct timeval time_limit; - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - - fd_set c_set; - FD_ZERO(&c_set); - FD_SET(socket_id, &c_set); +{ + /* 2.Oct.2019 - RP - Poll File Descriptor */ + struct pollfd fds[1]; - int npending = select(socket_id + 1, NULL, &c_set, NULL, &time_limit); - if (npending == -1) - { - npending = errno; + fds[0].fd = socket_id; + fds[0].events = POLLOUT; - syslog(LOG_ERR, "can_write_to_socket() select() ERRNO=%d",npending); + int npending = poll(fds, 1, 1); - return (-100); - } else if (npending == 0) { // select() timed out... - return(0); + if (npending == -1) + { + npending = errno; + syslog(LOG_ERR, "can_write_to_socket() : poll() ERRNO=%d",npending); + return (-100); + } else if (npending == 0) { // poll() timed out... + return(0); } - return(FD_ISSET(socket_id,&c_set)); + + return(fds[0].revents & POLLOUT); // 2.Oct.2019 - RP } //Receive string from socket and put it inside buffer. @@ -377,19 +373,20 @@ static int receive_string(int sock_id, char* buffer) while(1) { ret = can_read_from_socket(sock_id); - if (ret == 0) - { // select() had timed out. Retry... - usleep(1000); - continue; - } else - if (ret == -100) - { - return(-1); - } - break; + + if (ret == 0) + { // poll() had timed out. Retry... + usleep(1000); + continue; + } else if (ret == -100) + { + return(-1); + } + break; } nbytes = recv(sock_id, buffer, MAX_BUF_SIZE, 0); + if (nbytes < 0) { perror("READ FAILURE"); @@ -468,7 +465,7 @@ static void Data_Send(int sockid) free(out); return; } - else // select() timed out. Retry.... + else // poll() timed out. Retry.... { printf("\n Sleep \n"); usleep(1000); @@ -597,31 +594,30 @@ void Vhpi_Listen() while(1) { - new_sock = connect_to_client(server_socket_id); + new_sock = connect_to_client(server_socket_id); + if(new_sock > 0) { char receive_buffer[MAX_BUF_SIZE]; - int n = receive_string(new_sock, receive_buffer); - if(n > 0) + int n = receive_string(new_sock, receive_buffer); + if(n > 0) { - sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge - syslog(LOG_INFO, "Vhpi_Listen:New socket connection CLT:%d",new_sock); - - printf("\n\n%s\n\n", receive_buffer); - - if(strcmp(receive_buffer, "END")==0) - { - syslog(LOG_INFO, "RCVD:CLOSE REQUEST from CLT:%d", new_sock); - Vhpi_Exit(0); - } - else - { - parse_buffer(new_sock,receive_buffer); - } - break; + sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge + syslog(LOG_INFO, "Vhpi_Listen:New socket connection CLT:%d",new_sock); + + if(strcmp(receive_buffer, "END")==0) + { + syslog(LOG_INFO, "RCVD:CLOSE REQUEST from CLT:%d", new_sock); + Vhpi_Exit(0); + } + else + { + parse_buffer(new_sock,receive_buffer); + } + break; } } - else + else { break; } -- cgit From 0b2a123b6ea6c3d09fae9fb054849432373794cf Mon Sep 17 00:00:00 2001 From: rahul Date: Tue, 15 Oct 2019 16:46:04 +0530 Subject: blocking socket --- src/ghdlserver/Vhpi_Package.vhdl | 13 +-- src/ghdlserver/ghdlserver.c | 207 ++++++++------------------------------- src/ghdlserver/ghdlserver.h | 1 - 3 files changed, 42 insertions(+), 179 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/Vhpi_Package.vhdl b/src/ghdlserver/Vhpi_Package.vhdl index 77e0f3b..600804c 100755 --- a/src/ghdlserver/Vhpi_Package.vhdl +++ b/src/ghdlserver/Vhpi_Package.vhdl @@ -1,6 +1,7 @@ -- author: Madhav P. Desai --- modified: Rahul Paknikar (26.Sept.2019) --- (changed definition of Vhpi_Initialize) +-- modified: Rahul Paknikar +-- -changed procedure of Vhpi_Initialize - 26.Sept.2019 +-- -removed procedure Vhpi_Close - 15.Oct.2019 library ieee; use ieee.std_logic_1164.all; @@ -14,9 +15,6 @@ package Vhpi_Foreign is procedure Vhpi_Initialize(sock_port : in integer; sock_ip : in VhpiString); attribute foreign of Vhpi_Initialize : procedure is "VHPIDIRECT Vhpi_Initialize"; - procedure Vhpi_Close; -- close . - attribute foreign of Vhpi_Close : procedure is "VHPIDIRECT Vhpi_Close"; - procedure Vhpi_Listen; attribute foreign of Vhpi_Listen : procedure is "VHPIDIRECT Vhpi_Listen"; @@ -44,11 +42,6 @@ package body Vhpi_Foreign is assert false report "fatal: this should never be called" severity failure; end Vhpi_Initialize; - procedure Vhpi_Close is - begin - assert false report "fatal: this should never be called" severity failure; - end Vhpi_Close; - procedure Vhpi_Listen is begin assert false report "fatal: this should never be called" severity failure; diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index a38541c..e93ae9f 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -1,18 +1,14 @@ /********************************************************************************** * FOSSEE, IIT-Bombay - * 2.Oct.2019 - Rahul Paknikar - Removed select() file descriptors and replaced - * with poll() to handle sockets greater than 1024. + * 15.Oct.2019 - Rahul Paknikar - Switched to blocking sockets from non-blocking * 26.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to * support multiple digital models - * - Reads the ip set by its client to map - * with the same socket with the name of file - * NGHDL_IP in the model's local directory - * - On exit, the test bench removes this file as - * well as the NGHDL_COMMON_IP file. It is created - * by its client (cfunc) and stored in /tmp + * - On exit, the test bench removes the + * NGHDL_COMMON_IP file. It is shared by all the + * nghdl digital models and is stored in /tmp * directory. It tracks the used IPs for existing * digital models in current simulation. - * - Write PID file in append mode. + * - Writes PID file in append mode. * 5.July.2019 - Rahul Paknikar - Added loop to send all port values for * a given event. * - Removed bug to terminate multiple testbench @@ -55,7 +51,6 @@ #include #include #include -#include #include #include #include @@ -236,7 +231,6 @@ static void parse_buffer(int sock_id, char* receive_buffer) HASH_ADD_STR(users, key, s); } -// //Create Server and listen for client connections. // 26.Sept.2019 - RP - added parameter of socket ip static int create_server(int port_number, char my_ip[], int max_connections) @@ -280,129 +274,49 @@ static int create_server(int port_number, char my_ip[], int max_connections) return sockfd; } -// The server to wait (non-blocking) for a client connection. +// The server to wait (blocking) for a client connection. static int connect_to_client(int server_fd) { - int ret_val = 1; + int ret_val = 0; int newsockfd = -1; socklen_t clilen; struct sockaddr_in cli_addr; - /* 2.Oct.2019 - RP - Poll File Descriptor */ - struct pollfd fds[1]; - clilen = sizeof(cli_addr); - fds[0].fd = server_fd; - fds[0].events = POLLIN; - - poll(fds, 1, 1); - ret_val = (fds[0].revents & POLLIN); - - if(ret_val > 0) + /* 15.Oct.2019 - RP - Blocking Socket (Accept) */ + newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd >= 0) + { + syslog(LOG_INFO, "SRV:%d New Client Connection CLT:%d", server_fd, newsockfd); + } + else { - newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); - if (newsockfd >= 0) - { - syslog(LOG_INFO, "SRV:%d New Client Connection CLT:%d", server_fd, newsockfd); - } - else - { - syslog(LOG_ERR,"Error: failed in accept(), socket=%d", server_fd); - exit(1); - } - } + syslog(LOG_ERR,"Error: failed in accept(), socket=%d", server_fd); + exit(1); + } return(newsockfd); } -// -// Check if we can read from the socket.. -// -static int can_read_from_socket(int socket_id) -{ - /* 2.Oct.2019 - RP - Poll File Descriptor */ - struct pollfd fds[1]; - - fds[0].fd = socket_id; - fds[0].events = POLLIN; - - int npending = poll(fds, 1, 1); - - if (npending == -1) - { - npending = errno; - syslog(LOG_ERR, "can_read_from_socket:poll() ERRNO=%d",npending); - return(-100); - } - - return (fds[0].revents & POLLIN); // 2.Oct.2019 - RP -} - -// -// Check if we can write to the socket.. -// -static int can_write_to_socket(int socket_id) -{ - /* 2.Oct.2019 - RP - Poll File Descriptor */ - struct pollfd fds[1]; - - fds[0].fd = socket_id; - fds[0].events = POLLOUT; - - int npending = poll(fds, 1, 1); - - if (npending == -1) - { - npending = errno; - syslog(LOG_ERR, "can_write_to_socket() : poll() ERRNO=%d",npending); - return (-100); - } else if (npending == 0) { // poll() timed out... - return(0); - } - - return(fds[0].revents & POLLOUT); // 2.Oct.2019 - RP -} - //Receive string from socket and put it inside buffer. static int receive_string(int sock_id, char* buffer) { int nbytes = 0; int ret; - - while(1) - { - ret = can_read_from_socket(sock_id); - if (ret == 0) - { // poll() had timed out. Retry... - usleep(1000); - continue; - } else if (ret == -100) - { - return(-1); - } - break; - } - + /* 15.Oct.2019 - RP - Blocking Socket - Receive */ nbytes = recv(sock_id, buffer, MAX_BUF_SIZE, 0); if (nbytes < 0) { - perror("READ FAILURE"); + perror("READ FAILURE"); exit(1); } + return(nbytes); } -static void set_non_blocking(int sock_id) -{ - int x; - x = fcntl(sock_id, F_GETFL, 0); - fcntl(sock_id, F_SETFL, x | O_NONBLOCK); - syslog(LOG_INFO, "Setting server to non blocking state."); -} - static void Data_Send(int sockid) { static int trnum; @@ -419,7 +333,8 @@ static void Data_Send(int sockid) out = calloc(1, 2048); - for (i=0; i 2) // 22.Feb.2017 - Kludge - { - free(out); - return; - } - ret = can_write_to_socket(sockid); - if (ret > 0) break; - - if( ret == -100) - { - syslog(LOG_ERR,"Send aborted to CLT:%d buffer:%s ret=%d", - sockid, out,ret); - free(out); - return; - } - else // poll() timed out. Retry.... - { - printf("\n Sleep \n"); - usleep(1000); - wrt_retries++; - } - } - + /* 15.Oct.2019 - RP - Blocking Socket (Send) */ if ((send(sockid, out, strlen(out), 0)) == -1) { syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); @@ -505,25 +396,23 @@ void Vhpi_Initialize(int sock_port, char sock_ip[]) { syslog(LOG_INFO,"Started the server on port %d SRV:%d", DEFAULT_SERVER_PORT, server_socket_id); - set_non_blocking(server_socket_id); break; } else - { + { syslog(LOG_ERR,"Could not start server on port %d,will try again", DEFAULT_SERVER_PORT); - usleep(1000); - try_limit--; - - if(try_limit==0) - { - syslog(LOG_ERR, - "Error:Tried to start server on port %d, failed..giving up.", - DEFAULT_SERVER_PORT); - exit(1); + usleep(1000); + try_limit--; + + if(try_limit==0) + { + syslog(LOG_ERR, + "Error:Tried to start server on port %d, failed..giving up.", + DEFAULT_SERVER_PORT); + exit(1); } - - } + } } // //Reading Output Port name and storing in Out_Port_Array; @@ -594,7 +483,7 @@ void Vhpi_Listen() while(1) { - new_sock = connect_to_client(server_socket_id); + new_sock = connect_to_client(server_socket_id); if(new_sock > 0) { @@ -624,7 +513,6 @@ void Vhpi_Listen() } } - void Vhpi_Send() { // 22.Feb.2017 - RM - Kludge @@ -633,29 +521,12 @@ void Vhpi_Send() Data_Send(sendto_sock); prev_sendto_sock = sendto_sock; } -// 22.Feb.2017 End kludge - -} - -void Vhpi_Close() -{ - close(server_socket_id); - syslog(LOG_INFO, "*** Closed VHPI link. ***"); +// 22.Feb.2017 End kludge } void Vhpi_Exit(int sig) -{ - Vhpi_Close(); - - // 10.Mar.2017 - RM - if (pid_file_created) { - remove(pid_filename); - } - - // 26.Sept.2019 - RP - remove("/tmp/NGHDL_COMMON_IP.txt"); - - syslog(LOG_INFO, "*** Exiting ***"); - +{ + close(server_socket_id); + syslog(LOG_INFO, "*** Closed VHPI link. Exiting... ***"); exit(0); } \ No newline at end of file diff --git a/src/ghdlserver/ghdlserver.h b/src/ghdlserver/ghdlserver.h index 082a1af..9f23f0b 100644 --- a/src/ghdlserver/ghdlserver.h +++ b/src/ghdlserver/ghdlserver.h @@ -22,7 +22,6 @@ int DEFAULT_SERVER_PORT; //Vhpi Functions. void Vhpi_Initialize(int sock_port, char sock_ip[]); /* 26.Sept.2019 - RP */ -void Vhpi_Close(); void Vhpi_Exit(); void Vhpi_Listen(); void Vhpi_Send(); -- cgit From 1d46dd9b6064c3b1735df59d185eba89af3802d9 Mon Sep 17 00:00:00 2001 From: rahul Date: Mon, 11 Nov 2019 11:50:55 +0530 Subject: socket dont-route & reuse-port, close prev-sock to reuse it --- src/ghdlserver/ghdlserver.c | 383 +++++++++++++++++++++----------------------- 1 file changed, 182 insertions(+), 201 deletions(-) (limited to 'src/ghdlserver') diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index e93ae9f..e05a156 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -1,10 +1,14 @@ /********************************************************************************** * FOSSEE, IIT-Bombay - * 15.Oct.2019 - Rahul Paknikar - Switched to blocking sockets from non-blocking + ********************************************************************************** + * 08.Nov.2019 - Rahul Paknikar - Switched to blocking sockets from non-blocking + * - Close previous used socket to prevent from + * generating too many socket descriptors + * - Enabled SO_REUSEPORT, SO_DONTROUTE socket options * 26.Sept.2019 - Rahul Paknikar - Added reading of IP from a file to * support multiple digital models * - On exit, the test bench removes the - * NGHDL_COMMON_IP file. It is shared by all the + * NGHDL_COMMON_IP_ file, shared by all * nghdl digital models and is stored in /tmp * directory. It tracks the used IPs for existing * digital models in current simulation. @@ -13,7 +17,8 @@ * a given event. * - Removed bug to terminate multiple testbench * instances in ngpsice windows. - * + ********************************************************************************** + ********************************************************************************** * 24.Mar.2017 - Raj Mohan - Added signal handler for SIGUSR1, to handle an * orphan test bench process. * The test bench will now create a PID file in @@ -60,16 +65,10 @@ #define _XOPEN_SOURCE 500 #define MAX_NUMBER_PORT 100 - #define NGSPICE "ngspice" // 17.Mar.2017 - RM -extern char* __progname; // 26.Feb.2017 May not be portable to non-GNU systems. - -void Vhpi_Exit(int sig); - static FILE* pid_file; static char pid_filename[80]; - static char* Out_Port_Array[MAX_NUMBER_PORT]; static int out_port_num = 0; static int server_socket_id = -1; @@ -77,6 +76,10 @@ static int sendto_sock; // 22.Feb.2017 - RM - Kludge static int prev_sendto_sock; // 22.Feb.2017 - RM - Kludge static int pid_file_created; // 10.Mar.2017 - RM +extern char* __progname; // 26.Feb.2017 May not be portable to non-GNU systems. + +void Vhpi_Exit(int sig); + struct my_struct { char val[1024]; char key[1024]; @@ -85,6 +88,7 @@ struct my_struct { static struct my_struct *s, *users, *tmp = NULL; + /* 17.Mar.2017 - RM - Get the process id of ngspice program.*/ static int get_ngspice_pid(void) { @@ -96,8 +100,8 @@ static int get_ngspice_pid(void) if ((dirp = opendir("/proc/")) == NULL) { - perror("opendir /proc failed"); - exit(-1); + perror("opendir /proc failed"); + exit(-1); } while ((dir_entry = readdir(dirp)) != NULL) @@ -134,6 +138,7 @@ static int get_ngspice_pid(void) return(pid); } + /* 23.Mar.2017 - RM - Pass the sock_port argument. We need this if a netlist * uses more than one instance of the same test bench, so that we can uniquely * identify the PID files. @@ -143,16 +148,17 @@ static void create_pid_file(int sock_port) { pid_t my_pid = getpid(); pid_t ngspice_pid = get_ngspice_pid(); + if (ngspice_pid == -1) { - fprintf(stderr, "create_pid_file() Failed to get ngspice PID"); - syslog(LOG_ERR, "create_pid_file() Failed to get ngspice PID"); - exit(1); + fprintf(stderr, "create_pid_file() Failed to get ngspice PID"); + syslog(LOG_ERR, "create_pid_file() Failed to get ngspice PID"); + exit(1); } - sprintf(pid_filename, "/tmp/NGHDL_%d_%s_%d", ngspice_pid, __progname, - sock_port); - pid_file = fopen(pid_filename, "a"); // 26.Sept.2019 - RP + sprintf(pid_filename, "/tmp/NGHDL_%d_%s_%d", ngspice_pid, __progname, sock_port); + pid_file = fopen(pid_filename, "a"); // 26.Sept.2019 - RP - Open file in append mode + if (pid_file) { pid_file_created = 1; @@ -160,13 +166,12 @@ static void create_pid_file(int sock_port) fclose(pid_file); } else { perror("fopen() - PID file"); - syslog(LOG_ERR, "create_pid_file(): Unable to open PID file in /tmp"); + syslog(LOG_ERR, "create_pid_file(): Unable to open PID file in /tmp"); exit(1); } - - return; } + #ifdef DEBUG static char* curtim(void) { @@ -185,18 +190,18 @@ static char* curtim(void) } #endif + #ifdef DEBUG static void print_hash_table(void) { struct my_struct *sptr; for(sptr=users; sptr != NULL; sptr=sptr->hh.next) - { - syslog(LOG_INFO, "Hash table:val:%s: key: %s", sptr->val, sptr->key); - } + syslog(LOG_INFO, "Hash table:val:%s: key: %s", sptr->val, sptr->key); } #endif + static void parse_buffer(int sock_id, char* receive_buffer) { static int rcvnum; @@ -217,11 +222,11 @@ static void parse_buffer(int sock_id, char* receive_buffer) ptr1 = rest; while(var=strtok_r(token, ":", &value)) { - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->key, var, 64); - strncpy(s->val, value, 64); - HASH_ADD_STR(users, key, s ); - break; + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + strncpy(s->key, var, 64); + strncpy(s->val, value, 64); + HASH_ADD_STR(users, key, s ); + break; } } @@ -231,60 +236,69 @@ static void parse_buffer(int sock_id, char* receive_buffer) HASH_ADD_STR(users, key, s); } + //Create Server and listen for client connections. // 26.Sept.2019 - RP - added parameter of socket ip static int create_server(int port_number, char my_ip[], int max_connections) { - int sockfd, reuse = 1; - struct sockaddr_in serv_addr; - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if (sockfd < 0) - { - fprintf(stderr, "%s- Error: in opening socket on port %d\n", - __progname, port_number); - exit(1); - } - -/* 20.Mar.2017 - RM - SO_REUSEADDR option. To take care of TIME_WAIT state.*/ - int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); - if (ret < 0) - { - syslog(LOG_ERR, "create_server:setsockopt() failed...."); - } - - bzero((char *) &serv_addr, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = inet_addr(my_ip); // 26.Sept.2019 - RP - Bind to specific IP only - serv_addr.sin_port = htons(port_number); + int sockfd, reuse = 1; + struct sockaddr_in serv_addr; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) + { + fprintf(stderr, "%s- Error: in opening socket at server \n", __progname); + //exit(1); + return -1; + } + + /* 20.Mar.2017 - RM - SO_REUSEADDR option. To take care of TIME_WAIT state.*/ + int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); + + /* 08.Nov.2019 - RP - SO_REUSEPORT and SO_DONTROUTE option.*/ + ret += setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(int)); + ret += setsockopt(sockfd, SOL_SOCKET, SO_DONTROUTE, &reuse, sizeof(int)); + + if (ret < 0) + { + syslog(LOG_ERR, "create_server:setsockopt() failed...."); + // close(sockfd); + // return -1; + } + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr(my_ip); // 26.Sept.2019 - RP - Bind to specific IP only + serv_addr.sin_port = htons(port_number); - if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - { - fprintf(stderr,"%s- Error: could not bind socket to port %d\n", + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + { + fprintf(stderr,"%s- Error: could not bind socket to port %d\n", __progname, port_number); - syslog(LOG_ERR, "Error: could not bind socket to port %d", port_number); - close(sockfd); - exit(1); - } + syslog(LOG_ERR, "Error: could not bind socket to port %d", port_number); + close(sockfd); + exit(1); + } - // Start listening on the server. - listen(sockfd, max_connections); + // Start listening on the server. + listen(sockfd, max_connections); - return sockfd; + return sockfd; } + // The server to wait (blocking) for a client connection. static int connect_to_client(int server_fd) { int ret_val = 0; int newsockfd = -1; socklen_t clilen; - struct sockaddr_in cli_addr; + struct sockaddr_in cli_addr; clilen = sizeof(cli_addr); - /* 15.Oct.2019 - RP - Blocking Socket (Accept) */ + /* 08.Nov.2019 - RP - Blocking Socket (Accept) */ newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd >= 0) { @@ -296,85 +310,72 @@ static int connect_to_client(int server_fd) exit(1); } - return(newsockfd); + return newsockfd; } + //Receive string from socket and put it inside buffer. -static int receive_string(int sock_id, char* buffer) +static void receive_string(int sock_id, char* buffer) { - int nbytes = 0; - int ret; + int nbytes = 0; - /* 15.Oct.2019 - RP - Blocking Socket - Receive */ + /* 08.Nov.2019 - RP - Blocking Socket - Receive */ nbytes = recv(sock_id, buffer, MAX_BUF_SIZE, 0); - - if (nbytes < 0) + if (nbytes <= 0) { perror("READ FAILURE"); exit(1); } +} - return(nbytes); -} static void Data_Send(int sockid) -{ - static int trnum; - char* out; - - int i; - char colon = ':'; - char semicolon = ';'; - int wrt_retries = 0; - int ret; - - s = NULL; - int found = 0; - - out = calloc(1, 2048); - - // 5.July.2019 - RP - loop to send all ports at once for an event - for (i=0; ikey) == 0) - { - found=1; - } - - if(found) - { - strncat(out, s->key, strlen(s->key)); - strncat(out, &colon, 1); - strncat(out, s->val, strlen(s->val)); - strncat(out, &semicolon, 1); - } - else - { - - syslog(LOG_ERR,"The %s's value not found in the table.", +{ + static int trnum; + char* out; + + int i; + char colon = ':'; + char semicolon = ';'; + int wrt_retries = 0; + int ret; + + s = NULL; + + out = calloc(1, 2048); + + // 5.July.2019 - RP - loop to send all ports at once for an event + for (i=0; ikey) == 0) + { + strncat(out, s->key, strlen(s->key)); + strncat(out, &colon, 1); + strncat(out, s->val, strlen(s->val)); + strncat(out, &semicolon, 1); + } + else + { + syslog(LOG_ERR,"The %s's value not found in the table.", Out_Port_Array[i]); - free(out); - return; - } - + free(out); + return; + } } - /* 15.Oct.2019 - RP - Blocking Socket (Send) */ - if ((send(sockid, out, strlen(out), 0)) == -1) - { - syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); - exit(1); - } + /* 08.Nov.2019 - RP - Blocking Socket (Send) */ + if ((send(sockid, out, strlen(out), 0)) == -1) + { + syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); + exit(1); + } - syslog(LOG_INFO,"SNT:TRNUM:%d to CLT:%d buffer: %s", trnum++, sockid, out); - free(out); - + syslog(LOG_INFO,"SNT:TRNUM:%d to CLT:%d buffer: %s", trnum++, sockid, out); + free(out); } + // 26.Sept.2019 - RP - added parameter of socket ip void Vhpi_Initialize(int sock_port, char sock_ip[]) { @@ -382,41 +383,37 @@ void Vhpi_Initialize(int sock_port, char sock_ip[]) signal(SIGINT,Vhpi_Exit); signal(SIGTERM,Vhpi_Exit); - signal(SIGUSR1, Vhpi_Exit); //10.Mar.2017 - RM int try_limit = 100; while(try_limit > 0) { + // 26.Sept.2019 - RP + server_socket_id = create_server(DEFAULT_SERVER_PORT, sock_ip, DEFAULT_MAX_CONNECTIONS); - // 26.Sept.2019 - RP - server_socket_id = create_server(DEFAULT_SERVER_PORT, sock_ip, DEFAULT_MAX_CONNECTIONS); - if(server_socket_id > 0) - { + if(server_socket_id >= 0) + { syslog(LOG_INFO,"Started the server on port %d SRV:%d", DEFAULT_SERVER_PORT, server_socket_id); break; } - else - { - syslog(LOG_ERR,"Could not start server on port %d,will try again", + + syslog(LOG_ERR,"Could not start server on port %d,will try again", DEFAULT_SERVER_PORT); - usleep(1000); - try_limit--; + usleep(1000); + try_limit--; - if(try_limit==0) - { - syslog(LOG_ERR, - "Error:Tried to start server on port %d, failed..giving up.", + if(try_limit==0) + { + syslog(LOG_ERR, + "Error:Tried to start server on port %d, failed..giving up.", DEFAULT_SERVER_PORT); - exit(1); - } - } + exit(1); + } } - // - //Reading Output Port name and storing in Out_Port_Array; - // + + //Reading Output Port name and storing in Out_Port_Array; char* line = NULL; size_t len = 0; ssize_t read; @@ -425,105 +422,89 @@ void Vhpi_Initialize(int sock_port, char sock_ip[]) struct timespec ts; fp=fopen("connection_info.txt","r"); - if (! fp) + if (!fp) { - syslog(LOG_ERR,"Vhpi_Initialize: Failed to open connection_info.txt. Exiting..."); - exit(1); + syslog(LOG_ERR,"Vhpi_Initialize: Failed to open connection_info.txt. Exiting..."); + exit(1); } line = (char*) malloc(80); while ((read = getline(&line, &len, fp)) != -1) { - if (strstr(line,"OUT") != NULL || strstr(line,"out") != NULL ) - { - strtok_r(line, " ",&token); - Out_Port_Array[out_port_num] = line; - out_port_num++; - } - line = (char*) malloc(80); + if (strstr(line,"OUT") != NULL || strstr(line,"out") != NULL) + { + strtok_r(line, " ",&token); + Out_Port_Array[out_port_num] = line; + out_port_num++; + } + line = (char*) malloc(80); } fclose(fp); - free(line); ts.tv_sec = 2; ts.tv_nsec = 0; nanosleep(&ts, NULL); -// 10.Mar.2017 - RM - Create PID file for the test bench. + // 10.Mar.2017 - RM - Create PID file for the test bench. create_pid_file(sock_port); } + + void Vhpi_Set_Port_Value(char *port_name,char *port_value,int port_width) { - - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->key, port_name,64); - strncpy(s->val,port_value,64); - HASH_ADD_STR( users, key, s ); - + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + strncpy(s->key, port_name,64); + strncpy(s->val,port_value,64); + HASH_ADD_STR( users, key, s ); } + void Vhpi_Get_Port_Value(char* port_name,char* port_value,int port_width) { - - HASH_FIND_STR(users,port_name,s); - if(s) - { - snprintf(port_value,sizeof(port_value),"%s",s->val); - - HASH_DEL(users, s); - free(s); - s=NULL; - } + HASH_FIND_STR(users,port_name,s); + if(s) + { + snprintf(port_value,sizeof(port_value),"%s",s->val); + HASH_DEL(users, s); + free(s); + s=NULL; + } } + void Vhpi_Listen() { - int new_sock; + sendto_sock = connect_to_client(server_socket_id); // 22.Feb.2017 - RM - Kludge + char receive_buffer[MAX_BUF_SIZE]; + receive_string(sendto_sock, receive_buffer); + + syslog(LOG_INFO, "Vhpi_Listen:New socket connection CLT:%d",sendto_sock); - while(1) + if(strcmp(receive_buffer, "END")==0) { - new_sock = connect_to_client(server_socket_id); + syslog(LOG_INFO, "RCVD:CLOSE REQUEST from CLT:%d", sendto_sock); + Vhpi_Exit(0); + } - if(new_sock > 0) - { - char receive_buffer[MAX_BUF_SIZE]; - int n = receive_string(new_sock, receive_buffer); - if(n > 0) - { - sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge - syslog(LOG_INFO, "Vhpi_Listen:New socket connection CLT:%d",new_sock); - - if(strcmp(receive_buffer, "END")==0) - { - syslog(LOG_INFO, "RCVD:CLOSE REQUEST from CLT:%d", new_sock); - Vhpi_Exit(0); - } - else - { - parse_buffer(new_sock,receive_buffer); - } - break; - } - } - else - { - break; - } - } + parse_buffer(sendto_sock, receive_buffer); } + void Vhpi_Send() { // 22.Feb.2017 - RM - Kludge if (prev_sendto_sock != sendto_sock) { - Data_Send(sendto_sock); + Data_Send(sendto_sock); + + close(prev_sendto_sock); // 08.Nov.2019 - RP - Close previous socket prev_sendto_sock = sendto_sock; } // 22.Feb.2017 End kludge } + void Vhpi_Exit(int sig) { close(server_socket_id); -- cgit