diff -Naur flow-tools-0.67.orig/configure flow-tools-0.67/configure --- flow-tools-0.67.orig/configure 2004-12-21 22:05:41.871473800 -0200 +++ flow-tools-0.67/configure 2004-12-21 22:13:17.640186424 -0200 @@ -3279,7 +3279,7 @@ fi if test "x$WHERE_PGSQL" != "x"; then - LIBS="-L$WHERE_PGSQL/lib/pgsql" + LIBS="-L$WHERE_PGSQL/lib" echo "$as_me:$LINENO: checking for PQsetdbLogin in -lpq" >&5 echo $ECHO_N "checking for PQsetdbLogin in -lpq... $ECHO_C" >&6 if test "${ac_cv_lib_pq_PQsetdbLogin+set}" = set; then @@ -3336,7 +3336,7 @@ echo "${ECHO_T}$ac_cv_lib_pq_PQsetdbLogin" >&6 if test $ac_cv_lib_pq_PQsetdbLogin = yes; then - PGSQLCFLAGS="-L$WHERE_PGSQL/lib -I$WHERE_PGSQL/include/pgsql" + PGSQLCFLAGS="-L$WHERE_PGSQL/lib -I$WHERE_PGSQL/include" PGSQLLIB="-lpq" cat >>confdefs.h <<\_ACEOF #define HAVE_PGSQL 1 diff -Naur flow-tools-0.67.orig/src/flow-export.c flow-tools-0.67/src/flow-export.c --- flow-tools-0.67.orig/src/flow-export.c 2004-12-21 22:05:41.809483224 -0200 +++ flow-tools-0.67/src/flow-export.c 2004-12-21 22:18:07.051189264 -0200 @@ -67,7 +67,7 @@ #define DB_DEFAULT_DBHOST "localhost" #define DB_DEFAULT_DBNAME "netflow" #define DB_DEFAULT_DBPORT "5432" -#define DB_DEFAULT_DBTABLE "raw" +#define DB_DEFAULT_DBTABLE "netflow" #define DB_DEFAULT_DBUSER "netflow" #define DB_DEFAULT_DBPWD "netflow" @@ -115,13 +115,14 @@ struct jump format[] = {{format0}, {format1}, {format2}, {format3}, {format4}, {format5}}; +int debug; + int main(int argc, char **argv) { int i, format_index, ret, ascii_mask; struct ftio ftio; struct ftprof ftp; struct options opt; - int debug; /* init fterr */ fterr_setid(argv[0]); @@ -758,13 +759,13 @@ strcat (query, values); strcat (query, ")"); - if (debug) +/* if (debug) fprintf(stderr, "field=%s\n val=%s\n query=%s\n", fields, values, query); if (mysql_real_query(&mysql, query, strlen(query)) != 0) fterr_warnx("mysql_real_query(): %s", mysql_error(&mysql)); - +*/ } ++opt->records; @@ -868,6 +869,7 @@ char *db_port; int len; + static char connstring[100]; PGconn *conn; PGresult *res; @@ -887,39 +889,50 @@ db_user = strsep(&tmp, ":"); db_pwd = strsep(&tmp, ":"); db_host = strsep(&tmp, ":"); - db_port = strsep(&tmp, ":"); + db_tmp = strsep(&tmp, ":"); db_name = strsep(&tmp, ":"); db_table = strsep(&tmp, ":"); + db_port = atoi(db_tmp); if (!db_user || !db_pwd || !db_host || !db_tmp || !db_name || !db_table) { + fterr_warnx("Missing field in dbaseURI, expecting user:pwd:host:port:name:table."); - return -1; + + return -1; } } /* dbaseURI */ + ftio_get_ver(ftio, &ftv); fts3rec_compute_offsets(&fo, &ftv); - + /* remove invalid fields */ opt->ft_mask &= ftrec_xfield(&ftv); /* generate the field names once */ fmt_xfields_type(fields, opt->ft_mask); + + if (strlen(db_user)) { strcat(connstring,"user="); strcat(connstring,db_user); strcat(connstring," "); } + if (strlen(db_pwd)) { strcat(connstring,"password="); strcat(connstring,db_pwd); strcat(connstring," "); } + if (strlen(db_name)) { strcat(connstring,"dbname="); strcat(connstring,db_name); strcat(connstring," "); } + if (strlen(db_host)) { strcat(connstring,"host="); strcat(connstring,db_host); strcat(connstring," "); } + if (strlen(db_tmp)) { strcat(connstring,"port="); strcat(connstring,db_tmp); strcat(connstring," "); } - /* open PostgreSQL database */ - conn = PQsetdbLogin(db_host, db_port, (char *) NULL, (char *) NULL, db_name, db_user, db_pwd); + conn = PQconnectdb(connstring); if (PQstatus(conn) == CONNECTION_BAD) fterr_errx(1,"PQsetdbLogin(): %s\n", PQerrorMessage(conn)); /* foreach flow */ + while ((rec = ftio_read(ftio))) { len = fmt_xfields_val(values, rec, &fo, opt->ft_mask, 1); /* form SQL query and execute it */ + if (len) { strcpy (query, "INSERT INTO "); strcat (query, db_table); @@ -929,14 +942,16 @@ strcat (query, values); strcat (query, ")"); - if (debug) +/* if (debug) fprintf(stderr, "field=%s\n val=%s\n query=%s\n", fields, values, query); - +*/ res = PQexec(conn, query); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { PQclear(res); fterr_errx(1,"PQexec(): %s\n", PQerrorMessage(conn)); + } else if (res) { + PQclear(res); } } @@ -1199,10 +1214,10 @@ if (xfields & FT_XFIELD_EXADDR) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->exaddr)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; } @@ -1257,28 +1272,28 @@ if (xfields & FT_XFIELD_SRCADDR) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->srcaddr)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; } if (xfields & FT_XFIELD_DSTADDR) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->dstaddr)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; } if (xfields & FT_XFIELD_NEXTHOP) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->nexthop)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; } @@ -1375,19 +1390,19 @@ if (xfields & FT_XFIELD_PEER_NEXTHOP) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->peer_nexthop)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; } if (xfields & FT_XFIELD_ROUTER_SC) { if (comma) fmt_buf[len++] = ','; - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; len += fmt_ipv4(fmt_buf+len, *((u_int32*)(rec+fo->router_sc)), FMT_JUST_LEFT); - if (quote) fmt_buf[len++] = '"'; + if (quote) fmt_buf[len++] = '\''; comma = 1; }