segmentation fault once sending message with gnokii gn_sms_send

422 views Asked by At

here are the code folks :

#include <stdio.h>
#include <stdlib.h>
#include <gnokii.h>
#include <signal.h>
/*
 * 
 */

#define _(x) x

struct gn_statemachine *state = NULL;

void busterminate(void) {
    gn_lib_phone_close(state);
    gn_lib_phoneprofile_free(&state);
    gn_lib_library_free();
}

void businit(void) {
    gn_error    error;

    atexit(busterminate);

    error = gn_lib_phoneprofile_load(NULL, &state);
    if (GN_ERR_NONE == error) {
        error = gn_lib_phone_open(state);
    }

    if (GN_ERR_NONE != error) {
        fprintf(stderr, "%s\n", gn_error_print(error));
        exit(-1);
    }
}

void signal_handler(int signal) {
    (void)signal;
    exit(-2);
}

int main(int argc, char *argv[]) {
    gn_data     *data;
        gn_sms          sms;
    gn_error    error;


    businit();

    signal(SIGINT, signal_handler);

    gn_data_clear(data);

        sprintf(sms.remote.number,"%s","+628571641111");
        sprintf(sms.user_data[0].u.text,"%s","tesss");

        data->message_center    = calloc(1, sizeof(gn_sms_message_center));
        data->message_center->id= 1;

    error = gn_sm_functions(GN_OP_GetSMSCenter, data, state);
    if(error == GN_ERR_NONE)
    {
        snprintf(sms.smsc.number,sizeof(sms.smsc.number),"%s",data->sms->smsc.number); // set to sms.smsc.number from data.sms.smsc.number
        sms.smsc.type = data->message_center->smsc.type;
        //g_slice_free(gn_sms_message_center,data->message_center); // free the ram
        free(data->message_center);
    }
    if(!sms.smsc.number[0])
    {
        printf("failed once getting sms center number\n");

    }
    if(!sms.smsc.type)
    {
        sms.smsc.type = GN_GSM_NUMBER_Unknown;
    }

     data->sms = &sms;

    //send the message
    error = gn_sms_send(data,state);

    if(error == GN_ERR_NONE)
    {
        if(sms.parts > 1)
        {
            int j;
            printf("sms sent with : %d parts, and reference number is : ", sms.parts);

            for(j=0; j < sms.parts; j++)
            {
                printf("%d\n",sms.reference[j]);
            }
        }
        else
        {
            printf("one sms sent with reference number : %d\n",sms.reference[0]);
        }
    }
    else
    {
        printf("libgnokii error : %s\n",gn_error_print(error));
    }

    free(sms.reference);

    return 0;
}

im gonna send an sms to +628571641111, with the text "tesss", but unfortunately the OS said it segmentation fault, thus, where is my fault ?

$ gnokii --identify
GNOKII Version 0.6.29
IMEI         : 3556XXXXX509XXX
Manufacturer : ZTE INCORPORATED
Model        : MF627
Product name : MF627
Revision     : BD_3GHAP673A4V1.0.0
$ gdb -q ./gnokii_send_sms 
Reading symbols from /root/gnokii_send_sms...(no debugging symbols found)...done.
(gdb) r
Starting program: /root/gnokii_send_sms 
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00317334 in ?? () from /lib/libc.so.6
(gdb) 
1

There are 1 answers

4
ThePosey On BEST ANSWER

You're passing to gn_data_clear a pointer you haven't initialized yet. In the beginning of your main function you need to have

gn_data     data;

Not

gn_data     *data;

Here's the function implementation:

GNOKII_API void gn_data_clear(gn_data *data)
{
        memset(data, 0, sizeof(gn_data));
}