-2

I am receiving a buffer inside this function , I would like to ignore the first character by incrementing the buffer address by one.

I increment the buffer but out side the function the buffer contains the received data but it's not incremented.

It's strange !! could any one help me please !!

int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
                  uint32_t timeout )
{
int ret,recv;
struct timeval tv;
fd_set read_fds;
int fd = ((mbedtls_net_context *) ctx)->fd;

if( fd < 0 )
    return( MBEDTLS_ERR_NET_INVALID_CONTEXT );

FD_ZERO( &read_fds );
FD_SET( fd, &read_fds );

tv.tv_sec  = timeout / 1000;
tv.tv_usec = ( timeout % 1000 ) * 1000;

ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );

/* Zero fds ready means we timed out */
if( ret == 0 )
    return( MBEDTLS_ERR_SSL_TIMEOUT );

if( ret < 0 )
{
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32)
    if( WSAGetLastError() == WSAEINTR )
        return( MBEDTLS_ERR_SSL_WANT_READ );
#else
    if( errno == EINTR )
        return( MBEDTLS_ERR_SSL_WANT_READ );
#endif

    return( MBEDTLS_ERR_NET_RECV_FAILED );
}

/* This call will not block */
recv = mbedtls_net_recv( ctx, buf, len );
buf = buf + 1;
printf("Receiving\n");

return( recv );
}

2 Answers2

1

Like Eugene Sh said, arguments in C are passed by value.

Example :

void Test(int value)
{
  value++;
}

...

int foo = 3;
Test(foo);
// here, foo is still 3

If you want to pass foo by reference in C, you need to pass it's pointer

void Test(int* value)
{
  *value++;
  value++;
}

...

int foo = 3;
int *fooPtr = &foo;
Test(fooPtr);
// Now, foo is 4, but fooPtr still is &foo.

Note that I also incremented the pointer inside the Test() function, but since the pointer itself is passed by value, it's not incremented outside the Test() function.

In order to achieve what you want, you need to pass the pointer by reference (as a pointer) :

void Test(int** value)
{
  **value++;
  *value++;
}

...

int foo = 3;
int *fooPtr = &foo;
Test(&fooPtr);
// Now, foo is 4, and fooPtr was incremented.
// This is just an example ; don't use fooPtr beyond this point, its value is wrong.

You need to pass your buf pointer as a reference to be able to change the pointer value :

int mbedtls_net_recv_timeout( void *ctx, unsigned char **buf, size_t len,
                  uint32_t timeout )
{

  [... snip ...]

  /* This call will not block */
  recv = mbedtls_net_recv( ctx, *buf, len );
  *buf = *buf + 1;
  printf("Receiving\n");

  return( recv );
}
MartinVeronneau
  • 1,296
  • 7
  • 24
0

I think you should increment the pointer buf before passing it to the function 'mbedtls_net_recv' like this,

/* This call will not block */
    buf = buf + 1;
    recv = mbedtls_net_recv( ctx, buf, len );
    printf("Receiving\n");
    return( recv );
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rdssonawane
  • 125
  • 9