| 1 |
/* SCTP kernel Implementation |
| 2 |
* (C) Copyright IBM Corp. 2001, 2003 |
| 3 |
* Copyright (c) 1999 Cisco |
| 4 |
* Copyright (c) 1999, 2000, 2001 Motorola |
| 5 |
* Copyright (c) 2001 Nokia |
| 6 |
* Copyright (c) 2001 La Monte H.P. Yarroll |
| 7 |
* |
| 8 |
* The SCTP implementation is free software; |
| 9 |
* you can redistribute it and/or modify it under the terms of |
| 10 |
* the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* The SCTP implementation is distributed in the hope that it |
| 15 |
* will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 |
* ************************ |
| 17 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* See the GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with GNU CC; see the file COPYING. If not, write to |
| 22 |
* the Free Software Foundation, 59 Temple Place - Suite 330, |
| 23 |
* Boston, MA 02111-1307, USA. |
| 24 |
* |
| 25 |
* Please send any bug reports or fixes you make to the |
| 26 |
* email address(es): |
| 27 |
* lksctp developers <[email protected]> |
| 28 |
* |
| 29 |
* Or submit a bug report through the following website: |
| 30 |
* https://github.com/sctp/lksctp-tools/ |
| 31 |
* |
| 32 |
* Any bugs reported to us we will try to fix... any fixes shared will |
| 33 |
* be incorporated into the next SCTP release. |
| 34 |
* |
| 35 |
* Written or modified by: |
| 36 |
* La Monte H.P. Yarroll <[email protected]> |
| 37 |
* Karl Knutson <[email protected]> |
| 38 |
* Hui Huang <[email protected]> |
| 39 |
* Daisy Chang <[email protected]> |
| 40 |
* Sridhar Samudrala <[email protected]> |
| 41 |
*/ |
| 42 |
|
| 43 |
/* This is a userspace test application for the SCTP kernel |
| 44 |
* implementation state machine. It is vaguely inspired by Stevens' |
| 45 |
* program "sock". |
| 46 |
* |
| 47 |
* It has the limited ability to send messages and to listen for messages |
| 48 |
* sent via SCTP. |
| 49 |
*/ |
| 50 |
|
| 51 |
#include <stdio.h> |
| 52 |
#include <fcntl.h> |
| 53 |
#include <stdlib.h> |
| 54 |
//#define _GNU_SOURCE |
| 55 |
#include <getopt.h> |
| 56 |
#include <netdb.h> |
| 57 |
#include <inttypes.h> |
| 58 |
#include <ctype.h> |
| 59 |
#include <sys/types.h> |
| 60 |
#include <sys/socket.h> |
| 61 |
#include <sys/uio.h> |
| 62 |
#include <netinet/in.h> |
| 63 |
#include <sys/param.h> |
| 64 |
#include <sys/poll.h> |
| 65 |
#include <arpa/inet.h> |
| 66 |
#include <errno.h> |
| 67 |
#include <net/if.h> |
| 68 |
#include <netinet/sctp.h> |
| 69 |
#include <unistd.h> |
| 70 |
#include <string.h> |
| 71 |
#include <signal.h> |
| 72 |
#include "sctp_darn.h" |
| 73 |
|
| 74 |
char *TCID = __FILE__; |
| 75 |
int TST_TOTAL = 1; |
| 76 |
int TST_CNT = 0; |
| 77 |
|
| 78 |
#define GEN_DATA_FIRST 0x21 |
| 79 |
#define GEN_DATA_LAST 0x7e |
| 80 |
|
| 81 |
/* Display an IPv4 address in readable format. */ |
| 82 |
#define NIPQUAD(addr) \ |
| 83 |
((unsigned char *)&addr)[0], \ |
| 84 |
((unsigned char *)&addr)[1], \ |
| 85 |
((unsigned char *)&addr)[2], \ |
| 86 |
((unsigned char *)&addr)[3] |
| 87 |
|
| 88 |
/* Display an IPv6 address in readable format. */ |
| 89 |
#define NIP6(addr) \ |
| 90 |
ntohs((addr).s6_addr16[0]), \ |
| 91 |
ntohs((addr).s6_addr16[1]), \ |
| 92 |
ntohs((addr).s6_addr16[2]), \ |
| 93 |
ntohs((addr).s6_addr16[3]), \ |
| 94 |
ntohs((addr).s6_addr16[4]), \ |
| 95 |
ntohs((addr).s6_addr16[5]), \ |
| 96 |
ntohs((addr).s6_addr16[6]), \ |
| 97 |
ntohs((addr).s6_addr16[7]) |
| 98 |
|
| 99 |
/* These are the global options. */ |
| 100 |
char *local_host = NULL; |
| 101 |
int local_port = 0; |
| 102 |
char *remote_host = NULL; |
| 103 |
int remote_port = 0; |
| 104 |
command_t command = COMMAND_NONE; |
| 105 |
struct sockaddr *bindx_add_addrs = NULL; |
| 106 |
int bindx_add_count = 0; |
| 107 |
struct sockaddr *bindx_rem_addrs = NULL; |
| 108 |
int bindx_rem_count = 0; |
| 109 |
struct sockaddr *connectx_addrs = NULL; |
| 110 |
int connectx_count = 0; |
| 111 |
int interactive_mode = 0; |
| 112 |
int poll_skn = 0; |
| 113 |
int nonblocking = 0; |
| 114 |
int opt_space = 0; |
| 115 |
char gen_data = GEN_DATA_FIRST; |
| 116 |
char *inter_outbuf = NULL; |
| 117 |
int inter_outlen = 0; |
| 118 |
int inter_sk = 0; |
| 119 |
int poll_snd_size = 0; |
| 120 |
int use_poll = 0; |
| 121 |
int socket_type = SOCK_SEQPACKET; |
| 122 |
sctp_assoc_t associd = 0; |
| 123 |
int echo = 0; |
| 124 |
char *interface = "eth0"; |
| 125 |
int if_index = 0; |
| 126 |
sockaddr_storage_t remote_addr; |
| 127 |
sa_family_t ra_family; /* What family is remote_addr? */ |
| 128 |
int ra_len = 0; /* How long is remote_addr? */ |
| 129 |
int new_connection = 1; |
| 130 |
|
| 131 |
enum inter_cmd_num { |
| 132 |
INTER_SND = 0, |
| 133 |
INTER_RCV, |
| 134 |
INTER_SNDBUF, |
| 135 |
INTER_RCVBUF, |
| 136 |
INTER_BINDX_ADD, |
| 137 |
INTER_BINDX_REM, |
| 138 |
INTER_SET_PRIM, |
| 139 |
INTER_SET_PEER_PRIM, |
| 140 |
INTER_SHUTDOWN, |
| 141 |
INTER_ABORT, |
| 142 |
INTER_NODELAY, |
| 143 |
INTER_MAXSEG, |
| 144 |
INTER_HEARTBEAT, |
| 145 |
INTER_GET_STATS |
| 146 |
}; |
| 147 |
|
| 148 |
enum shutdown_type { |
| 149 |
SHUTDOWN_ABORT = 0, |
| 150 |
SHUTDOWN_SHUTDOWN |
| 151 |
}; |
| 152 |
|
| 153 |
struct inter_entry { |
| 154 |
char *cmd; |
| 155 |
int cmd_num; |
| 156 |
}; |
| 157 |
|
| 158 |
struct inter_entry inter_commands[] = { |
| 159 |
{"snd", INTER_SND}, |
| 160 |
{"rcv", INTER_RCV}, |
| 161 |
{"sndbuf", INTER_SNDBUF}, |
| 162 |
{"rcvbuf", INTER_RCVBUF}, |
| 163 |
{"bindx-add", INTER_BINDX_ADD}, |
| 164 |
{"bindx-rem", INTER_BINDX_REM}, |
| 165 |
{"primary", INTER_SET_PRIM}, |
| 166 |
{"peer_primary", INTER_SET_PEER_PRIM}, |
| 167 |
{"shutdown", INTER_SHUTDOWN}, |
| 168 |
{"abort", INTER_ABORT}, |
| 169 |
{"nodelay", INTER_NODELAY}, |
| 170 |
{"maxseg", INTER_MAXSEG}, |
| 171 |
{"heartbeat", INTER_HEARTBEAT}, |
| 172 |
{"stats", INTER_GET_STATS}, |
| 173 |
{NULL, -1}, |
| 174 |
}; |
| 175 |
|
| 176 |
#define POLL_SK_MAX 256 /* The max number of sockets to select/poll. */ |
| 177 |
int poll_sks[POLL_SK_MAX]; /* The array for using select(). */ |
| 178 |
struct pollfd poll_fds[POLL_SK_MAX]; /* The array for using poll(). */ |
| 179 |
#define POLL_SND_SIZE 16384 /* Default message size in the poll mode. */ |
| 180 |
|
| 181 |
|
| 182 |
struct sockaddr *append_addr(const char *parm, struct sockaddr *addrs, |
| 183 |
int *ret_count) ; |
| 184 |
int build_endpoint(char *argv0, int portnum); |
| 185 |
static int parse_inter_commands(char *, char *, int); |
| 186 |
static void snd_func(char *); |
| 187 |
static void sndbuf_func(char *, int, int, int); |
| 188 |
static void rcvbuf_func(char *, int, int, int); |
| 189 |
static struct sockaddr *get_bindx_addr(char *, int *); |
| 190 |
static int bindx_func(char *, int, struct sockaddr *, int, int, int); |
| 191 |
static int connectx_func(char *, int, struct sockaddr *, int); |
| 192 |
static void primary_func(char *, int, char *, int); |
| 193 |
static void peer_primary_func(char *, int, char *, int); |
| 194 |
static void spp_hb_demand_func(char *, int, char *, int); |
| 195 |
static int nodelay_func(char *, int, int val, int set); |
| 196 |
static int maxseg_func(char *, int, int val, int set); |
| 197 |
static int shutdown_func(char *argv0, int *skp, int shutdown_type); |
| 198 |
static int get_assocstats_func(int, sctp_assoc_t); |
| 199 |
static int test_sk_for_assoc(int sk, sctp_assoc_t assoc_id); |
| 200 |
static char * gen_message(int); |
| 201 |
static sctp_assoc_t test_recv_assoc_change(int); |
| 202 |
static sctp_assoc_t test_verify_assoc_change(struct msghdr *); |
| 203 |
void print_addr_buf(void * laddrs, int n_laddrs); |
| 204 |
int print_sockaddr(struct sockaddr *sa_addr); |
| 205 |
|
| 206 |
int |
| 207 |
main(int argc, char *argv[]) { |
| 208 |
int sk = -1; |
| 209 |
int error = 0; |
| 210 |
int i; |
| 211 |
|
| 212 |
signal(SIGPIPE, SIG_IGN); |
| 213 |
|
| 214 |
parse_arguments(argc, argv); |
| 215 |
|
| 216 |
switch(command) { |
| 217 |
case COMMAND_NONE: |
| 218 |
fprintf(stderr, "%s: Please specify a command.\n", |
| 219 |
argv[0]); |
| 220 |
exit(1); |
| 221 |
break; |
| 222 |
case COMMAND_LISTEN: |
| 223 |
sk = build_endpoint(argv[0], local_port); |
| 224 |
error = command_listen(argv[0], sk); |
| 225 |
break; |
| 226 |
case COMMAND_SEND: |
| 227 |
sk = build_endpoint(argv[0], local_port); |
| 228 |
error = command_send(argv[0], &sk); |
| 229 |
break; |
| 230 |
case COMMAND_POLL: |
| 231 |
if (use_poll) { |
| 232 |
for (i = 0; i < poll_skn; i++) { |
| 233 |
poll_fds[i].fd = build_endpoint(argv[0], |
| 234 |
local_port + i); |
| 235 |
} |
| 236 |
} else { |
| 237 |
for (i = 0; i < poll_skn; i++) { |
| 238 |
poll_sks[i] = build_endpoint(argv[0], |
| 239 |
local_port + i); |
| 240 |
} |
| 241 |
} |
| 242 |
error = command_poll(argv[0]); |
| 243 |
break; |
| 244 |
default: |
| 245 |
fprintf(stderr, "%s: illegal command %d\n", |
| 246 |
argv[0], command); |
| 247 |
exit(1); |
| 248 |
} |
| 249 |
|
| 250 |
/* Shut down the link. */ |
| 251 |
if (COMMAND_POLL != command) { |
| 252 |
close(sk); |
| 253 |
} else { |
| 254 |
/* Shutdown all links. */ |
| 255 |
if (use_poll) { |
| 256 |
for (i = 0; i < poll_skn; i++) { |
| 257 |
close(poll_fds[i].fd); |
| 258 |
} |
| 259 |
} else { |
| 260 |
for (i = 0; i < poll_skn; i++) { |
| 261 |
close(poll_sks[i]); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
exit(error); |
| 267 |
} |
| 268 |
|
| 269 |
/******************************************************************** |
| 270 |
* 2nd Level Abstractions |
| 271 |
********************************************************************/ |
| 272 |
|
| 273 |
void |
| 274 |
parse_arguments(int argc, char *argv[]) { |
| 275 |
int option_index = 0; |
| 276 |
int c; |
| 277 |
struct sockaddr *tmp_addrs = NULL; |
| 278 |
|
| 279 |
static struct option long_options[] = { |
| 280 |
{"local", 1, 0, 1}, |
| 281 |
{"local-port", 1, 0, 2}, |
| 282 |
{"remote", 1, 0, 3}, |
| 283 |
{"remote-port", 1, 0, 4}, |
| 284 |
{"listen", 0, 0, 10}, |
| 285 |
{"send", 0, 0, 11}, |
| 286 |
{"bindx-add", 1, 0, 15}, |
| 287 |
{"bindx-rem", 1, 0, 16}, |
| 288 |
{"use-poll", 0, 0, 20}, |
| 289 |
{"echo", 0, 0, 'e'}, |
| 290 |
{"interface", optional_argument, 0, 5,}, |
| 291 |
{"connectx", 1, 0, 17}, |
| 292 |
{0, 0, 0, 0} |
| 293 |
}; |
| 294 |
|
| 295 |
/* Parse the arguments. */ |
| 296 |
while (1) { |
| 297 |
c = getopt_long (argc, argv, "B:H:IP:b:h:i:p:lm:nstz:ec:", |
| 298 |
long_options, &option_index); |
| 299 |
if (c == -1) |
| 300 |
break; |
| 301 |
|
| 302 |
switch (c) { |
| 303 |
case 0: |
| 304 |
printf("option %s", long_options[option_index].name); |
| 305 |
if (optarg) { |
| 306 |
printf(" with arg %s", optarg); |
| 307 |
} |
| 308 |
printf("\n"); |
| 309 |
break; |
| 310 |
case 1: /* local host */ |
| 311 |
case 'H': |
| 312 |
local_host = optarg; |
| 313 |
break; |
| 314 |
case 2: /* local port */ |
| 315 |
case 'P': |
| 316 |
local_port = atoi(optarg); |
| 317 |
break; |
| 318 |
case 3: /* remote host */ |
| 319 |
case 'h': |
| 320 |
remote_host = optarg; |
| 321 |
break; |
| 322 |
case 4: /* remote port */ |
| 323 |
case 'p': |
| 324 |
remote_port = atoi(optarg); |
| 325 |
break; |
| 326 |
case 5: /* interface for sin6_scope_id */ |
| 327 |
if (optarg) |
| 328 |
interface = optarg; |
| 329 |
if_index = if_nametoindex(interface); |
| 330 |
if (!if_index) { |
| 331 |
printf("Interface %s unknown\n", interface); |
| 332 |
exit(1); |
| 333 |
} |
| 334 |
break; |
| 335 |
/* COMMANDS */ |
| 336 |
case 10: /* listen */ |
| 337 |
case 'l': |
| 338 |
if (command) { |
| 339 |
fprintf(stderr, |
| 340 |
"%s: pick ONE of listen or send\n", |
| 341 |
argv[0]); |
| 342 |
exit(1); |
| 343 |
} else { |
| 344 |
command = COMMAND_LISTEN; |
| 345 |
} |
| 346 |
break; |
| 347 |
|
| 348 |
case 11: /* send */ |
| 349 |
case 's': |
| 350 |
if (command) { |
| 351 |
fprintf(stderr, |
| 352 |
"%s: pick ONE of listen or send\n", |
| 353 |
argv[0]); |
| 354 |
exit(1); |
| 355 |
} else { |
| 356 |
command = COMMAND_SEND; |
| 357 |
} |
| 358 |
break; |
| 359 |
|
| 360 |
case 15: /* bindx_add */ |
| 361 |
case 'B': |
| 362 |
tmp_addrs = |
| 363 |
append_addr(optarg, bindx_add_addrs, |
| 364 |
&bindx_add_count); |
| 365 |
if (NULL == tmp_addrs) { |
| 366 |
/* We have no memory, so keep fprintf() |
| 367 |
* from trying to allocate more. |
| 368 |
*/ |
| 369 |
fprintf(stderr, "No memory to add "); |
| 370 |
fprintf(stderr, "%s\n", optarg); |
| 371 |
exit(2); |
| 372 |
} |
| 373 |
bindx_add_addrs = tmp_addrs; |
| 374 |
|
| 375 |
break; |
| 376 |
|
| 377 |
case 16: /* bindx_rem */ |
| 378 |
case 'b': |
| 379 |
tmp_addrs = |
| 380 |
append_addr(optarg, bindx_rem_addrs, |
| 381 |
&bindx_rem_count); |
| 382 |
if (NULL == tmp_addrs) { |
| 383 |
/* We have no memory, so keep fprintf() |
| 384 |
* from trying to allocate more. |
| 385 |
*/ |
| 386 |
fprintf(stderr, "No memory to add "); |
| 387 |
fprintf(stderr, "%s\n", optarg); |
| 388 |
exit(2); |
| 389 |
} |
| 390 |
bindx_rem_addrs = tmp_addrs; |
| 391 |
break; |
| 392 |
case 17: /* connectx */ |
| 393 |
case 'c': |
| 394 |
tmp_addrs = |
| 395 |
append_addr(optarg, connectx_addrs, |
| 396 |
&connectx_count); |
| 397 |
if (NULL == tmp_addrs) { |
| 398 |
/* We have no memory, so keep fprintf() |
| 399 |
* from trying to allocate more. |
| 400 |
*/ |
| 401 |
fprintf(stderr, "No memory to add "); |
| 402 |
fprintf(stderr, "%s\n", optarg); |
| 403 |
exit(2); |
| 404 |
} |
| 405 |
connectx_addrs = tmp_addrs; |
| 406 |
break; |
| 407 |
case 20: /* use-poll */ |
| 408 |
use_poll = 1; |
| 409 |
break; |
| 410 |
case 'I': |
| 411 |
interactive_mode = 1; |
| 412 |
break; |
| 413 |
case 'i': |
| 414 |
command = COMMAND_POLL; |
| 415 |
poll_skn = atoi(optarg); |
| 416 |
if (poll_skn <= 0 || poll_skn > POLL_SK_MAX) { |
| 417 |
fprintf(stderr, "Too many sockets for "); |
| 418 |
fprintf(stderr, "for polling\n"); |
| 419 |
exit(2); |
| 420 |
} |
| 421 |
break; |
| 422 |
case 'm': |
| 423 |
opt_space = atoi(optarg); |
| 424 |
break; |
| 425 |
case 'n': |
| 426 |
nonblocking = 1; |
| 427 |
break; |
| 428 |
case 't': |
| 429 |
socket_type = SOCK_STREAM; |
| 430 |
break; |
| 431 |
case 'z': |
| 432 |
poll_snd_size = atoi(optarg); |
| 433 |
if (poll_snd_size <= 0) { |
| 434 |
fprintf(stderr, "Bad message size.\n"); |
| 435 |
exit(2); |
| 436 |
} |
| 437 |
break; |
| 438 |
case 'e': |
| 439 |
echo = 1; |
| 440 |
break; |
| 441 |
case '?': |
| 442 |
usage(argv[0]); |
| 443 |
exit(1); |
| 444 |
|
| 445 |
default: |
| 446 |
printf ("%s: unrecognized option 0%c\n", |
| 447 |
argv[0], c); |
| 448 |
usage(argv[0]); |
| 449 |
exit(1); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
if (optind < argc) |
| 454 |
{ |
| 455 |
fprintf(stderr, "%s: non-option arguments are illegal: ", |
| 456 |
argv[0]); |
| 457 |
while (optind < argc) |
| 458 |
fprintf(stderr, "%s ", argv[optind++]); |
| 459 |
fprintf (stderr, "\n"); |
| 460 |
usage(argv[0]); |
| 461 |
exit(1); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
if (NULL == local_host) { |
| 466 |
fprintf(stderr, "%s: You MUST provide a local host.\n", |
| 467 |
argv[0]); |
| 468 |
usage(argv[0]); |
| 469 |
exit(1); |
| 470 |
} |
| 471 |
|
| 472 |
if (command == COMMAND_SEND && NULL == remote_host |
| 473 |
&& connectx_count == 0) { |
| 474 |
fprintf(stderr, "%s: You MUST provide a remote host for sending.\n", |
| 475 |
argv[0]); |
| 476 |
usage(argv[0]); |
| 477 |
exit(1); |
| 478 |
} |
| 479 |
|
| 480 |
if (remote_host != NULL && connectx_count != 0) { |
| 481 |
fprintf(stderr, "%s: You can not provide both -h and -c options.\n", |
| 482 |
argv[0]); |
| 483 |
usage(argv[0]); |
| 484 |
exit(1); |
| 485 |
} |
| 486 |
} /* parse_arguments() */ |
| 487 |
|
| 488 |
/* Set up the local endpoint. */ |
| 489 |
int |
| 490 |
build_endpoint(char *argv0, int portnum) |
| 491 |
{ |
| 492 |
int retval; |
| 493 |
struct addrinfo hints, *rp; |
| 494 |
sockaddr_storage_t local_addr; |
| 495 |
int error; |
| 496 |
struct sctp_event_subscribe subscribe; |
| 497 |
|
| 498 |
/* Get the transport address for the local host name. */ |
| 499 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 500 |
hints.ai_family = AF_INET; |
| 501 |
hints.ai_protocol = IPPROTO_SCTP; |
| 502 |
if (getaddrinfo(local_host, NULL, &hints, &rp) != 0) { |
| 503 |
hints.ai_family = AF_INET6; |
| 504 |
if (getaddrinfo(local_host, NULL, &hints, &rp) != 0) { |
| 505 |
fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host); |
| 506 |
exit(1); |
| 507 |
} |
| 508 |
} |
| 509 |
memcpy(&local_addr, rp->ai_addr, rp->ai_addrlen); |
| 510 |
local_addr.v4.sin_port = htons(portnum); /* equal to v6.sin6_port */ |
| 511 |
if (rp->ai_family == AF_INET6) |
| 512 |
local_addr.v6.sin6_scope_id = if_index; |
| 513 |
|
| 514 |
/* Create the local endpoint. */ |
| 515 |
retval = socket(rp->ai_family, socket_type, IPPROTO_SCTP); |
| 516 |
if (retval < 0) { |
| 517 |
fprintf(stderr, "%s: failed to create socket: %s.\n", |
| 518 |
argv0, strerror(errno)); |
| 519 |
exit(1); |
| 520 |
} |
| 521 |
|
| 522 |
if (SOCK_SEQPACKET == socket_type) { |
| 523 |
memset(&subscribe, 0, sizeof(subscribe)); |
| 524 |
subscribe.sctp_data_io_event = 1; |
| 525 |
subscribe.sctp_association_event = 1; |
| 526 |
error = setsockopt(retval, SOL_SCTP, SCTP_EVENTS, |
| 527 |
(char *)&subscribe, sizeof(subscribe)); |
| 528 |
if (error) { |
| 529 |
fprintf(stderr, "SCTP_EVENTS: error: %d\n", error); |
| 530 |
exit(1); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
/* Bind this socket to the test port. */ |
| 535 |
error = bind(retval, &local_addr.sa, rp->ai_addrlen); |
| 536 |
if (error != 0) { |
| 537 |
fprintf(stderr, "%s: can not bind to %s:%d: %s.\n", |
| 538 |
argv0, local_host, portnum, |
| 539 |
strerror(errno)); |
| 540 |
exit(1); |
| 541 |
} |
| 542 |
|
| 543 |
/* Do we need to do bindx() to add any additional addresses? */ |
| 544 |
if (bindx_add_addrs) { |
| 545 |
if (0 != bindx_func(argv0, retval, bindx_add_addrs, |
| 546 |
bindx_add_count, SCTP_BINDX_ADD_ADDR, portnum)) { |
| 547 |
fprintf(stderr, "bindx_func (add) failed.\n"); |
| 548 |
exit(1); |
| 549 |
} |
| 550 |
} /* if (bindx_add_addrs) */ |
| 551 |
|
| 552 |
/* Do we need to do bindx() to remove any bound addresses? */ |
| 553 |
if (bindx_rem_addrs) { |
| 554 |
if (0 != bindx_func(argv0, retval, bindx_rem_addrs, |
| 555 |
bindx_rem_count, SCTP_BINDX_REM_ADDR, portnum)) { |
| 556 |
fprintf(stderr, "bindx_func (remove) failed.\n"); |
| 557 |
exit(1); |
| 558 |
} |
| 559 |
} /* if (bindx_rem_addrs) */ |
| 560 |
|
| 561 |
/* Do we want to run in the non-blocking mode? */ |
| 562 |
if (nonblocking) { |
| 563 |
error = fcntl(retval, F_SETFL, O_NONBLOCK); |
| 564 |
if (error != 0) { |
| 565 |
fprintf(stderr, "%s: error fcntl: %s.\n", |
| 566 |
argv0, strerror(errno)); |
| 567 |
exit(1); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
if (opt_space) { |
| 572 |
sndbuf_func(argv0, retval, opt_space, 1); |
| 573 |
rcvbuf_func(argv0, retval, opt_space, 1); |
| 574 |
} |
| 575 |
|
| 576 |
return retval; |
| 577 |
|
| 578 |
} /* build_endpoint() */ |
| 579 |
|
| 580 |
/* Convenience structure to determine space needed for cmsg. */ |
| 581 |
typedef union { |
| 582 |
struct sctp_initmsg init; |
| 583 |
struct sctp_sndrcvinfo sndrcvinfo; |
| 584 |
} _sctp_cmsg_data_t; |
| 585 |
|
| 586 |
|
| 587 |
/* Listen on the socket, printing out anything that arrives. */ |
| 588 |
int |
| 589 |
command_listen(char *argv0, int sk) |
| 590 |
{ |
| 591 |
char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
| 592 |
struct iovec iov; |
| 593 |
struct msghdr inmessage; |
| 594 |
sockaddr_storage_t msgname; |
| 595 |
char message[REALLY_BIG]; |
| 596 |
int done = 0; |
| 597 |
int error; |
| 598 |
int c; |
| 599 |
int recvsk = 0; |
| 600 |
|
| 601 |
/* Mark sk as being able to accept new associations */ |
| 602 |
error = listen(sk, 5); |
| 603 |
if (error != 0) { |
| 604 |
printf("\n\n\t\tlisten Failure: %s.\n\n\n", |
| 605 |
strerror(errno)); |
| 606 |
exit(1); |
| 607 |
} |
| 608 |
|
| 609 |
if (nonblocking) { |
| 610 |
if (!interactive_mode) { |
| 611 |
printf("Use -I for interactive mode with"); |
| 612 |
printf(" -n nonblocking\n"); |
| 613 |
exit(1); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
/* Initialize the global value for interactive mode functions. */ |
| 618 |
if (interactive_mode) { |
| 619 |
inter_sk = sk; |
| 620 |
} |
| 621 |
|
| 622 |
/* Initialize inmessage with enough space for DATA... */ |
| 623 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 624 |
if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) { |
| 625 |
printf("%s: Can't allocate memory.\n", argv0); |
| 626 |
exit(1); |
| 627 |
} |
| 628 |
iov.iov_len = REALLY_BIG; |
| 629 |
inmessage.msg_iov = &iov; |
| 630 |
inmessage.msg_iovlen = 1; |
| 631 |
/* or a control message. */ |
| 632 |
inmessage.msg_control = incmsg; |
| 633 |
inmessage.msg_controllen = sizeof(incmsg); |
| 634 |
inmessage.msg_name = &msgname; |
| 635 |
inmessage.msg_namelen = sizeof(msgname); |
| 636 |
|
| 637 |
printf("%s listening...\n", argv0); |
| 638 |
/* Get the messages sent */ |
| 639 |
done = 0; |
| 640 |
while (!done) { |
| 641 |
if (interactive_mode) { |
| 642 |
/* Read from the user. */ |
| 643 |
if (remote_host) { |
| 644 |
printf("%s:%d-%s:%d Interactive mode> ", |
| 645 |
local_host, local_port, remote_host, |
| 646 |
remote_port); |
| 647 |
} else { |
| 648 |
printf("%s:%d-", local_host, local_port); |
| 649 |
if (associd) { |
| 650 |
print_sockaddr(&remote_addr.sa); |
| 651 |
} else { |
| 652 |
printf("?:%d", remote_port); |
| 653 |
} |
| 654 |
printf(" Interactive mode> "); |
| 655 |
} |
| 656 |
fflush(stdout); |
| 657 |
if (NULL == fgets(message, REALLY_BIG, stdin)) { |
| 658 |
if (socket_type == SOCK_STREAM && recvsk) { |
| 659 |
close(recvsk); |
| 660 |
recvsk = 0; |
| 661 |
} |
| 662 |
done = 1; |
| 663 |
continue; |
| 664 |
} |
| 665 |
|
| 666 |
if (0 <= (c = parse_inter_commands(argv0, message, |
| 667 |
0))) { |
| 668 |
if (INTER_RCV != c) { |
| 669 |
continue; |
| 670 |
} |
| 671 |
} else { |
| 672 |
continue; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
if (socket_type == SOCK_STREAM) { |
| 677 |
socklen_t len = 0; |
| 678 |
|
| 679 |
if (!recvsk) { |
| 680 |
if ((recvsk = accept(sk, NULL, &len)) < 0) { |
| 681 |
fprintf(stderr, "%s: error: %s.\n", |
| 682 |
argv0, strerror(errno)); |
| 683 |
exit(1); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
} else { |
| 688 |
recvsk = sk; |
| 689 |
} |
| 690 |
|
| 691 |
error = recvmsg(recvsk, &inmessage, MSG_WAITALL); |
| 692 |
if (error < 0) { |
| 693 |
if (nonblocking && (EAGAIN == errno)) { |
| 694 |
error = 0; |
| 695 |
continue; |
| 696 |
} |
| 697 |
|
| 698 |
if (socket_type == SOCK_STREAM) { |
| 699 |
if (ENOTCONN != errno) |
| 700 |
break; |
| 701 |
printf("No association is present now!!\n"); |
| 702 |
close(recvsk); |
| 703 |
recvsk = 0; |
| 704 |
continue; |
| 705 |
} |
| 706 |
break; |
| 707 |
} |
| 708 |
|
| 709 |
/* Update the associd when a notification is received on a |
| 710 |
* UDP-style socket. |
| 711 |
*/ |
| 712 |
if (inmessage.msg_flags & MSG_NOTIFICATION) |
| 713 |
associd = test_verify_assoc_change(&inmessage); |
| 714 |
|
| 715 |
if (echo) { |
| 716 |
if( !(MSG_NOTIFICATION & inmessage.msg_flags)) { |
| 717 |
if (sendto(recvsk, inmessage.msg_iov->iov_base, |
| 718 |
error, 0, |
| 719 |
(struct sockaddr *)&msgname, |
| 720 |
sizeof(msgname)) == -1) { |
| 721 |
fprintf(stderr, "%s: error: %s.\n", |
| 722 |
argv0, strerror(errno)); |
| 723 |
exit(1); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
test_print_message(sk, &inmessage, error); |
| 729 |
|
| 730 |
inmessage.msg_control = incmsg; |
| 731 |
inmessage.msg_controllen = sizeof(incmsg); |
| 732 |
inmessage.msg_name = &msgname; |
| 733 |
inmessage.msg_namelen = sizeof(msgname); |
| 734 |
iov.iov_len = REALLY_BIG; |
| 735 |
|
| 736 |
/* Verify that the association is no longer present. */ |
| 737 |
if (0 != test_sk_for_assoc(recvsk, associd)) { |
| 738 |
printf("No association is present now!!\n"); |
| 739 |
if (socket_type == SOCK_STREAM) { |
| 740 |
close(recvsk); |
| 741 |
recvsk = 0; |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
if (error < 0) { |
| 747 |
fprintf(stderr, "%s: error: %s.\n", |
| 748 |
argv0, strerror(errno)); |
| 749 |
exit(1); |
| 750 |
} |
| 751 |
|
| 752 |
return error; |
| 753 |
|
| 754 |
} /* command_listen() */ |
| 755 |
|
| 756 |
/* Read lines from stdin and send them to the socket. */ |
| 757 |
int |
| 758 |
command_send(char *argv0, int *skp) |
| 759 |
{ |
| 760 |
struct msghdr outmsg; |
| 761 |
struct iovec iov; |
| 762 |
int done = 0; |
| 763 |
char message[REALLY_BIG]; |
| 764 |
struct addrinfo hints, *rp; |
| 765 |
int c; |
| 766 |
struct sockaddr *addrs; |
| 767 |
int msglen; |
| 768 |
int error = 0; |
| 769 |
int sk = *skp; |
| 770 |
|
| 771 |
/* Set up the destination. */ |
| 772 |
if (remote_host != NULL) { |
| 773 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 774 |
hints.ai_family = AF_INET; |
| 775 |
hints.ai_protocol = IPPROTO_SCTP; |
| 776 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 777 |
hints.ai_family = AF_INET6; |
| 778 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 779 |
fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host); |
| 780 |
exit(1); |
| 781 |
} |
| 782 |
} |
| 783 |
memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen); |
| 784 |
remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */ |
| 785 |
if (rp->ai_family == AF_INET6) |
| 786 |
remote_addr.v6.sin6_scope_id = if_index; |
| 787 |
ra_len = rp->ai_addrlen; |
| 788 |
} |
| 789 |
|
| 790 |
/* Initialize the global value for interactive mode functions. */ |
| 791 |
if (interactive_mode) { |
| 792 |
inter_sk = sk; |
| 793 |
} |
| 794 |
|
| 795 |
printf("%s ready to send...\n", argv0); |
| 796 |
while (!done) { |
| 797 |
/* Read from the user. */ |
| 798 |
if (remote_host) { |
| 799 |
if (interactive_mode) { |
| 800 |
printf("%s:%d-%s:%d Interactive mode> ", |
| 801 |
local_host, local_port, remote_host, |
| 802 |
remote_port); |
| 803 |
} else { |
| 804 |
printf("%s:%d-%s:%d> ", |
| 805 |
local_host, local_port, |
| 806 |
remote_host, remote_port); |
| 807 |
} |
| 808 |
} else { |
| 809 |
printf("%s:%d-", local_host, local_port); |
| 810 |
if (associd) { |
| 811 |
print_sockaddr(&remote_addr.sa); |
| 812 |
} else { |
| 813 |
printf("XXXXXX:%d", remote_port); |
| 814 |
} |
| 815 |
if (interactive_mode) { |
| 816 |
printf(" Interactive mode> "); |
| 817 |
} else { |
| 818 |
printf("> "); |
| 819 |
} |
| 820 |
} |
| 821 |
fflush(stdout); |
| 822 |
if (NULL == fgets(message, REALLY_BIG, stdin)) { |
| 823 |
done = 1; |
| 824 |
continue; |
| 825 |
} |
| 826 |
|
| 827 |
if (interactive_mode) { |
| 828 |
/* This is the send only agent. */ |
| 829 |
if (0 <= (c = parse_inter_commands(argv0, message, |
| 830 |
1))) { |
| 831 |
if (INTER_SND == c) { |
| 832 |
iov.iov_base = inter_outbuf; |
| 833 |
msglen = inter_outlen; |
| 834 |
iov.iov_len = msglen; |
| 835 |
|
| 836 |
} else { |
| 837 |
continue; |
| 838 |
} |
| 839 |
|
| 840 |
} else { |
| 841 |
continue; |
| 842 |
} |
| 843 |
} else { |
| 844 |
/* Send to our neighbor. */ |
| 845 |
msglen = strlen(message) + 1; |
| 846 |
iov.iov_len = msglen; |
| 847 |
} |
| 848 |
|
| 849 |
/* For a UDP-style socket, verify if an existing association |
| 850 |
* has gone. If so, receive the pending SCTP_ASSOC_CHANGE |
| 851 |
* notification. |
| 852 |
*/ |
| 853 |
if ((SOCK_SEQPACKET == socket_type) && associd && |
| 854 |
(0 != test_sk_for_assoc(sk, associd))) { |
| 855 |
associd = test_recv_assoc_change(sk); |
| 856 |
printf("Old association gone, Starting a new one!\n"); |
| 857 |
new_connection = 1; |
| 858 |
} |
| 859 |
|
| 860 |
if (new_connection && connectx_count != 0) { |
| 861 |
/* Do a sctp_connectx() to establish a connection. */ |
| 862 |
error = connectx_func(argv0, sk, connectx_addrs, |
| 863 |
connectx_count); |
| 864 |
if (0 != error) { |
| 865 |
if (error == -2) { |
| 866 |
printf("Connection refused\n"); |
| 867 |
if (SOCK_SEQPACKET == socket_type) { |
| 868 |
associd = test_recv_assoc_change(sk); |
| 869 |
} |
| 870 |
continue; |
| 871 |
} |
| 872 |
fprintf(stderr, "connectx failed.\n"); |
| 873 |
exit(1); |
| 874 |
} |
| 875 |
if (SOCK_SEQPACKET == socket_type) { |
| 876 |
associd = test_recv_assoc_change(sk); |
| 877 |
} else { |
| 878 |
associd = 1; |
| 879 |
} |
| 880 |
int rc = sctp_getpaddrs(sk, associd, &addrs); |
| 881 |
if (0 >= rc) { |
| 882 |
if (rc == 0) { |
| 883 |
fprintf(stderr, "sctp_getpaddrs failed, no peers.\n"); |
| 884 |
} else { |
| 885 |
fprintf(stderr, "sctp_getpaddrs failed %s(%d).\n", strerror(errno), errno); |
| 886 |
} |
| 887 |
exit(1); |
| 888 |
} |
| 889 |
printf("New connection, peer addresses\n"); |
| 890 |
print_addr_buf(addrs, rc); |
| 891 |
ra_family = addrs[0].sa_family; |
| 892 |
switch (ra_family) { |
| 893 |
case AF_INET: |
| 894 |
ra_len = sizeof(remote_addr.v4); |
| 895 |
break; |
| 896 |
case AF_INET6: |
| 897 |
ra_len = sizeof(remote_addr.v6); |
| 898 |
break; |
| 899 |
default: |
| 900 |
fprintf(stderr, "Invalid address type.\n"); |
| 901 |
exit(1); |
| 902 |
} |
| 903 |
memcpy(&remote_addr, &addrs[0], ra_len); |
| 904 |
sctp_freepaddrs(addrs); |
| 905 |
new_connection = 0; |
| 906 |
} |
| 907 |
|
| 908 |
do { |
| 909 |
if (SOCK_SEQPACKET == socket_type || |
| 910 |
(connectx_count == 0 && new_connection)) { |
| 911 |
/* Initialize the message struct we use to pass |
| 912 |
* messages to the remote socket. |
| 913 |
*/ |
| 914 |
if (!interactive_mode) { |
| 915 |
iov.iov_base = message; |
| 916 |
iov.iov_len = msglen; |
| 917 |
} |
| 918 |
outmsg.msg_iov = &iov; |
| 919 |
outmsg.msg_iovlen = 1; |
| 920 |
outmsg.msg_control = NULL; |
| 921 |
outmsg.msg_controllen = 0; |
| 922 |
outmsg.msg_name = &remote_addr; |
| 923 |
outmsg.msg_namelen = ra_len; |
| 924 |
outmsg.msg_flags = 0; |
| 925 |
|
| 926 |
error = sendmsg(sk, &outmsg, 0); |
| 927 |
} else { |
| 928 |
error = send(sk, message, msglen, 0); |
| 929 |
if (error == -1 && errno == EPIPE) { |
| 930 |
error = close(sk); |
| 931 |
if (error != 0) { |
| 932 |
fprintf(stderr, "close failed %s\n", strerror(errno)); |
| 933 |
exit(1); |
| 934 |
} |
| 935 |
*skp = sk = build_endpoint(argv0, local_port); |
| 936 |
break; |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
if (error != msglen) { |
| 941 |
fprintf(stderr, "%s: error: %s.\n", |
| 942 |
argv0, strerror(errno)); |
| 943 |
if (nonblocking && EAGAIN == errno) { |
| 944 |
if (interactive_mode) { |
| 945 |
break; |
| 946 |
} |
| 947 |
continue; |
| 948 |
} |
| 949 |
exit(1); |
| 950 |
} else { |
| 951 |
break; |
| 952 |
} |
| 953 |
} while (error != msglen); |
| 954 |
|
| 955 |
/* If this is the first message sent over a UDP-style socket, |
| 956 |
* get the associd from the SCTP_ASSOC_CHANGE notification. |
| 957 |
*/ |
| 958 |
if ((SOCK_SEQPACKET == socket_type) && (0 == associd)) |
| 959 |
associd = test_recv_assoc_change(sk); |
| 960 |
|
| 961 |
/* Verify there is no association. */ |
| 962 |
if (0 != test_sk_for_assoc(sk, associd)) { |
| 963 |
printf("No association is present now!!\n"); |
| 964 |
new_connection = 1; |
| 965 |
} else { |
| 966 |
if (new_connection) { |
| 967 |
int rc = sctp_getpaddrs(sk, associd, &addrs); |
| 968 |
if (0 >= rc) { |
| 969 |
if (rc == 0) { |
| 970 |
fprintf(stderr, "sctp_getpaddrs failed, no peers.\n"); |
| 971 |
} else { |
| 972 |
fprintf(stderr, "sctp_getpaddrs failed %s(%d).\n", strerror(errno), errno); |
| 973 |
} |
| 974 |
exit(1); |
| 975 |
} |
| 976 |
printf("New connection, peer addresses\n"); |
| 977 |
print_addr_buf(addrs, rc); |
| 978 |
sctp_freepaddrs(addrs); |
| 979 |
new_connection = 0; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
/* Clean up. */ |
| 984 |
if (interactive_mode) { |
| 985 |
free(inter_outbuf); |
| 986 |
inter_outbuf = NULL; |
| 987 |
} |
| 988 |
} /* while(!done) */ |
| 989 |
|
| 990 |
return error; |
| 991 |
|
| 992 |
} /* command_send() */ |
| 993 |
|
| 994 |
/* Listen on the array of sockets, printing out anything that arrives. */ |
| 995 |
int |
| 996 |
command_poll(char *argv0) |
| 997 |
{ |
| 998 |
char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
| 999 |
struct iovec iov; |
| 1000 |
struct msghdr inmessage; |
| 1001 |
int done = 0; |
| 1002 |
int error = 0; |
| 1003 |
int max_fd, i, ret; |
| 1004 |
int size; |
| 1005 |
fd_set *ibitsp = NULL; |
| 1006 |
fd_set *obitsp = NULL; |
| 1007 |
fd_set *xbitsp = NULL; |
| 1008 |
|
| 1009 |
struct msghdr outmsg; |
| 1010 |
struct addrinfo hints, *rp; |
| 1011 |
int msglen; |
| 1012 |
int temp_fd, temp_set; |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
/* If a remote host is specified, initialize the destination. */ |
| 1017 |
if (remote_host) { |
| 1018 |
/* Set up the destination. */ |
| 1019 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 1020 |
hints.ai_family = AF_INET; |
| 1021 |
hints.ai_protocol = IPPROTO_SCTP; |
| 1022 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 1023 |
hints.ai_family = AF_INET6; |
| 1024 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 1025 |
fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host); |
| 1026 |
exit(1); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen); |
| 1030 |
remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */ |
| 1031 |
if (rp->ai_family == AF_INET6) |
| 1032 |
remote_addr.v6.sin6_scope_id = if_index; |
| 1033 |
|
| 1034 |
/* Initialize the message struct we use to pass messages to |
| 1035 |
* the remote socket. |
| 1036 |
*/ |
| 1037 |
outmsg.msg_iov = &iov; |
| 1038 |
outmsg.msg_iovlen = 1; |
| 1039 |
outmsg.msg_control = NULL; |
| 1040 |
outmsg.msg_controllen = 0; |
| 1041 |
outmsg.msg_name = &remote_addr; |
| 1042 |
outmsg.msg_namelen = rp->ai_addrlen; |
| 1043 |
outmsg.msg_flags = 0; |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
max_fd = -1; |
| 1048 |
|
| 1049 |
/* Set all of the sockets to be ready for listening. */ |
| 1050 |
if (use_poll) { |
| 1051 |
for (i = 0; i < poll_skn; i++) { |
| 1052 |
error = listen(poll_fds[i].fd, 1); |
| 1053 |
if (error != 0) { |
| 1054 |
printf("%s: Listen failed on socket number ", |
| 1055 |
argv0); |
| 1056 |
printf("%d: %s.\n", i, strerror(errno)); |
| 1057 |
exit(1); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
printf("%s listening...\n", argv0); |
| 1061 |
} else { |
| 1062 |
for (i = 0; i < poll_skn; i++) { |
| 1063 |
error = listen(poll_sks[i], 1); |
| 1064 |
if (error != 0) { |
| 1065 |
printf("%s: Listen failed on socket number ", |
| 1066 |
argv0); |
| 1067 |
printf("%d: %s.\n", i, strerror(errno)); |
| 1068 |
exit(1); |
| 1069 |
} |
| 1070 |
if (poll_sks[i] > max_fd) { |
| 1071 |
max_fd = poll_sks[i]; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
printf("%s listening...\n", argv0); |
| 1075 |
|
| 1076 |
size = howmany(max_fd + 1, NFDBITS) * sizeof(fd_mask); |
| 1077 |
if ((ibitsp = (fd_set *)malloc(size)) == NULL) { |
| 1078 |
printf("%s: Can't allocate memory.\n", argv0); |
| 1079 |
exit(1); |
| 1080 |
} |
| 1081 |
if ((obitsp = (fd_set *)malloc(size)) == NULL) { |
| 1082 |
printf("%s: Can't allocate memory.\n", argv0); |
| 1083 |
exit(1); |
| 1084 |
} |
| 1085 |
if ((xbitsp = (fd_set *)malloc(size)) == NULL) { |
| 1086 |
printf("%s: Can't allocate memory.\n", argv0); |
| 1087 |
exit(1); |
| 1088 |
} |
| 1089 |
memset(ibitsp, 0, size); |
| 1090 |
memset(obitsp, 0, size); |
| 1091 |
memset(xbitsp, 0, size); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
/* Initialize inmessage with enough space for DATA... */ |
| 1096 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 1097 |
if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) { |
| 1098 |
printf("%s: Can't allocate memory.\n", argv0); |
| 1099 |
exit(1); |
| 1100 |
} |
| 1101 |
iov.iov_len = REALLY_BIG; |
| 1102 |
inmessage.msg_iov = &iov; |
| 1103 |
inmessage.msg_iovlen = 1; |
| 1104 |
/* or a control message. */ |
| 1105 |
inmessage.msg_control = incmsg; |
| 1106 |
inmessage.msg_controllen = sizeof(incmsg); |
| 1107 |
|
| 1108 |
|
| 1109 |
done = 0; |
| 1110 |
/* Set the default send message size. */ |
| 1111 |
if (!poll_snd_size) { |
| 1112 |
poll_snd_size = POLL_SND_SIZE; |
| 1113 |
} |
| 1114 |
|
| 1115 |
while (!done) { |
| 1116 |
|
| 1117 |
if (use_poll) { |
| 1118 |
for (i = 0; i < poll_skn; i++) { |
| 1119 |
poll_fds[i].events = POLLIN; |
| 1120 |
} |
| 1121 |
if (remote_host) { |
| 1122 |
/* Poll output on the first socket. */ |
| 1123 |
poll_fds[0].events |= POLLOUT; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if ((ret = poll(poll_fds, poll_skn, -1))) { |
| 1127 |
if (ret == -1) { |
| 1128 |
break; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} else { |
| 1132 |
for (i = 0; i < poll_skn; i++) { |
| 1133 |
FD_SET(poll_sks[i], ibitsp); |
| 1134 |
FD_SET(poll_sks[i], xbitsp); |
| 1135 |
} |
| 1136 |
if (remote_host) { |
| 1137 |
/* Only select output on the first socket. */ |
| 1138 |
FD_SET(poll_sks[0], obitsp); |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
if ((ret = select(max_fd + 1, ibitsp, obitsp, xbitsp, |
| 1143 |
(struct timeval *)0)) < 0) { |
| 1144 |
if (ret == -1) { |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
} |
| 1150 |
|
| 1151 |
if (remote_host) { |
| 1152 |
if (use_poll) { |
| 1153 |
temp_set = poll_fds[0].revents & POLLOUT; |
| 1154 |
temp_fd = poll_fds[0].fd; |
| 1155 |
} else { |
| 1156 |
temp_set = FD_ISSET(poll_sks[0], obitsp); |
| 1157 |
temp_fd = poll_sks[0]; |
| 1158 |
} |
| 1159 |
|
| 1160 |
if (temp_set) { |
| 1161 |
inter_outbuf = gen_message(poll_snd_size); |
| 1162 |
if (!inter_outbuf) { |
| 1163 |
fprintf(stderr, |
| 1164 |
"Cannot allocate out message.\n"); |
| 1165 |
exit(1); |
| 1166 |
} |
| 1167 |
iov.iov_base = inter_outbuf; |
| 1168 |
msglen = poll_snd_size; |
| 1169 |
iov.iov_len = msglen; |
| 1170 |
|
| 1171 |
error = sendmsg(temp_fd, &outmsg, 0); |
| 1172 |
fprintf(stderr, |
| 1173 |
"sent a message, msglen = %d\n", |
| 1174 |
msglen); |
| 1175 |
|
| 1176 |
if (error != msglen) { |
| 1177 |
fprintf(stderr, "%s: error: %s.\n", |
| 1178 |
argv0, strerror(errno)); |
| 1179 |
if ((!nonblocking) || |
| 1180 |
(EAGAIN != errno)) { |
| 1181 |
exit(1); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
/* Clean up. */ |
| 1186 |
free(inter_outbuf); |
| 1187 |
inter_outbuf = NULL; |
| 1188 |
} |
| 1189 |
|
| 1190 |
} /* while(!done) */ |
| 1191 |
|
| 1192 |
for (i = 0; !done && (i < poll_skn); i++) { |
| 1193 |
if (use_poll) { |
| 1194 |
temp_set = poll_fds[i].revents & POLLIN; |
| 1195 |
temp_fd = poll_fds[i].fd; |
| 1196 |
} else { |
| 1197 |
temp_set = FD_ISSET(poll_sks[i], ibitsp); |
| 1198 |
temp_fd = poll_sks[i]; |
| 1199 |
} |
| 1200 |
if (temp_set) { |
| 1201 |
error = recvmsg(temp_fd, &inmessage, |
| 1202 |
MSG_WAITALL); |
| 1203 |
if (error < 0) { |
| 1204 |
if ((EAGAIN == errno)) { |
| 1205 |
error = 0; |
| 1206 |
continue; |
| 1207 |
} |
| 1208 |
else { |
| 1209 |
fprintf(stderr, |
| 1210 |
"%s: error: %s.\n", |
| 1211 |
argv0, |
| 1212 |
strerror(errno)); |
| 1213 |
exit(1); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
test_print_message(temp_fd, &inmessage, error); |
| 1217 |
inmessage.msg_control = incmsg; |
| 1218 |
inmessage.msg_controllen = sizeof(incmsg); |
| 1219 |
iov.iov_len = REALLY_BIG; |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* Update the associd when a notification is received |
| 1223 |
* on a UDP-style socket. |
| 1224 |
*/ |
| 1225 |
if (inmessage.msg_flags & MSG_NOTIFICATION) |
| 1226 |
associd = test_verify_assoc_change(&inmessage); |
| 1227 |
|
| 1228 |
/* Verify there is no association. */ |
| 1229 |
if (0 != test_sk_for_assoc(poll_sks[i], associd)) { |
| 1230 |
printf("No association is present in sk " |
| 1231 |
"No.%d now!!\n",i); |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
} |
| 1236 |
|
| 1237 |
if (!use_poll) { |
| 1238 |
free(ibitsp); |
| 1239 |
free(obitsp); |
| 1240 |
free(xbitsp); |
| 1241 |
} |
| 1242 |
|
| 1243 |
return error; |
| 1244 |
|
| 1245 |
} /* command_poll() */ |
| 1246 |
|
| 1247 |
/******************************************************************** |
| 1248 |
* 3rd Level Abstractions |
| 1249 |
********************************************************************/ |
| 1250 |
|
| 1251 |
#define FPS(arg) fprintf(stderr, arg) |
| 1252 |
|
| 1253 |
void |
| 1254 |
usage(char *argv0) |
| 1255 |
{ |
| 1256 |
/* |
| 1257 |
* The bindx options, --bindx-add and --bindx-rem, are added to |
| 1258 |
* |
| 1259 |
* 1. provide first testcases for the new bindx system call |
| 1260 |
* |
| 1261 |
* 2. continue to grow sctp_darn with more functions and |
| 1262 |
* features so it will be equivalent to the "sock" tool for |
| 1263 |
* TCP as for SCTP. |
| 1264 |
* |
| 1265 |
* FIXME - |
| 1266 |
* |
| 1267 |
* It is not very effective to use these two options in the |
| 1268 |
* current command line mode of sctp_darn. For example, the |
| 1269 |
* --bindx-rem option can only be used in conjunction with the |
| 1270 |
* --bindx-add simply to test the function in the kernel |
| 1271 |
* path. Ideally, bindx needs to be tested by a tool which |
| 1272 |
* provides an interactive mode for users to change parameters |
| 1273 |
* and configuration dynamically with existing endpoints and |
| 1274 |
* associations. |
| 1275 |
*/ |
| 1276 |
fprintf(stderr, "Usage: %s -H <localhost> -P <localport> " |
| 1277 |
"[-h <remotehost>] [-p <remoteport>] -l|s\n" |
| 1278 |
" -H, --local\t\tspecify one of the local addresses,\n" |
| 1279 |
" -P, --local-port\tspecify the port number for local addresses,\n" |
| 1280 |
" -h, --remote\t\tspecify the peer address,\n" |
| 1281 |
" -p, --remote-port\tspecify the port number for the peer address,\n" |
| 1282 |
" -l, --listen\t\tprint messages received from the peer,\n" |
| 1283 |
" -s, --send\t\tsend messages to the peer,\n" |
| 1284 |
" -B, --bindx-add" |
| 1285 |
"\tadd the specified address(es) as additional bind\n" |
| 1286 |
"\t\t\taddresses to the local socket. Multiple addresses can\n" |
| 1287 |
"\t\t\tbe specified by using this argument multiple times.\n" |
| 1288 |
"\t\t\tFor example, '-B 10.0.0.1 -B 20.0.0.2'.\n" |
| 1289 |
" -b, --bindx-rem" |
| 1290 |
"\tremove the specified address(es) from the bind\n" |
| 1291 |
"\t\t\taddresses of the local socket. Multiple addresses can\n" |
| 1292 |
"\t\t\tbe specified by using this argument multiple times.\n" |
| 1293 |
"\t\t\tFor example, '-b 10.0.0.1 -b 20.0.0.2'.\n" |
| 1294 |
" -c, --connectx" |
| 1295 |
"\t\tuse the specified address(es) for connection to the\n" |
| 1296 |
"\t\t\tpeer socket. Multiple addresses can be specified by\n" |
| 1297 |
"\t\t\tusing this argument multiple times.\n" |
| 1298 |
"\t\t\tFor example, '-c 10.0.0.1 -c 20.0.0.2'.\n" |
| 1299 |
"\t\t\tThis option is incompatible with the -h option.\n" |
| 1300 |
" -I\t\t\tuse the interactive mode.\n" |
| 1301 |
" -i\t\t\tsetup the specified number of endpoints by using the\n" |
| 1302 |
"\t\t\tspecified local host (-H) and local port (-P). The port\n" |
| 1303 |
"\t\t\tnumber will be incremented by one for each additional\n" |
| 1304 |
"\t\t\tendpoint. All of these endpoints will be listening.\n" |
| 1305 |
"\t\t\tIf a remote host (-h) and a remote port are also\n" |
| 1306 |
"\t\t\tspecified, the first endpoint will start sending fixed\n" |
| 1307 |
"\t\t\tsized messages to the remote host.\n" |
| 1308 |
" -m\t\t\tspecify the sockopt sndbuf/rcvbuf size.\n" |
| 1309 |
" -n\t\t\tset the socket(s) to be in the non-blocking mode.\n" |
| 1310 |
"\t\t\tcollect messages from stdin and deliver them to the\n" |
| 1311 |
"\t\t\tpeer,\n" |
| 1312 |
"--use-poll\t\tuse system call poll() for polling among the\n" |
| 1313 |
"\t\t\tnumber of endpoints specified by the -i option. Without\n" |
| 1314 |
"\t\t\tthis option, select() would be used as default.\n" |
| 1315 |
" -t\t\t\tuse SOCK_STREAM tcp-style sockets.\n" |
| 1316 |
" -z\t\t\tspecify the message size to be sent. The default\n" |
| 1317 |
"\t\t\tmessage size generated would be 16K.\n" |
| 1318 |
" --interface=\"ifname\"\tselect interface for sin6_scope_id.\n", |
| 1319 |
argv0); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
/* This function checks messages to see if they are of type 'event' |
| 1324 |
* and if they are well-formed. |
| 1325 |
*/ |
| 1326 |
int |
| 1327 |
user_test_check_message(struct msghdr *msg, |
| 1328 |
int controllen, |
| 1329 |
sctp_cmsg_t event) |
| 1330 |
{ |
| 1331 |
|
| 1332 |
|
| 1333 |
if (msg->msg_controllen != controllen) { |
| 1334 |
fprintf(stderr, |
| 1335 |
"Got control structure of length %zu, not %d\n", |
| 1336 |
msg->msg_controllen, controllen); |
| 1337 |
exit(1); |
| 1338 |
} |
| 1339 |
if (controllen > 0 && event != CMSG_FIRSTHDR(msg)->cmsg_type) { |
| 1340 |
fprintf(stderr, "Wrong kind of event: %d, not %d\n", |
| 1341 |
CMSG_FIRSTHDR(msg)->cmsg_type, event); |
| 1342 |
exit(1); |
| 1343 |
} |
| 1344 |
|
| 1345 |
return 1; |
| 1346 |
|
| 1347 |
} /* user_test_check_message() */ |
| 1348 |
|
| 1349 |
/* Add another address represented as the string 'parm' to the list |
| 1350 |
* addrs. The argument count is the number of addrs on input and is |
| 1351 |
* adjusted for output. |
| 1352 |
*/ |
| 1353 |
struct sockaddr * |
| 1354 |
append_addr(const char *parm, struct sockaddr *addrs, int *ret_count) |
| 1355 |
{ |
| 1356 |
struct sockaddr *new_addrs = NULL; |
| 1357 |
struct addrinfo hints, *res, *rp; |
| 1358 |
void *aptr; |
| 1359 |
struct sockaddr *sa_addr; |
| 1360 |
struct sockaddr_in *b4ap; |
| 1361 |
struct sockaddr_in6 *b6ap; |
| 1362 |
int j; |
| 1363 |
int orig_count = *ret_count; |
| 1364 |
int count = orig_count; |
| 1365 |
|
| 1366 |
|
| 1367 |
if (!parm) |
| 1368 |
return NULL; |
| 1369 |
|
| 1370 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 1371 |
hints.ai_family = AF_UNSPEC; |
| 1372 |
hints.ai_protocol = IPPROTO_SCTP; |
| 1373 |
if (getaddrinfo(parm, NULL, &hints, &res) != 0) { |
| 1374 |
fprintf(stderr, "bad hostname: %s\n", parm); |
| 1375 |
goto finally; |
| 1376 |
} |
| 1377 |
|
| 1378 |
for (rp = res; rp != NULL; rp = rp->ai_next) |
| 1379 |
count++; |
| 1380 |
|
| 1381 |
/* Expand memory for the new addresses. Assume all the addresses |
| 1382 |
* are v6 addresses. |
| 1383 |
*/ |
| 1384 |
new_addrs = (struct sockaddr *) |
| 1385 |
realloc(addrs, sizeof(struct sockaddr_in6) * count); |
| 1386 |
|
| 1387 |
if (NULL == new_addrs) { |
| 1388 |
count = *ret_count; |
| 1389 |
goto finally; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* Skip the existing addresses. */ |
| 1393 |
aptr = new_addrs; |
| 1394 |
for (j = 0; j < orig_count; j++) { |
| 1395 |
sa_addr = (struct sockaddr *)aptr; |
| 1396 |
switch(sa_addr->sa_family) { |
| 1397 |
case AF_INET: |
| 1398 |
aptr += sizeof(struct sockaddr_in); |
| 1399 |
break; |
| 1400 |
case AF_INET6: |
| 1401 |
aptr += sizeof(struct sockaddr_in6); |
| 1402 |
break; |
| 1403 |
default: |
| 1404 |
count = orig_count; |
| 1405 |
goto finally; |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* Put the new addresses away. */ |
| 1410 |
for (rp = res; rp != NULL; rp = rp->ai_next) { |
| 1411 |
b4ap = (struct sockaddr_in *)aptr; |
| 1412 |
b6ap = (struct sockaddr_in6 *)aptr; |
| 1413 |
bcopy(rp->ai_addr, aptr, rp->ai_addrlen); |
| 1414 |
b4ap->sin_port = htons(local_port); /* equal to b6ap.v6.sin6_port */ |
| 1415 |
if (rp->ai_family == AF_INET6) |
| 1416 |
b6ap->sin6_scope_id = if_index; |
| 1417 |
aptr += rp->ai_addrlen; |
| 1418 |
} |
| 1419 |
|
| 1420 |
finally: |
| 1421 |
|
| 1422 |
*ret_count = count; |
| 1423 |
|
| 1424 |
return new_addrs; |
| 1425 |
|
| 1426 |
} /* append_addr() */ |
| 1427 |
|
| 1428 |
static int |
| 1429 |
parse_inter_commands(char *argv0, char *input, int snd_only) |
| 1430 |
{ |
| 1431 |
int i; |
| 1432 |
char *p; |
| 1433 |
int len; |
| 1434 |
int set = 0; |
| 1435 |
int val; |
| 1436 |
struct sockaddr *tmp_addrs = NULL; |
| 1437 |
|
| 1438 |
|
| 1439 |
p = input; |
| 1440 |
if (*p == '?' || *p == '\n') { |
| 1441 |
printf("Interactive commands:\n"); |
| 1442 |
printf("snd=<int> - Do a sendmsg with the specified"); |
| 1443 |
printf(" length.\n"); |
| 1444 |
printf("rcv=<int> - Do a recvmsg."); |
| 1445 |
printf("The length is ignored for now.\n"); |
| 1446 |
printf("bindx-add=<addr> - Add a local address"); |
| 1447 |
printf(" with bindx. \n"); |
| 1448 |
printf("bindx-rem=<addr> - Remove a local address"); |
| 1449 |
printf(" with bindx. \n"); |
| 1450 |
printf("rcvbuf=<int> - Get/Set receive buffer size\n"); |
| 1451 |
printf("sndbuf=<int> - Get/Set send buffer size.\n"); |
| 1452 |
printf("primary=<addr> - Get/Set association's primary\n"); |
| 1453 |
printf("peer_primary=addr- Set association's peer_primary\n"); |
| 1454 |
printf("heartbeat=<addr> - Request a user initiated heartbeat\n"); |
| 1455 |
printf("maxseg=<int> - Get/Set Maximum fragment size.\n"); |
| 1456 |
printf("nodelay=<0|1> - Get/Set NODELAY option.\n"); |
| 1457 |
printf("shutdown - Shutdown the association.\n"); |
| 1458 |
printf("abort - Abort the association.\n"); |
| 1459 |
printf("stats - Print GET_ASSOC_STATS (if available in kernel).\n"); |
| 1460 |
printf("? - Help. Display this message.\n"); |
| 1461 |
return -1; |
| 1462 |
} |
| 1463 |
|
| 1464 |
for (i = 0; i < REALLY_BIG; i++) { |
| 1465 |
if (('=' == *p) || |
| 1466 |
('?' == *p) || |
| 1467 |
('\n' == *p)) { |
| 1468 |
if ('=' == *p) { |
| 1469 |
set = 1; |
| 1470 |
} |
| 1471 |
*p++ = '\0'; |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
p++; |
| 1475 |
} |
| 1476 |
if (i >= REALLY_BIG) { |
| 1477 |
printf("Invalid input.\n"); |
| 1478 |
return -1; |
| 1479 |
} |
| 1480 |
|
| 1481 |
i = 0; |
| 1482 |
while (NULL != inter_commands[i].cmd) { |
| 1483 |
if (!strcmp(input, inter_commands[i].cmd)) { |
| 1484 |
switch (i) { |
| 1485 |
case INTER_SND: |
| 1486 |
if (snd_only) { |
| 1487 |
if (*p < '0' || *p > '9') { |
| 1488 |
goto err_input; |
| 1489 |
} |
| 1490 |
snd_func(p); |
| 1491 |
} else { |
| 1492 |
goto err_input; |
| 1493 |
} |
| 1494 |
break; |
| 1495 |
case INTER_RCV: |
| 1496 |
if (snd_only) { |
| 1497 |
goto err_input; |
| 1498 |
} |
| 1499 |
break; |
| 1500 |
case INTER_SNDBUF: |
| 1501 |
if (set) { |
| 1502 |
if (*p < '0' || *p > '9') { |
| 1503 |
goto err_input; |
| 1504 |
} |
| 1505 |
} |
| 1506 |
len = (set) ? atoi(p) : 0; |
| 1507 |
sndbuf_func(argv0, inter_sk, len, set); |
| 1508 |
break; |
| 1509 |
case INTER_RCVBUF: |
| 1510 |
if (set) { |
| 1511 |
if (*p < '0' || *p > '9') { |
| 1512 |
goto err_input; |
| 1513 |
} |
| 1514 |
} |
| 1515 |
len = (set) ? atoi(p) : 0; |
| 1516 |
rcvbuf_func(argv0, inter_sk, len, set); |
| 1517 |
break; |
| 1518 |
case INTER_BINDX_ADD: |
| 1519 |
tmp_addrs = get_bindx_addr(p, &len); |
| 1520 |
bindx_func(argv0, inter_sk, tmp_addrs, len, |
| 1521 |
SCTP_BINDX_ADD_ADDR, local_port); |
| 1522 |
free(tmp_addrs); |
| 1523 |
break; |
| 1524 |
case INTER_BINDX_REM: |
| 1525 |
tmp_addrs = get_bindx_addr(p, &len); |
| 1526 |
bindx_func(argv0, inter_sk, tmp_addrs, len, |
| 1527 |
SCTP_BINDX_REM_ADDR, local_port); |
| 1528 |
free(tmp_addrs); |
| 1529 |
break; |
| 1530 |
case INTER_SET_PRIM: |
| 1531 |
primary_func(argv0, inter_sk, p, set); |
| 1532 |
break; |
| 1533 |
case INTER_SET_PEER_PRIM: |
| 1534 |
peer_primary_func(argv0, inter_sk, p, set); |
| 1535 |
break; |
| 1536 |
case INTER_HEARTBEAT: |
| 1537 |
spp_hb_demand_func(argv0, inter_sk, p, set); |
| 1538 |
break; |
| 1539 |
case INTER_SHUTDOWN: |
| 1540 |
shutdown_func(argv0, &inter_sk, SHUTDOWN_SHUTDOWN); |
| 1541 |
break; |
| 1542 |
case INTER_ABORT: |
| 1543 |
shutdown_func(argv0, &inter_sk, SHUTDOWN_ABORT); |
| 1544 |
break; |
| 1545 |
case INTER_NODELAY: |
| 1546 |
if (set) { |
| 1547 |
if (*p < '0' || *p > '9') { |
| 1548 |
goto err_input; |
| 1549 |
} |
| 1550 |
} |
| 1551 |
val = (set) ? atoi(p) : 0; |
| 1552 |
nodelay_func(argv0, inter_sk, val, set); |
| 1553 |
break; |
| 1554 |
case INTER_MAXSEG: |
| 1555 |
if (set) { |
| 1556 |
if (*p < '0' || *p > '9') { |
| 1557 |
goto err_input; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
val = (set) ? atoi(p) : 0; |
| 1561 |
maxseg_func(argv0, inter_sk, val, set); |
| 1562 |
break; |
| 1563 |
case INTER_GET_STATS: |
| 1564 |
get_assocstats_func(inter_sk, associd); |
| 1565 |
break; |
| 1566 |
default: |
| 1567 |
goto err_input; |
| 1568 |
break; |
| 1569 |
} |
| 1570 |
|
| 1571 |
return i; |
| 1572 |
} |
| 1573 |
i++; |
| 1574 |
} |
| 1575 |
|
| 1576 |
err_input: |
| 1577 |
printf("Invalid input.\n"); |
| 1578 |
return -1; |
| 1579 |
|
| 1580 |
} /* parse_inter_commands() */ |
| 1581 |
|
| 1582 |
static char * |
| 1583 |
gen_message(int len) |
| 1584 |
{ |
| 1585 |
|
| 1586 |
char *buf; |
| 1587 |
char *p; |
| 1588 |
int i; |
| 1589 |
|
| 1590 |
buf = malloc(len); |
| 1591 |
|
| 1592 |
if (NULL != buf) { |
| 1593 |
for (i = 0, p = buf; i < len; i++, p++) { |
| 1594 |
if (gen_data > GEN_DATA_LAST) { |
| 1595 |
gen_data = GEN_DATA_FIRST; |
| 1596 |
} |
| 1597 |
*p = gen_data++; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
return(buf); |
| 1602 |
|
| 1603 |
} /* gen_message() */ |
| 1604 |
|
| 1605 |
static void |
| 1606 |
snd_func(char *input) |
| 1607 |
{ |
| 1608 |
|
| 1609 |
int len; |
| 1610 |
|
| 1611 |
len = atoi(input); |
| 1612 |
if (!(inter_outbuf = gen_message(len))) { |
| 1613 |
fprintf(stderr, "Cannot allocate out message.\n"); |
| 1614 |
exit(1); |
| 1615 |
} |
| 1616 |
inter_outlen = len; |
| 1617 |
|
| 1618 |
} /* snd_func() */ |
| 1619 |
|
| 1620 |
static void |
| 1621 |
sndbuf_func(char *argv0, int sk, int len, int set) |
| 1622 |
{ |
| 1623 |
int error; |
| 1624 |
socklen_t optlen; |
| 1625 |
|
| 1626 |
if (set) { |
| 1627 |
error = setsockopt(sk, SOL_SOCKET, SO_SNDBUF, |
| 1628 |
(char *)&len, sizeof(len)); |
| 1629 |
} else { |
| 1630 |
optlen = sizeof(len); |
| 1631 |
error = getsockopt(sk, SOL_SOCKET, SO_SNDBUF, |
| 1632 |
(char *)&len, &optlen); |
| 1633 |
} |
| 1634 |
if (error != 0) { |
| 1635 |
fprintf(stderr, "%s: Error setting/getting sndbuf: %s.\n", |
| 1636 |
argv0, strerror(errno)); |
| 1637 |
exit(1); |
| 1638 |
} |
| 1639 |
|
| 1640 |
if (!set) { |
| 1641 |
printf("sndbuf is %d.\n", len); |
| 1642 |
} |
| 1643 |
|
| 1644 |
} /* sndbuf_func() */ |
| 1645 |
|
| 1646 |
static void |
| 1647 |
rcvbuf_func(char *argv0, int sk, int len, int set) |
| 1648 |
{ |
| 1649 |
int error; |
| 1650 |
socklen_t optlen; |
| 1651 |
|
| 1652 |
if (set) { |
| 1653 |
error = setsockopt(sk, SOL_SOCKET, SO_RCVBUF, |
| 1654 |
(char *)&len, sizeof(len)); |
| 1655 |
} else { |
| 1656 |
optlen = sizeof(len); |
| 1657 |
error = getsockopt(sk, SOL_SOCKET, SO_RCVBUF, |
| 1658 |
(char *)&len, &optlen); |
| 1659 |
} |
| 1660 |
if (error != 0) { |
| 1661 |
fprintf(stderr, "%s: Error setting/getting rcvbuf: %s.\n", |
| 1662 |
argv0, strerror(errno)); |
| 1663 |
exit(1); |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (!set) { |
| 1667 |
printf("rcvbuf is %d.\n", len); |
| 1668 |
} |
| 1669 |
|
| 1670 |
} /* rcvbuf_func() */ |
| 1671 |
|
| 1672 |
|
| 1673 |
static struct sockaddr * |
| 1674 |
get_bindx_addr(char *in, int *count) |
| 1675 |
{ |
| 1676 |
|
| 1677 |
struct sockaddr *tmp_addrs = NULL; |
| 1678 |
char *p = in; |
| 1679 |
|
| 1680 |
/* Set the buffer for address parsing. */ |
| 1681 |
while ('\n' != *p) { |
| 1682 |
p++; |
| 1683 |
} |
| 1684 |
*p = '\0'; |
| 1685 |
|
| 1686 |
*count = 0; |
| 1687 |
|
| 1688 |
tmp_addrs = append_addr(in, tmp_addrs, count); |
| 1689 |
if (NULL == tmp_addrs) { |
| 1690 |
/* We have no memory, so keep fprintf() |
| 1691 |
* from trying to allocate more. |
| 1692 |
*/ |
| 1693 |
fprintf(stderr, "No memory to add "); |
| 1694 |
fprintf(stderr, "%s\n", in); |
| 1695 |
exit(2); |
| 1696 |
} |
| 1697 |
return tmp_addrs; |
| 1698 |
|
| 1699 |
} /* get_bindx_addr() */ |
| 1700 |
|
| 1701 |
static int |
| 1702 |
bindx_func(char *argv0, int sk, struct sockaddr *addrs, int count, int flag, int portnum) |
| 1703 |
{ |
| 1704 |
|
| 1705 |
int error; |
| 1706 |
int i; |
| 1707 |
struct sockaddr *sa_addr; |
| 1708 |
void *aptr; |
| 1709 |
|
| 1710 |
|
| 1711 |
if (0 == portnum) { |
| 1712 |
fprintf(stderr, "%s: A non-0 local port number is ", argv0); |
| 1713 |
fprintf(stderr, "required for bindx to work!\n"); |
| 1714 |
return -1 ; |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* Set the port in every address. */ |
| 1718 |
aptr = addrs; |
| 1719 |
for (i = 0; i < count; i++) { |
| 1720 |
sa_addr = (struct sockaddr *)aptr; |
| 1721 |
|
| 1722 |
switch(sa_addr->sa_family) { |
| 1723 |
case AF_INET: |
| 1724 |
((struct sockaddr_in *)sa_addr)->sin_port = |
| 1725 |
htons(portnum); |
| 1726 |
aptr += sizeof(struct sockaddr_in); |
| 1727 |
break; |
| 1728 |
case AF_INET6: |
| 1729 |
((struct sockaddr_in6 *)sa_addr)->sin6_port = |
| 1730 |
htons(portnum); |
| 1731 |
aptr += sizeof(struct sockaddr_in6); |
| 1732 |
break; |
| 1733 |
default: |
| 1734 |
fprintf(stderr, "Invalid address family\n"); |
| 1735 |
return -1; |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
error = sctp_bindx(sk, addrs, count, flag); |
| 1740 |
|
| 1741 |
if (error != 0) { |
| 1742 |
if (flag == SCTP_BINDX_ADD_ADDR) { |
| 1743 |
fprintf(stderr, "%s: error adding addrs: %s.\n", |
| 1744 |
argv0, strerror(errno)); |
| 1745 |
return -1; |
| 1746 |
} else { |
| 1747 |
fprintf(stderr, "%s: error removing addrs: %s.\n", |
| 1748 |
argv0, strerror(errno)); |
| 1749 |
return -1; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
return 0; |
| 1754 |
|
| 1755 |
} /* bindx_func() */ |
| 1756 |
|
| 1757 |
static int |
| 1758 |
connectx_func(char *argv0, int sk, struct sockaddr *addrs, int count) |
| 1759 |
{ |
| 1760 |
|
| 1761 |
int error; |
| 1762 |
int i; |
| 1763 |
struct sockaddr *sa_addr; |
| 1764 |
void *aptr; |
| 1765 |
|
| 1766 |
|
| 1767 |
if (0 == remote_port) { |
| 1768 |
fprintf(stderr, "%s: A non-0 remote port number is ", argv0); |
| 1769 |
fprintf(stderr, "required for connectx to work!\n"); |
| 1770 |
return -1 ; |
| 1771 |
} |
| 1772 |
|
| 1773 |
/* Set the port in every address. */ |
| 1774 |
aptr = addrs; |
| 1775 |
for (i = 0; i < count; i++) { |
| 1776 |
sa_addr = (struct sockaddr *)aptr; |
| 1777 |
|
| 1778 |
switch(sa_addr->sa_family) { |
| 1779 |
case AF_INET: |
| 1780 |
((struct sockaddr_in *)sa_addr)->sin_port = |
| 1781 |
htons(remote_port); |
| 1782 |
aptr += sizeof(struct sockaddr_in); |
| 1783 |
break; |
| 1784 |
case AF_INET6: |
| 1785 |
((struct sockaddr_in6 *)sa_addr)->sin6_port = |
| 1786 |
htons(remote_port); |
| 1787 |
aptr += sizeof(struct sockaddr_in6); |
| 1788 |
break; |
| 1789 |
default: |
| 1790 |
fprintf(stderr, "Invalid address family\n"); |
| 1791 |
return -1; |
| 1792 |
} |
| 1793 |
} |
| 1794 |
|
| 1795 |
error = sctp_connectx(sk, addrs, count, NULL); |
| 1796 |
|
| 1797 |
if (error != 0) { |
| 1798 |
if (errno == ECONNREFUSED) |
| 1799 |
return -2; |
| 1800 |
fprintf(stderr, "%s: error connecting to addrs: %s.\n", |
| 1801 |
argv0, strerror(errno)); |
| 1802 |
return -1; |
| 1803 |
} |
| 1804 |
|
| 1805 |
return 0; |
| 1806 |
|
| 1807 |
} /* connectx_func() */ |
| 1808 |
|
| 1809 |
static void |
| 1810 |
primary_func(char *argv0, int sk, char *cp, int set) |
| 1811 |
{ |
| 1812 |
struct sctp_prim prim; |
| 1813 |
struct sockaddr_in *in_addr; |
| 1814 |
struct sockaddr_in6 *in6_addr; |
| 1815 |
struct sockaddr *saddr; |
| 1816 |
socklen_t prim_len; |
| 1817 |
int ret; |
| 1818 |
char *p = cp; |
| 1819 |
char addr_buf[INET6_ADDRSTRLEN]; |
| 1820 |
const char *ap = NULL; |
| 1821 |
|
| 1822 |
prim_len = sizeof(struct sctp_prim); |
| 1823 |
if (!set) { |
| 1824 |
prim.ssp_assoc_id = associd; |
| 1825 |
ret = getsockopt(sk, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, |
| 1826 |
&prim, &prim_len); |
| 1827 |
if (ret < 0) |
| 1828 |
goto err; |
| 1829 |
|
| 1830 |
saddr = (struct sockaddr *)&prim.ssp_addr; |
| 1831 |
if (AF_INET == saddr->sa_family) { |
| 1832 |
in_addr = (struct sockaddr_in *)&prim.ssp_addr; |
| 1833 |
ap = inet_ntop(AF_INET, &in_addr->sin_addr, addr_buf, |
| 1834 |
INET6_ADDRSTRLEN); |
| 1835 |
} else if (AF_INET6 == saddr->sa_family) { |
| 1836 |
in6_addr = (struct sockaddr_in6 *)&prim.ssp_addr; |
| 1837 |
ap = inet_ntop(AF_INET6, &in6_addr->sin6_addr, addr_buf, |
| 1838 |
INET6_ADDRSTRLEN); |
| 1839 |
} |
| 1840 |
if (!ap) |
| 1841 |
goto err; |
| 1842 |
printf("%s\n", ap); |
| 1843 |
return; |
| 1844 |
} |
| 1845 |
|
| 1846 |
/* Set the buffer for address parsing. */ |
| 1847 |
while ('\n' != *p) |
| 1848 |
p++; |
| 1849 |
*p = '\0'; |
| 1850 |
|
| 1851 |
prim.ssp_assoc_id = associd; |
| 1852 |
if (strchr(cp, '.')) { |
| 1853 |
in_addr = (struct sockaddr_in *)&prim.ssp_addr; |
| 1854 |
in_addr->sin_port = htons(remote_port); |
| 1855 |
in_addr->sin_family = AF_INET; |
| 1856 |
ret = inet_pton (AF_INET, cp, &in_addr->sin_addr); |
| 1857 |
if (ret <= 0) |
| 1858 |
goto err; |
| 1859 |
} else if (strchr(cp, ':')) { |
| 1860 |
in6_addr = (struct sockaddr_in6 *)&prim.ssp_addr; |
| 1861 |
in6_addr->sin6_port = htons(remote_port); |
| 1862 |
in6_addr->sin6_family = AF_INET6; |
| 1863 |
ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr); |
| 1864 |
if (ret <= 0) |
| 1865 |
goto err; |
| 1866 |
} else |
| 1867 |
goto err; |
| 1868 |
|
| 1869 |
ret = setsockopt(sk, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, |
| 1870 |
&prim, sizeof(struct sctp_prim)); |
| 1871 |
if (ret < 0) |
| 1872 |
goto err; |
| 1873 |
|
| 1874 |
return; |
| 1875 |
err: |
| 1876 |
if (!errno) |
| 1877 |
errno = EINVAL; |
| 1878 |
fprintf(stderr, "%s: error %s primary: %s.\n", argv0, |
| 1879 |
(set)?"setting":"getting", strerror(errno)); |
| 1880 |
} |
| 1881 |
|
| 1882 |
static void |
| 1883 |
peer_primary_func(char *argv0, int sk, char *cp, int set) |
| 1884 |
{ |
| 1885 |
struct sctp_setpeerprim setpeerprim; |
| 1886 |
struct sockaddr_in *in_addr; |
| 1887 |
struct sockaddr_in6 *in6_addr; |
| 1888 |
int ret; |
| 1889 |
char *p = cp; |
| 1890 |
|
| 1891 |
if (!set) { |
| 1892 |
goto err; |
| 1893 |
} |
| 1894 |
|
| 1895 |
/* Set the buffer for address parsing. */ |
| 1896 |
while ('\n' != *p) |
| 1897 |
p++; |
| 1898 |
*p = '\0'; |
| 1899 |
|
| 1900 |
setpeerprim.sspp_assoc_id = associd; |
| 1901 |
if (strchr(cp, '.')) { |
| 1902 |
in_addr = (struct sockaddr_in *)&setpeerprim.sspp_addr; |
| 1903 |
in_addr->sin_port = htons(local_port); |
| 1904 |
in_addr->sin_family = AF_INET; |
| 1905 |
ret = inet_pton (AF_INET, cp, &in_addr->sin_addr); |
| 1906 |
if (ret <= 0) |
| 1907 |
goto err; |
| 1908 |
} else if (strchr(cp, ':')) { |
| 1909 |
in6_addr = (struct sockaddr_in6 *)&setpeerprim.sspp_addr; |
| 1910 |
in6_addr->sin6_port = htons(local_port); |
| 1911 |
in6_addr->sin6_family = AF_INET6; |
| 1912 |
ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr); |
| 1913 |
if (ret <= 0) |
| 1914 |
goto err; |
| 1915 |
} else |
| 1916 |
goto err; |
| 1917 |
|
| 1918 |
ret = setsockopt(sk, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR, |
| 1919 |
&setpeerprim, sizeof(struct sctp_setpeerprim)); |
| 1920 |
if (ret < 0) |
| 1921 |
goto err; |
| 1922 |
|
| 1923 |
return; |
| 1924 |
err: |
| 1925 |
if (!errno) |
| 1926 |
errno = EINVAL; |
| 1927 |
fprintf(stderr, "%s: error %s peer_primary: %s.\n", argv0, |
| 1928 |
(set)?"setting":"getting", strerror(errno)); |
| 1929 |
} |
| 1930 |
|
| 1931 |
static void |
| 1932 |
spp_hb_demand_func(char *argv0, int sk, char *cp, int set) |
| 1933 |
{ |
| 1934 |
struct sctp_paddrparams params; |
| 1935 |
struct sockaddr_in *in_addr; |
| 1936 |
struct sockaddr_in6 *in6_addr; |
| 1937 |
int ret; |
| 1938 |
char *p = cp; |
| 1939 |
|
| 1940 |
memset(¶ms, 0, sizeof(struct sctp_paddrparams)); |
| 1941 |
params.spp_assoc_id = associd; |
| 1942 |
params.spp_flags = SPP_HB_DEMAND; |
| 1943 |
|
| 1944 |
if (set) { |
| 1945 |
/* Set the buffer for address parsing. */ |
| 1946 |
while ('\n' != *p) |
| 1947 |
p++; |
| 1948 |
*p = '\0'; |
| 1949 |
|
| 1950 |
if (strchr(cp, '.')) { |
| 1951 |
in_addr = (struct sockaddr_in *)¶ms.spp_address; |
| 1952 |
in_addr->sin_port = htons(remote_port); |
| 1953 |
in_addr->sin_family = AF_INET; |
| 1954 |
ret = inet_pton(AF_INET, cp, &in_addr->sin_addr); |
| 1955 |
if (ret <= 0) |
| 1956 |
goto err; |
| 1957 |
} else if (strchr(cp, ':')) { |
| 1958 |
in6_addr = (struct sockaddr_in6 *)¶ms.spp_address; |
| 1959 |
in6_addr->sin6_port = htons(remote_port); |
| 1960 |
in6_addr->sin6_family = AF_INET6; |
| 1961 |
ret = inet_pton(AF_INET6, cp, &in6_addr->sin6_addr); |
| 1962 |
if (ret <= 0) |
| 1963 |
goto err; |
| 1964 |
} else |
| 1965 |
goto err; |
| 1966 |
} |
| 1967 |
|
| 1968 |
ret = setsockopt(sk, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, |
| 1969 |
¶ms, sizeof(struct sctp_paddrparams)); |
| 1970 |
if (ret < 0) |
| 1971 |
goto err; |
| 1972 |
|
| 1973 |
return; |
| 1974 |
err: |
| 1975 |
if (!errno) |
| 1976 |
errno = EINVAL; |
| 1977 |
fprintf(stderr, "%s: error %s peer_addr_params: %s.\n", argv0, |
| 1978 |
(set)?"setting":"getting", strerror(errno)); |
| 1979 |
} |
| 1980 |
|
| 1981 |
static int |
| 1982 |
nodelay_func(char *argv0, int sk, int val, int set) |
| 1983 |
{ |
| 1984 |
socklen_t optlen; |
| 1985 |
int error; |
| 1986 |
|
| 1987 |
if (set) { |
| 1988 |
error = setsockopt(sk, SOL_SCTP, SCTP_NODELAY, |
| 1989 |
(char *)&val, sizeof(val)); |
| 1990 |
} else { |
| 1991 |
optlen = sizeof(val); |
| 1992 |
error = getsockopt(sk, SOL_SCTP, SCTP_NODELAY, |
| 1993 |
(char *)&val, &optlen); |
| 1994 |
} |
| 1995 |
if (error != 0) { |
| 1996 |
fprintf(stderr, "%s: Error setting/getting nodelay: %s.\n", |
| 1997 |
argv0, strerror(errno)); |
| 1998 |
exit(1); |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (!set) { |
| 2002 |
printf("nodelay is %d.\n", val); |
| 2003 |
} |
| 2004 |
|
| 2005 |
return error; |
| 2006 |
} |
| 2007 |
|
| 2008 |
static int |
| 2009 |
maxseg_func(char *argv0, int sk, int val, int set) |
| 2010 |
{ |
| 2011 |
socklen_t optlen; |
| 2012 |
int error; |
| 2013 |
|
| 2014 |
if (set) { |
| 2015 |
error = setsockopt(sk, SOL_SCTP, SCTP_MAXSEG, |
| 2016 |
(char *)&val, sizeof(val)); |
| 2017 |
} else { |
| 2018 |
optlen = sizeof(val); |
| 2019 |
error = getsockopt(sk, SOL_SCTP, SCTP_MAXSEG, |
| 2020 |
(char *)&val, &optlen); |
| 2021 |
} |
| 2022 |
if (error != 0) { |
| 2023 |
fprintf(stderr, "%s: Error setting/getting maxseg: %s.\n", |
| 2024 |
argv0, strerror(errno)); |
| 2025 |
exit(1); |
| 2026 |
} |
| 2027 |
|
| 2028 |
if (!set) { |
| 2029 |
printf("maxseg is %d.\n", val); |
| 2030 |
} |
| 2031 |
|
| 2032 |
return error; |
| 2033 |
} |
| 2034 |
|
| 2035 |
static int |
| 2036 |
shutdown_func(char *argv0, int *skp, int shutdown_type) |
| 2037 |
{ |
| 2038 |
struct msghdr outmessage; |
| 2039 |
char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 2040 |
struct cmsghdr *cmsg; |
| 2041 |
int error=0, bytes_sent; |
| 2042 |
struct sctp_sndrcvinfo *sinfo; |
| 2043 |
struct addrinfo hints, *rp; |
| 2044 |
char *sd_type; |
| 2045 |
int sk = *skp; |
| 2046 |
|
| 2047 |
if (shutdown_type == SHUTDOWN_ABORT) |
| 2048 |
sd_type = "ABORT"; |
| 2049 |
else |
| 2050 |
sd_type = "SHUTDOWN"; |
| 2051 |
|
| 2052 |
/* Verify that the association is present. */ |
| 2053 |
error = test_sk_for_assoc(sk, associd); |
| 2054 |
if (error != 0) { |
| 2055 |
printf("The association isn't present yet! Cannot %s!\n", sd_type); |
| 2056 |
return -1; |
| 2057 |
} |
| 2058 |
|
| 2059 |
if (socket_type == SOCK_SEQPACKET) { |
| 2060 |
/* Set up the destination. */ |
| 2061 |
if (remote_host) { |
| 2062 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 2063 |
hints.ai_family = AF_INET; |
| 2064 |
hints.ai_protocol = IPPROTO_SCTP; |
| 2065 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 2066 |
hints.ai_family = AF_INET6; |
| 2067 |
if (getaddrinfo(remote_host, NULL, &hints, &rp) != 0) { |
| 2068 |
fprintf(stderr, "%s: bad hostname: %s\n", |
| 2069 |
argv0, remote_host); |
| 2070 |
exit(1); |
| 2071 |
} |
| 2072 |
} |
| 2073 |
memcpy(&remote_addr, rp->ai_addr, rp->ai_addrlen); |
| 2074 |
remote_addr.v4.sin_port = htons(remote_port); /* equal to v6.sin6_port */ |
| 2075 |
ra_len = rp->ai_addrlen; |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* Initialize the message struct we use to pass messages to |
| 2079 |
* the remote socket. |
| 2080 |
*/ |
| 2081 |
outmessage.msg_name = &remote_addr; |
| 2082 |
outmessage.msg_namelen = ra_len; |
| 2083 |
|
| 2084 |
outmessage.msg_iov = NULL; |
| 2085 |
outmessage.msg_iovlen = 0; |
| 2086 |
outmessage.msg_control = outcmsg; |
| 2087 |
outmessage.msg_controllen = sizeof(outcmsg); |
| 2088 |
outmessage.msg_flags = 0; |
| 2089 |
|
| 2090 |
cmsg = CMSG_FIRSTHDR(&outmessage); |
| 2091 |
cmsg->cmsg_level = IPPROTO_SCTP; |
| 2092 |
cmsg->cmsg_type = SCTP_SNDRCV; |
| 2093 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 2094 |
outmessage.msg_controllen = cmsg->cmsg_len; |
| 2095 |
sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 2096 |
memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 2097 |
if (shutdown_type == SHUTDOWN_ABORT) |
| 2098 |
sinfo->sinfo_flags |= SCTP_ABORT; |
| 2099 |
else |
| 2100 |
sinfo->sinfo_flags |= SCTP_EOF; |
| 2101 |
|
| 2102 |
sinfo->sinfo_assoc_id = associd; |
| 2103 |
|
| 2104 |
bytes_sent = sendmsg(sk, &outmessage, 0); |
| 2105 |
if (bytes_sent != 0) { |
| 2106 |
printf("Failure: %s.\n", strerror(errno)); |
| 2107 |
return -1; |
| 2108 |
} |
| 2109 |
|
| 2110 |
/* Receive the COMM_LOST or SHUTDOWN_COMP event. */ |
| 2111 |
test_recv_assoc_change(sk); |
| 2112 |
} else { |
| 2113 |
if (shutdown_type == SHUTDOWN_ABORT) { |
| 2114 |
struct linger { |
| 2115 |
int l_onoff; |
| 2116 |
int l_linger; |
| 2117 |
} data = {1, 0}; |
| 2118 |
error = setsockopt(sk, SOL_SOCKET, SO_LINGER, |
| 2119 |
(char *)&data, sizeof(data)); |
| 2120 |
if (error != 0) { |
| 2121 |
printf("setsockopt failed %s\n", strerror(errno)); |
| 2122 |
exit(1); |
| 2123 |
} |
| 2124 |
} |
| 2125 |
error = close(sk); |
| 2126 |
if (error != 0) { |
| 2127 |
printf("close failed %s\n", strerror(errno)); |
| 2128 |
exit(1); |
| 2129 |
} |
| 2130 |
*skp = sk = build_endpoint(argv0, local_port); |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* Verify that the association is no longer present. */ |
| 2134 |
error = test_sk_for_assoc(sk, associd); |
| 2135 |
if (error != 0) { |
| 2136 |
printf("Successfully %s the original association\n", sd_type); |
| 2137 |
associd = 0; |
| 2138 |
new_connection = 1; |
| 2139 |
} else { |
| 2140 |
printf("%s failed\n", sd_type); |
| 2141 |
exit(1); |
| 2142 |
} |
| 2143 |
|
| 2144 |
return 0; |
| 2145 |
} |
| 2146 |
|
| 2147 |
static int |
| 2148 |
get_assocstats_func(int sk, sctp_assoc_t assoc_id) |
| 2149 |
{ |
| 2150 |
int error = 0; |
| 2151 |
struct sctp_assoc_stats stats; |
| 2152 |
socklen_t len; |
| 2153 |
|
| 2154 |
if (assoc_id == 0) { |
| 2155 |
printf("No association present yet\n"); |
| 2156 |
return -1; |
| 2157 |
} |
| 2158 |
|
| 2159 |
memset(&stats, 0, sizeof(struct sctp_assoc_stats)); |
| 2160 |
stats.sas_assoc_id = assoc_id; |
| 2161 |
len = sizeof(struct sctp_assoc_stats); |
| 2162 |
error = getsockopt(sk, SOL_SCTP, SCTP_GET_ASSOC_STATS, |
| 2163 |
(char *)&stats, &len); |
| 2164 |
if (error != 0) { |
| 2165 |
printf("get_assoc_stats() failed %s\n", strerror(errno)); |
| 2166 |
return error; |
| 2167 |
} |
| 2168 |
|
| 2169 |
printf("Retransmitted Chunks: %" PRIu64 "\n", (uint64_t) stats.sas_rtxchunks); |
| 2170 |
printf("Gap Acknowledgements Received: %" PRIu64 "\n", (uint64_t) stats.sas_gapcnt); |
| 2171 |
printf("TSN received > next expected: %" PRIu64 "\n", (uint64_t) stats.sas_outofseqtsns); |
| 2172 |
printf("SACKs sent: %" PRIu64 "\n", (uint64_t) stats.sas_osacks); |
| 2173 |
printf("SACKs received: %" PRIu64 "\n", (uint64_t) stats.sas_isacks); |
| 2174 |
printf("Control chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_octrlchunks); |
| 2175 |
printf("Control chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_ictrlchunks); |
| 2176 |
printf("Ordered data chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_oodchunks); |
| 2177 |
printf("Ordered data chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_iodchunks); |
| 2178 |
printf("Unordered data chunks sent: %" PRIu64 "\n", (uint64_t) stats.sas_ouodchunks); |
| 2179 |
printf("Unordered data chunks received: %" PRIu64 "\n", (uint64_t) stats.sas_iuodchunks); |
| 2180 |
printf("Dups received (ordered+unordered): %" PRIu64 "\n", (uint64_t) stats.sas_idupchunks); |
| 2181 |
printf("Packets sent: %" PRIu64 "\n", (uint64_t) stats.sas_opackets); |
| 2182 |
printf("Packets received: %" PRIu64 "\n", (uint64_t) stats.sas_ipackets); |
| 2183 |
printf("Maximum Observed RTO this period: %" PRIu64 " - Transport: ", (uint64_t) stats.sas_maxrto); |
| 2184 |
print_sockaddr((struct sockaddr *)&stats.sas_obs_rto_ipaddr); |
| 2185 |
printf("\n"); |
| 2186 |
|
| 2187 |
return 0; |
| 2188 |
} |
| 2189 |
|
| 2190 |
static int |
| 2191 |
test_sk_for_assoc(int sk, sctp_assoc_t assoc_id) |
| 2192 |
{ |
| 2193 |
int error = 0; |
| 2194 |
struct sctp_status status; |
| 2195 |
socklen_t status_len; |
| 2196 |
|
| 2197 |
memset(&status, 0, sizeof(status)); |
| 2198 |
if (assoc_id) |
| 2199 |
status.sstat_assoc_id = assoc_id; |
| 2200 |
status_len = sizeof(struct sctp_status); |
| 2201 |
error = getsockopt(sk, SOL_SCTP, SCTP_STATUS, |
| 2202 |
(char *)&status, &status_len); |
| 2203 |
return error; |
| 2204 |
} |
| 2205 |
|
| 2206 |
/* Receive a notification and return the corresponding associd if the event is |
| 2207 |
* SCTP_COMM_UP. Return 0 for any other event. |
| 2208 |
*/ |
| 2209 |
static sctp_assoc_t |
| 2210 |
test_recv_assoc_change(int sk) |
| 2211 |
{ |
| 2212 |
struct msghdr inmessage; |
| 2213 |
struct iovec iov; |
| 2214 |
char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
| 2215 |
int error; |
| 2216 |
|
| 2217 |
/* Initialize inmessage with enough space for DATA... */ |
| 2218 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 2219 |
if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) { |
| 2220 |
printf("%s: Can't allocate memory.\n", __FUNCTION__); |
| 2221 |
exit(1); |
| 2222 |
} |
| 2223 |
iov.iov_len = REALLY_BIG; |
| 2224 |
inmessage.msg_iov = &iov; |
| 2225 |
inmessage.msg_iovlen = 1; |
| 2226 |
/* or a control message. */ |
| 2227 |
inmessage.msg_control = incmsg; |
| 2228 |
inmessage.msg_controllen = sizeof(incmsg); |
| 2229 |
|
| 2230 |
error = recvmsg(sk, &inmessage, MSG_WAITALL); |
| 2231 |
if (error < 0) { |
| 2232 |
printf("%s: recvmsg: %s\n", __FUNCTION__, strerror(errno)); |
| 2233 |
exit(1); |
| 2234 |
} |
| 2235 |
|
| 2236 |
return test_verify_assoc_change(&inmessage); |
| 2237 |
} |
| 2238 |
|
| 2239 |
/* Verify a notification and return the corresponding associd if the event is |
| 2240 |
* SCTP_COMM_UP. Return 0 for any other event. |
| 2241 |
*/ |
| 2242 |
static sctp_assoc_t |
| 2243 |
test_verify_assoc_change(struct msghdr *msg) |
| 2244 |
{ |
| 2245 |
union sctp_notification *sn; |
| 2246 |
|
| 2247 |
if (!(msg->msg_flags & MSG_NOTIFICATION)) { |
| 2248 |
fprintf(stderr, "%s: Received data when notification is expected\n", |
| 2249 |
__FUNCTION__); |
| 2250 |
exit(1); |
| 2251 |
} |
| 2252 |
|
| 2253 |
sn = (union sctp_notification *)msg->msg_iov->iov_base; |
| 2254 |
if (SCTP_ASSOC_CHANGE != sn->sn_header.sn_type) { |
| 2255 |
fprintf(stderr, "%s: Received unexpected notification: %d", |
| 2256 |
__FUNCTION__, sn->sn_header.sn_type); |
| 2257 |
exit(1); |
| 2258 |
} |
| 2259 |
|
| 2260 |
switch(sn->sn_assoc_change.sac_state) |
| 2261 |
{ |
| 2262 |
case SCTP_COMM_UP: |
| 2263 |
printf("Received SCTP_COMM_UP\n"); |
| 2264 |
break; |
| 2265 |
case SCTP_COMM_LOST: |
| 2266 |
printf("Received SCTP_COMM_LOST\n"); |
| 2267 |
break; |
| 2268 |
case SCTP_RESTART: |
| 2269 |
printf("Received SCTP_RESTART\n"); |
| 2270 |
break; |
| 2271 |
case SCTP_SHUTDOWN_COMP: |
| 2272 |
printf("Received SCTP_SHUTDOWN_COMP\n"); |
| 2273 |
break; |
| 2274 |
case SCTP_CANT_STR_ASSOC: |
| 2275 |
printf("Received SCTP_CANT_STR_ASSOC\n"); |
| 2276 |
break; |
| 2277 |
} |
| 2278 |
|
| 2279 |
if (SCTP_COMM_UP == sn->sn_assoc_change.sac_state) |
| 2280 |
return sn->sn_assoc_change.sac_assoc_id; |
| 2281 |
else |
| 2282 |
return 0; |
| 2283 |
} |
| 2284 |
|
| 2285 |
void print_addr_buf(void * laddrs, int n_laddrs) |
| 2286 |
{ |
| 2287 |
void *addr_buf = laddrs; |
| 2288 |
int i; |
| 2289 |
|
| 2290 |
for (i = 0; i < n_laddrs; i++) { |
| 2291 |
addr_buf += print_sockaddr((struct sockaddr *)addr_buf); |
| 2292 |
printf("\n"); |
| 2293 |
} |
| 2294 |
} |
| 2295 |
|
| 2296 |
int print_sockaddr(struct sockaddr *sa_addr) |
| 2297 |
{ |
| 2298 |
struct sockaddr_in *in_addr; |
| 2299 |
struct sockaddr_in6 *in6_addr; |
| 2300 |
|
| 2301 |
if (AF_INET == sa_addr->sa_family) { |
| 2302 |
in_addr = (struct sockaddr_in *)sa_addr; |
| 2303 |
printf("%d.%d.%d.%d:%d", |
| 2304 |
NIPQUAD(in_addr->sin_addr), |
| 2305 |
ntohs(in_addr->sin_port)); |
| 2306 |
return sizeof(struct sockaddr_in); |
| 2307 |
} else { |
| 2308 |
in6_addr = (struct sockaddr_in6 *)sa_addr; |
| 2309 |
printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%d", |
| 2310 |
NIP6(in6_addr->sin6_addr), |
| 2311 |
ntohs(in6_addr->sin6_port)); |
| 2312 |
return sizeof(struct sockaddr_in6); |
| 2313 |
} |
| 2314 |
} |