Search code examples
ccontiki

Copy an ipaddress into the UIP_ICMP_PAYLOAD


I am trying to improve the RPL protocol, and have therefore implemented a new control-message. I am able to send integers, but not ip-addresses. Can anyone help med? I am working in the file rpl-icmp6.c located at contiki/core/net/rpl.

This is my first function, which is receiving the data:

static void
tru_input(void)
{   
   int trustValue;
   uip_ipaddr_t *trustAddr;
   unsigned char *buffer;
   buffer = UIP_ICMP_PAYLOAD;

   trustValue = buffer[0];
   memcpy(&trustAddr, buffer[1], 16);

   PRINT6ADDR(trustAddr);

}

And this is the function that is sending the data:

void
tru_output(uip_ipaddr_t *addr, uip_ipaddr_t *trustAddr, int *trustValue)
{
  unsigned char *buffer;
  buffer = UIP_ICMP_PAYLOAD;
  buffer[0] = &trustValue;
  memcpy(buffer[1], &trustAddr, 16);
  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_TRU, 1);
}

I receive an java.lang.ArrayIndexOutOfBoundsException: -1. Can anybody help me?

EDIT:

This is my new code which works:

static void
tru_input(void)
{   
  int trustValue;
  uip_ipaddr_t trustAddr;
  unsigned char *buffer;

  buffer = UIP_ICMP_PAYLOAD;

  trustValue = buffer[0];
  memcpy(&trustAddr, buffer + 1, 16);

  PRINT6ADDR(trustAddr);
  PRINTF("\n");

 }
 /*---------------------------------------------------------------------------*/
void
tru_output(uip_ipaddr_t *addr, uip_ipaddr_t *trustAddr, int trustValue)
{
  /*Array OF byte: Find out how to enter all the bytes into the PAYLOAD. */
  unsigned char *buffer;
  buffer = UIP_ICMP_PAYLOAD;
  buffer[0] = trustValue;

  memcpy(buffer + 1, trustAddr, 16);
  uip_icmp6_send(addr, ICMP6_RPL, RPL_CODE_TRU, 17);
 }

Solution

  • memcpy(&trustAddr, buffer[1], 16);
    

    You are coping the value pointing to buffer[1] (that is a char) to the value that is pointing trustAddr i think you would like to pass the address of buffer[1]:

    memcpy(&trustAddr, buffer + 1, 16);
    

    The same in tru_output (you don't need to pass the ponter of trustAddr to memcpy, because you already declared it as a pointer:

    memcpy(buffer+1, trustAddr, 16);
    

    I see that you also need to inizialize trustAddr with 16 byte, becouse you are coping memory to a random memory region, so don't declare trustAddr as a pointer:

    uip_ipaddr_t trustAddr;