extern int realmlist_reload(char const * filename) { t_elem * new_curr; t_elem * old_curr; t_realm * new_realm; t_realm * old_realm; int match; t_list * newlist = NULL; t_list * oldlist = realmlist_head; realmlist_head = NULL; if (!(newlist = realmlist_load(filename))) return -1; LIST_TRAVERSE(oldlist,old_curr) { if (!(old_realm = elem_get_data(old_curr))) { eventlog(eventlog_level_error,__FUNCTION__,"found NULL elem in list"); continue; } match = 0; LIST_TRAVERSE(newlist,new_curr) { if (!(new_realm = elem_get_data(new_curr))) { eventlog(eventlog_level_error,__FUNCTION__,"found NULL elem in list"); continue; } if (!strcmp(old_realm->name,new_realm->name)) { match = 1; rcm_chref(&old_realm->rcm,new_realm); break; } } if (!match) rcm_chref(&old_realm->rcm,NULL); realm_destroy(old_realm); list_remove_elem(oldlist,&old_curr); } list_destroy(oldlist); realmlist_head = newlist; realm_number = 0; return 0; }
extern int realmlist_unload(t_list * list_head) { t_elem * curr; t_realm * realm; if (list_head) { LIST_TRAVERSE(list_head,curr) { if (!(realm = elem_get_data(curr))) eventlog(eventlog_level_error,__FUNCTION__,"found NULL realm in list"); else realm_destroy(realm); list_remove_elem(list_head,&curr); } list_destroy(list_head); } return 0; }
extern int realmlist_destroy(void) { t_elem * curr; t_realm * realm; if (realmlist_head) { LIST_TRAVERSE(realmlist_head,curr) { if (!(realm = elem_get_data(curr))) eventlog(eventlog_level_error,"realmlist_destroy","found NULL realm in list"); else realm_destroy(realm); list_remove_elem(realmlist_head,curr); } list_destroy(realmlist_head); realmlist_head = NULL; } return 0; }
extern int realmlist_create(char const * filename) { FILE * fp; unsigned int line; unsigned int pos; unsigned int len; t_addr * raddr; char * temp; char * buff; char * name; char * desc; char * addr; t_realm * realm; if (!filename) { eventlog(eventlog_level_error,"realmlist_create","got NULL filename"); return -1; } if (!(realmlist_head = list_create())) { eventlog(eventlog_level_error,"realmlist_create","could not create list"); return -1; } if (!(fp = fopen(filename,"r"))) { eventlog(eventlog_level_error,"realmlist_create","could not open realm file \"%s\" for reading (fopen: %s)",filename,strerror(errno)); list_destroy(realmlist_head); realmlist_head = NULL; return -1; } for (line=1; (buff = file_get_line(fp)); line++) { for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++); if (buff[pos]=='\0' || buff[pos]=='#') { free(buff); continue; } if ((temp = strrchr(buff,'#'))) { unsigned int endpos; *temp = '\0'; len = strlen(buff)+1; for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--); buff[endpos+1] = '\0'; } len = strlen(buff)+1; if (!(name = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for name"); free(buff); continue; } if (!(desc = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for desc"); free(name); free(buff); continue; } if (!(addr = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for desc"); free(desc); free(name); free(buff); continue; } if (sscanf(buff," \"%[^\"]\" \"%[^\"]\" %s",name,desc,addr)!=3) { if (sscanf(buff," \"%[^\"]\" \"\" %s",name,addr)==2) desc[0] = '\0'; else { eventlog(eventlog_level_error,"realmlist_create","malformed line %u in file \"%s\"",line,filename); free(addr); free(desc); free(name); free(buff); continue; } } free(buff); if (!(raddr = addr_create_str(addr,0,BNETD_REALM_PORT))) /* 0 means "this computer" */ { eventlog(eventlog_level_error,"realmlist_create","invalid address value for field 3 on line %u in file \"%s\"",line,filename); free(addr); free(desc); free(name); continue; } free(addr); if (!(realm = realm_create(name,desc,addr_get_ip(raddr),addr_get_port(raddr)))) { eventlog(eventlog_level_error,"realmlist_create","could not create realm"); addr_destroy(raddr); free(desc); free(name); continue; } addr_destroy(raddr); free(desc); free(name); if (list_prepend_data(realmlist_head,realm)<0) { eventlog(eventlog_level_error,"realmlist_create","could not prepend realm"); realm_destroy(realm); continue; } } if (fclose(fp)<0) eventlog(eventlog_level_error,"realmlist_create","could not close realm file \"%s\" after reading (fclose: %s)",filename,strerror(errno)); return 0; }