예제 #1
0
/* Test that we can control listen/select on ipv6/v4 and listen on both by default */
static void test_ipv4_ipv6(test_t *t) {
  test_proactor_t tps[] ={ test_proactor(t, open_close_handler), test_proactor(t, listen_handler) };
  pn_proactor_t *client = tps[0].proactor, *server = tps[1].proactor;

  /* Listen on all interfaces for IPv4 only. */
  pn_listener_t *l4 = test_listen(&tps[1], "0.0.0.0");
  TEST_PROACTORS_DRAIN(tps);

  /* Empty address listens on both IPv4 and IPv6 on all interfaces */
  pn_listener_t *l = test_listen(&tps[1], "");
  TEST_PROACTORS_DRAIN(tps);

#define EXPECT_CONNECT(LISTENER, HOST) do {                             \
    char addr[1024];                                                    \
    pn_proactor_addr(addr, sizeof(addr), HOST, listener_info(LISTENER).port); \
    pn_proactor_connect2(client, NULL, NULL, addr);                     \
    TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));  \
    TEST_COND_EMPTY(t, last_condition);                                 \
    TEST_PROACTORS_DRAIN(tps);                                          \
  } while(0)

  EXPECT_CONNECT(l4, "127.0.0.1"); /* v4->v4 */
  EXPECT_CONNECT(l4, "");          /* local->v4*/

  EXPECT_CONNECT(l, "127.0.0.1"); /* v4->all */
  EXPECT_CONNECT(l, "");          /* local->all */

  /* Listen on ipv6 loopback, if it fails skip ipv6 tests.

     NOTE: Don't use the unspecified address "::" here - ipv6-disabled platforms
     may allow listening on "::" without complaining. However they won't have a
     local ipv6 loopback configured, so "::1" will force an error.
  */
  TEST_PROACTORS_DRAIN(tps);
  pn_listener_t *l6 = pn_listener();
  pn_proactor_listen(server, l6, "::1:0", 4);
  pn_event_type_t e = TEST_PROACTORS_RUN(tps);
  if (e == PN_LISTENER_OPEN && !pn_condition_is_set(last_condition)) {
    TEST_PROACTORS_DRAIN(tps);

    EXPECT_CONNECT(l6, "::1"); /* v6->v6 */
    EXPECT_CONNECT(l6, "");    /* local->v6 */
    EXPECT_CONNECT(l, "::1");  /* v6->all */

    pn_listener_close(l6);
  } else  {
    const char *d = pn_condition_get_description(last_condition);
    TEST_LOGF(t, "skip IPv6 tests: %s %s", pn_event_type_name(e), d ? d : "no condition");
  }

  pn_listener_close(l);
  pn_listener_close(l4);
  TEST_PROACTORS_DESTROY(tps);
}
예제 #2
0
/* Test pn_proactor_disconnect */
static void test_disconnect(test_t *t) {
  test_proactor_t tps[] ={ test_proactor(t, open_wake_handler), test_proactor(t, listen_handler) };
  pn_proactor_t *client = tps[0].proactor, *server = tps[1].proactor;

  /* Start two listeners */
  pn_listener_t *l = test_listen(&tps[1], "");
  pn_listener_t *l2 = test_listen(&tps[1], "");

  /* Only wait for one connection to remote-open before disconnect */
  pn_connection_t *c = pn_connection();
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_connection_t *c2 = pn_connection();
  pn_proactor_connect2(client, c2, NULL, listener_info(l2).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_PROACTORS_DRAIN(tps);

  /* Disconnect the client proactor */
  pn_condition_t *cond = pn_condition();
  pn_condition_set_name(cond, "test-name");
  pn_condition_set_description(cond, "test-description");
  pn_proactor_disconnect(client, cond);
  /* Verify expected client side first */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, test_proactors_run(&tps[0], 1));
  TEST_COND_NAME(t, "test-name", last_condition);
  TEST_COND_DESC(t, "test-description", last_condition);
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, test_proactors_run(&tps[0], 1));
  TEST_COND_NAME(t, "test-name", last_condition);
  TEST_COND_DESC(t, "test-description", last_condition);
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, test_proactors_run(&tps[0], 1));

  /* Now check server sees the disconnects */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));

  /* Now disconnect the server end (the listeners) */
  pn_proactor_disconnect(server, cond);
  pn_condition_free(cond);

  TEST_ETYPE_EQUAL(t, PN_LISTENER_CLOSE, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_LISTENER_CLOSE, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));

  /* Make sure the proactors are still functional */
  pn_listener_t *l3 = test_listen(&tps[1], "");
  pn_proactor_connect2(client, NULL, NULL, listener_info(l3).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_proactor_disconnect(client, NULL);

  TEST_PROACTORS_DRAIN(tps);
  TEST_PROACTORS_DESTROY(tps);
}
예제 #3
0
/* Test waking up a connection that is idle */
static void test_connection_wake(test_t *t) {
  test_proactor_t tps[] =  { test_proactor(t, open_wake_handler), test_proactor(t,  listen_handler) };
  pn_proactor_t *client = tps[0].proactor;
  pn_listener_t *l = test_listen(&tps[1], "");

  pn_connection_t *c = pn_connection();
  pn_incref(c);                 /* Keep a reference for wake() after free */
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_CHECK(t, pn_proactor_get(client) == NULL); /* Should be idle */
  pn_connection_wake(c);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_WAKE, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps)); /* Both ends */
  /* The pn_connection_t is still valid so wake is legal but a no-op */
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_EVENT_NONE, TEST_PROACTORS_GET(tps)); /* No more wake */

  /* Verify we don't get a wake after close even if they happen together */
  pn_connection_t *c2 = pn_connection();
  pn_proactor_connect2(client, c2, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_connection_wake(c2);
  pn_proactor_disconnect(client, NULL);
  pn_connection_wake(c2);

  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, test_proactors_run(&tps[0], 1));
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, test_proactors_run(&tps[0], 1));
  TEST_ETYPE_EQUAL(t, PN_EVENT_NONE, test_proactors_get(&tps[0], 1)); /* No late wake */

  TEST_PROACTORS_DESTROY(tps);
  /* The pn_connection_t is still valid so wake is legal but a no-op */
  pn_connection_wake(c);
  pn_decref(c);
}
예제 #4
0
/* Verify that pn_transport_close_head/tail aborts a connection without an AMQP protocol close */
static void test_refuse(test_t *t) {
  test_proactor_t tps[] = { test_proactor(t, open_close_handler), test_proactor(t, listen_refuse_handler) };
  pn_proactor_t *client = tps[0].proactor;
  pn_listener_t *l = test_listen(&tps[1], "");
  pn_proactor_connect2(client, NULL, NULL, listener_info(l).connect);

  /* client transport closes */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps)); /* client */
  TEST_COND_NAME(t, "amqp:connection:framing-error", last_condition);

  pn_listener_close(l);
  while (TEST_PROACTORS_RUN(tps) != PN_PROACTOR_INACTIVE) {}
  while (TEST_PROACTORS_RUN(tps) != PN_PROACTOR_INACTIVE) {}

  /* Verify expected event sequences, no unexpected events */
  TEST_HANDLER_EXPECT(
    &tps[0].handler,
    PN_CONNECTION_INIT, PN_CONNECTION_LOCAL_OPEN, PN_CONNECTION_BOUND,
    PN_TRANSPORT_TAIL_CLOSED, PN_TRANSPORT_ERROR, PN_TRANSPORT_HEAD_CLOSED, PN_TRANSPORT_CLOSED,
    PN_PROACTOR_INACTIVE,
    0);

  TEST_HANDLER_EXPECT(
    &tps[1].handler,
    PN_LISTENER_OPEN, PN_LISTENER_ACCEPT,
    PN_CONNECTION_INIT, PN_CONNECTION_BOUND,
    PN_TRANSPORT_TAIL_CLOSED, PN_TRANSPORT_ERROR, PN_TRANSPORT_HEAD_CLOSED, PN_TRANSPORT_CLOSED,
    PN_LISTENER_CLOSE,
    PN_PROACTOR_INACTIVE,
    0);

  TEST_PROACTORS_DESTROY(tps);
}
예제 #5
0
/* Make sure we clean up released connections and open sockets correctly */
static void test_release_free(test_t *t) {
  test_proactor_t tps[] = { test_proactor(t, open_wake_handler), test_proactor(t, listen_handler) };
  pn_proactor_t *client = tps[0].proactor;
  pn_listener_t *l = test_listen(&tps[1], "");

  /* leave one connection to the proactor  */
  pn_proactor_connect2(client, NULL, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));

  /* release c1 and free immediately */
  pn_connection_t *c1 = pn_connection();
  pn_proactor_connect2(client, c1, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_proactor_release_connection(c1); /* We free but socket should still be cleaned up */
  pn_connection_free(c1);
  TEST_CHECK(t, pn_proactor_get(client) == NULL); /* Should be idle */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps)); /* Server closed */

  /* release c2 and but don't free till after proactor free */
  pn_connection_t *c2 = pn_connection();
  pn_proactor_connect2(client, c2, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_proactor_release_connection(c2);
  TEST_CHECK(t, pn_proactor_get(client) == NULL); /* Should be idle */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps)); /* Server closed */

  TEST_PROACTORS_DESTROY(tps);
  pn_connection_free(c2);

  /* Check freeing a listener or connection that was never given to a proactor */
  pn_listener_free(pn_listener());
  pn_connection_free(pn_connection());
}
예제 #6
0
/* Test simple client/server connection with 2 proactors */
static void test_client_server(test_t *t) {
  test_proactor_t tps[] ={ test_proactor(t, open_close_handler), test_proactor(t, common_handler) };
  pn_listener_t *l = test_listen(&tps[1], "");
  /* Connect and wait for close at both ends */
  pn_proactor_connect2(tps[0].proactor, NULL, NULL, listener_info(l).connect);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);  
  TEST_PROACTORS_DESTROY(tps);
}
예제 #7
0
/* Test various SSL connections between proactors*/
static void test_ssl(test_t *t) {
  if (!pn_ssl_present()) {
    TEST_LOGF(t, "Skip SSL test, no support");
    return;
  }

  test_proactor_t tps[] ={ test_proactor(t, ssl_client_handler), test_proactor(t, ssl_server_handler) };
  test_proactor_t *client = &tps[0], *server = &tps[1];
  pn_ssl_domain_t *cd = client->handler.ssl_domain = pn_ssl_domain(PN_SSL_MODE_CLIENT);
  pn_ssl_domain_t *sd =  server->handler.ssl_domain = pn_ssl_domain(PN_SSL_MODE_SERVER);
  TEST_CHECK(t, 0 == SET_CREDENTIALS(sd, "tserver"));
  pn_listener_t *l = test_listen(server, "");

  /* Basic SSL connection */
  pn_proactor_connect2(client->proactor, NULL, NULL, listener_info(l).connect);
  /* Open ok at both ends */
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_COND_EMPTY(t, last_condition);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_COND_EMPTY(t, last_condition);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);

  /* Verify peer with good hostname */
  TEST_INT_EQUAL(t, 0, pn_ssl_domain_set_trusted_ca_db(cd, CERTIFICATE("tserver")));
  TEST_INT_EQUAL(t, 0, pn_ssl_domain_set_peer_authentication(cd, PN_SSL_VERIFY_PEER_NAME, NULL));
  pn_connection_t *c = pn_connection();
  pn_connection_set_hostname(c, "test_server");
  pn_proactor_connect2(client->proactor, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_COND_EMPTY(t, last_condition);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  TEST_COND_EMPTY(t, last_condition);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_TRANSPORT_CLOSED);

  /* Verify peer with bad hostname */
  c = pn_connection();
  pn_connection_set_hostname(c, "wrongname");
  pn_proactor_connect2(client->proactor, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_COND_NAME(t, "amqp:connection:framing-error",  last_condition);
  TEST_COND_DESC(t, "SSL",  last_condition);
  TEST_PROACTORS_DRAIN(tps);

  pn_ssl_domain_free(cd);
  pn_ssl_domain_free(sd);
  TEST_PROACTORS_DESTROY(tps);
}
예제 #8
0
static void test_netaddr(test_t *t) {
  test_proactor_t tps[] ={ test_proactor(t, open_wake_handler), test_proactor(t, listen_handler) };
  pn_proactor_t *client = tps[0].proactor;
  /* Use IPv4 to get consistent results all platforms */
  pn_listener_t *l = test_listen(&tps[1], "127.0.0.1");
  pn_connection_t *c = pn_connection();
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  if (!TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps))) {
    TEST_COND_EMPTY(t, last_condition); /* Show the last condition */
    return;                     /* don't continue if connection is closed */
  }

  /* client remote, client local, server remote and server local address strings */
  char cr[1024], cl[1024], sr[1024], sl[1024];

  pn_transport_t *ct = pn_connection_transport(c);
  const pn_netaddr_t *na = pn_transport_remote_addr(ct);
  pn_netaddr_str(na, cr, sizeof(cr));
  TEST_STR_IN(t, listener_info(l).port, cr); /* remote address has listening port */

  pn_connection_t *s = last_accepted; /* server side of the connection */

  pn_transport_t *st = pn_connection_transport(s);
  if (!TEST_CHECK(t, st)) return;
  pn_netaddr_str(pn_transport_local_addr(st), sl, sizeof(sl));
  TEST_STR_EQUAL(t, cr, sl);  /* client remote == server local */

  pn_netaddr_str(pn_transport_local_addr(ct), cl, sizeof(cl));
  pn_netaddr_str(pn_transport_remote_addr(st), sr, sizeof(sr));
  TEST_STR_EQUAL(t, cl, sr);    /* client local == server remote */

  char host[MAX_STR] = "";
  char serv[MAX_STR] = "";
  int err = pn_netaddr_host_port(na, host, sizeof(host), serv, sizeof(serv));
  TEST_CHECK(t, 0 == err);
  TEST_STR_EQUAL(t, "127.0.0.1", host);
  TEST_STR_EQUAL(t, listener_info(l).port, serv);

  /* Make sure you can use NULL, 0 to get length of address string without a crash */
  size_t len = pn_netaddr_str(pn_transport_local_addr(ct), NULL, 0);
  TEST_CHECKF(t, strlen(cl) == len, "%d != %d", strlen(cl), len);

  TEST_PROACTORS_DRAIN(tps);
  TEST_PROACTORS_DESTROY(tps);
}
예제 #9
0
/* Test that INACTIVE event is generated when last connections/listeners closes. */
static void test_inactive(test_t *t) {
  test_proactor_t tps[] =  { test_proactor(t, open_wake_handler), test_proactor(t, listen_handler) };
  pn_proactor_t *client = tps[0].proactor, *server = tps[1].proactor;

  /* Listen, connect, disconnect */
  pn_listener_t *l = test_listen(&tps[1], "");
  pn_connection_t *c = pn_connection();
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_connection_wake(c);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_WAKE, TEST_PROACTORS_RUN(tps));
  /* Expect TRANSPORT_CLOSED from client and server, INACTIVE from client */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));

  /* Immediate timer generates INACTIVE on client (no connections) */
  pn_proactor_set_timeout(client, 0);
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_TIMEOUT, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));

  /* Connect, set-timer, disconnect */
  pn_proactor_set_timeout(client, 1000000);
  c = pn_connection();
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_REMOTE_OPEN, TEST_PROACTORS_RUN(tps));
  pn_connection_wake(c);
  TEST_ETYPE_EQUAL(t, PN_CONNECTION_WAKE, TEST_PROACTORS_RUN(tps));
  /* Expect TRANSPORT_CLOSED from client and server */
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_TRANSPORT_CLOSED, TEST_PROACTORS_RUN(tps));
  /* No INACTIVE till timer is cancelled */
  TEST_CHECK(t, pn_proactor_get(server) == NULL);
  pn_proactor_cancel_timeout(client);
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));

  /* Server won't be INACTIVE until listener is closed */
  TEST_CHECK(t, pn_proactor_get(server) == NULL);
  pn_listener_close(l);
  TEST_ETYPE_EQUAL(t, PN_LISTENER_CLOSE, TEST_PROACTORS_RUN(tps));
  TEST_ETYPE_EQUAL(t, PN_PROACTOR_INACTIVE, TEST_PROACTORS_RUN(tps));

  TEST_PROACTORS_DESTROY(tps);
}
예제 #10
0
/* Test sending/receiving a message in chunks */
static void test_message_stream(test_t *t) {
  test_proactor_t tps[] ={
    test_proactor(t, message_stream_handler),
    test_proactor(t, message_stream_handler)
  };
  pn_proactor_t *client = tps[0].proactor;
  pn_listener_t *l = test_listen(&tps[1], "");
  struct message_stream_context ctx = { 0 };
  tps[0].handler.context = &ctx;
  tps[1].handler.context = &ctx;

  /* Encode a large (not very) message to send in chunks */
  char *body = (char*)malloc(BODY);
  memset(body, 'x', BODY);
  pn_message_t *m = pn_message();
  pn_data_put_binary(pn_message_body(m), pn_bytes(BODY, body));
  free(body);
  ctx.size = message_encode(m, &ctx.send_buf);
  pn_message_free(m);

  pn_connection_t *c = pn_connection();
  pn_proactor_connect2(client, c, NULL, listener_info(l).connect);
  pn_session_t *ssn = pn_session(c);
  pn_session_open(ssn);
  pn_link_t *snd = pn_sender(ssn, "x");
  pn_link_open(snd);
  TEST_PROACTORS_RUN_UNTIL(tps, PN_LINK_FLOW);

  /* Send and receive the message in chunks */
  do {
    pn_connection_wake(c);      /* Initiate send/receive of one chunk */
    do {                        /* May be multiple receives for one send */
      TEST_PROACTORS_RUN_UNTIL(tps, PN_DELIVERY);
    } while (ctx.received < ctx.sent);
  } while (!ctx.complete);
  TEST_CHECK(t, ctx.received == ctx.size);
  TEST_CHECK(t, ctx.sent == ctx.size);
  TEST_CHECK(t, !memcmp(ctx.send_buf.start, ctx.recv_buf.start, ctx.size));

  free(ctx.send_buf.start);
  free(ctx.recv_buf.start);
  TEST_PROACTORS_DESTROY(tps);
}
예제 #11
0
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk1, clt_sk2, peeloff_sk;
	sctp_assoc_t svr_associd1, svr_associd2, clt_associd1, clt_associd2; 
	struct iovec iov;
	struct msghdr inmessage;
	int error, i;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	int flags;
	struct sockaddr_in svr_loop[NUMADDR];
	struct sockaddr_in svr_try[NUMADDR];
	struct sockaddr_in clt_loop1[NUMADDR];
	struct sockaddr_in clt_loop2[NUMADDR];
	struct sockaddr_in clt_loop3[NUMADDR];
	sockaddr_storage_t svr_test[NUMADDR], clt_test1[NUMADDR], clt_test2[NUMADDR];

	/* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	for (i = 0; i < NUMADDR; i++) {
		/* Initialize the server and client addresses. */ 
		svr_loop[i].sin_family = AF_INET;
		svr_loop[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		svr_loop[i].sin_port = htons(SCTP_TESTPORT_1);
		svr_test[i].v4.sin_family = AF_INET;
		svr_test[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		svr_test[i].v4.sin_port = htons(SCTP_TESTPORT_1);
		svr_try[i].sin_family = AF_INET;
		if (i < (NUMADDR-1)) {
			svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		} else {
			/* Make last address invalid. */
			svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x400);
		}
		svr_try[i].sin_port = htons(SCTP_TESTPORT_1);
		clt_loop1[i].sin_family = AF_INET;
		clt_loop1[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
		clt_loop1[i].sin_port = htons(SCTP_TESTPORT_2);
		clt_test1[i].v4.sin_family = AF_INET;
		clt_test1[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
		clt_test1[i].v4.sin_port = htons(SCTP_TESTPORT_2);
		clt_loop2[i].sin_family = AF_INET;
		clt_loop2[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
		clt_loop2[i].sin_port = htons(SCTP_TESTPORT_2+1);
		clt_test2[i].v4.sin_family = AF_INET;
		clt_test2[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
		clt_test2[i].v4.sin_port = htons(SCTP_TESTPORT_2+1);
		clt_loop3[i].sin_family = AF_INET;
		clt_loop3[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x300);
		clt_loop3[i].sin_port = htons(SCTP_TESTPORT_2+2);
	}

	/* Create and bind the server socket.  */
	svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(svr_sk, (struct sockaddr *)&svr_loop[0], sizeof(svr_loop[0]));
	test_bindx_add(svr_sk, (struct sockaddr *)&svr_loop[1], NUMADDR-1);

	/* Mark server socket as being able to accept new associations.  */
	test_listen(svr_sk, 1);

	/* Create and bind the client sockets.  */
	clt_sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(clt_sk1, (struct sockaddr *)&clt_loop1[0], sizeof(clt_loop1));
	test_bindx_add(clt_sk1, (struct sockaddr *)&clt_loop1[1], NUMADDR-1);
	clt_sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(clt_sk2, (struct sockaddr *)&clt_loop2[0], sizeof(clt_loop2));
	test_bindx_add(clt_sk2, (struct sockaddr *)&clt_loop2[1], NUMADDR-1);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(svr_sk);
	test_enable_assoc_change(clt_sk1);
	test_enable_assoc_change(clt_sk2);

	/* Set clt_sk1 as non-blocking. */
	flags = fcntl(clt_sk1, F_GETFL, 0);
	if (flags < 0)
		tst_brkm(TBROK, tst_exit, "fcntl F_GETFL: %s", strerror(errno));
	if (fcntl(clt_sk1, F_SETFL, flags | O_NONBLOCK) < 0)
		tst_brkm(TBROK, tst_exit, "fcntl F_SETFL: %s", strerror(errno));

	/* Do a non-blocking connectx from clt_sk1 to svr_sk */      
	error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
	/* Non-blocking connectx should return immediately with EINPROGRESS. */
	if ((error != -1) || (EINPROGRESS != errno))
		tst_brkm(TBROK, tst_exit, "non-blocking connectx error: %d"
			 "errno:%d", error, errno);

	tst_resm(TPASS, "non-blocking connectx");

	/* Doing a connectx on a socket to create an association that is
	 * is already established should return EISCONN.
	 */
	error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
	if ((error != -1) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connectx on a socket to create an "
			 "assoc that is already established error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "connectx on a socket to create an assoc that is "
		 "already established");

	/* Initialize inmessage for all receives. */
	memset(&inmessage, 0, sizeof(inmessage));
	big_buffer = test_malloc(REALLY_BIG);
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = NULL;

	/* Get COMM_UP on clt_sk1 */
	error = test_recvmsg(clt_sk1, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	sac = (struct sctp_assoc_change *)iov.iov_base;
	clt_associd1 = sac->sac_assoc_id;

	/* Get COMM_UP on svr_sk */
	error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	sac = (struct sctp_assoc_change *)iov.iov_base;
	svr_associd1 = sac->sac_assoc_id;

	/* Do a blocking connectx from clt_sk2 to svr_sk. 
	 * Blocking connectx should block until the association is established
	 * and return success.
	 */
	test_connectx(clt_sk2, (struct sockaddr *)svr_try, NUMADDR);

	/* Get COMM_UP on clt_sk2 */
	error = test_recvmsg(clt_sk2, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	sac = (struct sctp_assoc_change *)iov.iov_base;
	clt_associd2 = sac->sac_assoc_id;

	/* Get COMM_UP on svr_sk */
	error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	sac = (struct sctp_assoc_change *)iov.iov_base;
	svr_associd2 = sac->sac_assoc_id;

	tst_resm(TPASS, "blocking connectx");

	peeloff_sk = test_sctp_peeloff(svr_sk, svr_associd1); 

	/* Doing a connectx on a peeled off socket should fail. */
	error = sctp_connectx(peeloff_sk, (struct sockaddr *)clt_loop3, NUMADDR);
	if ((error != -1) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connectx on a peeled off socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connectx on a peeled off socket");

	/* Trying to create an association on a socket that matches an 
	 * existing peeled-off association should fail.
	 */
	error = sctp_connectx(svr_sk, (struct sockaddr *)clt_loop1, NUMADDR);
	if ((error != -1) || (EADDRNOTAVAIL != errno))
		tst_brkm(TBROK, tst_exit, "connectx to create an assoc that "
			 "matches a peeled off assoc error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "connectx to create an assoc that matches a peeled off "
		 "assoc");

	test_peer_addr(peeloff_sk, svr_associd1, clt_test1, NUMADDR);
	tst_resm(TPASS, "server association 1 peers ok");
	test_peer_addr(svr_sk, svr_associd2, clt_test2, NUMADDR);
	tst_resm(TPASS, "server association 2 peers ok");
	test_peer_addr(clt_sk1, clt_associd1, svr_test, NUMADDR);
	tst_resm(TPASS, "client association 1 peers ok");
	test_peer_addr(clt_sk2, clt_associd2, svr_test, NUMADDR);
	tst_resm(TPASS, "client association 2 peers ok");
	close(svr_sk);
	close(clt_sk1);
	close(clt_sk2);
	close(peeloff_sk);

	/* Indicate successful completion.  */
	return 0; 
}
예제 #12
0
int main(int argc, char *argv[])
{
        int sk1, sk2;
        sockaddr_storage_t loop1;
        sockaddr_storage_t loop2;
        struct iovec iov;
        struct msghdr inmessage;
	struct msghdr outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
        struct iovec out_iov;
        int error;
	int pf_class, af_family;
	uint32_t ppid;
	uint32_t stream;
	sctp_assoc_t associd1, associd2;
	struct sctp_assoc_change *sac;
	struct sctp_event_subscribe subscribe;
	char *big_buffer;
	int offset;
	struct sctp_send_failed *ssf;
	socklen_t len; /* Really becomes 2xlen when set. */
	int orig_len;
	struct sctp_status gstatus;

        /* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);

	/* Set some basic values which depend on the address family. */
#if TEST_V6
	pf_class = PF_INET6;
	af_family = AF_INET6;

        loop1.v6.sin6_family = AF_INET6;
        loop1.v6.sin6_addr = in6addr_loopback;
        loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);

        loop2.v6.sin6_family = AF_INET6;
        loop2.v6.sin6_addr = in6addr_loopback;
        loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
#else
	pf_class = PF_INET;
	af_family = AF_INET;

        loop1.v4.sin_family = AF_INET;
        loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        loop1.v4.sin_port = htons(SCTP_TESTPORT_1);

        loop2.v4.sin_family = AF_INET;
        loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
#endif /* TEST_V6 */

        /* Create the two endpoints which will talk to each other.  */
        sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
        sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	len = sizeof(int);
	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &orig_len,
			   &len);
	if (error)
		tst_brkm(TBROK, NULL, "can't get rcvbuf size: %s",
			strerror(errno));
	/* Set the MAXSEG to something smallish. */
	{
		int val = SMALL_MAXSEG;
		test_setsockopt(sk1, SCTP_MAXSEG, &val, sizeof(val));
	}

	memset(&subscribe, 0, sizeof(subscribe));
	subscribe.sctp_data_io_event = 1;
	subscribe.sctp_association_event = 1;
	subscribe.sctp_send_failure_event = 1;
	test_setsockopt(sk1, SCTP_EVENTS, &subscribe, sizeof(subscribe));
	test_setsockopt(sk2, SCTP_EVENTS, &subscribe, sizeof(subscribe));

        /* Bind these sockets to the test ports.  */
        test_bind(sk1, &loop1.sa, sizeof(loop1));
        test_bind(sk2, &loop2.sa, sizeof(loop2));

	/*
	 * This code sets the associations RWND very small so we can
	 * fill it.  It does this by manipulating the rcvbuf as follows:
	 * 1) Reduce the rcvbuf size on the socket
	 * 2) create an association so that we advertize rcvbuf/2 as
	 *    our initial rwnd
	 * 3) raise the rcvbuf value so that we don't drop data wile
	 *    receiving later data
	 */
	len = SMALL_RCVBUF;
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &len,
			   sizeof(len));
	if (error)
		tst_brkm(TBROK, NULL, "setsockopt(SO_RCVBUF): %s",
			 strerror(errno));

       /* Mark sk2 as being able to accept new associations.  */
	test_listen(sk2, 1);

        /* Send the first message.  This will create the association.  */
        outmessage.msg_name = &loop2;
        outmessage.msg_namelen = sizeof(loop2);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
        outmessage.msg_iov->iov_base = message;
        outmessage.msg_iov->iov_len = strlen(message) + 1;
        test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
        memset(&inmessage, 0, sizeof(inmessage));
        iov.iov_base = big_buffer;
        iov.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;

        /* Get the communication up message on sk2.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	associd2 = sac->sac_assoc_id;

        /* Get the communication up message on sk1.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	associd1 = sac->sac_assoc_id;

	/* restore the rcvbuffer size for the receiving socket */
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &orig_len,
			   sizeof(orig_len));

	if (error)
		tst_brkm(TBROK, NULL, "setsockopt(SO_RCVBUF): %s",
			strerror(errno));

        /* Get the first data message which was sent.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR, stream, ppid);

	/* Figure out how big to make our fillmsg */
	len = sizeof(struct sctp_status);
	memset(&gstatus,0,sizeof(struct sctp_status));
	gstatus.sstat_assoc_id = associd1;
	error = getsockopt(sk1, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);

	if (error)
		tst_brkm(TBROK, NULL, "can't get rwnd size: %s",
			strerror(errno));
	tst_resm(TINFO, "Creating fillmsg of size %d",
		 gstatus.sstat_rwnd+RWND_SLOP);
	fillmsg = malloc(gstatus.sstat_rwnd+RWND_SLOP);

	/* Send a fillmsg */
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid++;
	stream++;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
	memset(fillmsg, 'X', gstatus.sstat_rwnd+RWND_SLOP);
	fillmsg[gstatus.sstat_rwnd+RWND_SLOP-1] = '\0';
	outmessage.msg_iov->iov_base = fillmsg;
	outmessage.msg_iov->iov_len = gstatus.sstat_rwnd+RWND_SLOP;
	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	sinfo->sinfo_assoc_id = associd1;
	sinfo->sinfo_timetolive = 0;
	test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL,
			 gstatus.sstat_rwnd+RWND_SLOP);

	/* Now send the message with timeout. */
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
	outmessage.msg_iov->iov_base = ttlmsg;
        outmessage.msg_iov->iov_len = strlen(ttlmsg) + 1;
	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	sinfo->sinfo_assoc_id = associd1;
	sinfo->sinfo_timetolive = 2000;
	test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(ttlmsg) + 1);

	tst_resm(TPASS, "Send a message with timeout");

	/* Next send a message with no timeout. */
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
	outmessage.msg_iov->iov_base = nottlmsg;
        outmessage.msg_iov->iov_len = strlen(nottlmsg) + 1;
	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	sinfo->sinfo_assoc_id = associd1;
	sinfo->sinfo_timetolive = 0;
	test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(nottlmsg)+1);

	tst_resm(TPASS, "Send a message with no timeout");

	/* And finally a fragmented message that will time out. */
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
	memset(ttlfrag, '0', sizeof(ttlfrag));
	ttlfrag[sizeof(ttlfrag)-1] = '\0';
	outmessage.msg_iov->iov_base = ttlfrag;
        outmessage.msg_iov->iov_len = sizeof(ttlfrag);
	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	sinfo->sinfo_assoc_id = associd1;
	sinfo->sinfo_timetolive = 2000;
	test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, sizeof(ttlfrag));

	tst_resm(TPASS, "Send a fragmented message with timeout");

	/* Sleep waiting for the message to time out. */
	tst_resm(TINFO, " **  SLEEPING for 3 seconds **");
	sleep(3);

	/* Read the fillmsg snuck in between the ttl'd messages. */
	do {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	} while (!(inmessage.msg_flags & MSG_EOR));

	/* Now get the message that did NOT time out. */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	test_check_msg_data(&inmessage, error, strlen(nottlmsg) + 1,
			    MSG_EOR, stream, ppid);
	if (0 != strncmp(iov.iov_base, nottlmsg, strlen(nottlmsg)+1))
		tst_brkm(TBROK, NULL, "Received Wrong Message !!!");

	tst_resm(TPASS, "Receive message with no timeout");

	/* Get the SEND_FAILED notification for the message that DID
	 * time out.
	 */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_send_failed) +
							strlen(ttlmsg) + 1,
				    SCTP_SEND_FAILED, 0);
	ssf = (struct sctp_send_failed *)iov.iov_base;
	if (0 != strncmp(ttlmsg, (char *)ssf->ssf_data, strlen(ttlmsg) + 1))
		tst_brkm(TBROK, NULL, "SEND_FAILED data mismatch");

	tst_resm(TPASS, "Receive SEND_FAILED for message with timeout");

	/* Get the SEND_FAILED notification for the fragmented message that
	 * DID time out.
	 */
	offset = 0;
	do {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_send_failed) +
								  SMALL_MAXSEG,
					    SCTP_SEND_FAILED, 0);
		ssf = (struct sctp_send_failed *)iov.iov_base;
		if (0 != strncmp(&ttlfrag[offset], (char *)ssf->ssf_data,
				 SMALL_MAXSEG))
			tst_brkm(TBROK, NULL, "SEND_FAILED data mismatch");
		offset += SMALL_MAXSEG;
	} while (!(ssf->ssf_info.sinfo_flags & 0x01)); /* LAST_FRAG */

	tst_resm(TPASS, "Receive SEND_FAILED for fragmented message with "
		 "timeout");

        /* Shut down the link.  */
        close(sk1);

        /* Get the shutdown complete notification. */
	inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

        close(sk2);

        /* Indicate successful completion.  */
      tst_exit();
}
예제 #13
0
int
main(int argc, char *argv[])
{
        int error;
	socklen_t len;
	int sk,lstn_sk,clnt_sk,acpt_sk,pf_class,sk1;
	struct msghdr outmessage;
        struct msghdr inmessage;
        char *message = "hello, world!\n";
        struct iovec iov;
        struct iovec iov_rcv;
        struct sctp_sndrcvinfo *sinfo;
        int msg_count;
        char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
        struct cmsghdr *cmsg;
        struct iovec out_iov;
        char * buffer_snd;
        char * buffer_rcv;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	struct sockaddr *laddrs, *paddrs;

        struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
	struct sockaddr_in *addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a regular socket*/
	clnt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a listen socket*/
        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/*Listening many sockets as we are calling too many connect here*/
	test_listen(lstn_sk, 1);

	len = sizeof(struct sockaddr_in);

	test_connect(clnt_sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

        msg_count = strlen(message) + 1;

	memset(&outmessage, 0, sizeof(outmessage));
        buffer_snd = malloc(REALLY_BIG);

        outmessage.msg_name = &lstn_addr;
        outmessage.msg_namelen = sizeof(lstn_addr);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;

        cmsg = CMSG_FIRSTHDR(&outmessage);
        cmsg->cmsg_level = IPPROTO_SCTP;
        cmsg->cmsg_type = SCTP_SNDRCV;
        cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
        outmessage.msg_controllen = cmsg->cmsg_len;
        sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
        memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

        iov.iov_base = buffer_snd;
	iov.iov_len = REALLY_BIG;
        outmessage.msg_iov->iov_base = message;

        outmessage.msg_iov->iov_len = msg_count;
	test_sendmsg(clnt_sk, &outmessage, MSG_NOSIGNAL, msg_count);

	test_recvmsg(acpt_sk, &inmessage, MSG_NOSIGNAL);

	/*sctp_getladdrs() TEST1: Bad socket descriptor, EBADF Expected error*/
	error = sctp_getladdrs(-1, 0, &laddrs);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with a bad socket descriptor - "
		 "EBADF");

	/*sctp_getladdrs() TEST2: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_getladdrs(0, 0, &laddrs);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with invalid socket - ENOTSOCK");

	/*sctp_getladdrs() TEST3: socket of different protocol
	EOPNOTSUPP Expected error*/
        sk1 = socket(pf_class, SOCK_STREAM, IPPROTO_IP);
	error = sctp_getladdrs(sk1, 0, &laddrs);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with socket of "
			 "different protocol error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with socket of different protocol - "
		 "EOPNOTSUPP");

	/*sctp_getladdrs() TEST4: Getting the local addresses*/
	error = sctp_getladdrs(lstn_sk, 0, &laddrs);
	if (error < 0)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with valid socket "
			 "error:%d, errno:%d", error, errno);

	addr = (struct sockaddr_in *)laddrs;
	if (addr->sin_port != lstn_addr.sin_port ||
	    addr->sin_family != lstn_addr.sin_family ||
	    addr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "sctp_getladdrs comparision failed");

	tst_resm(TPASS, "sctp_getladdrs() - SUCCESS");

	/*sctp_freealddrs() TEST5: freeing the local address*/
	if ((sctp_freeladdrs(laddrs)) < 0)
		tst_brkm(TBROK, NULL, "sctp_freeladdrs "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_freeladdrs() - SUCCESS");

	/*sctp_getpaddrs() TEST6: Bad socket descriptor, EBADF Expected error*/
	error = sctp_getpaddrs(-1, 0, &paddrs);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with a bad socket descriptor - "
		 "EBADF");

	/*sctp_getpaddrs() TEST7: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_getpaddrs(0, 0, &paddrs);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with invalid socket - ENOTSOCK");

	/*sctp_getpaddrs() TEST8: socket of different protocol
	EOPNOTSUPP Expected error*/
	error = sctp_getpaddrs(sk1, 0, &laddrs);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with socket of "
			 "different protocol error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with socket of different protocol - "
		 "EOPNOTSUPP");

	/*sctp_getpaddrs() TEST9: Getting the peer addresses*/
	error = sctp_getpaddrs(acpt_sk, 0, &paddrs);
	if (error < 0)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with valid socket "
			 "error:%d, errno:%d", error, errno);

	addr = (struct sockaddr_in *)paddrs;
	if (addr->sin_port != acpt_addr.sin_port ||
            addr->sin_family != acpt_addr.sin_family ||
            addr->sin_addr.s_addr != acpt_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs comparision failed");

	tst_resm(TPASS, "sctp_getpaddrs() - SUCCESS");

	/*sctp_freeapddrs() TEST10: freeing the peer address*/
	if ((sctp_freepaddrs(paddrs)) < 0)
		tst_brkm(TBROK, NULL, "sctp_freepaddrs "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_freepaddrs() - SUCCESS");

	close(clnt_sk);

	tst_exit();
}
예제 #14
0
int
main(int argc, char *argv[])
{
        socklen_t len,len_snd;
	int msg_count;
	int sk,sk1,pf_class,lstn_sk,acpt_sk,acpt1_sk, flag, count;
        char *message = "hello, world!\n";
        char *message_rcv;

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	len_snd = (strlen(message) + 1);

	flag = MSG_NOSIGNAL;
	/*send () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = send(-1, message, len_snd, flag);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "send with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with a bad socket descriptor - EBADF");
	
	/*send () TEST2: Invalid socket, ENOTSOCK Expected error*/
	count = send(0, message, len_snd, flag);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "send with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with invalid socket - ENOTSOCK");

	/*send () TEST3: send on listening socket, EPIPE Expected error*/
	count = send(lstn_sk, message, len_snd, flag);
	if (count != -1 || errno != EPIPE)
		tst_brkm(TBROK, tst_exit, "send on a listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() on a listening socket - EPIPE");
#if 0
	/*send () TEST4: Invalid message address, EFAULT Expected error*/
       /* FIXME this test should pass. Don't catch why...  */
	count = send(sk, (char *)0x1, len_snd, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "send with invalid message "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with invalid message ptr - EFAULT");
#endif

	test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
		 
	count = test_send(sk1, message, len_snd, flag);

	close(sk1);

	acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);

	/*send () TEST5: send on closed association, EPIPE Expected error*/
	count = send(acpt1_sk, message, len_snd, flag);
	if (count != -1 || errno != EPIPE)
		tst_brkm(TBROK, tst_exit, "send on a closed association "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() on a closed association - EPIPE");

	close(acpt1_sk);
	close(sk);
	close(lstn_sk);
	close(acpt_sk);

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	message_rcv = malloc(512);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	len = sizeof(struct sockaddr_in);

	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	
	msg_count = strlen(message) + 1;

	/*send() TEST6: Sending data from client socket to server socket*/
	count = send(sk, message, msg_count, flag);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "send from client to server "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() from client to server - SUCCESS");

	test_recv(acpt_sk, message_rcv, msg_count, flag);

	strncpy(message_rcv,"\0",512);

	/*send() TEST7: Sending data from accept socket to client socket*/
	count = send(acpt_sk, message, msg_count, flag);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "send from accept socket to client "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() from accept socket to client - SUCCESS");

	test_recv(sk, message_rcv, msg_count, flag);

	/*send() TEST8: Sending less number of data from the buffer*/
	/*Sending only 5 bytes so that only hello is received*/
	test_send(sk, message, 5 , flag);
	test_recv(acpt_sk, message_rcv, 5, flag);
	
	tst_resm(TPASS, "send() partial data from a buffer - SUCCESS");

	/* TEST9: sctp_send with no sinfo */
	test_sctp_send(sk, message, strlen(message) + 1 , NULL, flag);
	test_recv(acpt_sk, message_rcv, strlen(message) + 1, flag);
	tst_resm(TPASS, "sctp_send() with no sinfo - SUCCESS");

	close(sk1);
	close(lstn_sk);
	close(acpt_sk);

	return 0;
}
int
main(int argc, char *argv[])
{
        socklen_t len;
	int i;
	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk,pf_class;
	int new_sk[SK_MAX],clnt2_sk[SK_MAX];
	int error;

        struct sockaddr_in conn_addr,lstn_addr,acpt_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	for (i=0 ; i < SK_MAX ; i++)
		new_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Creating a regular socket */
	for (i = 0 ; i < SK_MAX ; i++)
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	for (i = 0 ; i < SK_MAX ; i++)
		clnt2_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Creating a listen socket */
        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/* Binding the listen socket */
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/* Listening many sockets as we are calling too many connect here */
	test_listen(lstn_sk, SK_MAX );

	/* connect() is called just to make sure accept() doesn't block the
	 * program
	 */
	i = 0;
	len = sizeof(struct sockaddr_in);
	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

	/* accept() TEST1: Bad socket descriptor EBADF, Expected error */
        error = accept(-1, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "accept with a bad socket descriptor"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with a bad socket descriptor - EBADF");

        /*accept() TEST2: Invalid socket ENOTSOCK, Expected error*/
        error = accept(0, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "accept with invalid socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with invalid socket - ENOTSOCK");

        /*accept() TEST3: Invalid address EFAULT, Expected error*/
        error = accept(lstn_sk, (struct sockaddr *) -1, &len);
        if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "accept with invalid address"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with invalid address - EFAULT");

	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

        /*accept() TEST4: on a non-listening socket EINVAL, Expected error*/
        error = accept(sk, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "accept on a non-listening socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() on a non-listening socket - EINVAL");
	
	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

	/*Calling accept to establish the connection*/
	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);

	/*accept() TEST5: On a established socket EINVAL, Expected error*/
	error = accept(acpt_sk, (struct sockaddr *) &acpt_addr, &len);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "accept on an established socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() on an established socket - EINVAL");

	/*Closing the previously established association*/
	close(acpt_sk);

	test_connect(clnt_sk[i], (struct sockaddr *) &conn_addr, len);

	/*accept() TEST6: On the CLOSED association should succeed*/
	acpt_sk = accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);
        if (acpt_sk < 0)
		tst_brkm(TBROK, tst_exit, "accept a closed association"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() a closed association - SUCCESS");

	close(acpt_sk);

	/*accept() TEST7: Extracting the association on the listening socket
	as new socket, new socket socket descriptor should return*/
	for (i = 0 ; i < (SK_MAX - 1); i++)
		test_connect(clnt2_sk[i], (struct sockaddr *) &conn_addr, len);

	for (i = 0 ; i < (SK_MAX - 1); i++)
		new_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
					&len);

	tst_resm(TPASS, "accept() on a listening socket - SUCCESS");

	
        /*close() TEST8: Bad socket descriptor, EBADF Expected error*/
	error = close(-1);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "close with a bad socket descriptor "
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a bad socket descriptor - EBADF");

	/*close() TEST9: valid socket descriptor should succeed*/
	error = close(sk);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "close with a valid socket descriptor"
                         " error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a valid socket descriptor - SUCCESS");

	/*close() TEST10: closed socket descriptor, EBADF Expected error*/
        error = close(sk);
        if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "close with a closed socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a closed socket descriptor - EBADF");
	
	for (i = 0 ; i < SK_MAX ; i++) {
		close(clnt_sk[i]);
		close(new_sk[i]);
		close(clnt2_sk[i]);
	}

	return 0;
}
예제 #16
0
int
main(void)
{
	int udp_svr_sk, udp_clt_sk, tcp_svr_sk, tcp_clt_sk;
	int accept_sk, peeloff_sk;
        sockaddr_storage_t udp_svr_loop, udp_clt_loop;
        sockaddr_storage_t tcp_svr_loop, tcp_clt_loop;
        struct iovec iov;
        struct msghdr inmessage;
	struct msghdr outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
        struct iovec out_iov;
        char *message = "hello, world!\n";
        int error;
	int pf_class, af_family;
	uint32_t ppid;
	uint32_t stream;
	sctp_assoc_t udp_svr_associd, udp_clt_associd;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	struct sctp_event_subscribe subscribe;
	struct sctp_initmsg initmsg;
	struct sctp_sndrcvinfo set_udp_sk_dflt_param, get_udp_sk_dflt_param;
	struct sctp_sndrcvinfo set_tcp_sk_dflt_param, get_tcp_sk_dflt_param;
	struct sctp_sndrcvinfo set_udp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_udp_assoc_dflt_param;
	struct sctp_sndrcvinfo set_tcp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_tcp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_peeloff_assoc_dflt_param;
	struct sctp_sndrcvinfo get_accept_assoc_dflt_param;
	struct sctp_paddrinfo pinfo;
	socklen_t optlen, addrlen;
	struct sctp_status status;

        /* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);

	/* Set some basic values which depend on the address family. */
#if TEST_V6
	pf_class = PF_INET6;
	af_family = AF_INET6;

        udp_svr_loop.v6.sin6_family = AF_INET6;
        udp_svr_loop.v6.sin6_addr = in6addr_loopback;
        udp_svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);

        udp_clt_loop.v6.sin6_family = AF_INET6;
        udp_clt_loop.v6.sin6_addr = in6addr_loopback;
        udp_clt_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+1);

        tcp_svr_loop.v6.sin6_family = AF_INET6;
        tcp_svr_loop.v6.sin6_addr = in6addr_loopback;
        tcp_svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+2);

        tcp_clt_loop.v6.sin6_family = AF_INET6;
        tcp_clt_loop.v6.sin6_addr = in6addr_loopback;
        tcp_clt_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+3);
#else
	pf_class = PF_INET;
	af_family = AF_INET;

        udp_svr_loop.v4.sin_family = AF_INET;
        udp_svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        udp_svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);

        udp_clt_loop.v4.sin_family = AF_INET;
        udp_clt_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        udp_clt_loop.v4.sin_port = htons(SCTP_TESTPORT_1+1);

        tcp_svr_loop.v4.sin_family = AF_INET;
        tcp_svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        tcp_svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1+2);

        tcp_clt_loop.v4.sin_family = AF_INET;
        tcp_clt_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        tcp_clt_loop.v4.sin_port = htons(SCTP_TESTPORT_2+3);
#endif /* TEST_V6 */

        /* Create the two endpoints which will talk to each other.  */
        udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
        udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

        /* Bind these sockets to the test ports.  */
        test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
        test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

       /* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

	/* TEST #1: SCTP_STATUS socket option. */
	/* Make sure that SCTP_STATUS getsockopt on a socket with no
	 * association fails.
	 */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	error = getsockopt(udp_svr_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) on a "
			 "socket with no assoc error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) on a socket with no assoc");

        /* Send the first message.  This will create the association.  */
        outmessage.msg_name = &udp_svr_loop;
        outmessage.msg_namelen = sizeof(udp_svr_loop);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
        outmessage.msg_iov->iov_base = message;
        outmessage.msg_iov->iov_len = strlen(message) + 1;
        test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
        memset(&inmessage, 0, sizeof(inmessage));
        iov.iov_base = big_buffer;
        iov.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;

        /* Get the communication up message on udp_svr_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_svr_associd = sac->sac_assoc_id;

        /* Get the communication up message on udp_clt_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

        /* Get the first message which was sent.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR, stream, ppid);

	/* Get SCTP_STATUS for udp_clt_sk's given association. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = udp_clt_associd;
	test_getsockopt(udp_clt_sk, SCTP_STATUS, &status, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS)");

	/* Make sure that SCTP_STATUS getsockopt with invalid associd fails. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = udp_svr_associd;
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
        	tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) with "
			 "associd error: %d errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) with invalid associd");

	/* Make sure that SCTP_STATUS getsockopt with NULL associd fails. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = 0;
	error = getsockopt(udp_svr_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
        	tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) with "
			 "NULL associd error: %d errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) with NULL associd");

        /* Shut down the link.  */
        close(udp_clt_sk);

        /* Get the shutdown complete notification. */
	inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	error = 0;
        close(udp_svr_sk);

	/* TEST #2: SCTP_EVENTS socket option and SCTP_SHUTDOWN_EVENT
	 * notification.
	 */
        /* Create the two endpoints which will talk to each other.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

	/* Bind these sockets to the test ports.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

	/* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

	/* Get the default events that are enabled on udp_svr_sk. */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe, &optlen);

	/* Get the default events that are enabled on udp_clt_sk. */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_EVENTS)");

	/* Disable all the events on udp_svr_sk and udp_clt_sk. */
	memset(&subscribe, 0, sizeof(struct sctp_event_subscribe));
	test_setsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));
	test_setsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));

	tst_resm(TPASS, "setsockopt(SCTP_EVENTS)");

	/* Get the updated list of enabled events on udp_svr_sk and
	 * udp_clt_sk.
	 */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe, &optlen);
	optlen = sizeof(subscribe);
	test_getsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe, &optlen);

	/* Send a message.  This will create the association.  */
	outmessage.msg_iov->iov_base = message;
	outmessage.msg_iov->iov_len = strlen(message) + 1;
	test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

	/* Get the message which was sent.  */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR, 0, 0);
	/* Verify that we received the msg without any ancillary data. */
	if (inmessage.msg_controllen != 0)
		tst_brkm(TBROK, NULL, "Receive unexpected ancillary"
			 "data");

	/* Enable SCTP_SHUTDOWN_EVENTs on udp_svr_sk. */
	memset(&subscribe, 0, sizeof(struct sctp_event_subscribe));
	subscribe.sctp_shutdown_event = 1;
	test_setsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));

	error = 0;
        /* Shut down the link.  */
        close(udp_clt_sk);

	/* Get the SHUTDOWN_EVENT notification on udp_svr_sk. */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_shutdown_event),
				    SCTP_SHUTDOWN_EVENT, 0);

	tst_resm(TPASS, "setsockopt(SCTP_EVENTS) - SCTP_SHUTDOWN_EVENT");

        close(udp_svr_sk);

	/* TEST #3: whether sctp_opt_info equals */
        /* Create the two endpoints which will talk to each other.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

	/* Bind these sockets to the test ports.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

	/* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

        /* Send the first message.  This will create the association.  */
        outmessage.msg_name = &udp_svr_loop;
        outmessage.msg_namelen = sizeof(udp_svr_loop);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
        outmessage.msg_iov->iov_base = message;
        outmessage.msg_iov->iov_len = strlen(message) + 1;
        test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

        /* Get the communication up message on udp_clt_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

	/* Compare the SCTP_STATUS result between sctp_opt_info and
	 * getsockopt
	 */
	{
		struct sctp_status status1, status2;

		memset(&status1, 0, sizeof(status1));
		memset(&status2, 0, sizeof(status2));
		optlen = sizeof(struct sctp_status);

		/* Test SCTP_STATUS for udp_clt_sk's given association. */
		error = sctp_opt_info(udp_clt_sk,udp_clt_associd,SCTP_STATUS,
				(char *)&status1, &optlen);
		if (error != 0)
	                tst_brkm(TBROK, NULL,
				 "sctp_opt_info(SCTP_STATUS): %s",
				 strerror(errno));

		status2.sstat_assoc_id = udp_clt_associd;
		error = getsockopt(udp_clt_sk, IPPROTO_SCTP, SCTP_STATUS,
                		(char *)&status2, &optlen);
		if (error != 0)
	                tst_brkm(TBROK, NULL,
				 "getsockopt(SCTP_STATUS): %s",
				 strerror(errno));
		if (strncmp((char *)&status1, (char *)&status2, optlen))
	                tst_brkm(TBROK, NULL, "sctp_opt_info(SCTP_STAUS)"
			       "doesn't match getsockopt(SCTP_STATUS)");

                tst_resm(TPASS, "sctp_opt_info(SCTP_STATUS)");
	}
	error = 0;
        /* Shut down the link.  */
        close(udp_svr_sk);
        close(udp_clt_sk);

	/* TEST #4: SCTP_INITMSG socket option. */
        /* Create a socket.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Bind this socket to the test port.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);

	/* Get the default parameters for association initialization. */
	optlen = sizeof(initmsg);
	test_getsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_INITMSG)");

	/* Change the parameters for association initialization. */
	initmsg.sinit_num_ostreams = 5;
	initmsg.sinit_max_instreams = 5;
	initmsg.sinit_max_attempts = 3;
	initmsg.sinit_max_init_timeo = 30;
	test_setsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, sizeof(initmsg));

	tst_resm(TPASS, "setsockopt(SCTP_INITMSG)");

	/* Get the updated parameters for association initialization. */
	optlen = sizeof(initmsg);
	test_getsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, &optlen);

	close(udp_svr_sk);

	/* TEST #5: SCTP_DEFAULT_SEND_PARAM socket option. */
	/* Create and bind 2 UDP-style sockets(udp_svr_sk, udp_clt_sk) and
	 * 2 TCP-style sockets. (tcp_svr_sk, tcp_clt_sk)
	 */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	tcp_svr_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	tcp_clt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);
	test_enable_assoc_change(tcp_svr_sk);
	test_enable_assoc_change(tcp_clt_sk);

	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));
	test_bind(tcp_svr_sk, &tcp_svr_loop.sa, sizeof(tcp_svr_loop));
	test_bind(tcp_clt_sk, &tcp_clt_loop.sa, sizeof(tcp_clt_loop));

	/* Mark udp_svr_sk and tcp_svr_sk as being able to accept new
	 * associations.
	 */
	test_listen(udp_svr_sk, 5);
	test_listen(tcp_svr_sk, 5);

	/* Set default send parameters on the unconnected UDP-style sockets. */
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
	test_setsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_sk_dflt_param, sizeof(set_udp_sk_dflt_param));
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
	test_setsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_sk_dflt_param, sizeof(set_udp_sk_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style socket");

	/* Get default send parameters on the unconnected UDP-style socket. */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_udp_sk_dflt_param.sinfo_ppid !=
			get_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the unconnected UDP-style socket. */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
		       &get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_udp_sk_dflt_param.sinfo_ppid !=
			get_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style socket");

	/* Verify that trying to set send params with an invalid assoc id
	 * on an UDP-style socket fails.
	 */
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
       	/* Invalid assoc id */
	set_udp_sk_dflt_param.sinfo_assoc_id = 1234;
        error = setsockopt(udp_clt_sk, SOL_SCTP, SCTP_DEFAULT_SEND_PARAM,
			   &set_udp_sk_dflt_param,
			   sizeof(set_udp_sk_dflt_param));
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "invalid associd error:%d, errno:%d\n",
			 error, errno);

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
		 "- one-to-many style invalid associd");

	/* Do a connect on a UDP-style socket and establish an association. */
	test_connect(udp_clt_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));

	/* Receive the COMM_UP notifications and get the associd's */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_svr_associd = sac->sac_assoc_id;

	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

	/* Verify that trying to set send params with an assoc id not
	 * belonging to the socket on an UDP-style socket fails.
	 */
	memset(&set_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_assoc_dflt_param.sinfo_ppid = 3000;
	set_udp_assoc_dflt_param.sinfo_assoc_id = udp_clt_associd;
	error = setsockopt(udp_svr_sk, SOL_SCTP, SCTP_DEFAULT_SEND_PARAM,
			   &set_udp_assoc_dflt_param,
			   sizeof(set_udp_assoc_dflt_param));
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "associd belonging to another socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style associd belonging to another socket");

	/* Set default send parameters of an association on the listening
	 * UDP-style socket with a valid associd.
	 */
	memset(&set_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_assoc_dflt_param.sinfo_ppid = 3000;
	set_udp_assoc_dflt_param.sinfo_assoc_id = udp_svr_associd;
	test_setsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_assoc_dflt_param,
			sizeof(set_udp_assoc_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style valid associd");

	/* Get default send parameters of an association on the listening
	 * UDP-style socket with a valid associd.
	 */
	memset(&get_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	get_udp_assoc_dflt_param.sinfo_assoc_id = udp_svr_associd ;
	optlen = sizeof(get_udp_assoc_dflt_param);
	test_getsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_assoc_dflt_param, &optlen);

	/* Verify that the get param matches the set param. */
	if (get_udp_assoc_dflt_param.sinfo_ppid !=
			set_udp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style valid associd");

	/* Get default send parameters of an association on the connected
	 * UDP-style socket with zero associd. This should return the
	 * socket wide default parameters.
	 */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	get_udp_sk_dflt_param.sinfo_assoc_id = 0 ;
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches the socket-wide set param. */
	if (get_udp_sk_dflt_param.sinfo_ppid !=
			set_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style zero associd");

	peeloff_sk = test_sctp_peeloff(udp_svr_sk, udp_svr_associd);

	/* Get default send parameters of an association on the peeled off
	 * UDP-style socket. This should return the association's default
	 * parameters.
	 */
	memset(&get_peeloff_assoc_dflt_param, 0,
	       sizeof(struct sctp_sndrcvinfo));
	get_peeloff_assoc_dflt_param.sinfo_assoc_id = 0 ;
	optlen = sizeof(get_peeloff_assoc_dflt_param);
	test_getsockopt(peeloff_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_peeloff_assoc_dflt_param, &optlen);

	/* Verify that the get param matches the association's set param. */
	if (get_peeloff_assoc_dflt_param.sinfo_ppid !=
			set_udp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style peeled off socket");

	/* Set default send parameters on the unconnected TCP-style sockets. */
	memset(&set_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_sk_dflt_param.sinfo_ppid = 2000;
	/* Invalid assoc id, ignored on a TCP-style socket. */
	set_tcp_sk_dflt_param.sinfo_assoc_id = 1234;
	test_setsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_sk_dflt_param,
			sizeof(set_tcp_sk_dflt_param));

	/* Set default send parameters on the unconnected TCP-style sockets. */
	memset(&set_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_sk_dflt_param.sinfo_ppid = 2000;
	/* Invalid assoc id, ignored on a TCP-style socket. */
	set_tcp_sk_dflt_param.sinfo_assoc_id = 1234;
	test_setsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_sk_dflt_param,
			sizeof(set_tcp_sk_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style socket");

	/* Get default send parameters on the unconnected TCP-style socket. */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_tcp_sk_dflt_param.sinfo_ppid !=
			get_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the unconnected TCP-style socket. */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_tcp_sk_dflt_param.sinfo_ppid !=
			get_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style socket");

	/* Do a connect on a TCP-style socket and establish an association. */
	test_connect(tcp_clt_sk, &tcp_svr_loop.sa, sizeof(tcp_svr_loop));

	/* Set default send parameters of an association on the connected
	 * TCP-style socket.
	 */
	memset(&set_tcp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_assoc_dflt_param.sinfo_ppid = 4000;
	set_tcp_assoc_dflt_param.sinfo_assoc_id = 0;
	test_setsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_assoc_dflt_param,
			sizeof(set_tcp_assoc_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style assoc");

	/* Get default send parameters of an association on the connected
	 * TCP-style socket.
	 */
	memset(&get_tcp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_assoc_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_assoc_dflt_param, &optlen);

	if (set_tcp_assoc_dflt_param.sinfo_ppid !=
			get_tcp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the connected TCP-style socket.  */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get parameters returned matches the set param
	 * set for the association, not the socket-wide param.
	 */
	if ((get_tcp_sk_dflt_param.sinfo_ppid ==
			set_tcp_sk_dflt_param.sinfo_ppid) ||
	    (get_tcp_sk_dflt_param.sinfo_ppid !=
	    		set_tcp_assoc_dflt_param.sinfo_ppid))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the listening TCP-style socket.  */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get parameters returned matches the socket-wide
	 * set param.
	 */
	if (get_tcp_sk_dflt_param.sinfo_ppid !=
			set_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style assoc");

	accept_sk = test_accept(tcp_svr_sk, NULL, &addrlen);

	/* Get default send parameters of an association on the accepted
	 * TCP-style socket.
	 */
	memset(&get_accept_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_accept_assoc_dflt_param);
	test_getsockopt(accept_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_accept_assoc_dflt_param, &optlen);

	error = 0;

	/* Verify that the get parameters returned matches the socket-wide
	 * set param.
	 */
	if (get_tcp_sk_dflt_param.sinfo_ppid !=
			set_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style accepted socket");

	/* TEST #6: SCTP_GET_PEER_ADDR_INFO socket option. */
	/* Try 0 associd and 0 addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "null associd, null addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "null associd and null addr");

	/* Try valid associd, but 0 addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "valid associd, null addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and null addr");

	/* Try valid associd, invalid addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	memcpy(&pinfo.spinfo_address, &udp_clt_loop, sizeof(udp_clt_loop));
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "valid associd, invalid addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and invalid addr");

	/* Try valid associd, valid addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	memcpy(&pinfo.spinfo_address, &udp_svr_loop, sizeof(udp_svr_loop));
	test_getsockopt(udp_clt_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr");

	/* Try valid addr, peeled off socket */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = 0;
	memcpy(&pinfo.spinfo_address, &udp_clt_loop, sizeof(udp_clt_loop));
	test_getsockopt(peeloff_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr peeled off socket");

	/* Try valid addr, TCP-style accept socket */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = 0;
	memcpy(&pinfo.spinfo_address, &tcp_clt_loop, sizeof(tcp_clt_loop));
	error = test_getsockopt(accept_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo,
				&optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr accepted socket");

	close(udp_svr_sk);
	close(udp_clt_sk);
	close(tcp_svr_sk);
	close(tcp_clt_sk);
	close(accept_sk);
	close(peeloff_sk);

        /* Indicate successful completion.  */
      tst_exit();
}
int main(int argc, char *argv[])
{
	int clnt_sk[MAX_CLIENTS], acpt_sk[MAX_CLIENTS], sk;
	int lstn_sk;
	struct sockaddr_in lstn_addr, acpt_addr;
	socklen_t addrlen;
	int error, i;
	char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	/* Initialize the server and client addresses. */
	pf_class = PF_INET;

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	test_bind(lstn_sk, (struct sockaddr *)&lstn_addr, sizeof(lstn_addr));

	test_listen(lstn_sk, MAX_CLIENTS);

	for (i = 0; i < MAX_CLIENTS; i++) {
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_connect(clnt_sk[i], (struct sockaddr *)&lstn_addr,
			     sizeof(lstn_addr));
	}

	for (i = 0; i < MAX_CLIENTS; i++) {
		addrlen = sizeof(acpt_addr);
		acpt_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
					 &addrlen);
	}

	/*shutdown() TEST1: Bad socket descriptor, EBADF Expected error */
	error = shutdown(-1, SHUT_WR);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "shutdown with a bad socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with a bad socket descriptor - EBADF");

	/*shutdown() TEST2: Invalid socket, ENOTSOCK Expected error */
	error = shutdown(0, SHUT_WR);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "shutdown with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with an invalid socket - ENOTSOCK");

	errno = 0;
	/*Do a send first before doing shutdown */
	test_send(acpt_sk[0], message, strlen(message), 0);

	/*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send */
	error = shutdown(clnt_sk[0], SHUT_WR);
	if (error < 0)
		tst_brkm(TBROK, NULL, "shutdown with SHUT_WR flag "
			 "error:%d, errno:%d", error, errno);

	/* Reading on a socket that has received SHUTDOWN should return 0
	 * indicating EOF.
	 */
	error = recv(acpt_sk[0], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUTDOWN received socket "
			 "error:%d, errno:%d", error, errno);

	/* Read the pending message on clnt_sk[0] that was received before
	 * SHUTDOWN call.
	 */
	test_recv(clnt_sk[0], msgbuf, 100, 0);

	/* No more messages and the association is SHUTDOWN, should fail. */
	error = recv(clnt_sk[0], msgbuf, 100, 0);
	if ((error != -1) || (errno != ENOTCONN))
		tst_brkm(TBROK, NULL, "recv on a SHUT_WR socket with no "
			 "messages error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_WR flag - SUCCESS");

	errno = 0;

	/*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive */
	test_shutdown(clnt_sk[1], SHUT_RD);

	error = recv(clnt_sk[1], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Sending a message on SHUT_RD socket. */
	error = test_send(clnt_sk[1], message, strlen(message), 0);
	if (error < 0)
		tst_brkm(TBROK, NULL, "send on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Receive the message sent on SHUT_RD socket. */
	test_recv(acpt_sk[1], msgbuf, 100, 0);

	/* Send a message to the SHUT_RD socket. */
	test_send(acpt_sk[1], message, strlen(message), 0);

	/* We should not receive the message as the socket is SHUT_RD */
	error = recv(clnt_sk[1], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_RD flag - SUCCESS");

	/*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new
	   receive/send */
	test_shutdown(clnt_sk[2], SHUT_RDWR);

	error = recv(acpt_sk[2], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUTDOWN received socket "
			 "error:%d, errno:%d", error, errno);

	error = recv(clnt_sk[2], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS");

	/*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error */
	error = shutdown(sk, SHUT_RD);
	if ((error != -1) || (errno != ENOTCONN))
		tst_brkm(TBROK, NULL, "shutdown on an unconnected socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() on an unconnected socket - SUCCESS");

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clnt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(acpt_sk[i]);

	close(lstn_sk);
	close(sk);

	tst_exit();
}
예제 #18
0
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk[MAX_CLIENTS];
	sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
	sctp_assoc_t svr_associd[MAX_CLIENTS];
	struct iovec iov;
	struct msghdr inmessage;
	struct msghdr outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
        struct iovec out_iov;
        int error;
	uint32_t ppid;
	uint32_t stream;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	int i;
        char *message = "hello, world!\n";
	struct sctp_status status;
	socklen_t status_len;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Create and bind the server socket.  */
        svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(svr_sk);

	/* Mark server socket as being able to accept new associations.  */
	test_listen(svr_sk, 1);

	/* Create and bind all the client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

		clt_loop[i].v4.sin_family = AF_INET;
		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));

		test_enable_assoc_change(clt_sk[i]);
	}

	/* Build up a msghdr structure we can use for all sending.  */
	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1; 
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
	out_iov.iov_base = message;
	out_iov.iov_len = strlen(message) + 1;
	
        /* Send the first message from all the clients to the server.  This 
	 * will create the associations.  
	 */
	for (i = 0; i < MAX_CLIENTS; i++)
		test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message) + 1);
        
	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
        memset(&inmessage, 0, sizeof(inmessage));	
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;

	/* Get the communication up message on all client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	}

	/* Get the communication up message and the data message on the
	 * server sockets for all the clients.  
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
		test_check_msg_data(&inmessage, error, strlen(message) + 1, 
				    MSG_EOR, stream, ppid);
		sac = (struct sctp_assoc_change *)iov.iov_base;
		svr_associd[i] = sac->sac_assoc_id;
	}

	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	outmessage.msg_iov = NULL;
	outmessage.msg_iovlen = 0;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	sinfo->sinfo_flags |= SCTP_ABORT;

	/* Shutdown all the associations of the server socket in a loop.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		sinfo->sinfo_assoc_id = svr_associd[i];

		/* Verify that the association is present. */
		memset(&status, 0, sizeof(struct sctp_status));
		status.sstat_assoc_id = sinfo->sinfo_assoc_id;
		status_len = sizeof(struct sctp_status);
		error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
				   &status, &status_len);
		if (error)
			tst_brkm(TBROK, tst_exit,
				 "getsockopt(SCTP_STATUS): %s",
				 strerror(errno));

		/* Call sendmsg() to abort the association.  */
		test_sendmsg(svr_sk, &outmessage, 0, 0);

		/* Verify that the association is no longer present.  */
		memset(&status, 0, sizeof(struct sctp_status));
		status.sstat_assoc_id = sinfo->sinfo_assoc_id;
		status_len = sizeof(struct sctp_status);
		error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS, 
				   &status, &status_len);
		if ((error != -1) && (errno != EINVAL))
			tst_brkm(TBROK, tst_exit,
				 "getsockopt(SCTP_STATUS) "
				 "error:%d errno:%d", error, errno);
	}

	close(svr_sk);

        /* Get the COMM_LOST notification. */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_LOST);	

		close(clt_sk[i]);
	}

	tst_resm(TPASS, "ABORT an association using SCTP_ABORT"); 

        /* Indicate successful completion.  */
        return 0;
}
예제 #19
0
int
main(int argc, char *argv[])
{
        int msg_count;
	socklen_t len;
	int sk,sk1,pf_class,lstn_sk,acpt_sk,flag;
        char *message = "hello, world!\n";
        char *message_rcv;
        int count;
	
        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbufferd
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	message_rcv = malloc(512);
	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	flag = MSG_NOSIGNAL;
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	msg_count = strlen(message) + 1;

	/*sendto() TEST1: Sending data from client socket to server socket*/
	count = sendto(sk, message, msg_count, flag,
		       (const struct sockaddr *) &conn_addr, len);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from client to server "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from client to server - SUCCESS");

	test_recv(acpt_sk, message_rcv, msg_count, flag);

	strncpy(message_rcv,"\0",512);

	/*sendto() TEST2: Sending data from accept socket to client socket*/
	count = sendto(acpt_sk, message, msg_count, flag,
		       (const struct sockaddr *) &svr_addr, len);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from accept socket to client "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from accept socket to client - SUCCESS");

	test_recv(sk, message_rcv, msg_count, flag);

        close(sk);
        close(acpt_sk);

        sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*sendto() TEST3: Sending data from unconnected client socket to
        server socket*/
        count = sendto(sk1, message, msg_count, flag,
		       (const struct sockaddr *) &conn_addr, len);
        if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from unconnected client to "
			 "server count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from unconnected client to server - SUCCESS");

        acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

        test_recv(acpt_sk, message_rcv, msg_count, flag);

	/*send() TEST4: Sending less number of data from the buffer*/
	/*Sending only 5 bytes so that only hello is received*/
	test_sendto(sk, message, 5 , flag, (const struct sockaddr *)&conn_addr,
		    len);
	test_recv(acpt_sk, message_rcv, 5, flag);
	
	tst_resm(TPASS, "sendto() partial data from a buffer - SUCCESS");

	close(sk1);
	close(lstn_sk);
	close(acpt_sk);
	return 0;
	
}
예제 #20
0
int 
main (int argc, char **argv)
{
	int sk1, sk2, sk3, pf_class;
	socklen_t len;
	struct sockaddr_in lstn_addr, acpt_addr;
	struct sockaddr_in conn_addr;
	char * buffer_rcv;
	struct sctp_initmsg sinmsg;
	char *message = "Hello World!\n";

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	/* Opening the socket*/
	
	pf_class = PF_INET;

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk3 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

        lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	test_bind(sk3, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	len = sizeof(struct sctp_initmsg);
	sinmsg.sinit_num_ostreams = 65535;
	sinmsg.sinit_max_instreams = 10;
	sinmsg.sinit_max_attempts = 1;
	sinmsg.sinit_max_init_timeo = 0;
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, len);
	sinmsg.sinit_num_ostreams = 10;
	sinmsg.sinit_max_instreams = 65535;
	test_setsockopt(sk3, SCTP_INITMSG, &sinmsg, len);

	test_listen(sk3, 1);

	len = sizeof(struct sockaddr_in);
	test_connect(sk1, (struct sockaddr *) &conn_addr, len);

	sk2 = test_accept(sk3, (struct sockaddr *) &acpt_addr, &len);

	test_sctp_sendmsg(sk1, message, strlen(message) + 1,
			  (struct sockaddr *)&conn_addr, len,
			  0, 0, 65534, 0, 0);

	buffer_rcv = malloc(100);
	test_recv(sk2, buffer_rcv, (strlen(message) + 1), MSG_NOSIGNAL);

	tst_resm(TPASS, "connect() with init timeout set to 0 - SUCCESS");

	close (sk1);
	close (sk2);
	close (sk3);
	
        return 0;
}
예제 #21
0
int
main(int argc, char *argv[])
{
        int sk, i;
        struct hostent *hst;
        sockaddr_storage_t host;
	sockaddr_storage_t msgname;
        struct iovec iov;
        struct msghdr inmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	int error, pf_class;
	char *big_buffer;
	char *local_host = NULL;
	int local_port = SCTP_TESTPORT_1; 
        int option_index = 0;
	time_t from, to;
	int bytes_received = 0;	
        int c;
        static struct option long_options[] = {
                {"local",	1, 0, 1},
		{"local-port",	1, 0, 2},
                {0,		0, 0, 0}
        };

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered. 
	 */
	setvbuf(stdout, NULL, _IONBF, 0); 

        /* Parse the arguments.  */
	while (1) {
		c = getopt_long (argc, argv, "H:P:",
                                 long_options, &option_index);
		if (c == -1)
                        break;

                switch (c) {
                case 0:
                        printf("option %s", long_options[option_index].name);
                        if (optarg) {
                                printf(" with arg %s", optarg);
                        }
                        printf("\n");
                        break;
                case 1:         /* local host */
                case 'H':
                        local_host = optarg;
                        break;
                case 2:         /* local port */
                case 'P':
                        local_port = atoi(optarg);
                        break;
                case '?':
			usage(argv[0]);
                        exit(0);

                default:
                        printf ("%s: unrecognized option 0%c\n", argv[0], c);
			usage(argv[0]);
                        exit(1);
                }
        }

        if (optind < argc)
        {
                fprintf(stderr, "%s: non-option arguments are illegal: ",
                        argv[0]);
                while (optind < argc)
                        fprintf(stderr, "%s ", argv[optind++]);
                fprintf (stderr, "\n");
		usage(argv[0]);
                exit(1);
        }

	if (!local_host) {
                fprintf(stderr, "%s: : option -H, --local is required\n",
                        argv[0]);
		usage(argv[0]);
                exit(1);
	}
	
	/* Set some basic values which depend on the address family. */
#if TEST_V6
        hst = gethostbyname2(local_host, AF_INET6);
        if (hst == NULL || hst->h_length < 1) {
                fprintf(stderr, "%s: bad hostname: %s\n", argv[0], local_host);
                exit(1);
        }
	pf_class = PF_INET6;

        host.v6.sin6_family = AF_INET6;
        memcpy(&host.v6.sin_addr, hst->h_addr_list[0], hst->h_length);
        host.v6.sin6_port = htons(local_port);

#else
	hst = gethostbyname(local_host);
        if (hst == NULL || hst->h_length < 1) {
                fprintf(stderr, "%s: bad hostname: %s\n", argv[0], local_host);
                exit(1);
        }
	pf_class = PF_INET;

        host.v4.sin_family = AF_INET;
        memcpy(&host.v4.sin_addr, hst->h_addr_list[0], hst->h_length);
        host.v4.sin_port = htons(local_port);

#endif /* TEST_V6 */

        /* Create the endpoint which will talk to nagle_snd.  */
        sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(sk);

        /* Bind the sockets to the test port.  */
        test_bind(sk, &host.sa, sizeof(host));
        
       /* Mark sk as being able to accept new associations.  */
	test_listen(sk, 1);
       
	printf("Listening on port:%d\n", local_port);
 
        /* Initialize inmessage for receives. */
        memset(&inmessage, 0, sizeof(inmessage));	
	big_buffer = test_malloc(REALLY_BIG);
        iov.iov_base = big_buffer;
        iov.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);
	inmessage.msg_name = &msgname;
	inmessage.msg_namelen = sizeof(msgname);
	memset(&msgname, 0, sizeof(msgname));

        /* Get the communication up message on sk.  */
        error = test_recvmsg(sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

	printf("Established connection with ");	
	if (AF_INET == msgname.sa.sa_family)
		printf("%d.%d.%d.%d(%d)\n", NIPQUAD(msgname.v4.sin_addr),
		       ntohs(msgname.v4.sin_port));
	if (AF_INET6 == msgname.sa.sa_family)
		printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x(%d)\n",
		       NIP6(msgname.v6.sin6_addr), ntohs(msgname.v6.sin6_port));

	time(&from);
	for (i=0; i<1000000; i++) {
	        inmessage.msg_controllen = sizeof(incmsg);
		inmessage.msg_namelen = sizeof(msgname);
	        error = test_recvmsg(sk, &inmessage, MSG_WAITALL);
		if (inmessage.msg_flags & MSG_NOTIFICATION)
			break;
		printf("Received %d bytes of data\n", error);
		bytes_received += error;
	}
	time(&to);

        printf("\t%d messages(%d bytes) successfully received in %ld "
	       "seconds.\n", i, bytes_received, to - from);
        printf("The receive rate is %ld bytes/second\n",
	       bytes_received/(to - from));

        /* Shut down the link.  */
	error = 0;
        close(sk);

	return 0;
}
int
main(int argc, char *argv[])
{
        int error,msg_count;
	socklen_t len;
	int sk,pf_class,lstn_sk,acpt_sk,flag,cflag,sflag;
	struct msghdr outmessage;
	struct msghdr inmessage;
        char *message = "hello, world!\n";
        struct iovec iov;
        struct iovec iov_rcv;
	struct sctp_sndrcvinfo *sinfo;
        int count;
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
        struct iovec out_iov;
        char * buffer_snd;
	char * buffer_rcv;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	
        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbufferd
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	flag = MSG_NOSIGNAL;
	
	/*Setting server socket non-blocking*/
	sflag = fcntl(lstn_sk, F_GETFL, 0);
	if (sflag < 0)
		tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed "
                         "sflag:%d, errno:%d", sflag, errno);

	error = fcntl(lstn_sk, F_SETFL, sflag | O_NONBLOCK);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed "
                         "error:%d, errno:%d", error, errno);

	/* TEST1: accept should return EAGAIN instead blocking. */
	error = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	if (error != -1 || errno != EAGAIN)
		tst_brkm(TBROK, tst_exit, "non-blocking accept "
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "non-blocking accept() - EAGAIN");

	/* TEST2: Non Block connect should return EINPROGRESS */
	/*Set client socket as non-blocking*/
	cflag = fcntl(sk, F_GETFL, 0);
	if (cflag < 0)
		tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed "
                         "cflag:%d, errno:%d", cflag, errno);

	error = fcntl(sk, F_SETFL, sflag | O_NONBLOCK);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed "
                         "error:%d, errno:%d", error, errno);

	error = connect(sk, (const struct sockaddr *) &conn_addr, len);
	if (error != -1 || errno != EINPROGRESS)
		tst_brkm(TBROK, tst_exit, "non-blocking connect "
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "non-blocking connect() - EINPROGRESS");

	/* TEST3: Now that connect() called, accept will succeed */
	acpt_sk = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	if (acpt_sk < 0)
		tst_brkm(TBROK, tst_exit, "accept after a non-blocking connect "
                         "error:%d, errno:%d", error, errno);
	
	tst_resm(TPASS, "accept() after a non-blocking connect - SUCCESS");

	memset(&outmessage, 0, sizeof(outmessage));
        buffer_snd = malloc(REALLY_BIG);

        outmessage.msg_name = &svr_addr;
        outmessage.msg_namelen = sizeof(svr_addr);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
        cmsg->cmsg_level = IPPROTO_SCTP;
        cmsg->cmsg_type = SCTP_SNDRCV;
        cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
        outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
        memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

	iov.iov_base = buffer_snd;
        iov.iov_len = REALLY_BIG;
        outmessage.msg_iov->iov_base = message;

        outmessage.msg_iov->iov_len = strlen(message) + 1;

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

	msg_count = strlen(message) + 1;

	/* TEST4: recvmsg() should return EAGAIN instead blocking */
	error = recvmsg(sk, &inmessage, MSG_WAITALL);
	if ( error != -1 || errno != EAGAIN)
		tst_brkm(TBROK, tst_exit, "non-blocking recvmsg "
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "non-blocking recvmsg() - EAGAIN");

	count = test_sendmsg(acpt_sk, &outmessage, flag, msg_count);

	/* TEST5: recvmsg() should succeed now as data is available. */
	error = test_recvmsg(sk, &inmessage, flag);
        test_check_msg_data(&inmessage, error, msg_count, MSG_EOR, 0, 0);

	tst_resm(TPASS, "non-blocking recvmsg() when data is available - "
		 "SUCCESS");

	close(lstn_sk);
	close(acpt_sk);
	return 0;
}
예제 #23
0
파일: test_getname.c 프로젝트: kraj/ltp
int
main(int argc, char *argv[])
{
	int clt_sk, svr_sk, accept_sk;
	sockaddr_storage_t svr_loop, accept_loop;
	sockaddr_storage_t svr_local_addr, svr_peer_addr;
	sockaddr_storage_t clt_local_addr, clt_peer_addr;
	socklen_t len;
	int error;
	int pf_class;
	int fd, err_no = 0;
	char filename[21];

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
#if TEST_V6
	pf_class = PF_INET6;
        svr_loop.v6.sin6_family = AF_INET6;
        svr_loop.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
        svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
#else
	pf_class = PF_INET;
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = INADDR_ANY;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
#endif

	/* Create and bind the listening server socket.  */
        svr_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));

	bzero(&svr_local_addr, sizeof(svr_local_addr));
	len = sizeof(svr_local_addr);
	/* Verify that getsockname() on an unconnected socket works fine. */
	error = getsockname(svr_sk, (struct sockaddr *)&svr_local_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on an unconnected socket");

	bzero(&svr_peer_addr, sizeof(svr_peer_addr));
	len = sizeof(svr_peer_addr);
	/* Verify that getpeername() on an unconnected socket fails. */
	error = getpeername(svr_sk, (struct sockaddr *)&svr_peer_addr, &len);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "getpeername on an unconnected "
			 "socket error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getpeername on an unconnected socket");

	/* Mark svr_sk as being able to accept new associations.  */
	test_listen(svr_sk, 5);

	/* Create the client socket.  */
	clt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	
	/* Do a blocking connect from clt_sk to svr_sk */      
#if TEST_V6
	svr_loop.v6.sin6_addr = in6addr_loopback;
#else
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
#endif
	test_connect(clt_sk, &svr_loop.sa, sizeof(svr_loop));

	bzero(&clt_local_addr, sizeof(clt_local_addr));
	len = sizeof(clt_local_addr);
	/* Get the client's local address. */
	error = getsockname(clt_sk, (struct sockaddr *)&clt_local_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname on a connected client "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on a connected client socket");

	bzero(&clt_peer_addr, sizeof(clt_peer_addr));
	len = sizeof(clt_peer_addr);
	/* Get the client's peer address. */
	error = getpeername(clt_sk, (struct sockaddr *)&clt_peer_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getpeername on a connected client "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getpeername on a connected client socket");

	/* Extract the association on the listening socket as a new socket. */
	len = sizeof(accept_loop);
	accept_sk = test_accept(svr_sk, &accept_loop.sa, &len); 

	bzero(&svr_local_addr, sizeof(svr_local_addr));
	len = sizeof(svr_local_addr);
	/* Get the server's local address. */
	error = getsockname(accept_sk, (struct sockaddr *)&svr_local_addr,
				&len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname on a connected server "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on a connected server socket");

	bzero(&svr_peer_addr, sizeof(svr_peer_addr));
	len = sizeof(svr_peer_addr);
	/* Get the server's peer address. */
	error = getpeername(accept_sk, (struct sockaddr *)&svr_peer_addr,
				&len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getpeername on a connected server "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getpeername on a connected server socket");

	if (svr_local_addr.v4.sin_port != clt_peer_addr.v4.sin_port)
		tst_brkm(TBROK, tst_exit, "Server's local port(%d) doesn't "
			 "match Client's peer port(%d)\n",
			 svr_local_addr.v4.sin_port, clt_peer_addr.v4.sin_port);

	if (svr_peer_addr.v4.sin_port != clt_local_addr.v4.sin_port)
		tst_brkm(TBROK, tst_exit, "Server's peer port(%d) doesn't "
			 "match Client's local port(%d)\n",
			 svr_peer_addr.v4.sin_port, clt_local_addr.v4.sin_port);
#if TEST_V6
	if (memcmp(&svr_local_addr, &clt_peer_addr, len) != 0)
		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
			 "peer addresses do not match\n");

	if (memcmp(&svr_peer_addr, &clt_local_addr, len) != 0)
		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
			 "local addresses do not match\n");
#else
	if (svr_local_addr.v4.sin_addr.s_addr !=
		 		clt_peer_addr.v4.sin_addr.s_addr)
		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
			 "peer addresses do not match\n");
	if (svr_peer_addr.v4.sin_addr.s_addr !=
		 		clt_local_addr.v4.sin_addr.s_addr)
		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
			 "local addresses do not match\n");
#endif
	tst_resm(TPASS, "getsockname/getpeername server/client match");

	bzero(&clt_local_addr, sizeof(clt_local_addr));
	len = sizeof(clt_local_addr);
	/*getsockname():  Bad socket descriptor, EBADF expected error*/
	error = getsockname(-1, (struct sockaddr *)&clt_local_addr, &len);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "getsockname on a bad socket "
			 "descriptor. error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getsockname on a bad socket descriptor - EBADF");

	/*getsockname(): Invalid socket, ENOTSOCK expected error*/
	strcpy(filename, "/tmp/sctptest.XXXXXX");
	fd = mkstemp(filename);
	if (fd == -1)
		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
				filename, strerror(errno));
	error = getsockname(fd, (struct sockaddr *)&clt_local_addr, &len);
	if (error == -1)
		err_no = errno;
	close(fd);
	unlink(filename);
	if (error != -1 || err_no != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "getsockname on an invalid socket "
			 "error:%d errno:%d", error, err_no);

	tst_resm(TPASS, "getsockname on an invalid socket - ENOTSOCK");

	/*getsockname(): Invalid structure, EFAULT expected error*/
	error = getsockname(clt_sk, (struct sockaddr *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "getsockname with invalid buffer "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getsockname with invalid buffer - EFAULT");
 
	bzero(&clt_peer_addr, sizeof(clt_peer_addr));
	len = sizeof(clt_peer_addr);
	/*getpeername():  Bad socket descriptor, EBADF expected error*/
	error = getpeername(-1, (struct sockaddr *)&clt_local_addr, &len);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "getpeername on a bad socket "
			 "descriptor. error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getpeername on a bad socket descriptor - EBADF");

	/*getpeername(): Invalid socket, ENOTSOCK expected error*/
	strcpy(filename, "/tmp/sctptest.XXXXXX");
	fd = mkstemp(filename);
	if (fd == -1)
		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
				filename, strerror(errno));
	error = getpeername(fd, (struct sockaddr *)&clt_local_addr, &len);
	if (error == -1)
		err_no = errno;
	close(fd);
	unlink(filename);
	if (error != -1 || err_no != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "getpeername on an invalid socket "
			 "error:%d errno:%d", error, err_no);

	tst_resm(TPASS, "getpeername on an invalid socket - ENOTSOCK");

	/*getpeername(): Invalid structure, EFAULT expected error*/
	error = getpeername(clt_sk, (struct sockaddr *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "getpeername with invalid buffer "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getpeername with invalid buffer - EFAULT");
 
	close(clt_sk);
	close(svr_sk);
	close(accept_sk);

        /* Indicate successful completion.  */
	return 0;
}
예제 #24
0
int
main(int argc, char *argv[])
{
	int error,i;
	socklen_t len;
	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
	int sk1,clnt2_sk;

	struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
	struct sockaddr *tmp_addr;

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	pf_class = PF_INET;

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a listen socket*/
	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a regular socket*/
	for (i = 0 ; i < SK_MAX ; i++)
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/*Listening the socket*/
	test_listen(lstn_sk, SK_MAX-1);


	/*sctp_connectx () TEST1: Bad socket descriptor, EBADF Expected error*/
	len = sizeof(struct sockaddr_in);
	error = sctp_connectx(-1, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with bad socket descriptor - EBADF");
	
	/*sctp_connectx () TEST2: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_connectx(0, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");

	/*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
	tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
	tmp_addr->sa_family = AF_INET;
	error = sctp_connectx(sk, tmp_addr, 1, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address - EINVAL");

	/*sctp_connectx () TEST4: Invalid address length, EINVAL Expected error*/
	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 0, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address length "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address length - EINVAL");

	/*sctp_connectx () TEST5: Invalid address family, EINVAL Expect error*/
	conn_addr.sin_family = 9090; /*Assigning invalid address family*/
	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address family "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address family - EINVAL");

	conn_addr.sin_family = AF_INET;

	/*sctp_connectx () TEST6: Blocking sctp_connectx, should pass*/
	/*All the be below blocking sctp_connectx should pass as socket will be 
	listening SK_MAX clients*/
	for (i = 0 ; i < SK_MAX ; i++) {
		error = sctp_connectx(clnt_sk[i], (struct sockaddr *)&conn_addr,
			      1, NULL);
		if (error < 0)
			tst_brkm(TBROK, tst_exit, "valid blocking sctp_connectx "
				 "error:%d, errno:%d", error, errno);
	}

	tst_resm(TPASS, "valid blocking sctp_connectx() - SUCCESS");

	/*sctp_connectx () TEST7: sctp_connectx when accept queue is full, ECONNREFUSED
	Expect error*/
	/*Now that accept queue is full, the below sctp_connectx should fail*/
	error = sctp_connectx(clnt2_sk, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != ECONNREFUSED)
		tst_brkm(TBROK, tst_exit, "sctp_connectx when accept queue is full "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() when accept queue is full - ECONNREFUSED");
	
	/*Calling a accept first to estblish the pending sctp_connectxions*/
	for (i=0 ; i < SK_MAX ; i++)
		acpt_sk[i] = test_accept(lstn_sk,
					 (struct sockaddr *) &acpt_addr, &len);

	/*sctp_connectx () TEST8: from a listening socket, EISCONN Expect error*/
	error = sctp_connectx(lstn_sk, (struct sockaddr *) &lstn_addr, 1, NULL);
	if (error != -1 || errno != EISCONN)
		tst_brkm(TBROK, tst_exit, "sctp_connectx on a listening socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() on a listening socket - EISCONN");

	/*sctp_connectx() TEST9: On established socket, EISCONN Expect error*/
	i=0;
	error = sctp_connectx(acpt_sk[i], (struct sockaddr *) &lstn_addr, 1, NULL);
	if (error != -1 || errno != EISCONN)
		tst_brkm(TBROK, tst_exit, "sctp_connectx on an established socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() on an established socket - EISCONN");

	for (i = 0 ; i < 4 ; i++) {
		close(clnt_sk[i]);
		close(acpt_sk[i]);
	} 

	/* sctp_connectx() TEST10: Re-establish an association that is closed.
	 * should succeed.
	 */
	error = sctp_connectx(sk1, (struct sockaddr *)&conn_addr, 1, NULL);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "Re-establish an association that "
				 "is closed error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() to re-establish a closed association - "
		 "SUCCESS");

	close(sk);
	close(sk1);
	close(lstn_sk);

	return 0;
}
예제 #25
0
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk,acpt_sk;
	struct sockaddr_in svr_loop, clt_loop,acpt_loop;
	struct iovec iov, out_iov;
	struct msghdr inmessage, outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	int error;
	socklen_t len;
	char *big_buffer;
	struct sctp_event_subscribe event;
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
	char *message = "hello, world!\n";
	uint32_t ppid;
	uint32_t stream;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
	svr_loop.sin_family = AF_INET;
	svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.sin_port = htons(SCTP_TESTPORT_1);

	clt_loop.sin_family = AF_INET;
	clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	clt_loop.sin_port = htons(SCTP_TESTPORT_1);

	/* Create and bind the server socket.  */
        svr_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(svr_sk, (struct sockaddr *) &svr_loop, sizeof(svr_loop));

	/* Mark server socket as being able to accept new associations.  */
	test_listen(svr_sk, 3);

	/* Create the client socket.  */
	clt_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);

	event.sctp_data_io_event = 1;
	event.sctp_association_event = 1;
	event.sctp_shutdown_event = 1;
	len = sizeof(struct sctp_event_subscribe);
	test_setsockopt(svr_sk, SCTP_EVENTS, &event, len);
	test_setsockopt(clt_sk, SCTP_EVENTS, &event, len);

	len = sizeof(struct sockaddr_in);
	test_connect(clt_sk, (struct sockaddr *) &clt_loop, len);
	
	acpt_sk = test_accept(svr_sk, (struct sockaddr *) &acpt_loop, &len);

	/* Build up a msghdr structure we can use for all sending.  */
	memset(&outmessage, 0, sizeof(outmessage));	
	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;

	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;

	outmessage.msg_iov->iov_base = message;
	outmessage.msg_iov->iov_len = (strlen(message) + 1);

	/* Send . This will create the association*/
	test_sendmsg(clt_sk, &outmessage, 0, strlen(message)+1);

        memset(&inmessage, 0, sizeof(inmessage));
	/* NOW initialize inmessage with enough space for DATA... */
	big_buffer = malloc(REALLY_BIG);
	if (!big_buffer) { DUMP_CORE; }

	/* Let's do a test to do a recvmsg when we are not listening and
	 * when we have no associations.
	 */
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	error = test_recvmsg(clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_assoc_change),
                                    SCTP_ASSOC_CHANGE,
                                    SCTP_COMM_UP);
	
	tst_resm(TPASS, "COMM_UP notification on client socket - SUCCESS");

	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_assoc_change),
                                    SCTP_ASSOC_CHANGE,
                                    SCTP_COMM_UP);
	
	tst_resm(TPASS, "COMM_UP notification on server socket - SUCCESS");

	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
                            MSG_EOR, stream, ppid);

	tst_resm(TPASS, "Data message on server socket - SUCCESS");

	close(clt_sk);
	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_shutdown_event),
                                    SCTP_SHUTDOWN_EVENT,
                                    0);

	tst_resm(TPASS, "SHUTDOWN notification on accepted socket - SUCCESS");
	close(svr_sk);
	close(acpt_sk);

	return 0;
}
예제 #26
0
int
main(int argc, char *argv[])
{
        int msg_count;
	socklen_t len;
	int sk,pf_class,lstn_sk,acpt_sk, flag;
        char *message = "hello, world!\n";
	char *message_rcv;
        int count;

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

	message_rcv = malloc(512);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	
	msg_count = (strlen(message) + 1);

	flag = MSG_NOSIGNAL;
	/*Sending the message*/
	count = test_send(sk, message, msg_count, flag);

	/*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvfrom(-1, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with a bad socket descriptor - EBADF");

	/*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvfrom(0, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with invalid socket - ENOTSOCK");

	/*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/
	count = recvfrom(acpt_sk, (char *)-1, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid message "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT");

	/*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/
	count = recvfrom(lstn_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message, msg_count, flag);

	test_shutdown(sk, SHUT_WR);

	/*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/
	count = recvfrom(acpt_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - "
		 "EOF");

	/*recvfrom () TEST6:reading the pending message on socket that sent 
	SHUTDOWN*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvfrom () TEST7: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	return 0;
	
}
예제 #27
0
int main(void)
{
	int error;
	socklen_t len;
	int sk, sk1, sk2, acpt_sk, pf_class;
	struct sctp_rtoinfo grtinfo;
	struct sockaddr_in lstn_addr, conn_addr;
	struct sctp_initmsg ginmsg;	/*get the value for SCTP_INITMSG */
	struct sctp_initmsg sinmsg;	/*set the value for SCTP_INITMSG */
	struct linger slinger;	/*SO_LINGER structure */
	struct linger glinger;	/*SO_LINGER structure */
	struct sockaddr_in addr;
	struct sockaddr_in *gaddr;
	struct sctp_status gstatus;	/*SCTP_STATUS option */
	int rcvbuf_val_get, rcvbuf_val_set;	/*get and set var for SO_RCVBUF */
	int sndbuf_val_get, sndbuf_val_set;	/*get and set var for SO_SNDBUF */
	struct sctp_prim gprimaddr;	/*SCTP_PRIMARY_ADDR get */
	struct sctp_prim sprimaddr;	/*SCTP_PRIMARY_ADDR set */
	struct sctp_assocparams sassocparams;	/* SCTP_ASSOCPARAMS set */
	struct sctp_assocparams gassocparams;	/* SCTP_ASSOCPARAMS get */

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	pf_class = PF_INET;

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*setsockopt() TEST1: Bad socket descriptor EBADF, Expected error */
	error = setsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "setsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with a bad socket descriptor - EBADF");

	/*setsockopt() TEST2: Invalid socket ENOTSOCK, Expected error */
	error = setsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "setsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid socket - ENOTSOCK");

	/*setsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	error = setsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid level - ENOPROTOOPT");

	/*setsockopt() TEST4: Invalid option buffer EFAULT, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (const struct sctp_rtoinfo *)-1,
			   sizeof(struct sctp_rtoinfo));
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option buffer - EFAULT");

	/*setsockopt() TEST5: Invalid option Name EOPNOTSUPP, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, 0, 0);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option name - EOPNOTSUPP");

	/*getsockopt() TEST6: Bad socket descriptor EBADF, Expected error */
	error = getsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "getsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with a bad socket descriptor - EBADF");

	/*getsockopt() TEST7: Invalid socket ENOTSOCK, Expected error */
	error = getsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "getsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid socket - ENOTSOCK");
#if 0
	/*getsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	/*I have commented this test case because it is returning EOPNOTSUPP.
	   When I checked the code there also it is returning EOPNOTSUPP. As this
	   is not specific to TCP style, I do not want to do the code change */

	error = getsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid level - ENOPROTOOPT");
#endif
	len = sizeof(struct sctp_rtoinfo);

	/*getsockopt() TEST8: Invalid option buffer EFAULT, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (struct sctp_rtoinfo *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option buffer - EFAULT");

	/*getsockopt() TEST9: Invalid option Name EOPNOTSUPP, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, &grtinfo, &len);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option name - EOPNOTSUPP");

	close(sk);

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk2 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	conn_addr.sin_family = AF_INET;
	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	len = sizeof(struct sctp_initmsg);

	/* TEST10: Test cases for getsockopt SCTP_INITMSG */
	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	tst_resm(TPASS, "getsockopt() SCTP_INITMSG - SUCCESS");

	sinmsg.sinit_num_ostreams = 5;
	sinmsg.sinit_max_instreams = 5;
	sinmsg.sinit_max_attempts = 3;
	sinmsg.sinit_max_init_timeo = 30;
	/* TEST11: Test case for setsockopt SCTP_INITMSG */
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, sizeof(sinmsg));

	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	if (sinmsg.sinit_num_ostreams != ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams != ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts != ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo != ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "compare failed");

	tst_resm(TPASS, "setsockopt() SCTP_INITMSG - SUCCESS");

	/*Now get the values on different endpoint */
	test_getsockopt(sk2, SCTP_INITMSG, &ginmsg, &len);

	/*Comparison should not succeed here */
	if (sinmsg.sinit_num_ostreams == ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams == ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts == ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo == ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "unexpected compare success");

	/* SO_LINGER Test with l_onff = 0 and l_linger = 0 */
	slinger.l_onoff = 0;
	slinger.l_linger = 0;
	test_bind(sk1, (struct sockaddr *)&lstn_addr, sizeof(lstn_addr));
	test_listen(sk1, 10);
	len = sizeof(struct sockaddr_in);
	test_connect(sk2, (struct sockaddr *)&conn_addr, len);

	acpt_sk = test_accept(sk1, (struct sockaddr *)&addr, &len);

	len = sizeof(struct linger);
	/* TEST12: Test case for setsockopt SO_LINGER */
	error = setsockopt(sk2, SOL_SOCKET, SO_LINGER, &slinger, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_LINGER - SUCCESS");

	/* TEST13: Test case for getsockopt SO_LINGER */
	error = getsockopt(sk2, SOL_SOCKET, SO_LINGER, &glinger, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_LINGER - SUCCESS");

	if (slinger.l_onoff != glinger.l_onoff ||
	    slinger.l_linger != glinger.l_linger)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SO_LINGER "
			 "compare failed");

	/*First gets the default SO_RCVBUF value and comapres with the
	   value obtained from SCTP_STATUS */
	len = sizeof(int);
	/* TEST14: Test case for getsockopt SO_RCVBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_RCVBUF - SUCCESS");

	len = sizeof(struct sctp_status);
	/* TEST15: Test case for getsockopt SCTP_STATUS */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_STATUS "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_STATUS - SUCCESS");

	/* Reducing the SO_RCVBUF value using setsockopt() */
	/*Minimum value is 128 and hence I am using it */
	len = sizeof(int);
	rcvbuf_val_set = 128;
	/* TEST16: Test case for setsockopt SO_RCVBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_RCVBUF - SUCCESS");

	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	if ((2 * rcvbuf_val_set) != rcvbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d",
			 (2 * rcvbuf_val_set), rcvbuf_val_get);

	sndbuf_val_set = 1024;
	/* TEST17: Test case for setsockopt SO_SNDBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_SNDBUF - SUCCESS");

	/* TEST18: Test case for getsockopt SO_SNDBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_SNDBUF - SUCCESS");

	if ((2 * sndbuf_val_set) != sndbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d\n",
			 (2 * sndbuf_val_set), sndbuf_val_get);

	/* Getting the primary address using SCTP_PRIMARY_ADDR */
	len = sizeof(struct sctp_prim);
	/* TEST19: Test case for getsockopt SCTP_PRIMARY_ADDR */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &gprimaddr,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	gaddr = (struct sockaddr_in *)&gprimaddr.ssp_addr;
	if (htons(gaddr->sin_port) != lstn_addr.sin_port &&
	    gaddr->sin_family != lstn_addr.sin_family &&
	    gaddr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR value "
			 "mismatch");

	memcpy(&sprimaddr, &gprimaddr, sizeof(struct sctp_prim));

	/* TEST20: Test case for setsockopt SCTP_PRIMARY_ADDR */
	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &sprimaddr,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	/* TEST21: Test case for getsockopt SCTP_PRIMARY_ADDR */
	/* Getting the association info using SCTP_ASSOCINFO */
	len = sizeof(struct sctp_assocparams);
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_ASSOCINFO - SUCCESS");

	/* TEST21: Test case for setsockopt SCTP_ASSOCINFO */
	memcpy(&sassocparams, &gassocparams, sizeof(struct sctp_assocparams));
	sassocparams.sasoc_asocmaxrxt += 5;
	sassocparams.sasoc_cookie_life += 10;

	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &sassocparams,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	if (sassocparams.sasoc_asocmaxrxt != gassocparams.sasoc_asocmaxrxt ||
	    sassocparams.sasoc_cookie_life != gassocparams.sasoc_cookie_life)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO value "
			 "mismatch");
	tst_resm(TPASS, "setsockopt() SCTP_ASSOCINFO - SUCCESS");

	close(sk2);
	close(sk1);
	close(acpt_sk);

	tst_exit();
}
예제 #28
0
int main(int argc, char *argv[])
{
	int sk1, sk2;
	sockaddr_storage_t loop1;
	sockaddr_storage_t loop2;
	sockaddr_storage_t msgname;
	int error;
	int pf_class;
	uint32_t ppid;
	uint32_t stream;
	struct sctp_event_subscribe subscribe;
	char *big_buffer;
	int offset, msg_flags;
	socklen_t msgname_len;
	size_t buflen;
	struct sctp_send_failed *ssf;
	struct sctp_sndrcvinfo sinfo;
	struct sctp_sndrcvinfo snd_sinfo;
	sctp_assoc_t associd1;
	socklen_t len, oldlen;
	struct sctp_status gstatus;

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);

	/* Set some basic values which depend on the address family. */
#if TEST_V6
	pf_class = PF_INET6;

	loop1.v6.sin6_family = AF_INET6;
	loop1.v6.sin6_addr = in6addr_loopback;
	loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);

	loop2.v6.sin6_family = AF_INET6;
	loop2.v6.sin6_addr = in6addr_loopback;
	loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
#else
	pf_class = PF_INET;

	loop1.v4.sin_family = AF_INET;
	loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	loop1.v4.sin_port = htons(SCTP_TESTPORT_1);

	loop2.v4.sin_family = AF_INET;
	loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
#endif /* TEST_V6 */

	/* Create the two endpoints which will talk to each other.  */
	sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
        sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Set the MAXSEG to something smallish. */
	{
		int val = SMALL_MAXSEG;
		test_setsockopt(sk1, SCTP_MAXSEG, &val, sizeof(val));
	}

	memset(&subscribe, 0, sizeof(subscribe));
	subscribe.sctp_data_io_event = 1;
	subscribe.sctp_association_event = 1;
	subscribe.sctp_send_failure_event = 1;
	test_setsockopt(sk1, SCTP_EVENTS, &subscribe, sizeof(subscribe));
	test_setsockopt(sk2, SCTP_EVENTS, &subscribe, sizeof(subscribe));

        /* Bind these sockets to the test ports.  */
        test_bind(sk1, &loop1.sa, sizeof(loop1));
        test_bind(sk2, &loop2.sa, sizeof(loop2));

	/*
	 * Set the RWND small so we can fill it up easily.
	 * then reset RCVBUF to avoid frame droppage
	 */
	len = sizeof(int);
	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &oldlen, &len);
       
	if (error)
		tst_brkm(TBROK, tst_exit, "can't get rcvbuf size: %s",
			 strerror(errno));

	len = SMALL_RCVBUF; /* Really becomes 2xlen when set. */

	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));
	if (error)
		tst_brkm(TBROK, tst_exit, "setsockopt(SO_RCVBUF): %s",
			 strerror(errno));

       /* Mark sk2 as being able to accept new associations.  */
	test_listen(sk2, 1);

	/* Send the first message.  This will create the association.  */
	ppid = rand();
	stream = 1;
	test_sctp_sendmsg(sk1, message, strlen(message) + 1,
			  (struct sockaddr *)&loop2, sizeof(loop2),
			  ppid, 0, stream, 0, 0);

	tst_resm(TPASS, "sctp_sendmsg");

	/* Get the communication up message on sk2.  */
	buflen = REALLY_BIG;
	big_buffer = test_malloc(buflen);
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk2, big_buffer, buflen,
				  (struct sockaddr *)&msgname, &msgname_len,
				  &sinfo, &msg_flags);
#if 0
	associd2 = ((struct sctp_assoc_change *)big_buffer)->sac_assoc_id;
#endif
	test_check_buf_notification(big_buffer, error, msg_flags,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);


	/* restore the rcvbuffer size for the receiving socket */
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &oldlen,
			   sizeof(oldlen));

	if (error)
		tst_brkm(TBROK, tst_exit, "setsockopt(SO_RCVBUF): %s",
			 strerror(errno));

	/* Get the communication up message on sk1.  */
	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk1, big_buffer, buflen,
				  (struct sockaddr *)&msgname, &msgname_len,
				  &sinfo, &msg_flags); 
	associd1 = ((struct sctp_assoc_change *)big_buffer)->sac_assoc_id;
	test_check_buf_notification(big_buffer, error, msg_flags,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);

	tst_resm(TPASS, "sctp_recvmsg SCTP_COMM_UP notification");

	/* Get the first message which was sent.  */
	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk2, big_buffer, buflen,
				  (struct sockaddr *)&msgname, &msgname_len,
				  &sinfo, &msg_flags); 
	test_check_buf_data(big_buffer, error, msg_flags, &sinfo,
			    strlen(message) + 1, MSG_EOR, stream, ppid); 

	tst_resm(TPASS, "sctp_recvmsg data");

	/* Figure out how big to make our fillmsg */
	len = sizeof(struct sctp_status);
	memset(&gstatus,0,sizeof(struct sctp_status));
	gstatus.sstat_assoc_id = associd1;
	error = getsockopt(sk1, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);

	if (error)
		tst_brkm(TBROK, tst_exit, "can't get rwnd size: %s",
			strerror(errno));
	tst_resm(TINFO, "creating a fillmsg of size %d",
		gstatus.sstat_rwnd+RWND_SLOP);
        fillmsg = malloc(gstatus.sstat_rwnd+RWND_SLOP);

	/* Send a fillmsg */
	memset(fillmsg, 'X', gstatus.sstat_rwnd+RWND_SLOP);
	fillmsg[gstatus.sstat_rwnd+RWND_SLOP-1] = '\0';
	ppid++;
	stream++;
	test_sctp_sendmsg(sk1, fillmsg, gstatus.sstat_rwnd+RWND_SLOP, 
			  (struct sockaddr *)&loop2, sizeof(loop2),
			  ppid, 0, stream, 0, 0);

	/* Now send a message that will timeout. */
	test_sctp_sendmsg(sk1, ttlmsg, strlen(ttlmsg) + 1,
			  (struct sockaddr *)&loop2, sizeof(loop2),
			  ppid, 0, stream, 2000, 0);

	tst_resm(TPASS, "sctp_sendmsg with ttl");

	/* Next send a message that won't time out. */
	test_sctp_sendmsg(sk1, nottlmsg, strlen(nottlmsg) + 1,
			  (struct sockaddr *)&loop2, sizeof(loop2),
			  ppid, 0, stream, 0, 0);

	tst_resm(TPASS, "sctp_sendmsg with zero ttl");

	/* And finally a fragmented message that will time out. */
	memset(ttlfrag, '0', sizeof(ttlfrag));
	ttlfrag[sizeof(ttlfrag)-1] = '\0';
	test_sctp_sendmsg(sk1, ttlfrag, sizeof(ttlfrag),
			  (struct sockaddr *)&loop2, sizeof(loop2),
			  ppid, 0, stream, 2000, 0);

	tst_resm(TPASS, "sctp_sendmsg fragmented msg with ttl");

	/* Sleep waiting for the message to time out. */
	tst_resm(TINFO, "**  SLEEPING for 3 seconds **");
	sleep(3);

	/* Get the fillmsg. */
	do {
		buflen = REALLY_BIG;
		msgname_len = sizeof(msgname);
		msg_flags = 0;
		test_sctp_recvmsg(sk2, big_buffer, buflen,
			  (struct sockaddr *)&msgname, &msgname_len,
			  &sinfo, &msg_flags); 
	} while (!(msg_flags & MSG_EOR));

	/* Get the message that did NOT time out. */
	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk2, big_buffer, buflen,
			  (struct sockaddr *)&msgname, &msgname_len,
			  &sinfo, &msg_flags); 
	test_check_buf_data(big_buffer, error, msg_flags, &sinfo,
			    strlen(nottlmsg) + 1, MSG_EOR, stream, ppid); 
	if (0 != strncmp(big_buffer, nottlmsg, strlen(nottlmsg)))
		tst_brkm(TBROK, tst_exit, "sctp_recvmsg: Wrong Message !!!");

	tst_resm(TPASS, "sctp_recvmsg msg with zero ttl");

	/* Get the SEND_FAILED notification for the message that DID
	 * time out.
	 */
	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk1, big_buffer, buflen,
			  (struct sockaddr *)&msgname, &msgname_len,
			  &sinfo, &msg_flags); 
	test_check_buf_notification(big_buffer, error, msg_flags,
				    sizeof(struct sctp_send_failed) +
							strlen(ttlmsg) + 1,
				    SCTP_SEND_FAILED, 0);
	ssf = (struct sctp_send_failed *)big_buffer;
	if (0 != strncmp(ttlmsg, (char *)ssf->ssf_data, strlen(ttlmsg) + 1))
		tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch");

	tst_resm(TPASS, "sctp_recvmsg SEND_FAILED for message with ttl");

	offset = 0;

	/* Get the SEND_FAILED notifications for the fragmented message that 
	 * timed out.
	 */
	do {
		buflen = REALLY_BIG;
		msgname_len = sizeof(msgname);
		msg_flags = 0;
		error = test_sctp_recvmsg(sk1, big_buffer, buflen,
			  (struct sockaddr *)&msgname, &msgname_len,
			  &sinfo, &msg_flags); 
		test_check_buf_notification(big_buffer, error, msg_flags,
					    sizeof(struct sctp_send_failed) +
							          SMALL_MAXSEG,
					    SCTP_SEND_FAILED, 0);
		ssf = (struct sctp_send_failed *)big_buffer;
		if (0 != strncmp(&ttlfrag[offset], (char *)ssf->ssf_data,
				 SMALL_MAXSEG))
			tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch");
		offset += SMALL_MAXSEG;
	} while (!(ssf->ssf_info.sinfo_flags & 0x01)); /* LAST FRAG */

	tst_resm(TPASS, "sctp_recvmsg SEND_FAILED for fragmented message with "
		 "ttl");

	snd_sinfo.sinfo_ppid = rand();
	snd_sinfo.sinfo_flags = 0; 
	snd_sinfo.sinfo_stream = 2; 
	snd_sinfo.sinfo_timetolive = 0; 
	snd_sinfo.sinfo_assoc_id = associd1; 
	test_sctp_send(sk1, message, strlen(message) + 1, &snd_sinfo,
		       MSG_NOSIGNAL);

	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk2, big_buffer, buflen,
				  (struct sockaddr *)&msgname, &msgname_len,
				  &sinfo, &msg_flags); 
	test_check_buf_data(big_buffer, error, msg_flags, &sinfo,
			    strlen(message) + 1, MSG_EOR, snd_sinfo.sinfo_stream,
			    snd_sinfo.sinfo_ppid); 

	tst_resm(TPASS, "sctp_send");

	/* Shut down the link.  */
	close(sk1);

	/* Get the shutdown complete notification. */
	buflen = REALLY_BIG;
	msgname_len = sizeof(msgname);
	msg_flags = 0;
	error = test_sctp_recvmsg(sk2, big_buffer, buflen,
		  (struct sockaddr *)&msgname, &msgname_len,
		  &sinfo, &msg_flags); 
	test_check_buf_notification(big_buffer, error, msg_flags,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	close(sk2);

	/* Indicate successful completion.  */
	return 0;	
}
예제 #29
0
int
main(int argc, char *argv[])
{
	int clt_sk[MAX_CLIENTS], accept_sk[MAX_CLIENTS];
	int listen_sk, clt2_sk, accept2_sk;
	sockaddr_storage_t clt_loop[MAX_CLIENTS];
	sockaddr_storage_t svr_loop, accept_loop, clt2_loop;
	socklen_t addrlen;
	int error, i;
        char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;
	struct pollfd poll_fd;
	fd_set set;
	struct msghdr outmessage;
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct iovec out_iov;
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
	struct msghdr inmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *big_buffer;
	struct iovec iov;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
#if TEST_V6
	pf_class = PF_INET6;
        svr_loop.v6.sin6_family = AF_INET6;
        svr_loop.v6.sin6_addr = in6addr_loopback;
        svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
        	clt_loop[i].v6.sin6_family = AF_INET6;
        	clt_loop[i].v6.sin6_addr = in6addr_loopback;
        	clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
	}
        clt2_loop.v6.sin6_family = AF_INET6;
        clt2_loop.v6.sin6_addr = in6addr_loopback;
        clt2_loop.v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
#else
	pf_class = PF_INET;
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_loop[i].v4.sin_family = AF_INET;
		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
	}
	clt2_loop.v4.sin_family = AF_INET;
	clt2_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	clt2_loop.v4.sin_port = htons(SCTP_TESTPORT_2 + i);
#endif

	/* Create and bind the listening server socket.  */
        listen_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(listen_sk, &svr_loop.sa, sizeof(svr_loop));

	/* Mark listen_sk as being able to accept new associations.  */
	test_listen(listen_sk, MAX_CLIENTS-1);

	/* Create and bind the client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
	}
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	addrlen = sizeof(accept_loop);
	/* Try to do accept on a non-listening socket. It should fail. */
	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept on non-listening socket");

	/* Try to do a connect from a listening socket. It should fail. */
	error = connect(listen_sk, (struct sockaddr *)&clt_loop[0],
			sizeof(clt_loop[0]));
	if ((-1 != error) && (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect to non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect to non-listening socket");

	/* Do a blocking connect from clt_sk's to listen_sk */      
	for (i = 0; i < MAX_CLIENTS; i++)
		test_connect(clt_sk[i], &svr_loop.sa, sizeof(svr_loop));

	tst_resm(TPASS, "connect to listening socket");

	/* Verify that no more connect's can be done after the acceptq
	 * backlog has reached the max value.
	 */
	error = connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));
	if ((-1 != error) && (ECONNREFUSED != errno))
		tst_brkm(TBROK, tst_exit, "connect after max backlog "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect after max backlog");

	/* Extract the associations on the listening socket as new sockets. */
	for (i = 0; i < MAX_CLIENTS; i++) {
		poll_fd.fd = listen_sk;
		poll_fd.events = POLLIN;
		poll_fd.revents = 0;
		error = poll(&poll_fd, 1, -1);
		if ((1 != error) && (1 != poll_fd.revents))
			tst_brkm(TBROK, tst_exit, "Unexpected return value "
				 "with poll, error:%d errno:%d, revents:%d",
				 error, errno, poll_fd.revents);

		addrlen = sizeof(accept_loop);
		accept_sk[i] = test_accept(listen_sk, &accept_loop.sa,
					   &addrlen); 
	}

	tst_resm(TPASS, "accept from listening socket");

	/* Try to do a connect on an established socket. It should fail. */
	error = connect(accept_sk[0], &clt_loop[0].sa, sizeof(clt_loop[0]));
	if ((-1 != error) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect on an established socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "connect on an established socket");

	/* Try to do accept on an established socket. It should fail. */
	error = accept(accept_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "error:%d errno:%d", error, errno);

	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "failure: error:%d errno:%d", error, errno);

	tst_resm(TPASS, "accept on an established socket");

	/* Send and receive a message from the client sockets to the accepted
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(clt_sk[i], message, strlen(message), 0);
		test_recv(accept_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "client sockets -> accepted sockets");

	/* Send and receive a message from the accepted sockets to the client
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(accept_sk[i], message, strlen(message), 0);
		test_recv(clt_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "accepted sockets -> client sockets");

	/* Sending a message on a listening socket should fail. */
	error = send(listen_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send on a listening socket");

	/* Trying to receive a message on a listening socket should fail. */
	error = recv(listen_sk, msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a listening socket");

	/* TESTCASES for shutdown() */
	errno = 0;
	test_send(accept_sk[0], message, strlen(message), 0);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(clt_sk[0]);

	/* Do a SHUT_WR on clt_sk[0] to disable any new sends. */
	test_shutdown(clt_sk[0], SHUT_WR);

	/* Reading on a socket that has received SHUTDOWN should return 0 
	 * indicating EOF.
	 */
	error = recv(accept_sk[0], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN received socket");

	/* Read the pending message on clt_sk[0] that was received before
	 * SHUTDOWN call.
	 */  
	test_recv(clt_sk[0], msgbuf, 100, 0);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
	memset(&inmessage, 0, sizeof(inmessage));	
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	/* Receive the SHUTDOWN_COMP notification as they are enabled. */
	error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	tst_resm(TPASS, "recv SHUTDOWN_COMP notification on a SHUT_WR socket");

	/* No more messages and the association is SHUTDOWN, should fail. */
	error = recv(clt_sk[0], msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN sent socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN sent socket");

	errno = 0;

	/* Do a SHUT_RD on clt_sk[1] to disable any new receives. */
	test_shutdown(clt_sk[1], SHUT_RD);

	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Sending a message on SHUT_RD socket. */
	test_send(clt_sk[1], message, strlen(message), 0);

	/* Receive the message sent on SHUT_RD socket. */
	test_recv(accept_sk[1], msgbuf, 100, 0);

	/* Send a message to the SHUT_RD socket. */
	test_send(accept_sk[1], message, strlen(message), 0);

	/* We should not receive the message as the socket is SHUT_RD */ 
	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RD socket");

	/* Do a SHUT_RDWR on clt_sk[2] to disable any new sends/receives. */
	test_shutdown(clt_sk[2], SHUT_RDWR);

	error = recv(accept_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	error = recv(clt_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RDWR socket");

	error = 0;

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(accept_sk[i]);

	/* Test case to verify accept of a CLOSED association. */
	/* Do a connect, send and a close to ESTABLISH and CLOSE an
	 * association on the listening socket.
	 */
	test_connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));

	test_send(clt2_sk, message, strlen(message), 0);

	close(clt2_sk);

	FD_ZERO(&set);
	FD_SET(listen_sk, &set);

	error = select(listen_sk + 1, &set, NULL, NULL, NULL);
	if (1 != error)
		tst_brkm(TBROK, tst_exit, "select error:%d, "
			 "errno: %d", error, errno);

	/* Now accept the CLOSED association waiting on the listening 
	 * socket.
	 */  
	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	/* Receive the message sent before doing a close. */
	test_recv(accept2_sk, msgbuf, 100, 0);

	/* Receive EOF indication as there are no more messages and the
	 * socket is SHUTDOWN.
	 */
	error = recv(accept2_sk, msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "Unexpected error return on "
			 "recv(error:%d, errno:%d)", error, errno);

	tst_resm(TPASS, "accept of a CLOSED association");

	/* Trying to send a message over the CLOSED association should
	 * generate EPIPE.
	 */
	error = send(accept2_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send to a CLOSED association "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send to a CLOSED association");

	error = 0;
	close(accept2_sk);

	/* Verify that auto-connect can be done on a TCP-style socket using
	 * sendto/sendmsg.
	 */
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	/* Do a sendto() without a connect() */
	test_sendto(clt2_sk, message, strlen(message), 0, &svr_loop.sa,
		    sizeof(svr_loop));

	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	test_recv(accept2_sk, msgbuf, 100, 0);

	tst_resm(TPASS, "auto-connect using sendto");

	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = NULL;
	outmessage.msg_iovlen = 0;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

	/* Verify that SCTP_EOF cannot be used to shutdown an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_EOF;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_EOF flag");

	/* Verify that SCTP_ABORT cannot be used to abort an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_ABORT;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_ABORT flag");

	/* Verify that a normal message can be sent using sendmsg. */
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	out_iov.iov_base = message;
	out_iov.iov_len = strlen(message) + 1;
	sinfo->sinfo_flags = 0;
	test_sendmsg(clt2_sk, &outmessage, 0, strlen(message)+1);

	test_recv(accept2_sk, msgbuf, 100, 0);
	
	tst_resm(TPASS, "sendmsg with no flags");

	close(clt2_sk);
	close(accept2_sk);
	close(listen_sk);

        /* Indicate successful completion.  */
	return 0;
}
예제 #30
0
int
main(int argc, char *argv[])
{
        socklen_t len;
	int ret;
	int sk,pf_class,lstn_sk,acpt_sk;
	int flag = 0;
	struct msghdr inmessage;
        char *message = "hello, world!\n";
	struct iovec iov_rcv;
        int count;
	char * buffer_rcv;
        char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *message1 = "hello, world!\n";

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);

	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

	/*recvmsg () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvmsg(-1, &inmessage, flag);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "recvmsg with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with a bad socket descriptor - EBADF");

	/*recvmsg () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvmsg(0, &inmessage, flag);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "recvmsg with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid socket - ENOTSOCK");

	/*recvmsg () TEST3: Invalid iovec pointer EFAULT, Expected error*/
	inmessage.msg_iov = (struct iovec *)-1;
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid iovec "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid iovec ptr - EFAULT");

	inmessage.msg_iov = &iov_rcv;

	/*recvmsg () TEST4: Invalid msghdr pointer EFAULT, Expected error*/
	count = recvmsg(acpt_sk, (struct msghdr *)-1, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid msghdr "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid msghdr ptr - EFAULT");

	/*recvmsg () TEST5:recvmsg on listening socket,ENOTCONN Expected error*/
	count = recvmsg(lstn_sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message1, strlen(message), 0);

	ret = test_shutdown(sk, SHUT_WR);

	flag = MSG_NOSIGNAL;
	/*recvmsg () TEST6:reading on a socket that received SHUTDOWN*/
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket that has received shutdown - "
		 "EOF");

	/*recvmsg () TEST7:reading the pending message socket that sent
	SHUTDOWN*/
	count = recvmsg(sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvmsg () TEST8: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvmsg(sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	tst_exit();
}