NCBI C++ ToolKit
common.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #include "common.h"
2 
3 #include <ctype.h>
4 
5 #if HAVE_SYS_SOCKET_H
6 #include <sys/socket.h>
7 #endif /* HAVE_SYS_SOCKET_H */
8 
9 #if HAVE_SYS_STAT_H
10 #include <sys/stat.h>
11 #endif /* HAVE_SYS_STAT_H */
12 
13 #if HAVE_NETINET_IN_H
14 #include <netinet/in.h>
15 #endif /* HAVE_NETINET_IN_H */
16 
17 #if defined(UNIXODBC) || defined(_WIN32)
18 #include <odbcinst.h>
19 #endif
20 
21 #ifndef _WIN32
22 #include <freetds/sysdep_private.h>
23 #else
24 #define TDS_SDIR_SEPARATOR "\\"
25 #endif
26 
27 #include <freetds/odbc.h>
28 
29 #include <common/test_assert.h>
30 
35 void (*odbc_set_conn_attr)(void) = NULL;
36 
37 char odbc_user[512];
38 char odbc_server[512];
39 char odbc_password[512];
40 char odbc_database[512];
41 char odbc_driver[1024];
42 
43 static int
44 check_lib(char *path, const char *file)
45 {
46  size_t len = strlen(path);
47  FILE *f;
48 
49  strcat(path, file);
50  f = fopen(path, "rb");
51  if (f) {
52  fclose(f);
53  return 1;
54  }
55  path[len] = 0;
56  return 0;
57 }
58 
59 /* some platforms do not have setenv, define a replacement */
60 #if !HAVE_SETENV
61 void
62 odbc_setenv(const char *name, const char *value, int overwrite)
63 {
64 #if HAVE_PUTENV
65  char buf[1024];
66 
67  sprintf(buf, "%s=%s", name, value);
68  putenv(buf);
69 #endif
70 }
71 #endif
72 
73 /* this should be extended with all possible systems... */
74 static const char *const search_driver[] = {
75  ".libs/libtdsodbc.so",
76  ".libs/libtdsodbc.sl",
77  ".libs/libtdsodbc.dylib",
78  ".libs/libtdsodbc.dll",
79  "_libs/libtdsodbc.dll",
80  "debug/tdsodbc.dll",
81  "release/tdsodbc.dll",
82  "libtdsodbc.so",
83  "tdsodbc.dll",
84  NULL
85 };
86 
87 int
89 {
90  static const char *PWD = "../../../PWD";
91  FILE *in = NULL;
92  char line[512];
93  char *s1, *s2;
94  const char *const *search_p;
95  char path[1024];
96  size_t len;
97 #if defined(_WIN32) && !defined(TDS_NO_DM)
98  UWORD old_config_mode;
99 #endif
100 
101  setbuf(stdout, NULL);
102  setbuf(stderr, NULL);
103 
104  s1 = getenv("TDSPWDFILE");
105  if (s1 && s1[0])
106  in = fopen(s1, "r");
107  if (!in)
108  in = fopen(PWD, "r");
109  if (!in)
110  in = fopen("PWD", "r");
111 
112  if (!in) {
113  fprintf(stderr, "Can not open PWD file\n\n");
114  return 1;
115  }
116  while (fgets(line, 512, in)) {
117  s1 = strtok(line, "=");
118  s2 = strtok(NULL, "\n");
119  if (!s1 || !s2)
120  continue;
121  if (!strcmp(s1, "UID")) {
122  strcpy(odbc_user, s2);
123  } else if (!strcmp(s1, "SRV")) {
124  strcpy(odbc_server, s2);
125  } else if (!strcmp(s1, "PWD")) {
126  strcpy(odbc_password, s2);
127  } else if (!strcmp(s1, "DB")) {
128  strcpy(odbc_database, s2);
129  }
130  }
131  fclose(in);
132 
133  /* find our driver */
134 #ifndef _WIN32
135  if (!getcwd(path, sizeof(path)))
136 #else
137  if (!_getcwd(path, sizeof(path)))
138 #endif
139  return 0;
140 #ifdef __VMS
141  {
142  /* A hard-coded driver path has to be in unix syntax to be recognized as such. */
143  const char *unixspec = decc$translate_vms(path);
144  if ( (int)unixspec != 0 && (int)unixspec != -1 ) strcpy(path, unixspec);
145  }
146 #endif
147  len = strlen(path);
148  if (len < 10 || (strcasecmp(path + len - 10, "/unittests") != 0
149 #ifdef _WIN32
150  && strcasecmp(path + len - 10, "\\unittests") != 0
151 #endif
152  ))
153  return 0;
154  path[len - 9] = 0;
155  for (search_p = search_driver; *search_p; ++search_p) {
156  if (check_lib(path, *search_p))
157  break;
158  }
159  if (!*search_p)
160  return 0;
161  strcpy(odbc_driver, path);
162 
163 #if !defined(_WIN32) || defined(TDS_NO_DM)
164  /* craft out odbc.ini, avoid to read wrong one */
165  sprintf(path, "odbc.ini.%d", (int) getpid());
166  in = fopen(path, "w");
167  if (in) {
168  fprintf(in, "[%s]\nDriver = %s\nDatabase = %s\nServername = %s\n", odbc_server, odbc_driver, odbc_database, odbc_server);
169  fclose(in);
170  setenv("ODBCINI", "./odbc.ini", 1);
171  setenv("SYSODBCINI", "./odbc.ini", 1);
172  rename(path, "odbc.ini");
173  unlink(path);
174  }
175 #else
176  if (SQLGetConfigMode(&old_config_mode)) {
178  SQLWritePrivateProfileString(odbc_server, "Driver", odbc_driver, "odbc.ini");
179  SQLWritePrivateProfileString(odbc_server, "Database", odbc_database, "odbc.ini");
180  SQLWritePrivateProfileString(odbc_server, "Servername", odbc_server, "odbc.ini");
181  SQLSetConfigMode(old_config_mode);
182  }
183 #endif
184  return 0;
185 }
186 
187 void
188 odbc_report_error(const char *errmsg, int line, const char *file)
189 {
190  SQLSMALLINT handletype;
191  SQLHANDLE handle;
192  SQLRETURN ret;
193  SQLTCHAR sqlstate[6];
194  SQLTCHAR msg[256];
196 
197  if (odbc_stmt) {
198  handletype = SQL_HANDLE_STMT;
199  handle = odbc_stmt;
200  } else if (odbc_conn) {
201  handletype = SQL_HANDLE_DBC;
202  handle = odbc_conn;
203  } else {
204  handletype = SQL_HANDLE_ENV;
205  handle = odbc_env;
206  }
207  if (errmsg[0]) {
208  if (line)
209  fprintf(stderr, "%s:%d %s\n", file, line, errmsg);
210  else
211  fprintf(stderr, "%s\n", errmsg);
212  }
213  ret = SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, msg, ODBC_VECTOR_SIZE(msg), NULL);
214  if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
215  fprintf(stderr, "SQL error %s -- %s\n", C(sqlstate), C(msg));
216  odbc_disconnect();
217  ODBC_FREE();
218  exit(1);
219 }
220 
221 static void
222 ReportODBCError(const char *errmsg, SQLSMALLINT handletype, SQLHANDLE handle, SQLRETURN rc, int line, const char *file)
223 {
224  SQLRETURN ret;
225  SQLTCHAR sqlstate[6];
226  SQLTCHAR msg[256];
228 
229  if (errmsg[0]) {
230  if (line)
231  fprintf(stderr, "%s:%d rc=%d %s\n", file, line, (int) rc, errmsg);
232  else
233  fprintf(stderr, "rc=%d %s\n", (int) rc, errmsg);
234  }
235  ret = SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, msg, ODBC_VECTOR_SIZE(msg), NULL);
236  if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
237  fprintf(stderr, "SQL error %s -- %s\n", C(sqlstate), C(msg));
238  odbc_disconnect();
239  ODBC_FREE();
240  exit(1);
241 }
242 
243 int
245 {
247  char command[520];
248  const char *p;
249 
250  if (odbc_read_login_info())
251  exit(1);
252 
253  if (odbc_use_version3) {
257  } else {
258  CHKAllocEnv(&odbc_env, "S");
259  CHKAllocConnect(&odbc_conn, "S");
260  }
261 
262  printf("odbctest\n--------\n\n");
263  printf("connection parameters:\nserver: '%s'\nuser: '%s'\npassword: '%s'\ndatabase: '%s'\n",
264  odbc_server, odbc_user, "????" /* odbc_password */ , odbc_database);
265 
266  p = getenv("ODBC_MARS");
267  if (p && atoi(p) != 0)
268  SQLSetConnectAttr(odbc_conn, 1224 /*SQL_COPT_SS_MARS_ENABLED*/, (SQLPOINTER) 1 /*SQL_MARS_ENABLED_YES*/, SQL_IS_UINTEGER);
269  if (odbc_set_conn_attr)
270  (*odbc_set_conn_attr)();
271 
273 
274  CHKAllocStmt(&odbc_stmt, "S");
275 
276  sprintf(command, "use %s", odbc_database);
277  printf("%s\n", command);
278 
279  CHKExecDirect(T(command), SQL_NTS, "SI");
280 
281 #ifndef TDS_NO_DM
282  /* unixODBC seems to require it */
284 #endif
285  ODBC_FREE();
286  return 0;
287 }
288 
289 int
291 {
292  if (odbc_stmt) {
295  }
296 
297  if (odbc_conn) {
301  }
302 
303  if (odbc_env) {
306  }
307  ODBC_FREE();
308  return 0;
309 }
310 
311 SQLRETURN
313 {
314  SQLRETURN ret;
316 
317  printf("%s\n", command);
318  ret = SQLExecDirect(stmt, T(command), SQL_NTS);
319  ODBC_FREE();
320  return ret;
321 }
322 
323 static int ms_db = -1;
324 int
326 {
328  SQLTCHAR buf[64];
330  int i;
331 
332  if (ms_db < 0) {
333  buf[0] = 0;
335  for (i = 0; buf[i]; ++i)
336  buf[i] = tolower(buf[i]);
337  ms_db = (strstr(C(buf), "microsoft") != NULL);
338  }
339  ODBC_FREE();
340  return ms_db;
341 }
342 
343 static int freetds_driver = -1;
344 int
346 {
348  SQLTCHAR buf[64];
350  int i;
351 
352  if (freetds_driver < 0) {
353  buf[0] = 0;
355  for (i = 0; buf[i]; ++i)
356  buf[i] = tolower(buf[i]);
357  freetds_driver = (strstr(C(buf), "tds") != NULL);
358  }
359  ODBC_FREE();
360  return freetds_driver;
361 }
362 
363 /* Detect protocol version using queries
364  * This to make possible protocol discovery on drivers like MS
365  */
366 static int
368 {
369  SQLRETURN ret;
370  SQLSMALLINT scale, nullable, type;
371  SQLULEN prec;
373 
374  /* statement must be in a consistent state to do the check */
375  CHKExecDirect(T("select 1"), SQL_NTS, "S");
377 
378  /* select cast(123 as sql_variant) -> nvarchar('123') is 7.0 failure query is 5.0 ?? */
379  ret = CHKExecDirect(T("select cast('123' as sql_variant)"), SQL_NTS, "SNoE");
381  if (ret == SQL_ERROR) {
382  ODBC_FREE();
383  return 0x500;
384  }
385 
386  /* see how bigint is returned, numeric means 7.0 */
387  CHKExecDirect(T("select cast('123' as bigint)"), SQL_NTS, "S");
388  CHKDescribeCol(1, NULL, 0, NULL, &type, &prec, &scale, &nullable, "S");
390  if (type == SQL_NUMERIC || type == SQL_DECIMAL) {
391  ODBC_FREE();
392  return 0x700;
393  }
394  if (type != SQL_BIGINT) {
395  fprintf(stderr, "Strange type returned trying to detect protocol version\n");
396  odbc_disconnect();
397  ODBC_FREE();
398  exit(1);
399  }
400 
401  /* select cast('123' as varchar(max)) -> ??? SQL_VARCHAR is 7.2 ?? */
402  ret = CHKExecDirect(T("select cast('123' as varchar(max))"), SQL_NTS, "SE");
403  if (ret == SQL_ERROR) {
405  ODBC_FREE();
406  return 0x701;
407  }
408  CHKDescribeCol(1, NULL, 0, NULL, &type, &prec, &scale, &nullable, "S");
410  if (type == SQL_LONGVARCHAR) {
411  ODBC_FREE();
412  return 0x701;
413  }
414  if (type != SQL_VARCHAR) {
415  fprintf(stderr, "Strange type returned trying to detect protocol version\n");
416  odbc_disconnect();
417  ODBC_FREE();
418  exit(1);
419  }
420 
421  /* select cast('12:13:14.1234' as time(4)) -> NVARCHAR('12:13:14.1234') is 7.2 else 7.3 */
422  ret = CHKExecDirect(T("select cast('12:13:14.1234' as time(4))"), SQL_NTS, "SE");
423  if (ret == SQL_ERROR) {
425  ODBC_FREE();
426  return 0x702;
427  }
428  CHKDescribeCol(1, NULL, 0, NULL, &type, &prec, &scale, &nullable, "S");
430  if (scale == 4)
431  return 0x703;
432  if (scale != 0 || type != SQL_WVARCHAR) {
433  fprintf(stderr, "Strange type or scale returned trying to detect protocol version\n");
434  odbc_disconnect();
435  ODBC_FREE();
436  exit(1);
437  }
438 
439  ODBC_FREE();
440  return 0x702;
441 }
442 
443 int
445 {
446  static int tds_version = -1;
447 
451 
452  if (odbc_driver_is_freetds() && tds_version < 0) {
453  version = 0;
454  len = 0;
455  SQLGetInfo(odbc_conn, 1300 /* SQL_INFO_FREETDS_TDS_VERSION */, &version, sizeof(version), &len);
456  if (len == sizeof(version))
457  tds_version = (version >> 16) << 8 | (version & 0xff);
458  }
459  if (tds_version < 0) {
461  }
462  ODBC_FREE();
463  return tds_version;
464 }
465 
466 static char db_str_version[32];
467 
468 const char *odbc_db_version(void)
469 {
470  if (!db_str_version[0]) {
472  SQLTCHAR buf[32];
473  SQLSMALLINT version_len;
474 
475  CHKR(SQLGetInfo, (odbc_conn, SQL_DBMS_VER, buf, sizeof(buf), &version_len), "S");
476  strcpy(db_str_version, C(buf));
477  ODBC_FREE();
478  }
479 
480  return db_str_version;
481 }
482 
483 unsigned int odbc_db_version_int(void)
484 {
485  unsigned int h, l;
486  if (sscanf(odbc_db_version(), "%u.%u.", &h, &l) != 2) {
487  fprintf(stderr, "Wrong db version: %s\n", odbc_db_version());
488  odbc_disconnect();
489  exit(1);
490  }
491 
492  return (h << 24) | ((l & 0xFFu) << 16);
493 }
494 
495 void
496 odbc_check_cols(int n, int line, const char * file)
497 {
498  SQLSMALLINT cols;
499 
500  if (n < 0) {
501  CHKNumResultCols(&cols, "E");
502  return;
503  }
504  CHKNumResultCols(&cols, "S");
505  if (cols != n) {
506  fprintf(stderr, "%s:%d: Expected %d columns returned %d\n", file, line, n, (int) cols);
507  odbc_disconnect();
508  exit(1);
509  }
510 }
511 
512 void
513 odbc_check_rows(int n, int line, const char * file)
514 {
515  SQLLEN rows;
516 
517  if (n < -1) {
518  CHKRowCount(&rows, "E");
519  return;
520  }
521 
522  CHKRowCount(&rows, "S");
523  if (rows != n) {
524  fprintf(stderr, "%s:%d: Expected %d rows returned %d\n", file, line, n, (int) rows);
525  odbc_disconnect();
526  exit(1);
527  }
528 }
529 
530 void
531 odbc_reset_statement_proc(SQLHSTMT *stmt, const char *file, int line)
532 {
534  *stmt = SQL_NULL_HSTMT;
535  odbc_check_res(file, line, SQLAllocStmt(odbc_conn, stmt), SQL_HANDLE_DBC, odbc_conn, "SQLAllocStmt", "S");
536 }
537 
538 void
540 {
541  const char *p = getenv("TDS_SKIP_SUCCESS");
542  if (p && atoi(p) != 0)
543  exit(0);
544  exit(77);
545 }
546 
547 void
549 {
550  SQLRETURN retcode;
552 
554  if (retcode != SQL_SUCCESS) {
555  SQLTCHAR output[256];
556  SQLTCHAR sqlstate[6];
557 
559  sqlstate[5] = 0;
560  if (strcmp(C(sqlstate), "01S02") == 0) {
561  printf("Your connection seems to not support cursors, probably you are using wrong protocol version or Sybase\n");
562  odbc_disconnect();
563  ODBC_FREE();
565  }
566  ReportODBCError("SQLSetStmtAttr", SQL_HANDLE_STMT, odbc_stmt, retcode, __LINE__, __FILE__);
567  }
569  ODBC_FREE();
570 }
571 
572 SQLRETURN
573 odbc_check_res(const char *file, int line, SQLRETURN rc, SQLSMALLINT handle_type, SQLHANDLE handle, const char *func, const char *res)
574 {
575  const char *p = res;
576  for (;;) {
577  if (*p == 'S') {
578  if (rc == SQL_SUCCESS)
579  return rc;
580  ++p;
581  } else if (*p == 'I') {
582  if (rc == SQL_SUCCESS_WITH_INFO)
583  return rc;
584  ++p;
585  } else if (*p == 'E') {
586  if (rc == SQL_ERROR)
587  return rc;
588  ++p;
589  } else if (strncmp(p, "No", 2) == 0) {
590  if (rc == SQL_NO_DATA)
591  return rc;
592  p += 2;
593  } else if (strncmp(p, "Ne", 2) == 0) {
594  if (rc == SQL_NEED_DATA)
595  return rc;
596  p += 2;
597  } else if (!*p) {
598  break;
599  } else {
600  odbc_report_error("Wrong results specified", line, file);
601  return rc;
602  }
603  }
604  ReportODBCError(func, handle_type, handle, rc, line, file);
605  return rc;
606 }
607 
610 {
611  switch (type) {
612  case SQL_HANDLE_DESC:
613  return SQL_HANDLE_STMT;
614  case SQL_HANDLE_STMT:
615  return SQL_HANDLE_DBC;
616  case SQL_HANDLE_DBC:
617  return SQL_HANDLE_ENV;
618  }
619  return 0;
620 }
621 
622 SQLRETURN
623 odbc_command_proc(HSTMT stmt, const char *command, const char *file, int line, const char *res)
624 {
625  SQLRETURN ret;
627 
628  printf("%s\n", command);
629  ret = odbc_check_res(file, line, SQLExecDirect(stmt, T(command), SQL_NTS), SQL_HANDLE_STMT, stmt, "odbc_command", res);
630  ODBC_FREE();
631  return ret;
632 }
633 
634 char odbc_err[512];
636 
637 void
639 {
641  SQLTCHAR *err = (SQLTCHAR *) ODBC_GET(sizeof(odbc_err)*sizeof(SQLTCHAR));
642  SQLTCHAR *state = (SQLTCHAR *) ODBC_GET(sizeof(odbc_sqlstate)*sizeof(SQLTCHAR));
643 
644  memset(odbc_err, 0, sizeof(odbc_err));
645  memset(odbc_sqlstate, 0, sizeof(odbc_sqlstate));
646  CHKGetDiagRec(SQL_HANDLE_STMT, odbc_stmt, 1, state, NULL, err, sizeof(odbc_err), NULL, "SI");
647  strcpy(odbc_err, C(err));
648  strcpy(odbc_sqlstate, C(state));
649  ODBC_FREE();
650  printf("Message: '%s' %s\n", odbc_sqlstate, odbc_err);
651 }
652 
653 SQLLEN
654 odbc_to_sqlwchar(SQLWCHAR *dst, const char *src, SQLLEN n)
655 {
656  SQLLEN i = n;
657  while (--i >= 0)
658  dst[i] = (unsigned char) src[i];
659  return n * sizeof(SQLWCHAR);
660 }
661 
662 SQLLEN
663 odbc_from_sqlwchar(char *dst, const SQLWCHAR *src, SQLLEN n)
664 {
665  SQLLEN i;
666  if (n < 0) {
667  const SQLWCHAR *p = src;
668  for (n=1; *p++ != 0; ++n)
669  continue;
670  }
671  for (i = 0; i < n; ++i) {
672  assert(src[i] < 256);
673  dst[i] = (char) src[i];
674  }
675  return n;
676 }
677 
679 
680 void *
682 {
683  ODBC_BUF *p = (ODBC_BUF*) calloc(1, sizeof(ODBC_BUF));
684  assert(p);
685  p->buf = malloc(s);
686  assert(p->buf);
687  p->next = *buf;
688  *buf = p;
689  return p->buf;
690 }
691 
692 void
694 {
695  ODBC_BUF *cur = *buf;
696  *buf = NULL;
697  while (cur) {
698  ODBC_BUF *next = cur->next;
699  free(cur->buf);
700  free(cur);
701  cur = next;
702  }
703 }
704 
705 SQLWCHAR *
706 odbc_get_sqlwchar(ODBC_BUF** buf, const char *s)
707 {
708  size_t l;
709  SQLWCHAR *buffer;
710 
711  if (!s) return NULL;
712  l = strlen(s) + 1;
713  buffer = (SQLWCHAR*) odbc_buf_get(buf, l * sizeof(SQLWCHAR));
714  odbc_to_sqlwchar(buffer, s, l);
715  return buffer;
716 }
717 
718 char*
720 {
721  int n;
722  const SQLWCHAR *p = s;
723  char *out;
724 
725  for (n=1; *p++ != 0; ++n)
726  continue;
727  out = (char *) odbc_buf_get(buf, n);
728  odbc_from_sqlwchar(out, s, n);
729  return out;
730 }
731 
732 typedef union {
733  struct sockaddr sa;
734  struct sockaddr_in sin;
735  char dummy[256];
736 } long_sockaddr;
737 
738 static int
740 {
741  long_sockaddr addr;
742  socklen_t addr_len;
743 
744 #ifndef _WIN32
745  struct stat file_stat;
746 
747  if (fstat(fd, &file_stat))
748  return 0;
749  if ((file_stat.st_mode & S_IFSOCK) != S_IFSOCK)
750  return 0;
751 #endif
752 
753  addr_len = sizeof(addr);
754  if (tds_getpeername((TDS_SYS_SOCKET) fd, &addr.sa, &addr_len))
755  return 0;
756 
757  addr_len = sizeof(addr);
758  if (tds_getsockname((TDS_SYS_SOCKET) fd, &addr.sa, &addr_len))
759  return 0;
760 
761  return 1;
762 }
763 
764 enum {NUM_FDS = 4096*4};
765 static unsigned char fd_bitmask[NUM_FDS / 8];
766 
767 static int
768 mark_fd(int fd)
769 {
770  unsigned shift;
771  unsigned char mask;
772 
773  if (fd < 0 || fd >= NUM_FDS)
774  return 0;
775 
776  shift = fd & 7;
777  mask = fd_bitmask[fd >> 3];
778  fd_bitmask[fd >> 3] = mask | (1 << shift);
779 
780  return (mask >> shift) & 1;
781 }
782 
783 #ifdef _WIN32
784 #define FOR_ALL_SOCKETS(i) for (i = 4; i <= (4096*4); i += 4)
785 #else
786 #define FOR_ALL_SOCKETS(i) for (i = 3; i < 1024; ++i)
787 #endif
788 
789 void
791 {
792  int i;
793 
794  memset(fd_bitmask, 0, sizeof(fd_bitmask));
795  FOR_ALL_SOCKETS(i) {
796  if (fd_is_socket(i))
797  mark_fd(i);
798  }
799 }
800 
803 {
804 #if 1
805  return tds_get_s(((TDS_DBC*)odbc_conn)->tds_socket);
806 #else
807  typedef struct {
808  TDS_SYS_SOCKET sock;
809  int local_port;
810  int remote_port;
811  } sock_info;
812  sock_info found[8];
813  unsigned num_found = 0, n;
814  int i;
815 
816  FOR_ALL_SOCKETS(i) {
817  long_sockaddr remote_addr, local_addr;
818  struct sockaddr_in *in;
819  socklen_t remote_addr_len, local_addr_len;
820  sock_info *info;
821 
822  /* check if is a socket */
823  if (!fd_is_socket(i))
824  continue;
825  if (mark_fd(i))
826  continue;
827 
828  remote_addr_len = sizeof(remote_addr);
829  if (tds_getpeername((TDS_SYS_SOCKET) i, &remote_addr.sa, &remote_addr_len))
830  continue;
831  if (remote_addr.sa.sa_family != AF_INET
832 #ifdef AF_INET6
833  && remote_addr.sa.sa_family != AF_INET6
834 #endif
835  )
836  continue;
837  local_addr_len = sizeof(local_addr);
838  if (tds_getsockname((TDS_SYS_SOCKET) i, &local_addr.sa, &local_addr_len))
839  continue;
840 
841  /* save in the array */
842  if (num_found >= 8) {
843  memmove(found, found+1, sizeof(found) - sizeof(found[0]));
844  num_found = 7;
845  }
846  info = &found[num_found++];
847  info->sock = (TDS_SYS_SOCKET) i;
848  info->local_port = -1;
849  info->remote_port = -1;
850 
851  /* now check if is a socketpair */
852  in = &remote_addr.sin;
853  if (in->sin_family != AF_INET)
854  continue;
855  if (in->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
856  continue;
857  info->remote_port = ntohs(in->sin_port);
858  in = &local_addr.sin;
859  if (in->sin_family != AF_INET)
860  continue;
861  if (in->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
862  continue;
863  info->local_port = ntohs(in->sin_port);
864  for (n = 0; n < num_found - 1; ++n) {
865  if (found[n].remote_port != info->local_port
866  || found[n].local_port != info->remote_port)
867  continue;
868  --num_found;
869  memmove(found+n, found+n+1, num_found-n-1);
870  --num_found;
871  break;
872  }
873  }
874 
875  /* return last */
876  if (num_found == 0)
877  return INVALID_SOCKET;
878  return found[num_found-1].sock;
879 #endif
880 }
881 
882 void
884 {
885  SQLRETURN rc;
886 
887  rc = CHKExecDirect(T(query), SQL_NTS, "SINo");
888  if (rc == SQL_NO_DATA)
889  return;
890 
891  do {
892  SQLSMALLINT cols;
893 
894  CHKNumResultCols(&cols, "S");
895  if (cols != 0) {
896  fprintf(stderr, "Data not expected here, query:\n\t%s\n", query);
897  odbc_disconnect();
898  exit(1);
899  }
900  } while (CHKMoreResults("SNo") == SQL_SUCCESS);
901 }
ncbi::TMaskedQueryRegions mask
unsigned dummy
Definition: block_cipher.h:0
#define CHKMoreResults(res)
Definition: common.h:138
#define ODBC_GET(s)
Definition: common.h:217
#define odbc_reset_statement()
Definition: common.h:71
#define C(s)
Definition: common.h:231
#define CHKAllocHandle(a, b, c, res)
Definition: common.h:92
#define CHKNumResultCols(a, res)
Definition: common.h:140
#define CHKAllocStmt(a, res)
Definition: common.h:90
#define CHKR(func, params, res)
Definition: common.h:79
#define CHKExecDirect(a, b, res)
Definition: common.h:112
#define CHKDescribeCol(a, b, c, d, e, f, g, h, res)
Definition: common.h:104
#define CHKConnect(a, b, c, d, e, f, res)
Definition: common.h:106
#define CHKAllocConnect(a, res)
Definition: common.h:86
#define T(s)
Definition: common.h:230
#define ODBC_FREE()
Definition: common.h:218
#define CHKGetDiagRec(a, b, c, d, e, f, g, h, res)
Definition: common.h:128
#define ODBC_VECTOR_SIZE(x)
Definition: common.h:188
#define CHKAllocEnv(a, res)
Definition: common.h:88
#define CHKRowCount(a, res)
Definition: common.h:150
std::ofstream out("events_result.xml")
main entry point for tests
static const char * PWD
Definition: common.c:39
#define strcat(s, k)
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
#define SQLULEN
Definition: odbc.h:49
#define SQLLEN
Definition: odbc.h:52
int TDS_SYS_SOCKET
#define INVALID_SOCKET
#define tds_getpeername
#define tds_getsockname
#define tds_get_s(tds)
Definition: tds.h:1298
#define SQLSetConnectAttr(h, n, p, t)
Definition: odbc.c:50
HSTMT odbc_stmt
Definition: common.c:33
const char * odbc_db_version(void)
Definition: common.c:468
#define FOR_ALL_SOCKETS(i)
Definition: common.c:786
char odbc_sqlstate[6]
Definition: common.c:635
unsigned int odbc_db_version_int(void)
Definition: common.c:483
void odbc_report_error(const char *errmsg, int line, const char *file)
Definition: common.c:188
void odbc_test_skipped(void)
Definition: common.c:539
void odbc_check_cursor(void)
Definition: common.c:548
void odbc_check_rows(int n, int line, const char *file)
Definition: common.c:513
char odbc_database[512]
Definition: common.c:40
static int ms_db
Definition: common.c:323
static unsigned char fd_bitmask[NUM_FDS/8]
Definition: common.c:765
int odbc_disconnect(void)
Definition: common.c:290
int odbc_read_login_info(void)
Definition: common.c:88
void odbc_buf_free(ODBC_BUF **buf)
Definition: common.c:693
HENV odbc_env
Definition: common.c:31
char odbc_err[512]
Definition: common.c:634
SQLWCHAR * odbc_get_sqlwchar(ODBC_BUF **buf, const char *s)
Definition: common.c:706
char odbc_driver[1024]
Definition: common.c:41
static char db_str_version[32]
Definition: common.c:466
SQLRETURN odbc_check_res(const char *file, int line, SQLRETURN rc, SQLSMALLINT handle_type, SQLHANDLE handle, const char *func, const char *res)
Definition: common.c:573
int odbc_connect(void)
Definition: common.c:244
void odbc_check_cols(int n, int line, const char *file)
Definition: common.c:496
SQLINTEGER odbc_to_sqlwchar(SQLWCHAR *dst, const char *src, SQLINTEGER n)
Definition: common.c:654
void odbc_reset_statement_proc(SQLHSTMT *stmt, const char *file, int line)
Definition: common.c:531
int odbc_use_version3
Definition: common.c:34
char odbc_server[512]
Definition: common.c:38
static void ReportODBCError(const char *errmsg, SQLSMALLINT handletype, SQLHANDLE handle, SQLRETURN rc, int line, const char *file)
Definition: common.c:222
static const char *const search_driver[]
Definition: common.c:74
void odbc_mark_sockets_opened(void)
Definition: common.c:790
@ NUM_FDS
Definition: common.c:764
SQLRETURN odbc_command_proc(HSTMT stmt, const char *command, const char *file, int line, const char *res)
Definition: common.c:623
static int odbc_tds_version_long(void)
Definition: common.c:367
int odbc_db_is_microsoft(void)
Definition: common.c:325
char odbc_password[512]
Definition: common.c:39
static int fd_is_socket(int fd)
Definition: common.c:739
static int check_lib(char *path, const char *file)
Definition: common.c:44
void(* odbc_set_conn_attr)(void)
Definition: common.c:35
TDS_SYS_SOCKET odbc_find_last_socket(void)
Definition: common.c:802
void odbc_check_no_row(const char *query)
Definition: common.c:883
char * odbc_get_sqlchar(ODBC_BUF **buf, SQLWCHAR *s)
Definition: common.c:719
void odbc_read_error(void)
Definition: common.c:638
HDBC odbc_conn
Definition: common.c:32
SQLSMALLINT odbc_alloc_handle_err_type(SQLSMALLINT type)
Definition: common.c:609
int odbc_tds_version(void)
Definition: common.c:444
static int mark_fd(int fd)
Definition: common.c:768
int odbc_driver_is_freetds(void)
Definition: common.c:345
void odbc_setenv(const char *name, const char *value, int overwrite)
Definition: common.c:62
void * odbc_buf_get(ODBC_BUF **buf, size_t s)
Definition: common.c:681
static int freetds_driver
Definition: common.c:343
char odbc_user[512]
Definition: common.c:37
SQLRETURN odbc_command_with_result(HSTMT stmt, const char *command)
Definition: common.c:312
SQLINTEGER odbc_from_sqlwchar(char *dst, const SQLWCHAR *src, SQLINTEGER n)
Definition: common.c:663
static int type
Definition: getdata.c:31
static SQLCHAR output[256]
Definition: print.c:5
static HSTMT stmt
Definition: rebindpar.c:12
#define setenv(n, v, o)
Definition: replacements.h:185
static int tds_version
Definition: common.c:46
#define NULL
Definition: ncbistd.hpp:225
int socklen_t
Definition: config.h:95
exit(2)
FILE * file
char * buf
int i
yy_size_t n
int len
static MDB_envinfo info
Definition: mdb_load.c:37
static int version
Definition: mdb_load.c:29
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int strncmp(const char *str1, const char *str2, size_t count)
Definition: odbc_utils.hpp:133
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strcasecmp
#define INADDR_LOOPBACK
Definition: ncbi_localnet.c:45
#define putenv
int tolower(Uchar c)
Definition: ncbictype.hpp:72
const char * command
std::istream & in(std::istream &in_, double &x_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
int SQLSetConfigMode(UWORD wConfigMode)
int SQLWritePrivateProfileString(LPCSTR lpszSection, LPCSTR lpszEntry, LPCSTR lpszString, LPCSTR lpszFilename)
#define ODBC_USER_DSN
Definition: odbcinst.h:117
int SQLGetConfigMode(UWORD *pwConfigMode)
#define memmove(a, b, c)
static pcre_uint8 * buffer
Definition: pcretest.c:1051
SQLRETURN SQLSetEnvAttr(SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
SQLRETURN SQLExecDirect(SQLHSTMT StatementHandle, SQLCHAR *StatementText, SQLINTEGER TextLength)
Definition: odbc_export.h:812
#define SQL_NULL_HANDLE
Definition: sql.h:285
SQLRETURN SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLCHAR *Sqlstate, SQLINTEGER *NativeError, SQLCHAR *MessageText, SQLSMALLINT BufferLength, SQLSMALLINT *TextLength)
#define SQL_HANDLE_STMT
Definition: sql.h:66
#define SQL_NULL_HSTMT
Definition: sql.h:277
SQLRETURN SQLGetInfo(SQLHDBC ConnectionHandle, SQLUSMALLINT InfoType, SQLPOINTER InfoValue, SQLSMALLINT BufferLength, SQLSMALLINT *StringLength)
#define SQL_SUCCESS
Definition: sql.h:31
SQLRETURN SQLFreeStmt(SQLHSTMT StatementHandle, SQLUSMALLINT Option)
Definition: odbc.c:4237
#define SQL_NULL_HENV
Definition: sql.h:275
#define SQL_DBMS_VER
Definition: sql.h:414
#define SQL_NULL_HDBC
Definition: sql.h:276
SQLRETURN SQLFreeEnv(SQLHENV EnvironmentHandle)
Definition: odbc.c:4142
SQLRETURN SQLAllocStmt(SQLHDBC ConnectionHandle, SQLHSTMT *StatementHandle)
#define SQL_NEED_DATA
Definition: sql.h:39
#define SQL_NUMERIC
Definition: sql.h:168
#define SQL_DECIMAL
Definition: sql.h:169
SQLRETURN SQLSetStmtAttr(SQLHSTMT StatementHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
#define SQL_HANDLE_ENV
Definition: sql.h:64
SQLRETURN SQLFreeConnect(SQLHDBC ConnectionHandle)
#define SQL_SUCCESS_WITH_INFO
Definition: sql.h:32
#define SQL_DROP
Definition: sql.h:254
#define SQL_NTS
Definition: sql.h:49
#define SQL_HANDLE_DESC
Definition: sql.h:67
SQLRETURN SQLDisconnect(SQLHDBC ConnectionHandle)
Definition: odbc.c:2313
#define SQL_DBMS_NAME
Definition: sql.h:413
#define SQL_HANDLE_DBC
Definition: sql.h:65
#define SQL_VARCHAR
Definition: sql.h:178
#define SQL_ERROR
Definition: sql.h:36
#define SQL_NO_DATA
Definition: sql.h:34
#define SQL_OV_ODBC3
Definition: sqlext.h:98
#define SQL_DRIVER_NAME
Definition: sqlext.h:775
#define SQL_LONGVARCHAR
Definition: sqlext.h:432
SQLRETURN SQLMoreResults(SQLHSTMT hstmt)
Definition: odbc.c:865
#define SQL_ATTR_CONCURRENCY
Definition: sqlext.h:257
#define SQL_IS_UINTEGER
Definition: sqlext.h:306
#define SQL_ATTR_ODBC_VERSION
Definition: sqlext.h:74
#define SQL_CONCUR_ROWVER
Definition: sqlext.h:344
#define SQL_BIGINT
Definition: sqlext.h:436
SQLHANDLE HSTMT
Definition: sqltypes.h:238
unsigned long SQLUINTEGER
Definition: sqltypes.h:177
void * SQLPOINTER
Definition: sqltypes.h:195
SQLHANDLE HDBC
Definition: sqltypes.h:237
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
WCHAR SQLWCHAR
Definition: sqltypes.h:458
SQLHANDLE SQLHSTMT
Definition: sqltypes.h:216
SQLHANDLE HENV
Definition: sqltypes.h:236
void * SQLHANDLE
Definition: sqltypes.h:213
unsigned short int UWORD
Definition: sqltypes.h:262
signed short int SQLSMALLINT
Definition: sqltypes.h:201
SQLSMALLINT SQLRETURN
Definition: sqltypes.h:210
#define SQL_WVARCHAR
Definition: sqlucode.h:15
#define assert(x)
Definition: srv_diag.hpp:58
static string query
Definition: odbc.h:282
void * buf
Definition: common.h:212
struct odbc_buf * next
Definition: common.h:211
Information for a server connection.
Definition: tds.h:1211
Definition: type.c:6
struct sockaddr_in sin
Definition: common.c:734
struct sockaddr sa
Definition: common.c:733
void free(voidpf ptr)
voidp malloc(uInt size)
voidp calloc(uInt items, uInt size)
Modified on Tue May 21 10:56:59 2024 by modify_doxy.py rev. 669887