Beispiel #1
0
void create_table()
{
	printf("create_table\n");
	SQLHSTMT stmt;
	RETCODE	r;
	char errorMsg[LOG_MSG_WIDTH];
	
	connect_to_database();
	
	/* Allocate a statement handle */
	SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
	
	r = SQLPrepare(stmt, (SQLCHAR *)"CREATE TABLE process_log (process_id bigint NOT NULL, order_id integer NOT NULL, finish_date timestamp, PRIMARY KEY (process_id));", SQL_NTS);
	if (r != SQL_SUCCESS) {
		printf("SQLPrepare: executeSqlStatement failed\n");
	}
	else
	{
		r = SQLExecute(stmt);
		if (!((r == SQL_SUCCESS) || (r == SQL_SUCCESS_WITH_INFO)))
		{
			printf("return value: %d\n", r);
			printf("Error:%s\n", dbErrorMsg(env, dbc, stmt, &errorMsg[0], LOG_MSG_WIDTH));
			printf("SQLExecute: executeSqlStatement failed\n");
		}
	}
	
	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
	disconnect_from_database();
}
ret_t
cherokee_handler_dbslayer_init (cherokee_handler_dbslayer_t *hdl)
{
	ret_t                              ret;
	cherokee_connection_t             *conn  = HANDLER_CONN(hdl);
	cherokee_handler_dbslayer_props_t *props = HANDLER_DBSLAYER_PROPS(hdl);

	/* Check client headers
	 */
	cherokee_client_headers (hdl);

	/* Get a reference to the target host
	 */
	if (hdl->src_ref == NULL) {
		ret = cherokee_balancer_dispatch (props->balancer, conn, &hdl->src_ref);
		if (ret != ret_ok)
			return ret;
	}

	/* Connect to the MySQL server
	 */
	ret = connect_to_database(hdl);
	if (unlikely (ret != ret_ok))
		return ret;

	/* Send query:
	 * Do not check whether it failed, ::step() will do
	 * it and send an error message if needed.
	 */
	send_query(hdl);

	return ret_ok;
}
Beispiel #3
0
int full_connection()
{	
	connect_to_database();
	
	disconnect_from_database();
	
	return 0;
}
Beispiel #4
0
void getCatalogs()
{
	printf("getCatalogs\n");
	SQLHSTMT stmt;
	SQLSMALLINT columns; /* number of columns in result-set */
	SQLCHAR buf[5][64];
	SQLINTEGER indicator[ 5 ];
	int i;
	
	connect_to_database();
	
	/* Allocate a statement handle */
	SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
	/* Retrieve a list of tables */
	
	SQLTables(stmt, (SQLCHAR*)SQL_ALL_CATALOGS, SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS );
	/* How many columns are there */
	
	SQLNumResultCols(stmt, &columns);
	
	/* Loop through the rows in the result-set binding to */
	/* local variables */
	for (i = 0; i < columns; i++) {
		SQLBindCol( stmt, i + 1, SQL_C_CHAR,
					buf[ i ], sizeof( buf[ i ] ), &indicator[ i ] );
	}
	
	/* Fetch the data */
	while (SQL_SUCCEEDED(SQLFetch(stmt))) {
		/* display the results that will now be in the bound area's */
		for ( i = 0; i < columns; i ++ ) {
			if (indicator[ i ] == SQL_NULL_DATA) {
				printf("  Column %u : NULL\n", i);
			}
			else {
				if (strlen((char*)(buf[i])) > 0) {
					printf("  Column %u : %s\n", i, buf[i]);
				} else {
					printf("  Column %u : Empty string\n", i);
				}
			}
		}
	}
	
	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
	disconnect_from_database();
}
// Connect to the database containing customer information.
DatabaseConnection* connect_to_customer_database(void) {
    return connect_to_database("customers.abcd.org", 321);
}
Beispiel #6
0
int main(int argc, char *argv[])
{
    int ret;
    int num_pkts; 
    char *dev = NULL;  
    char errbuf[PCAP_ERRBUF_SIZE]; 

    pcap_t *dev_handle;
    const u_char *packet;
    struct pcap_pkthdr hdr;
    struct ether_header *eptr;

#ifdef WITH_HISTORY
    conn = connect_to_database(LOCALHOST, DATABASE, USER, PW);
#endif

    char *ival = NULL;
    int iflag = 0;
    int c;
    opterr = 0;

    while ((c = getopt(argc, argv, "i:csh")) != -1)
    {
        switch (c)
        {

            /* select interface */
            case 'i':
                ival = optarg;
                iflag = 1;
                break;

            /* clear history table */
            case 'c':
#ifdef WITH_HISTORY
                clear_history(conn);
                exit_nicely("History cleared");
#else
                exit_nicely("History database not in use.");
#endif

            /* show history */
            case 's':
#ifdef WITH_HISTORY
                show_history(conn);
                exit_nicely(NULL);
#else
                exit_nicely("History database not in use.");
#endif
  
            /* print help */
            case 'h':
                fprintf(stdout, "netSnarf Help:\n\tsnarf [OPTION]"
                                "\nOPTIONS\n"
                                "\t-c    "
                                "clear history records (requires database module)\n"
                                "\t-h    "
                                "help, prints cli options\n"
                                "\t-i <interface>    "
                                "selects network interface to use for sniffing\n"
                                "\t-s    "
                                "show history records (requires database module)\n");
                exit_nicely("");

            case '?':
                if (optopt == 'i')
                {
                    fprintf(stderr,
                            "Option -%c requires an argurment.\n", optopt);
                }
                else if (isprint(optopt))
                {
                    fprintf(stderr, "Unknown option '-%c'.\n", optopt);
                }
                else
                {
                    fprintf(stderr, 
                            "Unknown option character '\\x%x'.\n", optopt);
                }
                return 1;

            default:
                fprintf(stdout, "Starting packet sniffer\n");

        }
    }

    /* Select device: wlan if developing, else lookup or supply via cli */
    if (iflag)
        dev = ival;
    else
    {
#ifdef WLAN
        dev = "wlan0";
#else
        dev = pcap_lookupdev(errbuf);
#endif
    }

    if (dev == NULL) error_nicely(errbuf, __LINE__);

    /* Get basic information on capture interface/device */
    device_info(dev, errbuf);

    dev_handle = pcap_open_live(dev, BUFSIZ, 0, -1, errbuf);
    if (!dev_handle) error_nicely(errbuf, __LINE__);

    /* primary packet handling engine */
    pcap_loop(dev_handle, num_pkts, handle_packet, NULL);

    if (conn)
        mysql_close(conn);

    return 0;
}
Beispiel #7
0
void *log_func(void *param) {
  int err;
  struct user_data_log *userdata = param;
  log_entry* ent = NULL;

  connect_to_database(userdata->configs->serv_addr, userdata->configs->username, userdata->configs->passwd);

  while(!need_quit(&mxq)) {
    // TODO add proper polling system
    if((ent = lstack_pop(&log_stack)) != NULL) {
      if(ent->severity >= userdata->log_level) {
        char buffer[20], lls_buffer[10];

        strftime(buffer, 20, "%F %H:%M:%S", localtime(ent->rawtime));
        log_level_string(lls_buffer, ent->severity);

        pthread_mutex_lock(&mxs);
        fprintf(log_stream, "[%s: %s] %s", lls_buffer, buffer, ent->event);
        pthread_mutex_unlock(&mxs);
      }
      if((err = log_to_database(ent)) != 0) {
        if((err != CR_SERVER_GONE_ERROR && err != -1) ||
          (err = connect_to_database(userdata->configs->serv_addr, userdata->configs->username, userdata->configs->passwd)) != 0 ||
          (err = log_to_database (ent)) != 0) {

          pthread_mutex_lock(&mxs);
          fprintf(log_stream, "could not log to database (%d)\n", err);
          pthread_mutex_unlock(&mxs);
        }
      }
      free(ent->rawtime);
      free(ent);
    }

    usleep(50000);
  }

  while((ent = lstack_pop(&log_stack)) != NULL) {
    if(ent->severity >= userdata->log_level) {
      char buffer[20], lls_buffer[10];

      strftime(buffer, 20, "%F %H:%M:%S", localtime(ent->rawtime));
      log_level_string(lls_buffer, ent->severity);

      pthread_mutex_lock(&mxs);
      fprintf(log_stream, "[%s: %s] %s", lls_buffer, buffer, ent->event);
      pthread_mutex_unlock(&mxs);
    }
    if((err = log_to_database(ent)) != 0) {
      if((err != CR_SERVER_GONE_ERROR && err != -1) ||
        (err = connect_to_database(userdata->configs->serv_addr, userdata->configs->username, userdata->configs->passwd)) != 0 ||
        (err = log_to_database (ent)) != 0) {
        const char *error = dblogger_error();

        if(error != NULL) {
          pthread_mutex_lock(&mxs);
          fprintf(log_stream, "could not log to database (%d : %s)\n", err, error);
          pthread_mutex_unlock(&mxs);
        }else {
          pthread_mutex_lock(&mxs);
          fprintf(log_stream, "could not log to database (%d)\n", err);
          pthread_mutex_unlock(&mxs);
        }
      }
    }
    free(ent->rawtime);
    free(ent);
  }

  if((err = disconnect()) != 0) {
    fprintf(log_stream, "error while disconnecting from database (%d)\n", err);
  }
  return NULL;
}