繁体   English   中英

[C] PQfinish(conn)上的[Postgresql]分段错误线程2

[英][C][Postgresql] segmentation fault thread 2 on PQfinish(conn)

当执行PQfinish(conn)时,程序进入隔离错误:

这些是功能:

主要:

int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , *new_sock;
    struct sockaddr_in server , client;
    readFile(PATH); //legge TX ID e EEP mettendoli nella struct

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(10000);

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);


    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        pthread_t sniffer_thread;
        new_sock = malloc( sizeof(new_sock)/*1*/);
        *new_sock = client_sock;

        if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        //Now join the thread , so that we dont terminate before the thread
        //pthread_join( sniffer_thread , NULL);
        puts("Handler assigned");
    }

    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }

    return 0;
}

**connection_handler**
void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[2000];

    //Send some messages to the client
    /*message = "Greetings! I am your connection handler\n";
    write(sock , message , strlen(message));

    message = "Now type something and i shall repeat what you type \n";
    write(sock , message , strlen(message));*/

    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
    {
       //new here stamp message in output
       //chiamata alla funzione che fa il parsing

       client_message[read_size]=0;
       //parsing((unsigned char *)client_message,read_size+1);
       xdump(client_message,read_size+1,stdout);
       dump(client_message,read_size+1);
       //puts(client_message);
       pthread_detach(pthread_self());

    }

    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    //Free the socket pointer
    free(socket_desc);

    return 0;
}

dump:

void dump(/*const*/ unsigned char *data, int length) {

//print di test
printf("NEL DUMP ENTRA\n");
//provvisorio DB
PGconn *conn = PQconnectdb("user=postgres dbname=sensoridb");

    if (PQstatus(conn) == CONNECTION_BAD)
    {

        fprintf(stderr, "Connection to database failed: %s\n",
            PQerrorMessage(conn));

        PQfinish(conn);
        exit(1);
    }
  PGresult *res;
//se la tabella Sensor esiste la butta giu e ne crea un'altra


    unsigned int i;
    static unsigned long pcount = 0;
    char rorg[4];

    // Decode Packet Header
    struct ether_header *eth_header = (struct ether_header *) data;

    printf("\n\n === PACKET %ld HEADER ===\n", pcount);

    printf("\nSource MAC: ");
    for (i = 0; i < 6; ++i) {
        printf("%02x", eth_header->ether_shost[i]);
        if (i < 5) {
            printf(":");
        }
    }

    printf("\nDestination MAC: ");
    unsigned short ethernet_type = ntohs(eth_header->ether_type);
    printf("\nType: %hu\n", ethernet_type); //Why not nthos?

    /*//if (ethernet_type == ETHERTYPE_IP) { //IP Header
        printf("\n == IP HEADER ==\n");
        struct ip *ip_hdr = (struct ip*) (data + sizeof(struct ether_header));
        unsigned int size_ip = ip_hdr->ip_hl * 4; //why no nthos or nthol
        printf("\nip_hdr->ip_hl: %u", ip_hdr->ip_hl); //why no nthos or nthol
        printf("\nIP Version: %u", ip_hdr->ip_v); //why no nthos or nthol
        printf("\nHeader Length: %u", ip_hdr->ip_hl); //why no nthos or nthol
        printf("\nTotal Length: %hu", ntohs(ip_hdr->ip_len)); //?is this right?*/

        // TCP Header //ESP3 Header
         printf("\n== ESP3 Header  ==\n");
         uint8_t *p1;
  p1 = data;
  //0x55
  char syncByte[4];
  sprintf(syncByte, "0x%02X", (uint8_t)*p1);
  printf("||0x55:%u|",(uint8_t) *p1);  
  //u16DataLen
  p1++;
  printf("DataLen:%u|", ntohs(*(uint16_t *)p1));
  int m = ntohs(*(uint16_t *) p1);

  //u8OptionLen
  p1=p1+2;
  printf("OptionLen:%u|",(uint8_t) *p1);
  int n = (uint8_t) *p1;
  //u8Type
  p1++;
  printf("Type:%u|",(uint8_t) *p1);
  uint8_t u8Type = (uint8_t) * p1; 
  //CRC8H
  p1++;
  printf("CRC8H:%u|",(uint8_t) *p1);
  uint8_t crc8h = (uint8_t) * p1;


  //Data
  p1++;
  printf("\n Data: |");
  /*for( i=0;i<m;i++)
  {
   printf ("%u,", (uint8_t) *p1 );
   p1++;
  }*/
  //prendiamo subito il TXID
    char txid[20];
    getTxIdData(p1,txid); //prendiamo il TX ID
    printf("Txid nella parte dati è: %s", txid);
    char eep[20];
    if(strcmp(getEEP(txid,SIZE_OF_IDS,eep),"A5-12-01")==0)//Clamp
    {
    //chiamata alla funzione parsingClampData
     parsingClampData(conn,res,rorg,txid,p1);
     p1=p1+4;
    }

    else if(strcmp(getEEP(txid,SIZE_OF_IDS,eep),"A5-04-01")==0)//TempHum
    {
    //chiamata alla funzione parsingTempHumData
     parsingTempHumData(conn,res,rorg,txid,p1);
     p1=p1+4;
    }
   else
   {
    //implementazione la parte per vedere se il bit è LRNB
    printf("\nPARTE CON UN NUOVO SENSORE ELSE!\n");
    parsingLRNB(conn,res,p1); //nuovo sensore //la parte DBMS da implementare
    p1=p1+4;
   }         
   //TX ID
   p1++;
    printf("TX ID:");
    for( i=0; i<4;i++)
    {
     printf("%02X ", *p1);
     p1++;
    }
   //STATUS
    printf("|STATUS %02x |",*p1 );
    p1++;

  //Optional
   // flag SubTelNum
        unsigned char* SubTelNum;
    printf("|Optional è: ");
    SubTelNum = p1;
        uint8_t stn = (uint8_t) * SubTelNum;
    printf("SubTelNum %u|", (unsigned char) *SubTelNum);
        p1++;
   //flag Destination id
        //unsigned char* ind;
        char ind[50];
        sprintf(ind,"%u:%u:%u:%u",(unsigned char) * p1,(unsigned char) * (p1+1),(unsigned char) * (p1+2),(unsigned char) * (p1+3) );
        printf("Destination id: %s ",ind);

        /*for(i=0;i<4;i++)
        {
         ind = p1;
         printf("%u:", (unsigned char) *ind);
         p1++;
        }*/
        p1 = p1 + 4;
   //flag dBm
        unsigned char* dbm;
        dbm = p1;
        printf("|dBm:- %u ", (unsigned char) *dbm);

   //flag SecurityLevel
        p1++;
        unsigned char* sl;
        sl = p1;
        printf("|SecurityLevel : %u |", (unsigned char) * sl);

   //CRC8D
        p1++;
        unsigned char* crc;
        crc = p1;
        uint8_t crc8d = (uint8_t)* p1; 
        printf("CRC8D: %u\n",  crc8d);
insSensInformationHeader(conn,res,txid, rorg,syncByte,m,n,u8Type,crc8h,crc8d,stn,ind,(unsigned char)* dbm,(unsigned char) *sl );
//print di test
printf("prima di PQFINISH ENTRA\n");

    //chiusura connessione base di dati
    PQclear(res);  
    PQfinish(conn);
//print di test
printf("dopo di PQFINISH ENTRA\n");



        /*struct tcphdr *tcp_hdr = (struct tcphdr*) (data + sizeof(struct ether_header) + size_ip);
        unsigned int size_tcp = tcp_hdr->doff * 4; //why no nthos or nthol
        printf("\n 0x55: %" PRIu8, ntohs(tcp_hdr->th_sport)); 
        printf("\n DataLen: %" PRIu16, ntohs(tcp_hdr->th_dport));
        printf("\n OptionalLen: %" PRIu8, tcp_hdr->fin ); //As this is 1 bit, both nthos or nthol will work
        printf("\n Type: %" PRIu8, tcp_hdr->urg ); //As this is 1 bit, both nthos or nthol will work
        printf("\n CRC8H: %" PRIu8, ntohl(tcp_hdr->ack_seq));

        u_int16_t sourcePort = ntohs(tcp_hdr->th_sport);
        u_int16_t destinationPort = ntohs(tcp_hdr->th_sport);

        if (sourcePort == 80 || destinationPort == 80){
            printf("\n\nPORT 80!!!\n");

            //Transport payload!
            printf("\n\  === TCP PAYLOAD DATA == \n");

            // Decode Packet Data (Skipping over the header)
            unsigned int headers_size = ETH_HLEN + size_ip + size_tcp;
            unsigned int data_bytes = length - headers_size;
            const unsigned char *payload = data + headers_size;

            const static int output_sz = 500; // Output this many bytes at a time
            while (data_bytes > 0) {
                int output_bytes = data_bytes < output_sz ? data_bytes : output_sz;
                // Print data in raw hexadecimal form
                printf("| ");
                // Print data in ascii form
                for (i = 0; i < output_bytes; ++i) {
                    char byte = payload[i];
                    if ( (byte > 31 && byte < 127) || byte == '\n') {
                        // Byte is in printable ascii range
                        printf("%c", byte);  //why no nthos or nthol
                    } else {
                        printf(".");
                    }
                }
                payload += output_bytes;
                data_bytes -= output_bytes;
            }
        }*/

    //}

    pcount++;
}

还有其他功能,如果您想别人,请问我! 问题出在转储中,当它调用PQfinish(conn)时

调试调试:gdb)接下来

Thread 2 "ServGatewayv6" received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x294840538b48287f) at malloc.c:2951
2951    malloc.c: File o directory non esistente.

输出:

A5-12-01 01-85-59-AA
A5-04-01 01-82-B5-13
A5-12-01 01-87-CA-7E
Socket created
bind done
Waiting for incoming connections...
Waiting for incoming connections...
Connection accepted
Handler assigned
55 00 0A 07 01 EB A5 00 00 08 5D 01 85 59 AA 00     U.........]..Y..
01 FF FF FF FF 43 00 90 00                          .....C...       
NEL DUMP ENTRA


 === PACKET 0 HEADER ===

Source MAC: a5:00:00:08:5d:01
Destination MAC: 
Type: 34137

== ESP3 Header  ==
||0x55:85|DataLen:10|OptionLen:7|Type:1|CRC8H:235|
 Data: |TXID: 01-85-59-AA
Txid nella parte dati è: 01-85-59-AA
SIAMO DENTRO GETEEP
quanto vale eep dopo l'assegnamento di no: noTXID dentro GET è : 01-85-59-AA
ident[0] ha txid: 01-85-59-AA, ha eep: A5-12-01
ident[1] ha txid: 01-82-B5-13, ha eep: A5-04-01
ident[2] ha txid: 01-87-CA-7E, ha eep: A5-12-01

OK 01-85-59-AA=01-85-59-AA dovrebbe tornare A5-12-01
NEL parsingClampData ENTRA
RORG 165|32 bit: 000000000000000000001000|0101|1|1|01 Meter Reading 8 Tariff Info 5 LRN: Data Telegram DataType: Current Value W Div:0.800000 NEL insMeasureClamp ENTRA
FINE insMeasureClamp
Stiamo per prendere il valore idMeasureClamp
16
Fine della presa del valore idMeasureClamp
Inizio della presa del valore value da FattoreConversione
220
Fine prendere volt
Inizio fattorePotenza
220
Fine fattorePotenza
pow vale 387200.000000
NEL insPotenza ENTRA
10
NEL insPotFat ENTRA
NEL insPotFat ENTRA
TX ID:01 85 59 AA |STATUS 00 ||Optional è: SubTelNum 1|Destination id: 255:255:255:255 |dBm:- 67 |SecurityLevel : 0 |CRC8D: 144
NEL insSensInformationHeader ENTRA
FINE insSensInformationHeader 
prima di PQFINISH ENTRA
Errore di segmentazione (core dump creato)

我该如何解决? 错误是转储的地方。数据被写入数据库。 它会产生什么细分错误?

PGConn结构将传递到dump函数调用的其他两个函数。 奇怪的是,其中之一(也许是最后一个使用它的)对它有害。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM