| 1 |
/* SCTP kernel Implementation |
| 2 |
* (C) Copyright IBM Corp. 2003 |
| 3 |
* |
| 4 |
* The SCTP implementation is free software; |
| 5 |
* you can redistribute it and/or modify it under the terms of |
| 6 |
* the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2, or (at your option) |
| 8 |
* any later version. |
| 9 |
* |
| 10 |
* The SCTP implementation is distributed in the hope that it |
| 11 |
* will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 |
* ************************ |
| 13 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
* See the GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with GNU CC; see the file COPYING. If not, write to |
| 18 |
* the Free Software Foundation, 59 Temple Place - Suite 330, |
| 19 |
* Boston, MA 02111-1307, USA. |
| 20 |
* |
| 21 |
* Please send any bug reports or fixes you make to the |
| 22 |
* email address(es): |
| 23 |
* lksctp developers <[email protected]> |
| 24 |
* |
| 25 |
* Or submit a bug report through the following website: |
| 26 |
* https://github.com/sctp/lksctp-tools/ |
| 27 |
* |
| 28 |
* Any bugs reported given to us we will try to fix... any fixes shared will |
| 29 |
* be incorporated into the next SCTP release. |
| 30 |
* |
| 31 |
* Written or modified by: |
| 32 |
* Ryan Layer <[email protected]> |
| 33 |
*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <fcntl.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/socket.h> |
| 42 |
#include <sys/uio.h> |
| 43 |
#include <netinet/in.h> |
| 44 |
#include <errno.h> |
| 45 |
#include <netinet/sctp.h> |
| 46 |
#include <sctputil.h> |
| 47 |
#include <netdb.h> |
| 48 |
#include <getopt.h> |
| 49 |
|
| 50 |
char *TCID = __FILE__; |
| 51 |
int TST_TOTAL = 1; |
| 52 |
int TST_CNT = 0; |
| 53 |
|
| 54 |
#define MAXHOSTNAME 64 |
| 55 |
|
| 56 |
#define MAXCLIENTNUM 10000 |
| 57 |
|
| 58 |
#define TRUE 1 |
| 59 |
|
| 60 |
#define SERVER 1 |
| 61 |
#define CLIENT 0 |
| 62 |
#define NOT_DEFINED -1 |
| 63 |
|
| 64 |
int mode = NOT_DEFINED; |
| 65 |
|
| 66 |
int assoc_num; |
| 67 |
int active = 0; |
| 68 |
|
| 69 |
char *local_host; |
| 70 |
char *remote_host; |
| 71 |
char *local_port; |
| 72 |
char *remote_port; |
| 73 |
sockaddr_storage_t client_loop, |
| 74 |
server_loop; |
| 75 |
struct hostent *hst; |
| 76 |
char big_buffer[REALLY_BIG]; |
| 77 |
|
| 78 |
void usage(char *argv0); |
| 79 |
void parse_arguments(int argc, char*argv[]); |
| 80 |
void data_received(struct msghdr *inmessage, int len, int stream, |
| 81 |
int server_socket); |
| 82 |
int event_received(struct msghdr *inmessage, int assoc_num); |
| 83 |
void process_ready_sockets(int client_socket[], int assoc_num, fd_set *rfds); |
| 84 |
void server_mode(void); |
| 85 |
void client_mode(void); |
| 86 |
|
| 87 |
/* Print the syntax/usage */ |
| 88 |
void usage(char *argv0) |
| 89 |
{ |
| 90 |
printf("usage: %s -H localhost -P localport -l|c [-h remotehost]\n" |
| 91 |
"\t\t[-p remoteport] [-a] [-n <cnt>]\n" |
| 92 |
" -H\t\tspecify a local address.\n" |
| 93 |
" -P\t\tspecify the local port number to be used\n" |
| 94 |
" -l\t\trun in server mode.\n" |
| 95 |
" -c\t\trun in client mode.\n" |
| 96 |
" -h\t\tspecify the peer address.\n" |
| 97 |
" -p\t\tspecify the port number for the peer address.\n" |
| 98 |
" -a\t\tactively generate traffic with the server.\n" |
| 99 |
" -n\t\tspecify the number of associations to create.\n", |
| 100 |
argv0); |
| 101 |
} |
| 102 |
|
| 103 |
/* Parse command line options */ |
| 104 |
void parse_arguments(int argc, char*argv[]) { |
| 105 |
struct addrinfo hints, *rp; |
| 106 |
int c; |
| 107 |
|
| 108 |
while ((c = getopt(argc, argv, ":H:P:ach:ln:p:")) >= 0) { |
| 109 |
switch (c) { |
| 110 |
case 'H': |
| 111 |
local_host = optarg; |
| 112 |
break; |
| 113 |
case 'P': |
| 114 |
local_port = optarg; |
| 115 |
break; |
| 116 |
case 'c': |
| 117 |
if (mode == NOT_DEFINED) |
| 118 |
mode = CLIENT; |
| 119 |
else { |
| 120 |
usage(argv[0]); |
| 121 |
exit(0); |
| 122 |
} |
| 123 |
break; |
| 124 |
case 'a': |
| 125 |
active = 1; |
| 126 |
break; |
| 127 |
case 'h': |
| 128 |
remote_host = optarg; |
| 129 |
break; |
| 130 |
case 'l': |
| 131 |
if (mode == NOT_DEFINED) |
| 132 |
mode = SERVER; |
| 133 |
else { |
| 134 |
usage(argv[0]); |
| 135 |
exit(0); |
| 136 |
} |
| 137 |
break; |
| 138 |
case 'n': |
| 139 |
assoc_num = atoi(optarg); |
| 140 |
break; |
| 141 |
case 'p': |
| 142 |
remote_port = optarg; |
| 143 |
break; |
| 144 |
default: |
| 145 |
usage(argv[0]); |
| 146 |
exit(0); |
| 147 |
} |
| 148 |
} /* while() */ |
| 149 |
|
| 150 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 151 |
hints.ai_family = AF_INET; |
| 152 |
hints.ai_protocol = IPPROTO_SCTP; |
| 153 |
if (mode == CLIENT) { |
| 154 |
if (assoc_num) { |
| 155 |
if (assoc_num > MAXCLIENTNUM) { |
| 156 |
printf("The number of associations indicated " |
| 157 |
"is greater than the"); |
| 158 |
printf("max number of associations " |
| 159 |
"allowed(%d).", MAXCLIENTNUM); |
| 160 |
usage(argv[0]); |
| 161 |
exit(0); |
| 162 |
} |
| 163 |
} else |
| 164 |
assoc_num = 1; |
| 165 |
|
| 166 |
if (remote_host && remote_port) { |
| 167 |
if (getaddrinfo(remote_host, remote_port, &hints, &rp) != 0) { |
| 168 |
printf("%s: bad remote hostname or port: %s, %s\n", |
| 169 |
argv[0], remote_host, remote_port); |
| 170 |
usage(argv[0]); |
| 171 |
exit(0); |
| 172 |
} |
| 173 |
memcpy(&server_loop, rp->ai_addr, rp->ai_addrlen); |
| 174 |
} else { |
| 175 |
printf("Remote host and remote port must be defined " |
| 176 |
"in client mode\n"); |
| 177 |
usage(argv[0]); |
| 178 |
exit(0); |
| 179 |
} |
| 180 |
|
| 181 |
if (getaddrinfo(local_host, local_port, &hints, &rp) != 0) { |
| 182 |
printf("%s: bad local hostname or port: %s, %s\n", |
| 183 |
argv[0], local_host, local_port); |
| 184 |
exit(0); |
| 185 |
} |
| 186 |
memcpy(&client_loop, rp->ai_addr, rp->ai_addrlen); |
| 187 |
} else if (mode == SERVER) { |
| 188 |
if (active) { |
| 189 |
printf("This option if for client use only"); |
| 190 |
usage(argv[0]); |
| 191 |
exit(0); |
| 192 |
} |
| 193 |
|
| 194 |
if (remote_host || remote_port) { |
| 195 |
printf("Remote values not needed in server mode.\n"); |
| 196 |
usage(argv[0]); |
| 197 |
exit(0); |
| 198 |
} |
| 199 |
|
| 200 |
if (!local_port) { |
| 201 |
printf("Specify a local port in server mode.\n"); |
| 202 |
usage(argv[0]); |
| 203 |
exit(0); |
| 204 |
} |
| 205 |
if (getaddrinfo(local_host, local_port, &hints, &rp) != 0) { |
| 206 |
printf("%s: bad local hostname or port: %s, %s\n", |
| 207 |
argv[0], local_host, local_port); |
| 208 |
exit(0); |
| 209 |
} |
| 210 |
memcpy(&server_loop, rp->ai_addr, rp->ai_addrlen); |
| 211 |
} else { |
| 212 |
printf("Must assisgn a client or server mode.\n"); |
| 213 |
usage(argv[0]); |
| 214 |
exit(0); |
| 215 |
} |
| 216 |
} /* parse_arguments() */ |
| 217 |
|
| 218 |
/* Handle data received */ |
| 219 |
void data_received(struct msghdr *inmessage, int len, int stream, int socket) { |
| 220 |
|
| 221 |
int ppid, error; |
| 222 |
char *ping = "PING"; |
| 223 |
|
| 224 |
if (mode == SERVER) { |
| 225 |
ppid = rand(); |
| 226 |
|
| 227 |
error = sctp_sendmsg(socket, |
| 228 |
inmessage->msg_iov->iov_base, |
| 229 |
len, |
| 230 |
(struct sockaddr *)inmessage->msg_name, |
| 231 |
inmessage->msg_namelen, |
| 232 |
ppid, |
| 233 |
0, |
| 234 |
stream, |
| 235 |
0, 0); |
| 236 |
|
| 237 |
if (error < 0) { |
| 238 |
printf("Send Failure: %s.\n", strerror(errno)); |
| 239 |
DUMP_CORE; |
| 240 |
} |
| 241 |
} else { |
| 242 |
ppid = rand(); |
| 243 |
|
| 244 |
printf("Data Received by socket #: %d.\n", socket); |
| 245 |
printf("\tMessage = %s\n", |
| 246 |
(char *)inmessage->msg_iov->iov_base); |
| 247 |
|
| 248 |
if (active) { |
| 249 |
error = sctp_sendmsg(socket, ping, strlen(ping) + 1, |
| 250 |
(struct sockaddr *)&server_loop, |
| 251 |
sizeof(server_loop), ppid, 0, |
| 252 |
stream, 0, 0); |
| 253 |
if (error < 0) { |
| 254 |
printf("Send Failure: %s.\n", |
| 255 |
strerror(errno)); |
| 256 |
DUMP_CORE; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/* This will print what type of SCTP_ASSOC_CHANGE state that was received */ |
| 263 |
void print_sctp_sac_state(struct msghdr *msg) { |
| 264 |
|
| 265 |
char *data; |
| 266 |
union sctp_notification *sn; |
| 267 |
|
| 268 |
if (msg->msg_flags & MSG_NOTIFICATION) { |
| 269 |
data = (char *)msg->msg_iov[0].iov_base; |
| 270 |
|
| 271 |
sn = (union sctp_notification *)data; |
| 272 |
|
| 273 |
switch (sn->sn_assoc_change.sac_state) { |
| 274 |
case SCTP_COMM_UP: |
| 275 |
printf("SCTP_COMM_UP\n"); |
| 276 |
break; |
| 277 |
case SCTP_COMM_LOST: |
| 278 |
printf("SCTP_COMM_LOST\n"); |
| 279 |
break; |
| 280 |
case SCTP_RESTART: |
| 281 |
printf("SCTP_RESTART"); |
| 282 |
break; |
| 283 |
case SCTP_SHUTDOWN_COMP: |
| 284 |
printf("SCTP_SHUTDOWN_COMP\n"); |
| 285 |
break; |
| 286 |
case SCTP_CANT_STR_ASSOC: |
| 287 |
printf("SCTP_CANT_STR_ASSOC\n"); |
| 288 |
break; |
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
} /* void print_sctp_sac_state() */ |
| 294 |
|
| 295 |
/* Tests what type of MSG_NOTIFICATION has been received. |
| 296 |
* For now this fucntion only works with SCTP_ASSOC_CHANGE |
| 297 |
* types, but can be easily expanded. |
| 298 |
* |
| 299 |
* Will return... |
| 300 |
* -1 if the msg_flags is not MSG_NOTIFICATION |
| 301 |
* 0 if the MSG_NOTIFICATION type differs from the type |
| 302 |
* passed into the additional variable |
| 303 |
* 1 if the MSG_NOTIFICATION type matches the type |
| 304 |
* passed into the additional variable |
| 305 |
*/ |
| 306 |
int test_check_notification_type(struct msghdr *msg, |
| 307 |
uint16_t sn_type, |
| 308 |
uint32_t additional) { |
| 309 |
|
| 310 |
char *data; |
| 311 |
union sctp_notification *sn; |
| 312 |
|
| 313 |
if (!(msg->msg_flags & MSG_NOTIFICATION)) { |
| 314 |
return -1; |
| 315 |
} else { |
| 316 |
|
| 317 |
/* Fixup for testframe. */ |
| 318 |
data = (char *)msg->msg_iov[0].iov_base; |
| 319 |
|
| 320 |
sn = (union sctp_notification *)data; |
| 321 |
|
| 322 |
if (sn->sn_header.sn_type != sn_type) |
| 323 |
return 0; |
| 324 |
else if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) |
| 325 |
if (sn->sn_assoc_change.sac_state == additional) |
| 326 |
return 1; |
| 327 |
return 0; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/* Determine the type of event and make correct adjustments to the |
| 332 |
* association count |
| 333 |
*/ |
| 334 |
int event_received(struct msghdr *inmessage, int assoc_num) { |
| 335 |
|
| 336 |
int error; |
| 337 |
|
| 338 |
printf("Event Received\n"); |
| 339 |
|
| 340 |
print_sctp_sac_state(inmessage); |
| 341 |
|
| 342 |
if (mode == SERVER) { |
| 343 |
/* Test type of Event */ |
| 344 |
error = test_check_notification_type(inmessage, |
| 345 |
SCTP_ASSOC_CHANGE, |
| 346 |
SCTP_COMM_UP); |
| 347 |
if (error > 0) { |
| 348 |
assoc_num++; |
| 349 |
printf("Assosiation Established: count = %d.\n", |
| 350 |
assoc_num); |
| 351 |
} else { |
| 352 |
error = test_check_notification_type(inmessage, |
| 353 |
SCTP_ASSOC_CHANGE, |
| 354 |
SCTP_SHUTDOWN_COMP); |
| 355 |
|
| 356 |
if (error > 0) { |
| 357 |
assoc_num--; |
| 358 |
printf("Assosiation Shutdown: count = %d.\n", |
| 359 |
assoc_num); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
return assoc_num; |
| 364 |
} |
| 365 |
|
| 366 |
void server_mode() { |
| 367 |
sockaddr_storage_t msgname; |
| 368 |
int server_socket, |
| 369 |
error, |
| 370 |
stream; |
| 371 |
int assoc_num =0; |
| 372 |
struct msghdr inmessage; |
| 373 |
struct iovec iov; |
| 374 |
char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 375 |
|
| 376 |
|
| 377 |
printf("Running in Server Mode...\n"); |
| 378 |
|
| 379 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 380 |
iov.iov_base = big_buffer; |
| 381 |
iov.iov_len = REALLY_BIG; |
| 382 |
inmessage.msg_iov = &iov; |
| 383 |
inmessage.msg_iovlen =1; |
| 384 |
inmessage.msg_control = incmsg; |
| 385 |
inmessage.msg_controllen = sizeof(incmsg); |
| 386 |
inmessage.msg_name = &msgname; |
| 387 |
inmessage.msg_namelen = sizeof (msgname); |
| 388 |
|
| 389 |
stream = 1; |
| 390 |
|
| 391 |
server_socket = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 392 |
if (server_socket < 0) { |
| 393 |
printf("Socket Failure: %s.\n", strerror(errno)); |
| 394 |
DUMP_CORE; |
| 395 |
} |
| 396 |
|
| 397 |
error = bind(server_socket, &server_loop.sa, sizeof(server_loop)); |
| 398 |
if (error != 0 ) { |
| 399 |
printf("Bind Failure: %s.\n", strerror(errno)); |
| 400 |
DUMP_CORE; |
| 401 |
} |
| 402 |
|
| 403 |
error = listen(server_socket, 1); |
| 404 |
if (error != 0) { |
| 405 |
printf("Listen Failure: %s.\n", strerror(errno)); |
| 406 |
DUMP_CORE; |
| 407 |
} |
| 408 |
while (TRUE) { |
| 409 |
error = recvmsg(server_socket, &inmessage, MSG_WAITALL); |
| 410 |
if (error < 0) { |
| 411 |
printf("Receive Failure: %s\n", |
| 412 |
strerror(errno)); |
| 413 |
} else { |
| 414 |
if (inmessage.msg_flags & MSG_NOTIFICATION) |
| 415 |
assoc_num = event_received(&inmessage, assoc_num); |
| 416 |
else |
| 417 |
data_received(&inmessage, error, stream, server_socket); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void client_mode() { |
| 423 |
|
| 424 |
int i, error, stream, max_socket = 0; |
| 425 |
uint32_t ppid = 0; |
| 426 |
int client_socket[assoc_num]; |
| 427 |
char *message = "Awake"; |
| 428 |
fd_set rfds; |
| 429 |
struct timeval tv; |
| 430 |
|
| 431 |
stream = 1; |
| 432 |
|
| 433 |
printf("Running in Client Mode...\n"); |
| 434 |
|
| 435 |
/* Create the sockets */ |
| 436 |
for (i = 0; i < assoc_num; i++) { |
| 437 |
client_socket[i] = socket(PF_INET, SOCK_SEQPACKET, |
| 438 |
IPPROTO_SCTP); |
| 439 |
if (client_socket[i] < 0 ){ |
| 440 |
printf("Socket Failure: %s.\n", strerror(errno)); |
| 441 |
DUMP_CORE; |
| 442 |
} |
| 443 |
|
| 444 |
if (local_port) { |
| 445 |
error = bind(client_socket[i], &client_loop.sa, |
| 446 |
sizeof(client_loop)); |
| 447 |
if (error < 0) { |
| 448 |
printf("Bind Failure: %s\n", strerror(errno)); |
| 449 |
DUMP_CORE; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
printf("Create Socket #: %d\n", client_socket[i]); |
| 454 |
|
| 455 |
/* Connect to server and send initial message */ |
| 456 |
error = connect(client_socket[i], &server_loop.sa, |
| 457 |
sizeof(server_loop)); |
| 458 |
if (error < 0){ |
| 459 |
printf("Connect Failure: %s.\n", strerror(errno)); |
| 460 |
DUMP_CORE; |
| 461 |
} |
| 462 |
|
| 463 |
max_socket = client_socket[i]; |
| 464 |
|
| 465 |
ppid++; |
| 466 |
|
| 467 |
/* Send initial message */ |
| 468 |
error = sctp_sendmsg(client_socket[i], |
| 469 |
message, |
| 470 |
strlen(message) + 1, |
| 471 |
(struct sockaddr *)&server_loop, |
| 472 |
sizeof(server_loop), |
| 473 |
ppid, |
| 474 |
0, |
| 475 |
stream, |
| 476 |
0, 0); |
| 477 |
if (error < 0 ) { |
| 478 |
printf("Send Failure: %s.\n", strerror(errno)); |
| 479 |
DUMP_CORE; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
while (TRUE){ |
| 484 |
|
| 485 |
/* Clear the set for select() */ |
| 486 |
FD_ZERO(&rfds); |
| 487 |
|
| 488 |
/* Set time out values for select() */ |
| 489 |
tv.tv_sec = 5; |
| 490 |
tv.tv_usec = 0; |
| 491 |
|
| 492 |
/* Add the sockets select() will examine */ |
| 493 |
for (i = 0; i < assoc_num; i++) { |
| 494 |
FD_SET(client_socket[i], &rfds); |
| 495 |
} |
| 496 |
|
| 497 |
/* Wait until there is data to be read from one of the |
| 498 |
* sockets, or until the timer expires |
| 499 |
*/ |
| 500 |
error = select(max_socket + 1, &rfds, NULL, NULL, &tv); |
| 501 |
|
| 502 |
if (error < 0) { |
| 503 |
printf("Select Failure: %s.\n", strerror(errno)); |
| 504 |
DUMP_CORE; |
| 505 |
} else if (error) { |
| 506 |
/* Loop through the array of sockets to find the ones |
| 507 |
*that have information to be read |
| 508 |
*/ |
| 509 |
process_ready_sockets(client_socket, assoc_num, &rfds); |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
void process_ready_sockets(int client_socket[], int assoc_num, fd_set *rfds) { |
| 515 |
|
| 516 |
int i, stream, error; |
| 517 |
struct msghdr inmessage; |
| 518 |
struct iovec iov; |
| 519 |
char incmsg[CMSG_SPACE(sizeof (sctp_cmsg_data_t))]; |
| 520 |
sockaddr_storage_t msgname; |
| 521 |
|
| 522 |
/* Setup inmessage to be able to receive in incomming message */ |
| 523 |
memset(&inmessage, 0, sizeof (inmessage)); |
| 524 |
iov.iov_base = big_buffer; |
| 525 |
iov.iov_len = REALLY_BIG; |
| 526 |
inmessage.msg_iov = &iov; |
| 527 |
inmessage.msg_iovlen =1; |
| 528 |
inmessage.msg_control = incmsg; |
| 529 |
inmessage.msg_controllen = sizeof (incmsg); |
| 530 |
inmessage.msg_name = &msgname; |
| 531 |
inmessage.msg_namelen = sizeof (msgname); |
| 532 |
|
| 533 |
stream = 1; |
| 534 |
|
| 535 |
for( i = 0; i < assoc_num; i++) { |
| 536 |
if (FD_ISSET(client_socket[i], rfds)) { |
| 537 |
error = recvmsg(client_socket[i], &inmessage, |
| 538 |
MSG_WAITALL); |
| 539 |
if (error < 0) |
| 540 |
printf("Receive Failure: %s\n", |
| 541 |
strerror(errno)); |
| 542 |
else { |
| 543 |
/* Test to find the type of message that was read(event/data) */ |
| 544 |
if (inmessage.msg_flags & |
| 545 |
MSG_NOTIFICATION) |
| 546 |
event_received(&inmessage, |
| 547 |
0); |
| 548 |
|
| 549 |
else |
| 550 |
data_received(&inmessage, error, |
| 551 |
stream, |
| 552 |
client_socket[i]); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
int main(int argc, char *argv[]) { |
| 559 |
|
| 560 |
parse_arguments(argc, argv); |
| 561 |
|
| 562 |
if (mode == SERVER) { |
| 563 |
server_mode(); |
| 564 |
} else if (mode == CLIENT){ |
| 565 |
client_mode(); |
| 566 |
} |
| 567 |
exit(1); |
| 568 |
} |
| 569 |
|