📄 config.c
字号:
} else { num = ntohs(service->s_port); }#endif } if (num==0) { if (port) port[0] = '\0'; } else { sprintf(port, "%d", num); }} /* lookup_host() */static int hexdigit(char c){ if (c>='a' && c<='f') { return c - 'a' + 10; } else if (c>='A' && c<='F') { return c - 'A' + 10; } else if (c>='0' && c<='9') { return c - '0'; } else { return 0; /* bad hex digit */ }}static int hex2num(char *hex){ return hexdigit(hex[0])*16 + hexdigit(hex[1]); }/* ========================= search_interface_file() ========================= * * Def: Open and read the file 'file' searching for a logical server * by the name of 'host'. If one is found then lookup * the IP address and port number and store them in 'ip_addr', and * 'ip_port'. * * Ret: void * * =========================================================================== */static void search_interface_file( const char *dir, /* (I) Name of base directory for interface file */ const char *file, /* (I) Name of the interface file */ const char *host, /* (I) Logical host to search for */#ifdef NCBI_FTDS int entry_num, /* (I) entry number for host in interface file */#endif char *ip_addr, /* (O) dotted-decimal IP address */ char *ip_port, /* (O) Port number for database server */ char *tds_ver) /* (O) Protocol version to use when connecting */{char *pathname;char line[255];char tmp_ip[sizeof(line)];char tmp_port[sizeof(line)];char tmp_ver[sizeof(line)];FILE *in;char *field;int found=0;char *lasts; ip_addr[0] = '\0'; ip_port[0] = '\0'; line[0] = '\0'; tmp_ip[0] = '\0'; tmp_port[0] = '\0'; tmp_ver[0] = '\0'; tdsdump_log(TDS_DBG_INFO1, "%L Searching interfaces file %s/%s.\n",dir,file); /* FIXME check result or use open fchdir fopen ... */ pathname = (char *) malloc(strlen(dir) + strlen(file) + 10); /* * create the full pathname to the interface file */ /* FIXME file and dir can't be NULL, used before in strlen */ if (file==NULL || file[0]=='\0') { pathname[0] = '\0'; } else { if (dir==NULL || dir[0]=='\0') { pathname[0] = '\0'; } else { strcpy(pathname, dir); strcat(pathname, "/"); } strcat(pathname, file); } /* * parse the interfaces file and find the server and port */ if ((in = fopen(pathname,"r"))==NULL) { tdsdump_log(TDS_DBG_INFO1, "%L Couldn't open %s.\n", pathname); free(pathname); return; } tdsdump_log(TDS_DBG_INFO1, "%L Interfaces file %s opened.\n", pathname); while (fgets(line,sizeof(line)-1,in)) { if (line[0]=='#') continue; /* comment */ if (!isspace(line[0])) { field = tds_strtok_r(line,"\n\t ", &lasts); if (!strcmp(field,host)) { found=1; tdsdump_log(TDS_DBG_INFO1, "%L Found matching entry for host %s.\n,host"); } else found=0; } else if (found && isspace(line[0])) { field = tds_strtok_r(line,"\n\t ", &lasts); if (field!=NULL && !strcmp(field,"query")) {#ifdef NCBI_FTDS if(--entry_num > 0) continue;#endif field = tds_strtok_r(NULL,"\n\t ", &lasts); /* tcp or tli */ if (!strcmp(field,"tli")) { tdsdump_log(TDS_DBG_INFO1, "%L TLI service.\n"); field = tds_strtok_r(NULL,"\n\t ", &lasts); /* tcp */ field = tds_strtok_r(NULL,"\n\t ", &lasts); /* device */ field = tds_strtok_r(NULL,"\n\t ", &lasts); /* host/port */ if (strlen(field)>=18) { sprintf(tmp_port,"%d", hex2num(&field[6])*256 + hex2num(&field[8])); sprintf(tmp_ip,"%d.%d.%d.%d", hex2num(&field[10]), hex2num(&field[12]), hex2num(&field[14]), hex2num(&field[16])); tdsdump_log(TDS_DBG_INFO1, "%L tmp_port = %d.mtp_ip = %s.\n", tmp_port, tmp_ip); } } else { field = tds_strtok_r(NULL,"\n\t ", &lasts); /* ether */ strcpy(tmp_ver,field); field = tds_strtok_r(NULL,"\n\t ", &lasts); /* host */ strcpy(tmp_ip,field); tdsdump_log(TDS_DBG_INFO1, "%L host field %s.\n",tmp_ip); field = tds_strtok_r(NULL,"\n\t ", &lasts); /* port */ strcpy(tmp_port,field); } /* else */#ifdef NCBI_FTDS break;#endif } /* if */ } /* else if */ } /* while */fclose(in);free(pathname); /* * Look up the host and service */#ifdef NCBI_FTDS if(*tmp_ip != '\0') #endif lookup_host(tmp_ip, tmp_port, ip_addr, ip_port); tdsdump_log(TDS_DBG_INFO1, "%L Resolved IP as '%s'.\n",ip_addr); strcpy(tds_ver,tmp_ver);} /* search_interface_file() *//* ============================ get_server_info() ============================ * * Def: Try to find the IP number and port for a (possibly) logical server * name. * * Note: It is the callers responsibility to supply large enough buffers * to hold the ip and port numbers. ip_addr should be at least 17 * bytes long and ip_port should be at least 6 bytes long. * * Note: This function uses only the interfaces file and is deprecated. * * Ret: True if it found the server, false otherwise. * * =========================================================================== */#ifdef NCBI_FTDSstatic#endifint get_server_info( char *server, /* (I) logical or physical server name */#ifdef NCBI_FTDS int entry_num, /* (I) entry number in interface file */#endif char *ip_addr, /* (O) string representation of IP address */ char *ip_port, /* (O) string representation of port number */ char *tds_ver) /* (O) string value specifying which protocol version */{ ip_addr[0] = '\0'; ip_port[0] = '\0'; tds_ver[0] = '\0'; tdsdump_log(TDS_DBG_INFO1, "%L Looking for server....\n"); if (!server || strlen(server) == 0) { server = getenv("TDSQUERY"); if(!server || strlen(server) == 0) { server = "SYBASE"; } tdsdump_log(TDS_DBG_INFO1, "%L Setting server to %s from $TDSQUERY.\n",server); } /* * Look for the server in the interf_file iff interf_file has been set. */ if (ip_addr[0]=='\0' && interf_file[0]!='\0') { tdsdump_log(TDS_DBG_INFO1, "%L Looking for server in interf_file %s.\n",interf_file);#ifdef NCBI_FTDS search_interface_file("", interf_file, server, entry_num, ip_addr,#else search_interface_file("", interf_file, server, ip_addr,#endif ip_port, tds_ver); } /* * if we haven't found the server yet then look for a $HOME/.interfaces file */ if (ip_addr[0]=='\0') { /* FIXME use getpwent, see above */ char *home = getenv("HOME"); if (home!=NULL && home[0]!='\0') { tdsdump_log(TDS_DBG_INFO1, "%L Looking for server in %s/.interfaces.\n", home);#ifdef NCBI_FTDS search_interface_file(home, ".interfaces", server, entry_num, ip_addr,#else search_interface_file(home, ".interfaces", server, ip_addr,#endif ip_port, tds_ver); } } /* * if we haven't found the server yet then look in $SYBBASE/interfaces file */ if (ip_addr[0]=='\0') { char *sybase = getenv("SYBASE"); if (sybase!=NULL && sybase[0]!='\0') { tdsdump_log(TDS_DBG_INFO1, "%L Looking for server in %s/interfaces.\n", sybase);#ifdef NCBI_FTDS search_interface_file(sybase, "interfaces", server, entry_num, ip_addr,#else search_interface_file(sybase, "interfaces", server, ip_addr,#endif ip_port, tds_ver); } else { tdsdump_log(TDS_DBG_INFO1, "%L Looking for server in /etc/freetds/interfaces.\n"); search_interface_file("/etc/freetds", "interfaces", server,#ifdef NCBI_FTDS entry_num,#endif ip_addr, ip_port, tds_ver); } } /* * If we still don't have the server and port then assume the user * typed an actual server name. */#ifdef NCBI_FTDS if (ip_addr[0]=='\0' && entry_num < 2) {#else if (ip_addr[0]=='\0') {#endif char *tmp_port; /* * Make a guess about the port number */#ifdef TDS50 tmp_port = "4000";#else tmp_port = "1433";#endif /* FIX ME -- Need a symbolic constant for the environment variable */ if (getenv("TDSPORT")!=NULL) { tmp_port = getenv("TDSPORT"); tdsdump_log(TDS_DBG_INFO1, "%L Setting 'tmp_port' to %s from $TDSPORT.\n",tmp_port); } else tdsdump_log(TDS_DBG_INFO1, "%L Setting 'tmp_port' to %s as a guess.\n",tmp_port); /* * lookup the host and service */ lookup_host(server, tmp_port, ip_addr, ip_port); } return ip_addr[0]!='\0' && ip_port[0]!='\0';} /* get_server_info() *//** * Check the server name to find port info first * return 1 when found, else 0 * Warning: config-> & login-> are all modified when needed */static int parse_server_name_for_port( TDSCONFIGINFO *config, TDSLOGIN *login ){ char *pSep, *pEnd; if( ! login->server_name ) return 0;/* FALSE */ /* seek the ':' in login server_name */ pEnd = login->server_name + strlen( login->server_name ); for( pSep = login->server_name; pSep < pEnd; pSep ++ ) if( *pSep == ':' ) break; if(( pSep < pEnd )&&( pSep != login->server_name ))/* yes, i found it! */ { if( config->server_name ) free( config->server_name ); config->server_name = strdup( login->server_name ); /* modify config-> && login->server_name & ->port */#ifdef NCBI_FTDS login->port = config->port[0] = atoi( pSep + 1 );#else login->port = config->port = atoi( pSep + 1 );#endif config->server_name[pSep - login->server_name] = 0;/* end the server_name before the ':' */ *pSep = 0; /* config->ip_addr needed */ { char tmp[256]; lookup_host (config->server_name, NULL, tmp, NULL);#ifdef NCBI_FTDS if (config->ip_addr[0]) free (config->ip_addr[0]); config->ip_addr[0] = strdup (tmp);#else if (config->ip_addr) free (config->ip_addr); config->ip_addr = strdup (tmp);#endif } return 1;/* TRUE */ } else return 0;/* FALSE */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -