On Thu, 2013-10-03 at 18:14 +0000, Rafael Ratacheski wrote:
> Hello, I'm having troubles in request and save the value of the
> ipNetToMediaPhysAddress OID. 
> 
> My routine get the MAC Adress, but only a half of the value.
> 
> What I'm doing wrong? Can you help me?
> 
> if (vars->type == ASN_OCTET_STR) {
>             u_long *sp = malloc(1+id_len);

What is the value of id_len? I'll guess it is 6 for further analysis.
In that case you have just allocated 7 bytes of memory for a 32-bit
unsigned integer (or a 64-bit one - even worse...)

>             memcpy(sp, vars->val.string, id_len);

You then copy the six bytes of the mac address to the newly allocated
memory. This is actually correct, but rather odd given the types.

>             const char *teste = htonl(*sp);

And then you assign the byte swapped value of the first four octets of
the mac address to the pointer variable teste. This is undefined
according to the C standard.

>             printf("value #%d is a string: %x \n", count++, teste);


Then you print the address that teste refers to, 

>             free(sp);

Finally you frees the allocated memory. Good.

> }
> 

So, to answer your questions:

> What am I doing wrong?

Just about everything.

> Can you help me?

Well...
I would write that snippet as

if (vars->type == ASN_OCTET_STR) {
    int i;
    printf("value #%d is a string: ", count++);
    for (i = 0; i < vars->val_len; ++i)
        printf("%x", vars->val.string[i]);
    puts("");
}

(Ok, I probably would put it in a char buffer and print it all at once
rather than repeatedly call printf but this is the easy version)

... but that probably isn't what you really wanted to know.

/MF


------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to