user567
user567

Reputation: 3862

encode and send data with ASN.1

I want to use socket to send an int from the client to the server with ASN.1 . This is the ASN definition

Message01 ::= SEQUENCE
        {
          number INTEGER  -- inital integer
        }

And this is the C code

INTEGER_t clientNumber;
    printf("Enter a number :\n  "); 
    scanf("int *",&clientNumber);
    Message01_t *message1;

    message1 = calloc(1, sizeof(Message01_t));
    message1->number = clientNumber;
      der_encode(&asn_DEF_Message01, message1, 1, 0);

I got an error with der_encode

warning: passing argument 3 of ‘der_encode’ makes pointer from integer without a cast client.c:117: error: incompatible types in assignment

In the example example ASN.1 they wrote

der_encode(&asn_DEF_Rect, rect,write_stream, ostream);

But I don't understand what is write_stream.

EDIT:

I tried this

static int
write_out(const void *buffer, size_t size, void *app_key) {
     FILE *out_fp = app_key;
    size_t wrote;
    wrote = send( to_server_socket, &buffer, sizeof( buffer ), 0 );

    return (wrote == size) ? 0 : -1;
}

int main ( int argc, char* argv[] )

and

der_encode(&asn_DEF_Message01, message1, write_out, 0);

But I have an error

ndefined symbols for architecture x86_64:
  "_asn_DEF_Message01", referenced from:
      _main in cccwTrYO.o
  "_der_encode", referenced from:
      _main in cccwTrYO.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Upvotes: 1

Views: 467

Answers (1)

zwol
zwol

Reputation: 140758

Wow, this documentation is terrible. But there's a clue in the "rectangle" example ...

/*
 * This is a custom function which writes the
 * encoded output into some FILE stream.
 */
static int
write_out(const void *buffer, size_t size, void *app_key) {
    FILE *out_fp = app_key;
    size_t wrote;

    wrote = fwrite(buffer, 1, size, out_fp);

    return (wrote == size) ? 0 : -1;
}

and then

ec = der_encode(&asn_DEF_Rectangle, rectangle, write_out, fp);

I deduce from this that the write_stream argument is supposed to be a callback function that you write, and the ostream argument is supposed to be application-supplied data which is passed verbatim to the callback. This is a pretty common pattern for callbacks in C.

So you need to write a callback function that writes to your network socket (using write or send), provide that as write_stream, and pass the socket descriptor number as the ostream.

Upvotes: 1

Related Questions