int resolve_function(types_t **head) { types_t *next_type; if (!head) { g_warning("Empty list detected"); return RESOLV_LIST_EMPTY; } for (next_type = *head; next_type; next_type = next_type->next) { /* Only resolve typedef */ if (next_type->type != TYPE_FUNCTION) continue; g_debug("Trying to resolve function id %d with type %d", next_type->id, next_type->type_xml); if (search_id(*head, &next_type->child, next_type->type_xml) != RESOLV_OK) { /* We have to remove this reference */ } // next_type->type_hr_display(next_type, 0); } return 0; }
int resolve_typedefs(types_t **head) { types_t *next_type; if (!head) { g_warning("Empty list detected"); return RESOLV_LIST_EMPTY; } for (next_type = *head; next_type; next_type = next_type->next) { /* Only resolve typedef */ if (next_type->type != TYPE_TYPEDEF) continue; // printf("Trying to resolve typedef %s with type %d\n", next_type->name, next_type->id); if (search_id(*head, &next_type->child, next_type->type_xml) != RESOLV_OK) { /* We have to remove this reference */ }/* else { next_type->type_hr_display(next_type, 0); }*/ } return 0; }
void search() { int sh; clrscr(); design(); gotoxy(17,6); printf("\xDB\xDB\xB2 Two options available to search \xB2\xDB\xDB"); gotoxy(15,9); printf("[ 1 ] - Search by ID number ."); gotoxy(15,11); printf("[ 2 ] - Search by Description ."); gotoxy(17,15); printf("Enter your choice:: "); sh=toupper(getch()); switch(sh) { case '1': search_id(); break; case '2': search_name(); break; default: gotoxy(18,18); printf("\a\xDB\xDB wrong entry try again"); getch(); } getche(); }
int resolve_struct(types_t **head) { types_t *next_type; if (!head) { g_warning("Empty list detected"); return RESOLV_LIST_EMPTY; } for (next_type = *head; next_type; next_type = next_type->next) { char *member; char *members; /* Only resolve typedef */ if (next_type->type != TYPE_STRUCT) continue; g_debug("Trying to resolve struct members %s with type %d", next_type->name, next_type->id); /* Struct may have no member */ if (next_type->members == NULL) continue; /* We have to copy the string as strtok will split the string in argument */ members = strdup(next_type->members); /* Split the string on spaces and _ */ member = strtok(members, " _"); while(member != NULL) { if (next_type->nb_members == 0) { next_type->members_child = malloc(sizeof(types_t *)); } else { next_type->members_child = realloc(next_type->members_child, (next_type->nb_members + 1) * sizeof(types_t *)); } if (search_id(*head, &next_type->members_child[next_type->nb_members], atoi(member)) != RESOLV_OK) { /* We have to remove this reference */ } if ((next_type->members_child[next_type->nb_members] != NULL) && (next_type->members_child[next_type->nb_members]->type != TYPE_FIELD)) { /* Only keep field child for structure, other member can be present * for defining types (union or struct) used by fields but should not be considered. */ next_type->members_child[next_type->nb_members] = NULL; /* We have to remove this reference */ } next_type->nb_members++; /* Pick up the next string available */ member = strtok(NULL, " _"); } free(members); } return 0; }
void search_func(MENU_TYPE funcNum, pUSR_INFOR phead_infor) { if(funcNum == 0) search_all(phead_infor); if(funcNum == 1) search_name(phead_infor); if(funcNum == 2) search_id(phead_infor); if(funcNum == 3) return; }
int resolve_union(types_t **head) { types_t *next_type; if (!head) { g_warning("Empty list detected"); return RESOLV_LIST_EMPTY; } for (next_type = *head; next_type; next_type = next_type->next) { char *member; char *members; /* Only resolve typedef */ if (next_type->type != TYPE_UNION) continue; g_debug("Trying to resolve union members %s with type %d\n", next_type->name, next_type->id); /* Struct may have no member */ if (next_type->members == NULL) continue; /* We have to copy the string as strtok will modify the string in argument */ members = strdup(next_type->members); /* Split the string on spaces and _ */ member = strtok(members, " _"); while(member != NULL) { if (next_type->nb_members == 0) { next_type->members_child = malloc(sizeof(types_t *)); } else { next_type->members_child = realloc(next_type->members_child, (next_type->nb_members + 1) * sizeof(types_t *)); } if (search_id(*head, &next_type->members_child[next_type->nb_members], atoi(member)) != RESOLV_OK) { /* We have to remove this reference */ } next_type->nb_members++; /* Pick up the next string available */ member = strtok(NULL, " _"); } free(members); } return 0; }
void *ping_thread(void *ptr){ /*Binding*/ struct sockaddr_in server,dst; char message[BUFLEN]; char buf[BUFLEN]; int sfd,i,slen=sizeof(dst),index; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(My_p_port); inet_aton(MyIP,&server.sin_addr); if( bind(sfd,(struct sockaddr *)&server,sizeof(server)) == -1) { die("bind"); } while(1){ /*Reveive every thing sent to My_p_port, and reply the same thing.*/ memset(buf,'\0', BUFLEN); if (recvfrom(sfd, buf, BUFLEN, 0, (struct sockaddr *) &dst, (socklen_t *)&slen) == -1) { perror("recvfrom"); exit(1); } if(buf[0]==PING){ //printf("Recevied ping from %s:%d\n",inet_ntoa(dst.sin_addr),ntohs(dst.sin_port)); //printf("%d\n",buf[1]); index = search_id((int)buf[1]); if(index!=-1){ /*The sender is on my list*/ /*reply*/ //printf("reply\n"); if (sendto(sfd, buf, strlen(buf) , 0 , (struct sockaddr *) &dst, slen)==-1) { die("sendto()"); } if(MyClntList[index].no_response>0){ MyClntList[index].no_response--; } }else{ printf("Not my peer. ignore.\n"); } } } }
int add_clnt(char *IP, char *port_d, int id){ if(search_id(id)==-1){ pthread_mutex_lock(&lock); MyClntList[clnt_num].id=id; MyClntList[clnt_num].no_response=0; strcpy(MyClntList[clnt_num].IPaddr,IP); strcpy(MyClntList[clnt_num].port_d,port_d); MyClntList[clnt_num].no_response=0; clnt_num++; pthread_mutex_unlock(&lock); print_clnt(); return 0;/*Add one*/ }else{ return 1;/*Add nothing*/ } }
void *listen_thread(void *ptr){ /*Binding*/ int sfd,slen,recv_len,index,i; char *pch; char buf[BUFLEN]; char cmd[BUFLEN]; struct sockaddr_in server,dst; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(My_port); inet_aton(MyIP,&server.sin_addr); if( bind(sfd,(struct sockaddr *)&server,sizeof(server)) == -1) { die("bind"); } slen=sizeof(server); //printf("in Ping_thread\n"); while(1) { fflush(stdout); memset(buf,'\0', BUFLEN); if ((recv_len = recvfrom(sfd, buf, BUFLEN, 0, (struct sockaddr *) &dst, (socklen_t *)&slen)) == -1) { die("recvfrom()"); } printf("Received packet from %s:%d\n", inet_ntoa(dst.sin_addr), ntohs(dst.sin_port)); printf("Data: %s\n" , buf); char *p; p=buf; p++; strcpy(cmd,p); /*Deal with received cmd*/ if(buf[0]==REGISTER){ /*Add new clnt to MyClntList*/ printf("REGISTER\n"); char IP[INET_ADDRSTRLEN],port_d[PORTLEN]; char s[PIDLEN]; int id; //printf("%s\n",cmd); pch = strtok (cmd,";"); id = atoi(pch); pch = strtok (NULL,";"); //printf("%d\n",id); if(pch!=NULL){ strcpy(IP,pch); pch = strtok (NULL,";"); if(pch!=NULL){ strcpy(port_d,pch); index=search_id(id); if(index!=-1){ /*This peer has crashed*/ /*deal with crash*/ /*1. remove old information*/ printf("Peer crashed\n"); clnt_rm(index); /*2. Add new info*/ add_clnt(IP, port_d, id); }else{ printf("Add new Peer\n"); add_clnt(IP, port_d, id); } /*then reply with pid*/ sprintf(s,"%d",My_pid); if (sendto(sfd, s, strlen(s) , 0 , (struct sockaddr *) &dst, slen)==-1) { die("sendto()"); } } } }else if(buf[0]==UPDATELIST){ /*update*/ printf("UPDATELIST\n"); int count, id, checksum,index; char filename[FILENAME]; char *pch; pch = strtok (cmd,";"); id=atoi(pch); /*check id*/ index=search_id(id); if(index!=-1){ pch = strtok (NULL,";"); if(pch!=NULL){ count = atoi(pch); for(i=0;i<count;i++){ pch = strtok (NULL,";"); if(pch!=NULL){ strcpy(filename,pch); pch = strtok (NULL,";"); if(pch!=NULL){ checksum=atoi(pch); /*Add or update file*/ add_file(filename, checksum, id); } } } print_file_list(); } }else{ printf("received UPDATELIST request from peer %d, which is not on my list\n",id); } }else if(buf[0]==FIND){ /*find file*/ printf("FIND\n"); int id, index_c; char filename[FILENAME],msg[BUFLEN],loc[LOCLEN+3]; char *pch; pch = strtok (cmd,";"); id=atoi(pch); /*chech id*/ index=search_id(id); if(index!=-1){ pch = strtok (NULL,";"); if(pch!=NULL){ strcpy(filename,pch); /*Build a message to reply*/ index = search_file(filename); if(index!=-1){ memset(msg,'\0', BUFLEN); sprintf(msg,"%d;%d;",MyFileList[index].checksum,MyFileList[index].copy_num); for(i=0;i<MyFileList[index].copy_num;i++){ index_c =search_id(MyFileList[index].loc_list[i]); sprintf(loc,"%d;%s;%s;",MyClntList[index_c].id,MyClntList[index_c].IPaddr,MyClntList[index_c].port_d); strcat(msg,loc); } printf("Sending msg : %s\n",msg); if (sendto(sfd, msg, strlen(msg) , 0 , (struct sockaddr *) &dst, slen)==-1) { die("sendto()"); } }else{ printf("Cannot find the file\n"); memset(msg,'\0', BUFLEN); msg[0]=FIND; printf("%s(%d)\n",msg,strlen(msg)); if (sendto(sfd, msg, strlen(msg) , 0 , (struct sockaddr *) &dst, slen)==-1) { die("sendto()"); } } } }else{ printf("received FIND request from peer %d, which is not on my list\n",id); } }else if(buf[0]==REPORTPEERDOWN){ printf("REPORTPEERDOWN\n"); int id; char *pch; pch = strtok (cmd,";"); id=atoi(pch); /*check id*/ index=search_id(id); if(index!=-1){ printf("received crash report of peer %d, which is not on my list\n",id); }else{ /*kick its ass*/ clnt_rm(index); printf("Peer %d removed\n",MyClntList[i].id); } } } }