Re: Ms sql backslash in named instance 2005

Frediano Ziglio <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
2009/10/23  <[email protected]>:
> On Fri, Oct 23, 2009 at 06:56:00PM +0100, [email protected] wrote:
>> thanks freedy and cedric for your quick response. Although that works, I'm in a difficult situation where I need it to work as before. I won't go into all the details but I have an existing app that not only uses freetds as its intended but also parses it and uses it within an application. I can't alter the app and need the functionality we have been using up to now.
>> Do you know if the problem I have is a bug or a feature?
>
> Maybe a little of both.  I would say it's undesirable.
>
> What's happening: tds_read_config_info() calls parse_server_name_for_port()
> whose real purpose is to interpret instance\hostname and hostname:port strings.
> It's not supposed to interpret the [servername] string in freetds.conf,
> but that's what it's doing.
>

I readed again original mail and I understood that problem is that in
freetds.conf he has something like

[myserver\one]
...

[myserver\two]
...

or something like that. 0.62 understand only "server:port" syntax so
it finds "myserver\one" correctly. 0.82 change "myserver\one" to
"myserver" with instance "one" so it doesn't find "myserver" section
in file. The solution would be to search for server before parsing for
instance. This way does not break compatibility.

> If you set TDSDUMPCONFIG, you'll see near the top of the log it's looking for
> your 'server\name' name, and then another message, "Parsed servername, now back on 0" that means it parsed for a port number and came up with zero.
>
> I think the idea, once upon a time, was that
>
>        tsql -S 'instance\servername'
> or
>        tsql -S 'servername:port'
>
> would be a way to override the instance/port in freetds.conf.  But I'm not so
> sure that's such a great feature and, even if it is, it belongs properly to
> the tsql application, not libtds.
>

The problem is that many users used server:port syntax (which is also
used by odbc and perhaps ms dblib) so I extend it to recognize
server\instance. So removing these syntaxes from libTDS now breaks
compatibility...

See attached patch

> I would try deleting the call to parse_server_name_for_port() and see if
> that solves your problem.
>
> HTH.
>
> --jkl
>

freddy77

_______________________________________________
FreeTDS mailing list
[email protected]
http://lists.ibiblio.org/mailman/listinfo/freetds
instance_fix.diff (application/octet-stream, 4 KB)
Fix compatibility problem with "server:port" syntax
Index: freetds83/src/tds/config.c
===================================================================
--- freetds83.orig/src/tds/config.c	2009-10-26 10:49:11.677512693 +0100
+++ freetds83/src/tds/config.c	2009-10-26 10:49:32.156514645 +0100
@@ -86,9 +86,8 @@
 static int tds_read_conf_sections(FILE * in, const char *server, TDSCONNECTION * connection);
 static int tds_read_interfaces(const char *server, TDSCONNECTION * connection);
 static int tds_config_boolean(const char *value);
-#if PARSE_SERVER_NAME_FOR_PORT
 static int parse_server_name_for_port(TDSCONNECTION * connection, TDSLOGIN * login);
-#endif
+static int parse_server_name_for_instance(TDSCONNECTION * connection, TDSLOGIN * login);
 static int tds_lookup_port(const char *portname);
 static void tds_config_encryption(const char * value, TDSCONNECTION * connection);
 
@@ -184,16 +183,16 @@
 	tdsdump_log(TDS_DBG_INFO1, "Getting connection information for [%s].\n", 
 			    tds_dstr_cstr(&login->server_name));	/* (The server name is set in login.c.) */
 
-#if PARSE_SERVER_NAME_FOR_PORT
-	/* Don't parse the [servername] key to a freetds.conf section. */
 	if (parse_server_name_for_port(connection, login)) {
 		tdsdump_log(TDS_DBG_INFO1, "Parsed servername, now %s on %d.\n", 
 			    tds_dstr_cstr(&connection->server_name), login->port);
 	}
-#endif
+
 	/* Read the config files. */
 	tdsdump_log(TDS_DBG_INFO1, "Attempting to read conf files.\n");
-	if (!tds_read_conf_file(connection, tds_dstr_cstr(&login->server_name))) {
+	if (!tds_read_conf_file(connection, tds_dstr_cstr(&login->server_name)) &&
+		(!parse_server_name_for_instance(connection, login) ||
+		 !tds_read_conf_file(connection, tds_dstr_cstr(&login->server_name)))) {
 		/* fallback to interfaces file */
 		tdsdump_log(TDS_DBG_INFO1, "Failed in reading conf file.  Trying interface files.\n");
 		if (!tds_read_interfaces(tds_dstr_cstr(&login->server_name), connection)) {
@@ -1072,7 +1071,6 @@
 	return found;
 }
 
-#if PARSE_SERVER_NAME_FOR_PORT
 /**
  * Check the server name to find port info first
  * Warning: connection-> & login-> are all modified when needed
@@ -1081,34 +1079,52 @@
 static int
 parse_server_name_for_port(TDSCONNECTION * connection, TDSLOGIN * login)
 {
-	const char *pSep;
-	const char *server;
+	const char *sep, *server;
 
 	/* seek the ':' in login server_name */
 	server = tds_dstr_cstr(&login->server_name);
-	pSep = strrchr(server, ':');
+	sep = strrchr(server, ':');
 
-	if (pSep && pSep != server) {	/* yes, i found it! */
-		/* modify connection-> && login->server_name & ->port */
-		login->port = connection->port = atoi(pSep + 1);
-		tds_dstr_copy(&connection->instance_name, "");
-	} else {
-		/* handle instance name */
-		pSep = strrchr(server, '\\');
-		if (!pSep || pSep == server)
-			return 0;
+	if (!sep || sep == server)
+		return 0;
 
-		login->port = connection->port = 0;
-		tds_dstr_copy(&connection->instance_name, pSep + 1);
-	}
+	/* modify connection-> && login->server_name & ->port */
+	login->port = connection->port = atoi(sep + 1);
+	tds_dstr_copy(&connection->instance_name, "");
 
-	tds_dstr_setlen(&login->server_name, pSep - server);
+	tds_dstr_setlen(&login->server_name, sep - server);
+	if (!tds_dstr_dup(&connection->server_name, &login->server_name))
+		return 0;
+
+	return 1;
+}
+
+/**
+ * Check the server name to find instance info
+ * Warning: connection-> & login-> are all modified when needed
+ * \return 1 when found, else 0
+ */
+static int
+parse_server_name_for_instance(TDSCONNECTION * connection, TDSLOGIN * login)
+{
+	const char *sep, *server;
+
+	/* seek the '\' in login server_name */
+	server = tds_dstr_cstr(&login->server_name);
+	sep = strrchr(server, '\\');
+
+	if (!sep || sep == server)
+		return 0;
+
+	login->port = connection->port = 0;
+	tds_dstr_copy(&connection->instance_name, sep + 1);
+
+	tds_dstr_setlen(&login->server_name, sep - server);
 	if (!tds_dstr_dup(&connection->server_name, &login->server_name))
 		return 0;
 
 	return 1;
 }
-#endif
 
 /**
  * Return a structure capturing the compile-time settings provided to the
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.