#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <gtk/gtk.h>
#include "ipdcGui.h"
#include "connections.h"
#include "parser.h"
#include "global.h"
#include "align_sort.h"
Go to the source code of this file.
Functions | |
void | sigchld_handler (int s) |
void | setup () |
void * | UL_udp () |
void * | UL_tcp () |
void * | UL_tcp_connection (void *temp_pdc) |
void | PMU_process_UDP (unsigned char *udp_buffer, struct sockaddr_in PMU_addr, int sockfd) |
void | PMU_process_TCP (unsigned char tcp_buffer[], int sockfd) |
Variables | |
int | yes = 1 |
char | display_buf [200] |
void PMU_process_TCP | ( | unsigned char | tcp_buffer[], | |
int | sockfd | |||
) |
Definition at line 555 of file connections.c.
References cfgparser(), create_command_frame(), dataparser(), fp_log, and to_intconvertor().
Referenced by connect_pmu_tcp().
00555 { 00556 00557 int stat_status; 00558 unsigned int id; 00559 unsigned char id_char[2]; 00560 00561 id_char[0] = tcp_buffer[4]; 00562 id_char[1] = tcp_buffer[5]; 00563 id = to_intconvertor(id_char); 00564 00565 unsigned char c = tcp_buffer[1]; 00566 c <<= 1; 00567 c >>= 5; 00568 00569 if(c == 0x00){ /* If data frame */ 00570 00571 fprintf(fp_log,"\nData frame\n"); 00572 stat_status = dataparser(tcp_buffer); 00573 00574 /* Handle the Stat word */ 00575 if((stat_status == 10)||(stat_status == 14)) { 00576 00577 unsigned char *cmdframe = malloc(19); 00578 cmdframe[18] = '\0'; 00579 create_command_frame(1,id,(char *)cmdframe); 00580 00581 if (send(sockfd,cmdframe,18, 0)== -1) 00582 perror("send"); 00583 free(cmdframe); 00584 00585 } else if (stat_status == 15) { /* Data Invalid */ 00586 00587 fprintf(fp_log,"Data Invalid\n"); 00588 } 00589 00590 } else if(c == 0x03) { /* If configuration frame */ 00591 00592 fprintf(fp_log,"\nConfiguration frame\n"); 00593 cfgparser(tcp_buffer); 00594 unsigned char *cmdframe = malloc(19); 00595 cmdframe[18] = '\0'; 00596 create_command_frame(2,id,(char *)cmdframe); 00597 fprintf(fp_log,"\nReturn from create_command_frame\n"); 00598 /* Command frame sent to send the data frames */ 00599 if (send(sockfd,cmdframe,18, 0)== -1) 00600 perror("send"); 00601 free(cmdframe); 00602 00603 } else { 00604 00605 fprintf(fp_log,"\nErroneous frame\n"); 00606 } 00607 fflush(stdout); 00608 }
void PMU_process_UDP | ( | unsigned char * | udp_buffer, | |
struct sockaddr_in | PMU_addr, | |||
int | sockfd | |||
) |
Definition at line 491 of file connections.c.
References cfgparser(), create_command_frame(), dataparser(), fp_log, and to_intconvertor().
Referenced by connect_pmu_udp().
00491 { 00492 00493 int stat_status; 00494 unsigned int id; 00495 unsigned char id_char[2]; 00496 00497 id_char[0] = udp_buffer[4]; 00498 id_char[1] = udp_buffer[5]; 00499 id = to_intconvertor(id_char); 00500 00501 unsigned char c = udp_buffer[1]; 00502 c <<= 1; 00503 c >>= 5; 00504 if(c == 0x00){ /* If data frame */ 00505 00506 stat_status = dataparser(udp_buffer); 00507 00508 /* Change in cfg frame is handled */ 00509 if((stat_status == 10)||(stat_status == 14)) { 00510 00511 unsigned char *cmdframe = malloc(19); 00512 cmdframe[18] = '\0'; 00513 create_command_frame(1,id,(char *)cmdframe); 00514 00515 if (sendto(sockfd,cmdframe,18, 0, 00516 (struct sockaddr *)&PMU_addr,sizeof(PMU_addr)) == -1) 00517 perror("sendto"); 00518 free(cmdframe); 00519 00520 } else if (stat_status == 15) { /* Data Invalid */ 00521 00522 fprintf(fp_log,"Data Invalid\n"); 00523 00524 } 00525 00526 } else if(c == 0x03) { /* If configuration frame */ 00527 00528 fprintf(fp_log,"\nConfiguration frame\n"); 00529 cfgparser(udp_buffer); 00530 00531 unsigned char *cmdframe = malloc(19); 00532 cmdframe[18] = '\0'; 00533 create_command_frame(2,id,(char *)cmdframe); 00534 fprintf(fp_log,"\nReturn from create_command_frame\n"); 00535 /* Command frame sent to send the data frames */ 00536 if (sendto(sockfd,cmdframe, 18, 0, 00537 (struct sockaddr *)&PMU_addr,sizeof(PMU_addr)) == -1) 00538 perror("sendto"); 00539 free(cmdframe); 00540 00541 } else { 00542 00543 fprintf(fp_log,"Erroneous frame\n"); 00544 00545 } 00546 fflush(stdout); 00547 }
void setup | ( | ) |
Definition at line 87 of file connections.c.
References BACKLOG, DB_addr_len, DBPORT, sa, sigchld_handler(), TCP_my_addr, TCP_thread, TCPPORT, UDP_thread, UDPPORT, UL_tcp(), UL_TCP_sin_size, UL_TCP_sockfd, UL_udp(), UL_UDP_addr_len, UL_UDP_sockfd, and yes.
Referenced by fill_pdc_details(), and validation_pdc_detail().
00087 { 00088 /* ---------------------------------------------------------------- */ 00089 /* Initialize Global Mutex Variables from global.h */ 00090 /* ---------------------------------------------------------------- */ 00091 00092 int err; 00093 00094 /* Create UDP socket and bind to port */ 00095 00096 if ((UL_UDP_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 00097 00098 perror("socket"); 00099 exit(1); 00100 00101 } else { 00102 00103 printf("UDP Socket:Sucessfully created\n"); 00104 00105 } 00106 00107 if (setsockopt(UL_UDP_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { 00108 perror("setsockopt"); 00109 exit(1); 00110 } 00111 00112 UDP_my_addr.sin_family = AF_INET; // host byte order 00113 UDP_my_addr.sin_port = htons(UDPPORT); // short, network byte order 00114 UDP_my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP 00115 memset(&(UDP_my_addr.sin_zero),'\0', 8); // zero the rest of the struct 00116 00117 if (bind(UL_UDP_sockfd, (struct sockaddr *)&UDP_my_addr, 00118 sizeof(struct sockaddr)) == -1) { 00119 perror("bind"); 00120 exit(1); 00121 } else { 00122 00123 printf("UDP Socket Bind :Sucessfull\n"); 00124 } 00125 00126 00127 /* Created socket and bound to port */ 00128 00129 /* Create TCP socket and bind and listen on port */ 00130 00131 if ((UL_TCP_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 00132 perror("socket"); 00133 exit(1); 00134 } else { 00135 00136 printf("TCP Socket:Sucessfully created\n"); 00137 } 00138 00139 if (setsockopt(UL_TCP_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { 00140 perror("setsockopt"); 00141 exit(1); 00142 } 00143 00144 TCP_my_addr.sin_family = AF_INET; // host byte order 00145 TCP_my_addr.sin_port = htons(TCPPORT); // short, network byte order 00146 TCP_my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP 00147 memset(&(TCP_my_addr.sin_zero), '\0', 8); // zero the rest of the struct 00148 00149 if (bind(UL_TCP_sockfd, (struct sockaddr *)&TCP_my_addr, sizeof(struct sockaddr)) 00150 == -1) { 00151 perror("bind"); 00152 exit(1); 00153 00154 } else { 00155 00156 printf("TCP Socket Bind :Sucessfull\n"); 00157 } 00158 00159 if (listen(UL_TCP_sockfd, BACKLOG) == -1) { 00160 00161 perror("listen"); 00162 exit(1); 00163 00164 } else { 00165 00166 printf("TCP Listen :Sucessfull\n"); 00167 } 00168 00169 sa.sa_handler = sigchld_handler; // reap all dead processes 00170 sigemptyset(&sa.sa_mask); 00171 sa.sa_flags = SA_RESTART; 00172 if (sigaction(SIGCHLD, &sa, NULL) == -1) { 00173 perror("sigaction"); 00174 exit(1); 00175 } 00176 00177 /* TCP created socket and is litening for connections */ 00178 00179 printf("\nUDP Listening on port %d for command frames from Upper PDC\n",UDPPORT); 00180 printf("\nTCP Listening on port %d for command frames from Upper PDC\n",TCPPORT); 00181 printf("\nPort %d for Sending data for archival from Upper PDC\n",DBPORT); 00182 00183 UL_TCP_sin_size = sizeof(struct sockaddr_in); 00184 UL_UDP_addr_len = sizeof(struct sockaddr); 00185 DB_addr_len = sizeof(struct sockaddr); 00186 00187 /* Threads are created for UDP and TCP to listen on port 6001 and 6000 respectively in default attr mode*/ 00188 if((err = pthread_create(&UDP_thread,NULL,UL_udp,NULL))) { 00189 00190 perror(strerror(err)); 00191 exit(1); 00192 } 00193 00194 if((err = pthread_create(&TCP_thread,NULL,UL_tcp,NULL))) { 00195 00196 perror(strerror(err)); 00197 exit(1); 00198 } 00199 }
void sigchld_handler | ( | int | s | ) |
Definition at line 77 of file connections.c.
Referenced by setup().
void* UL_tcp | ( | ) |
Definition at line 324 of file connections.c.
References fp_log, Upper_Layer_Details::ip, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Upper_Layer_Details::pdc_addr, Upper_Layer_Details::port, Upper_Layer_Details::protocol, Upper_Layer_Details::sockfd, TCPPORT, UL_TCP_addr, UL_tcp_connection(), UL_TCP_sockfd, and ULfirst.
Referenced by setup().
00324 { 00325 00326 int err; 00327 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 00328 pthread_attr_t attr; 00329 pthread_attr_init(&attr); 00330 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 00331 // immediately freed when it terminates, but 00332 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 00333 exit(1); // on the thread termination. 00334 00335 } 00336 00337 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 00338 00339 perror(strerror(err)); 00340 exit(1); 00341 } 00342 00343 int sin_size,new_fd,pdc_flag = 0; 00344 00345 while (1) { 00346 00347 sin_size = sizeof(struct sockaddr_in); 00348 if (((new_fd = accept(UL_TCP_sockfd, (struct sockaddr *)&UL_TCP_addr, 00349 (socklen_t *)&sin_size)) == -1)) { // main if starts 00350 perror("accept"); 00351 00352 } else { /* New TCP connection has been received*/ 00353 00354 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00355 struct Upper_Layer_Details *temp_pdc = ULfirst; 00356 if(ULfirst == NULL) { 00357 00358 pdc_flag = 0; 00359 00360 } else { 00361 00362 while(temp_pdc != NULL ) { 00363 00364 if((!strcmp(temp_pdc->ip,inet_ntoa(UL_TCP_addr.sin_addr))) && 00365 (!strncasecmp(temp_pdc->protocol,"TCP",3)) && (temp_pdc->port == TCPPORT)) { 00366 00367 pdc_flag = 1; 00368 break; 00369 } else { 00370 00371 temp_pdc = temp_pdc->next; 00372 } 00373 } 00374 } 00375 if(pdc_flag) { 00376 00377 temp_pdc->sockfd = new_fd; 00378 /* PDC is authentic. Send the command frame for cfg frame */ 00379 pthread_t t; 00380 fprintf(fp_log,"server: got connection from %s\n", 00381 inet_ntoa(temp_pdc->pdc_addr.sin_addr)); 00382 /* Creates a new thread for each TCP connection. */ 00383 if((err = pthread_create(&t,&attr,UL_tcp_connection,(void *)temp_pdc))) { 00384 00385 perror(strerror(err)); 00386 exit(1); 00387 } 00388 00389 } else { /* If PMU ip is not in the upperpdc_ip.txt */ 00390 00391 fprintf(fp_log,"Request from %s TCP which is un-authentic\n", 00392 inet_ntoa(UL_TCP_addr.sin_addr)); 00393 } 00394 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00395 00396 } // main if ends 00397 00398 } // While ends 00399 00400 pthread_attr_destroy(&attr); 00401 }
void* UL_tcp_connection | ( | void * | temp_pdc | ) |
Definition at line 409 of file connections.c.
References cfgframe, Upper_Layer_Details::config_change, create_cfgframe(), fp_log, mutex_Upper_Layer_Details, numbytes, root_pmuid, Upper_Layer_Details::sockfd, Upper_Layer_Details::tcpup, Upper_Layer_Details::thread_id, Upper_Layer_Details::UL_data_transmission_off, UL_tcp_command, and Upper_Layer_Details::UL_upper_pdc_cfgsent.
Referenced by UL_tcp().
00409 { 00410 00411 struct Upper_Layer_Details *udetails = (struct Upper_Layer_Details *) temp_pdc; 00412 int UL_new_fd = udetails->sockfd; 00413 printf("\nframe Re\n"); 00414 udetails->thread_id = pthread_self(); 00415 while(1) { 00416 00417 memset(UL_tcp_command,19,0); 00418 int bytes_read = recv(UL_new_fd,UL_tcp_command,18,0); 00419 if(bytes_read == -1) { 00420 00421 perror("recv"); 00422 udetails->tcpup = 0; 00423 pthread_exit(NULL); 00424 00425 } else if(bytes_read == 0){ 00426 00427 printf("The Client connection exited \n"); 00428 udetails->tcpup = 0; 00429 pthread_exit(NULL); 00430 00431 } else { 00432 00433 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00434 unsigned char c = UL_tcp_command[1]; 00435 c <<= 1; 00436 c >>= 5; 00437 00438 if(c == 0x04) { /* Check if it is a command frame from Upper PDC*/ 00439 00440 fprintf(fp_log,"Command frame Received\n"); // Need to further check if the command is for cfg or data 00441 c = UL_tcp_command[15]; 00442 00443 if((c & 0x05) == 0x05){ //Send CFg frame to PDC 00444 00445 while(root_pmuid != NULL); // Wait till the staus chage list becomes empty 00446 numbytes = create_cfgframe(); 00447 udetails->tcpup = 1; 00448 if (send(UL_new_fd,cfgframe,numbytes, 0)== -1) 00449 perror("send"); 00450 free(cfgframe); 00451 00452 udetails->UL_upper_pdc_cfgsent = 1; 00453 udetails->config_change = 0; 00454 00455 } else if((c & 0x02) == 0x02) { 00456 00457 if(udetails->UL_upper_pdc_cfgsent == 1) { // Only if cfg is sent send the data 00458 00459 udetails->UL_data_transmission_off = 0; 00460 00461 } else { 00462 00463 fprintf(fp_log,"Data cannot be sent as CMD for CFG not received\n"); 00464 00465 } 00466 00467 } else if ((c & 0x01) == 0x01){ // Put the data transmission off 00468 00469 udetails->UL_data_transmission_off = 1; 00470 } 00471 } else { /* If it is a frame other than command frame */ 00472 00473 fprintf(fp_log,"Not a command frame\n"); 00474 } 00475 00476 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00477 } 00478 00479 } // while 00480 close(UL_new_fd); 00481 pthread_exit(NULL); 00482 }
void* UL_udp | ( | ) |
Definition at line 206 of file connections.c.
References Upper_Layer_Details::address_set, cfgframe, Upper_Layer_Details::config_change, create_cfgframe(), display_buf, fp_log, Upper_Layer_Details::ip, mutex_Upper_Layer_Details, Upper_Layer_Details::next, numbytes, Upper_Layer_Details::pdc_addr, Upper_Layer_Details::port, Upper_Layer_Details::protocol, root_pmuid, Upper_Layer_Details::sockfd, UDPPORT, Upper_Layer_Details::UL_data_transmission_off, UL_UDP_addr_len, UL_udp_command, UL_UDP_sockfd, Upper_Layer_Details::UL_upper_pdc_cfgsent, and ULfirst.
Referenced by setup().
00206 { 00207 00208 /* UDP data Received */ 00209 while(1) { 00210 00211 memset(UL_udp_command,'\0',19); 00212 memset(display_buf,'\0',200); 00213 00214 if ((numbytes = recvfrom(UL_UDP_sockfd,UL_udp_command, 18, 0,(struct sockaddr *)&UL_UDP_addr, (socklen_t *)&UL_UDP_addr_len)) == -1) { 00215 // Main if 00216 perror("recvfrom"); 00217 exit(1); 00218 00219 } else { /* New datagram has been received */ 00220 00221 int pdc_flag = 0; 00222 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00223 struct Upper_Layer_Details *temp_pdc = ULfirst; 00224 if(ULfirst == NULL) { 00225 00226 pdc_flag = 0; 00227 00228 } else { 00229 00230 while(temp_pdc != NULL ) { 00231 00232 if((!strcmp(temp_pdc->ip,inet_ntoa(UL_UDP_addr.sin_addr))) && 00233 (!strncasecmp(temp_pdc->protocol,"UDP",3)) && (temp_pdc->port == UDPPORT)) { 00234 00235 pdc_flag = 1; 00236 break; 00237 } else { 00238 00239 temp_pdc = temp_pdc->next; 00240 } 00241 } 00242 } 00243 00244 if(pdc_flag){ 00245 00246 unsigned char c = UL_udp_command[1]; 00247 c <<= 1; 00248 c >>= 5; 00249 temp_pdc->sockfd = UL_UDP_sockfd; 00250 00251 if(c == 0x04) { /* Check if it is a command frame from Upper PDC */ 00252 00253 fprintf(fp_log,"\nCommand frame Received\n"); 00254 c = UL_udp_command[15]; 00255 00256 if((c & 0x05) == 0x05){ //Send CFg frame to PDC 00257 printf("\nCommand frame for CFG Received\n"); 00258 00259 while(root_pmuid != NULL); // Wait till all the status change has been cleared 00260 00261 printf("sockfd = %d,ipaddress = %s\n",temp_pdc->sockfd,inet_ntoa(temp_pdc->pdc_addr.sin_addr)); 00262 // validation_result ("Configuration request received");/* Show the success message to user */ 00263 if(temp_pdc->address_set == 0) { 00264 00265 memcpy(&temp_pdc->pdc_addr,&UL_UDP_addr,sizeof(UL_UDP_addr)); 00266 00267 } 00268 numbytes = create_cfgframe(); 00269 00270 if ((numbytes = sendto (temp_pdc->sockfd,cfgframe, numbytes, 0, 00271 (struct sockaddr *)&temp_pdc->pdc_addr,sizeof(temp_pdc->pdc_addr)) == -1)) { 00272 00273 perror("sendto"); 00274 00275 } else { 00276 00277 fprintf(fp_log,"Sent CFG FRAME\n"); 00278 } 00279 free(cfgframe); 00280 00281 temp_pdc->UL_upper_pdc_cfgsent = 1; 00282 temp_pdc->config_change = 0; 00283 00284 } else if((c & 0x02) == 0x02) { // if data frame 00285 00286 if(temp_pdc->UL_upper_pdc_cfgsent == 1) { // Only if cfg is sent send the data 00287 00288 temp_pdc->UL_data_transmission_off = 0; 00289 00290 } else { 00291 00292 fprintf(fp_log,"Data cannot be sent as CMD for CFG not received\n"); 00293 00294 } 00295 00296 } else if ((c & 0x01) == 0x01){ 00297 00298 temp_pdc->UL_data_transmission_off = 1; 00299 00300 } 00301 00302 } else { /* If it is a frame other than command frame */ 00303 00304 fprintf(fp_log,"Not a command frame\n"); 00305 } 00306 00307 00308 } else { /* If the command frame is not from authentic PDC*/ 00309 00310 fprintf(fp_log,"Command frame from un-authentic PDC\n"); 00311 } 00312 00313 } // Main if ends 00314 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00315 } // while ends 00316 }
char display_buf[200] |
Definition at line 71 of file connections.c.
Referenced by UL_udp().
int yes = 1 |
Definition at line 70 of file connections.c.
Referenced by connect_pmu_tcp(), connect_pmu_udp(), recreate_cfg_objects(), and setup().