c中mysql_query中的分段错误

时间:2018-03-30 05:33:26

标签: mysql c segmentation-fault

我正在更新两个表:tcp_packet和icmp_packet。更新tcp_packet表是完美的,但是我在更新mysql_query函数中的icmp_packet表时遇到了Segmentation错误。有人请帮助我。

这是update_tcp_packet.c文件:

        //This function updates the tcp_packet table periodically.

    #include<my_global.h>
    #include<mysql.h>

    #include "store_db.h"

    //
    //This is the error function for errors found in
    //the update_tcp_packet() and send_tcp_query()
    //functions.
    //
    //Parameters: dbConn: The connection handler to MySQL
    //
    //Return Value: 0

    int error_sql(MYSQL* dbConn)
    {
        printf("Error: %s\n", mysql_error(dbConn));
        mysql_close(dbConn);
        return 0;
    }

    int getTupleNum(MYSQL* dbConn, char* table_name)
    {
      char query[100];
      sprintf(query, "SELECT * FROM %s;", table_name);

      int len = strlen(query);

      //The query is called here
      if(mysql_real_query(dbConn, (const char*)query, len)!=0)
        error_sql(dbConn);


      //The result of the query is captured
      MYSQL_RES* result = mysql_store_result(dbConn);

      if(result==NULL)
        error_sql(dbConn);

       int num_rows = 0;

      //We are finding the number of rows in the 'result'
      //table.
      while(mysql_fetch_row(result))
        num_rows++;// break;

      return ++num_rows;
    }

    // Function to send the query to MySQL, to create instances of the
    // tcp_packet table
    //
    // Parameters: query: The query for MySQL in the form of a string.
    //             dbConn: The connection handler of MySQL.
    //             SerNo: The tuple number of the new tuple (Primary key).
    //             ip_src: The source ip address.
    //             ip_dst: The destination ip address.
    //             ether_src: The source ethernet address.
    //             ether_dst: The destination ethernet address.
    //             protocol: The protocol used by the packet.
    //             length: The length of the packet.
    //             timestamp: The time of capture of packet.
    //
    // Return Value: If successful, returns 1. Else, returns 0;

    int send_tcp_query(char* query, MYSQL* dbConn, int SerNo, char* buff)
    {

      char* timestamp = strtok(buff, ",");
      int length = atoi(strtok(NULL, ","));
      char* ether_src = strtok(NULL, ",");
      char* ether_dst = strtok(NULL, ",");
      char* protocol3 = strtok(NULL, ",");
      char* ip_src = strtok(NULL, ",");
      char* ip_dst = strtok(NULL, ",");
      char* protocol4 = strtok(NULL, ",");
      int port_src = atoi(strtok(NULL, ","));
      int port_dst = atoi(strtok(NULL, "\n"));

      if(sprintf(query, "INSERT INTO tcp_packet(SerNo, timestamp, length, \
    ether_src, ether_dst, protocol3, ip_src, ip_dst, protocol4,\
    port_src, port_dst) VALUES (%d, '%s', %d, '%s', '%s' \
    , '%s', '%s', '%s', '%s', %d, %d);", \
    SerNo, timestamp, length, ether_src, ether_dst, \
    protocol3, ip_src, ip_dst, protocol4, port_src, \
    port_dst)==0)
        return error_sql(dbConn);

        //printf("%s\n", query);

      if(mysql_query(dbConn, (const char*)query)!=0)
        return error_sql(dbConn);

       strcpy(query, "\0");

      return 1;
    }

    //Function to update the tcp_packet table of
    //packet_analyser database
    //
    //Parameters: csv_file: The file name of .csv file where the
    //                      packet information are stored.
    //            dbConn: The connection handler of MYSQL
    //            tup_no: The value of the last tuple
    //
    //Returm value: If successful, returns the row value of
    //              the last added tuple. Else, returns 0
    //

    int update_tcp_packet(char* csv_file, MYSQL* dbConn, int tup_no)
    {
        FILE* csv_file_des = fopen(csv_file, "r");
        char buff[1024], query[1024];

        if(tup_no==-1)
          tup_no = getTupleNum(dbConn, "tcp_packet");

         while(fgets(buff, sizeof(buff), (FILE*)csv_file_des) != NULL)
            if(send_tcp_query(query, dbConn, tup_no++, buff)==0)
                return 0;

        return tup_no;
    }

这是update_icmp_file.c文件:

        //This function updates the icmp_packet table periodically.

    #include<my_global.h>
    #include<mysql.h>

    #include "store_db.h"

    // Function to send the query to MySQL, to create instances of the
    // icmp_packet table
    //
    // Parameters: query: The query for MySQL in the form of a string.
    //             dbConn: The connection handler of MySQL.
    //             SerNo: The tuple number of the new tuple (Primary key).
    //             type:
    //             seq: The sequence number of the packet.
    //             ip_gateway: The IP address of the gateway.
    //
    // Return Value: If successful, returns 1. Else, returns 0;

    int send_icmp_query(char* query, MYSQL* dbConn, int SerNo, char* buff)
    {
      char* type = strtok(buff, ",");
      int seq = atoi(strtok(NULL, ","));
      char* ip_gateway = strtok(NULL, "\n");

      if(sprintf(query, "INSERT INTO icmp_packet(SerNo, type, seq, ip_gateway) \
    VALUES (%d, '%s', %d, '%s');", SerNo, type, seq, ip_gateway)==0)
        return error_sql(dbConn);

        printf("%s\n", query);

      if(mysql_query(dbConn, (const char*)query)!=0)
        return error_sql(dbConn);

      strcpy(query, "\0");

      return 1;
    }

    //Function to update the icmp_packet table of
    //packet_analyser database
    //
    //Parameters: csv_file: The file name of .csv file where the
    //                      packet information are stored.
    //            dbConn: The connection handler of MYSQL
    //            tup_no: The value of the last tuple
    //
    //Returm value: If successful, returns the row value of
    //              the last added tuple. Else, returns 0
    //

    int update_icmp_packet(char* csv_file, MYSQL* dbConn, int tup_no)
    {
        FILE* csv_file_des = fopen(csv_file, "r");
        char buff[1024], query[1024];

        if(tup_no==-1)
          tup_no = getTupleNum(dbConn, "icmp_packet");

        while(fgets(buff, 1024, (FILE*)csv_file_des) != NULL)
            if(send_icmp_query(query, dbConn, tup_no++, buff)==0)
                return 0;

        return tup_no;
    }

这是他们的头文件。

        #ifndef __STORE_DB_H__
    #define __STORE_DB_H__

    #include <my_global.h>
    #include <mysql.h>

    extern MYSQL* initDB(char* user, char* passwd);

    extern int update_pcap_table(char* filename, MYSQL* dbConn, int tup_no);

    extern int update_tcp_packet(char* csv_file, MYSQL* dbConn, int tup_no);

    extern int update_icmp_packet(char* csv_file, MYSQL* dbConn, int tup_no);

    extern int error_sql(MYSQL* dbConn);

    extern int getTupleNum(MYSQL* dbConn, char* table_name);

    #endif

0 个答案:

没有答案