00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <unistd.h>
00033 #include <errno.h>
00034 #include <string.h>
00035 #include <sys/types.h>
00036 #include <sys/socket.h>
00037 #include <netinet/in.h>
00038 #include <arpa/inet.h>
00039 #include <sys/wait.h>
00040 #include <signal.h>
00041 #include <pthread.h>
00042 #include <gtk/gtk.h>
00043 #include "ipdcGui.h"
00044 #include "connections.h"
00045 #include "parser.h"
00046 #include "global.h"
00047 #include "new_pmu_or_pdc.h"
00048 #include "align_sort.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 int yes = 1;
00071 char display_buf[200];
00072
00073
00074
00075
00076
00077 void sigchld_handler(int s) {
00078 while(wait(NULL) > 0);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 void setup(){
00088
00089
00090
00091
00092 int err;
00093
00094
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;
00113 UDP_my_addr.sin_port = htons(UDPPORT);
00114 UDP_my_addr.sin_addr.s_addr = INADDR_ANY;
00115 memset(&(UDP_my_addr.sin_zero),'\0', 8);
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
00128
00129
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;
00145 TCP_my_addr.sin_port = htons(TCPPORT);
00146 TCP_my_addr.sin_addr.s_addr = INADDR_ANY;
00147 memset(&(TCP_my_addr.sin_zero), '\0', 8);
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;
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
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
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 }
00200
00201
00202
00203
00204
00205
00206 void* UL_udp(){
00207
00208
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
00216 perror("recvfrom");
00217 exit(1);
00218
00219 } else {
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) {
00252
00253 fprintf(fp_log,"\nCommand frame Received\n");
00254 c = UL_udp_command[15];
00255
00256 if((c & 0x05) == 0x05){
00257 printf("\nCommand frame for CFG Received\n");
00258
00259 while(root_pmuid != NULL);
00260
00261 printf("sockfd = %d,ipaddress = %s\n",temp_pdc->sockfd,inet_ntoa(temp_pdc->pdc_addr.sin_addr));
00262
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) {
00285
00286 if(temp_pdc->UL_upper_pdc_cfgsent == 1) {
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 {
00303
00304 fprintf(fp_log,"Not a command frame\n");
00305 }
00306
00307
00308 } else {
00309
00310 fprintf(fp_log,"Command frame from un-authentic PDC\n");
00311 }
00312
00313 }
00314 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00315 }
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 void* UL_tcp() {
00325
00326 int err;
00327
00328 pthread_attr_t attr;
00329 pthread_attr_init(&attr);
00330 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
00331
00332 perror(strerror(err));
00333 exit(1);
00334
00335 }
00336
00337 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) {
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)) {
00350 perror("accept");
00351
00352 } else {
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
00379 pthread_t t;
00380 fprintf(fp_log,"server: got connection from %s\n",
00381 inet_ntoa(temp_pdc->pdc_addr.sin_addr));
00382
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 {
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 }
00397
00398 }
00399
00400 pthread_attr_destroy(&attr);
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 void* UL_tcp_connection(void * temp_pdc) {
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) {
00439
00440 fprintf(fp_log,"Command frame Received\n");
00441 c = UL_tcp_command[15];
00442
00443 if((c & 0x05) == 0x05){
00444
00445 while(root_pmuid != NULL);
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) {
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){
00468
00469 udetails->UL_data_transmission_off = 1;
00470 }
00471 } else {
00472
00473 fprintf(fp_log,"Not a command frame\n");
00474 }
00475
00476 pthread_mutex_unlock(&mutex_Upper_Layer_Details);
00477 }
00478
00479 }
00480 close(UL_new_fd);
00481 pthread_exit(NULL);
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491 void PMU_process_UDP(unsigned char *udp_buffer,struct sockaddr_in PMU_addr,int sockfd){
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){
00505
00506 stat_status = dataparser(udp_buffer);
00507
00508
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) {
00521
00522 fprintf(fp_log,"Data Invalid\n");
00523
00524 }
00525
00526 } else if(c == 0x03) {
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
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 }
00548
00549
00550
00551
00552
00553
00554
00555 void PMU_process_TCP(unsigned char tcp_buffer[],int sockfd) {
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){
00570
00571 fprintf(fp_log,"\nData frame\n");
00572 stat_status = dataparser(tcp_buffer);
00573
00574
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) {
00586
00587 fprintf(fp_log,"Data Invalid\n");
00588 }
00589
00590 } else if(c == 0x03) {
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
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 }