/* * This routine splits the individual objects into their seperate fields * and formats them to be inserted into the proper structure */ #include "objects.h" /* Name: parse_obj() * * Description: This routine parses the objects fed to it from the find_obj() * and breaks it into tokens to be inserted into linked list * * By this point we already know which service it is -- this routine simply * breaks down each object into its individual fields. * * */ void parse_obj(FILE *fp, int objcount, char *objname) { /* set stack counter */ int objstack=1; /* set interface stack counter */ int ifstack=0; /* number of interfaces per host */ int ifcount=0; char iftmp[LNSIZE]; /* loop counters */ int i=0, j=0; /* contains contents of a line */ char *lineptr; char *buf; int grpnum=0; /* allocate space for temporary storage */ if((buf = (char *)malloc(LNSIZE)) == NULL) { fprintf(stderr,"parse_obj: malloc failed\n"); exit(1); } /* set line counter to 0 */ j=0; /* Find the correct position in the linked list to insert our new entry */ oprev = ohead; opresent = ohead->nodelink; onext = (objlist *)malloc(sizeof(objlist)); while(1) { if(objcount < opresent->objcount) { onext->nodelink = opresent; oprev->nodelink = onext; break; } else { oprev = opresent; opresent = opresent->nodelink; } } /* We are now at the next available entry in the linked list */ /* Set the object name correctly */ sprintf(onext->objname,"%s",objname); onext->objcount = objcount; /* Beginning of netobjs section */ while((objstack > 0) && (fgets(buf,LNSIZE,fp) != NULL)) { /* Special case for specific interfaces -- parse that first */ sprintf(iftmp,"if-%d",ifcount); if(strstr(buf,iftmp)) { ifcount++; ifstack=1; /* continue while still reading interface data */ while(ifstack > 0) { /* get another line, and update inteface stack counter. We * quit when it equals zero */ if(fgets(buf,LNSIZE,fp) != NULL) { for(i=0;iiface[ifcount-1].ipaddr[j] = buf[i]; j++; } } continue; } if(strstr(buf,":netmask (")) { lineptr = strchr(buf,'('); lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->iface[ifcount-1].netmask,"%s",lineptr); continue; } /* find the interface name */ if(strstr(buf,":iffullname (")) { j=0; if((lineptr = strchr(buf,'(')) != NULL) { for(i=0;iiface[ifcount-1].ifname[j] = lineptr[i]; j++; } } } onext->iface[ifcount-1].ifname[j-1] = '\0'; if(strstr(onext->iface[ifcount-1].ifname,"PCI Fast Ethernet")) sprintf(onext->iface[ifcount-1].ifname,"%s","PC Ethernet"); } } continue; } /* determine if we are working with a group of objects, then * store the objects in the proper structure */ if((strstr(buf,": ")) && (strcmp(onext->type,"group") == 0)) { /* Allocate a line to contain the next group */ if(((onext->group)[grpnum] = (char *)malloc(LNSIZE)) == NULL) { fprintf(stderr,"parse_obj: malloc failed\n"); exit(1); } if((lineptr = strchr(buf,' ')) != NULL) { lineptr++; lineptr[strlen(lineptr)-1] = '\0'; sprintf(onext->group[grpnum],"%s",lineptr); grpnum++; } continue; } /* * These next sections deal with the individual elements in the main * object section */ if(strstr(buf,":type (")) { if((lineptr = strchr(buf,'(')) != NULL) { /* advance the pointer past the first '(' and chop off the * ')' and '\n' at the end as well. */ lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->type,"%s",lineptr); /* found another group -- reset counter */ if(strcmp(onext->type,"group") == 0) grpnum=0; } continue; } if(strstr(buf,":ipaddr (")) { if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->ipaddr,"%s",lineptr); } continue; } if(strstr(buf,":netmask (")) { if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->netmask,"%s",lineptr); } continue; } if(strstr(buf,":gateways (")) { if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->gateways,"%s",lineptr); } continue; } if(strstr(buf,":broadcast (")) { if((lineptr = strchr(buf,'(')) != NULL) { lineptr++; lineptr[strlen(lineptr)-2] = '\0'; sprintf(onext->broadcast,"%s",lineptr); } continue; } for(i=0;inodelink = opresent; oprev->nodelink = onext; free(buf); }