Beispiel #1
0
int
lwes_net_recv_bytes
  (struct lwes_net_connection *conn,
   LWES_BYTE_P bytes,
   size_t len)
{
  int ret = 0;
  int flags = 0;

  if (conn == NULL || bytes == NULL)
    {
      return -1;
    }

  if ( (ret = lwes_net_recv_bind (conn)) < 0) 
    {
      return ret;
    }

  ret = recvfrom (conn->socketfd,
                  bytes,
                  len,
                  flags,
                  (struct sockaddr *)&(conn->sender_ip_addr),
                  (socklen_t *)&(conn->sender_ip_socket_size));
  return ret;
}
Beispiel #2
0
static void test_null_args (void)
{
  LWES_BYTE buffer[500];
  unsigned int i;
  struct lwes_net_connection connection;

  /* initialize to nothing */
  for (i = 0; i < 500; i++)
    buffer[i]=(LWES_BYTE)0;

  /* now set 45 bytes */
  for (i = 0; i < 45; i++)
    {
      buffer[i]=(LWES_BYTE)i;
    }


  /* test NULL connection to all calls */

  assert (lwes_net_open (NULL, 
                         (char*)mcast_ip,
                         (char*)mcast_iface,
                         (int)mcast_port) == -1);
  assert (lwes_net_close (NULL) == -1);

  assert (lwes_net_get_ttl (NULL) == -1);

  assert (lwes_net_set_ttl (NULL, 5) == -1);

  assert (lwes_net_get_sock_fd (NULL) == -1);

  assert (lwes_net_send_bytes (NULL, buffer, 45) == -1);
  assert (lwes_net_send_bytes (&connection, NULL, 45) == -1);

  assert (lwes_net_sendto_bytes (NULL,
                                 (char*)mcast_ip,
                                 (char*)mcast_iface,
                                 (int)mcast_port,
                                 buffer,
                                 45) == -1);

  assert (lwes_net_sendto_bytes (&connection,
                                 (char*)mcast_ip,
                                 (char*)mcast_iface,
                                 (int)mcast_port,
                                 NULL,
                                 45) == -1);

  assert (lwes_net_recv_bind (NULL) == -1);

  assert (lwes_net_recv_bytes (NULL, buffer, 500) == -1);
  assert (lwes_net_recv_bytes (&connection, NULL, 500) == -1);

  assert (lwes_net_recv_bytes_by (NULL, buffer, 500, 10000) == -1);
  assert (lwes_net_recv_bytes_by (&connection, NULL, 500, 10000) == -1);
}
Beispiel #3
0
int
lwes_net_recv_bytes_by
  (struct lwes_net_connection *conn, 
   LWES_BYTE_P bytes,
   size_t len,
   unsigned int timeout_ms)
{
  int ret = 0;
  int flags = 0;
  struct timeval timeout;
  fd_set read_sel;

  if (conn == NULL || bytes == NULL)
    {
      return -1;
    }

  if ((ret = lwes_net_recv_bind (conn)) < 0)
    {
      return ret;
    }

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

  FD_ZERO(&read_sel);
  FD_SET(conn->socketfd, &read_sel);

  /* Just wait once, as we *should* get the packet as a chunk */
  ret = select (conn->socketfd+1, &read_sel, NULL, NULL, &timeout);
  if (ret <= 0)
    {
      return -2;
    }

  ret = recvfrom (conn->socketfd,
                  bytes,
                  len,
                  flags,
                  (struct sockaddr *)&(conn->sender_ip_addr),
                  (socklen_t *)&(conn->sender_ip_socket_size));
  return ret;
}
Beispiel #4
0
static void test_socket_function_failures (void)
{
  struct lwes_net_connection connection;
  LWES_BYTE buffer[500];

  /* socket system call failure */
  socket_error = 1;
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface,
                         (int)mcast_port) == -2);
  socket_error = 0;

  /* setsockopt of IP_MULTICAST_IF failure */
  setsockopt_error_when = IP_MULTICAST_IF;
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface2,
                         (int)mcast_port) == -3);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;

  /* setsockopt of SO_SNDBUF failure */
  setsockopt_error_when = SO_SNDBUF;
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface2,
                         (int)mcast_port) == -4);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;

  /* test failures in recv_bind */

  /* need a successful open */
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface,
                         (int)mcast_port) == 0);

  /* First setsockopt SO_REUSEADDR failure */
  setsockopt_error_when = SO_REUSEADDR;
  assert (lwes_net_recv_bind (&connection) == -2);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;
  
  /* Second setsockopt SO_RCVBUF failure */
  setsockopt_error_when = SO_RCVBUF;
  assert (lwes_net_recv_bind (&connection) == -3);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;
  
  /* Third bind () failure */
  bind_error = 1;
  assert (lwes_net_recv_bind (&connection) == -4);
  bind_error = 0;

  /* Fourth IP_ADD_MEMBERSHIP failure */
  setsockopt_error_when = IP_ADD_MEMBERSHIP;
  assert (lwes_net_recv_bind (&connection) == -5);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;

  assert (lwes_net_close (&connection) == 0);

  /* successful so we can test close failure */
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface,
                         (int)mcast_port) == 0);

  assert (lwes_net_recv_bind (&connection) == 0);

  setsockopt_error_when = IP_DROP_MEMBERSHIP;
  assert (lwes_net_close (&connection) == -2);
  setsockopt_error_when = SETSOCKOPT_NO_ERROR;

  assert (lwes_net_close (&connection) == 0);

  /* Also want to test bind failures in recv functions */
  assert (lwes_net_open (&connection,
                         (char*)mcast_ip,
                         (char*)mcast_iface,
                         (int)mcast_port) == 0);
  bind_error = 1;
  assert (lwes_net_recv_bytes (&connection, buffer, 500) == -4);
  assert (lwes_net_recv_bytes_by (&connection, buffer, 500, 10000) == -4);
  bind_error = 0;

  assert (lwes_net_close (&connection) == 0);
}