| 1 |
/* myftp - simple file transfer over sctp testing tool. |
| 2 |
* Copyright (c) 2002 Intel Corp. |
| 3 |
* |
| 4 |
* This file is part of the LKSCTP kernel Implementation. This |
| 5 |
* is a submission by Xingang Guo from the Intel Corporation while |
| 6 |
* participating on the LKSCTP project. |
| 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 |
* Written or modified by: |
| 33 |
* Xingang Guo <[email protected]> |
| 34 |
* Jon Grimm <[email protected]> |
| 35 |
* |
| 36 |
* Any bugs reported given to us we will try to fix... any fixes shared will |
| 37 |
* be incorporated into the next SCTP release. |
| 38 |
*/ |
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#define _GNU_SOURCE |
| 43 |
#include <getopt.h> |
| 44 |
#include <netdb.h> |
| 45 |
#include <fcntl.h> |
| 46 |
#include <unistd.h> |
| 47 |
|
| 48 |
#include <ctype.h> |
| 49 |
#include <sys/types.h> |
| 50 |
#include <sys/stat.h> |
| 51 |
#include <sys/socket.h> |
| 52 |
#include <sys/uio.h> |
| 53 |
#include <netinet/in.h> /* for sockaddr_in */ |
| 54 |
#include <errno.h> |
| 55 |
#include <netinet/sctp.h> |
| 56 |
|
| 57 |
#define BUFSIZE 1024 |
| 58 |
static char buffer[BUFSIZE]; |
| 59 |
#define DUMP_CORE { char *diediedie = 0; *diediedie = 0; } |
| 60 |
|
| 61 |
typedef enum { COMMAND_NONE, COMMAND_RECV, COMMAND_SEND } command_t; |
| 62 |
|
| 63 |
/* These are the global options. */ |
| 64 |
#define MAX_NUM_HOST 5 |
| 65 |
static char *local_host[MAX_NUM_HOST]; |
| 66 |
static int num_local_host = 0; |
| 67 |
static char *local_port = "4444"; |
| 68 |
|
| 69 |
static int buffer_size = BUFSIZE; |
| 70 |
static char *remote_host = NULL; |
| 71 |
static char *remote_port = "4444"; |
| 72 |
static command_t command = COMMAND_NONE; |
| 73 |
static char *filename = NULL; |
| 74 |
static int interactive = 0; |
| 75 |
static unsigned long delay = 0; |
| 76 |
static int verbose = 0; |
| 77 |
|
| 78 |
static void |
| 79 |
usage(char *argv0) |
| 80 |
{ |
| 81 |
fprintf(stderr, "Usage: %s [options]\n",argv0); |
| 82 |
fprintf(stderr, "Options:\n"); |
| 83 |
fprintf(stderr, "\t--local, -H <hostname> Specify local interface\n"); |
| 84 |
fprintf(stderr, "\t--local-port, -P <port> Specify local port (default 4444)\n"); |
| 85 |
fprintf(stderr, "\t--remote, -h <hostname> Specify interface on remote host\n"); |
| 86 |
fprintf(stderr, "\t--remote-port, -p <port> Specify remote port (default 4444)\n"); |
| 87 |
fprintf(stderr, "\t--listen, -l Work in receiving mode\n"); |
| 88 |
fprintf(stderr, "\t--send, -s Work in sending mode\n"); |
| 89 |
fprintf(stderr, "\t--file, -f <filename> File to read or write,\n"); |
| 90 |
fprintf(stderr, "\t--buffer, -b <size> Buffer size. (default 1024 bytes)\n"); |
| 91 |
fprintf(stderr, "\t by default use standard input/output.\n"); |
| 92 |
fprintf(stderr, "\t--quick, -q Send packets continueously,\n"); |
| 93 |
fprintf(stderr, "\t do not wait for <ENTER> key. Default wait.\n"); |
| 94 |
fprintf(stderr, "\t--delay, -d <usec> Delay between consecutive sends (see --quick)\n"); |
| 95 |
fprintf(stderr, "\t--verbose, -v In verbose mode, display the progress.\n"); |
| 96 |
fprintf(stderr, "\n\t--help, Print this message.\n\n"); |
| 97 |
} /* usage() */ |
| 98 |
|
| 99 |
static int parse_arguments(int argc, char *argv[]) |
| 100 |
{ |
| 101 |
int option_index = 0; |
| 102 |
int c; |
| 103 |
static struct option long_options[] = { |
| 104 |
{"local", 1, 0, 1}, |
| 105 |
{"local-port", 1, 0, 2}, |
| 106 |
{"remote", 1, 0, 3}, |
| 107 |
{"remote-port", 1, 0, 4}, |
| 108 |
{"file", 1, 0, 5}, |
| 109 |
{"delay", 1, 0, 6}, |
| 110 |
{"buffer", 1, 0, 7}, |
| 111 |
{"listen", 0, 0, 10}, |
| 112 |
{"send", 0, 0, 11}, |
| 113 |
{"quick", 0, 0, 12}, |
| 114 |
{"verbose", 0, 0, 13}, |
| 115 |
{"help", 0, 0, 99}, |
| 116 |
{0, 0, 0, 0} |
| 117 |
}; |
| 118 |
|
| 119 |
/* Parse the arguments. */ |
| 120 |
while (1) { |
| 121 |
c = getopt_long(argc, argv, "H:P:h:p:f:d:b:qlsv",long_options,&option_index); |
| 122 |
if (c == -1) break; |
| 123 |
|
| 124 |
switch (c) { |
| 125 |
case 0: |
| 126 |
printf ("option %s", long_options[option_index].name); |
| 127 |
if (optarg) printf (" with arg %s", optarg); |
| 128 |
printf ("\n"); |
| 129 |
break; |
| 130 |
case 1: /* local host */ |
| 131 |
case 'H': |
| 132 |
local_host[num_local_host++] = optarg; |
| 133 |
break; |
| 134 |
case 2: /* local port */ |
| 135 |
case 'P': |
| 136 |
local_port = optarg; |
| 137 |
break; |
| 138 |
case 3: /* remote host */ |
| 139 |
case 'h': |
| 140 |
remote_host = optarg; |
| 141 |
break; |
| 142 |
case 4: /* remote port */ |
| 143 |
case 'p': |
| 144 |
remote_port = optarg; |
| 145 |
break; |
| 146 |
case 5: |
| 147 |
case 'f': |
| 148 |
filename = optarg; |
| 149 |
break; |
| 150 |
|
| 151 |
case 6: |
| 152 |
case 'd': |
| 153 |
delay = strtoul(optarg,NULL,10); |
| 154 |
printf("delay is %ld usec\n",delay); |
| 155 |
break; |
| 156 |
|
| 157 |
case 7: |
| 158 |
case 'b': |
| 159 |
buffer_size = atoi(optarg); |
| 160 |
if ( buffer_size > BUFSIZE ) { |
| 161 |
buffer_size = BUFSIZE; |
| 162 |
fprintf(stderr,"Warning, buffer size too large, set to %d\n",buffer_size); |
| 163 |
} |
| 164 |
break; |
| 165 |
|
| 166 |
case 12: |
| 167 |
case 'q': interactive = 0; break; |
| 168 |
|
| 169 |
case 13: |
| 170 |
case 'v': verbose = 1; break; |
| 171 |
/* COMMANDS */ |
| 172 |
case 10: /* listen */ |
| 173 |
case 'l': |
| 174 |
if (command) { |
| 175 |
fprintf(stderr, "%s: pick ONE of listen or send\n", argv[0]); |
| 176 |
return 1; |
| 177 |
} |
| 178 |
else command = COMMAND_RECV; |
| 179 |
break; |
| 180 |
|
| 181 |
case 11: /* send */ |
| 182 |
case 's': |
| 183 |
if (command) { |
| 184 |
fprintf(stderr, "%s: pick ONE of listen or send\n", argv[0]); |
| 185 |
return 2; |
| 186 |
} else command = COMMAND_SEND; |
| 187 |
break; |
| 188 |
|
| 189 |
case '?': |
| 190 |
case 99: |
| 191 |
usage(argv[0]); |
| 192 |
return 3; |
| 193 |
break; |
| 194 |
|
| 195 |
default: |
| 196 |
printf ("%s: unrecognized option 0%c\n", argv[0], c); |
| 197 |
usage(argv[0]); |
| 198 |
return 4; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (optind < argc) { |
| 203 |
fprintf(stderr, "%s: non-option arguments are illegal: ", argv[0]); |
| 204 |
while (optind < argc) fprintf(stderr, "%s ", argv[optind++]); |
| 205 |
fprintf(stderr, "\n"); |
| 206 |
usage(argv[0]); |
| 207 |
return 5; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
if (0 == num_local_host) { |
| 212 |
fprintf(stderr, "%s: You MUST provide a local host.\n", argv[0]); |
| 213 |
usage(argv[0]); |
| 214 |
return 6; |
| 215 |
} |
| 216 |
|
| 217 |
if ( filename == NULL && command == COMMAND_SEND) |
| 218 |
fprintf(stderr,"%s: Use standard input to send\n",argv[0]); |
| 219 |
|
| 220 |
if ( filename == NULL && command == COMMAND_RECV ) |
| 221 |
fprintf(stderr,"%s: Use standard output to write\n",argv[0]); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} /* parse_arguments() */ |
| 225 |
|
| 226 |
static void |
| 227 |
emsg(char *prog,char *s) |
| 228 |
{ |
| 229 |
if ( prog != NULL ) fprintf(stderr,"%s: ",prog); |
| 230 |
perror(s); |
| 231 |
fflush(stdout); |
| 232 |
//DUMP_CORE; |
| 233 |
|
| 234 |
exit(-1); |
| 235 |
} |
| 236 |
|
| 237 |
static int build_endpoint(char *argv0) |
| 238 |
{ |
| 239 |
struct addrinfo hints, *rp; |
| 240 |
int retval,i; |
| 241 |
|
| 242 |
/* Create the local endpoint. */ |
| 243 |
if ( (retval = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0 ) { |
| 244 |
emsg(argv0,"socket"); |
| 245 |
exit(retval); |
| 246 |
} |
| 247 |
|
| 248 |
for ( i = 0;i < num_local_host;i++ ) { |
| 249 |
/* Get the transport address for the local host name. */ |
| 250 |
fprintf(stderr,"Hostname %d is %s\n",i+1,local_host[i]); |
| 251 |
|
| 252 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 253 |
hints.ai_family = AF_INET; |
| 254 |
hints.ai_protocol = IPPROTO_SCTP; |
| 255 |
if (getaddrinfo(local_host[i], local_port, &hints, &rp) != 0) { |
| 256 |
fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host[i]); |
| 257 |
exit(1); |
| 258 |
} |
| 259 |
|
| 260 |
/* Bind this socket to the test port. */ |
| 261 |
if (bind(retval, rp->ai_addr, rp->ai_addrlen)) { |
| 262 |
emsg(argv0,"bind"); |
| 263 |
exit(-1); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
fprintf(stderr,"Endpoint built.\n"); |
| 268 |
|
| 269 |
return retval; |
| 270 |
} /* build_endpoint() */ |
| 271 |
|
| 272 |
/* Convenience structure to determine space needed for cmsg. */ |
| 273 |
typedef union { |
| 274 |
struct sctp_initmsg init; |
| 275 |
struct sctp_sndrcvinfo sndrcvinfo; |
| 276 |
} _sctp_cmsg_data_t; |
| 277 |
|
| 278 |
|
| 279 |
/* Listen on the socket, printing out anything that arrives. */ |
| 280 |
static void |
| 281 |
command_recv(char *argv0, int sk) |
| 282 |
{ |
| 283 |
struct msghdr inmessage; |
| 284 |
char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
| 285 |
struct iovec iov; |
| 286 |
int ret; |
| 287 |
int fd; |
| 288 |
int ct; |
| 289 |
|
| 290 |
if (listen(sk, 1) == -1) |
| 291 |
emsg(argv0, "listen"); |
| 292 |
/* Initialize inmessage with enough space for DATA... */ |
| 293 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 294 |
iov.iov_base = buffer; |
| 295 |
iov.iov_len = buffer_size; |
| 296 |
inmessage.msg_iov = &iov; |
| 297 |
inmessage.msg_iovlen = 1; |
| 298 |
/* or a control message. */ |
| 299 |
inmessage.msg_control = incmsg; |
| 300 |
inmessage.msg_controllen = sizeof(incmsg); |
| 301 |
|
| 302 |
/* creat a file */ |
| 303 |
if ( filename == NULL ) fd = 1; |
| 304 |
else if ( (fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE)) == -1 ) |
| 305 |
emsg(argv0,"open"); |
| 306 |
|
| 307 |
fprintf(stderr,"%s Receiving...\n", argv0); |
| 308 |
/* Get the messages sent */ |
| 309 |
ct = 0; |
| 310 |
while ( (ret = recvmsg(sk, &inmessage, MSG_WAITALL)) >= 0 ) { |
| 311 |
if ( verbose ) |
| 312 |
fprintf(stderr,"%s-%d received %d bytes\n", |
| 313 |
argv0, ++ct, ret); |
| 314 |
if ( !(inmessage.msg_flags & MSG_NOTIFICATION) ) { |
| 315 |
//printf("%s write %d bytes\n",argv0,ret); |
| 316 |
if ( write(fd,buffer,ret) != ret ) emsg(argv0,"write"); |
| 317 |
} else { |
| 318 |
union sctp_notification *sn; |
| 319 |
sn = (union sctp_notification *)iov.iov_base; |
| 320 |
if ((sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) && |
| 321 |
(sn->sn_assoc_change.sac_state |
| 322 |
== SCTP_SHUTDOWN_COMP)) |
| 323 |
break; |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
if ( ret < 0 ) emsg(argv0,"recvmsg"); |
| 329 |
|
| 330 |
close(fd); |
| 331 |
close(sk); |
| 332 |
} /* command_recv() */ |
| 333 |
|
| 334 |
/* Read lines from stdin and send them to the socket. */ |
| 335 |
static void |
| 336 |
command_send(char *argv0, int sk) |
| 337 |
{ |
| 338 |
struct msghdr outmsg; |
| 339 |
struct iovec iov; |
| 340 |
struct addrinfo hints, *rp; |
| 341 |
int fd; |
| 342 |
int msglen; |
| 343 |
int ct; |
| 344 |
|
| 345 |
/* Set up the destination. */ |
| 346 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 347 |
hints.ai_family = AF_INET; |
| 348 |
hints.ai_protocol = IPPROTO_SCTP; |
| 349 |
if (getaddrinfo(remote_host, remote_port, &hints, &rp) != 0) { |
| 350 |
fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host); |
| 351 |
exit(1); |
| 352 |
} |
| 353 |
|
| 354 |
/* Initialize the message struct we use to pass messages to |
| 355 |
* the remote socket. |
| 356 |
*/ |
| 357 |
iov.iov_base = buffer; |
| 358 |
iov.iov_len = buffer_size; |
| 359 |
outmsg.msg_iov = &iov; |
| 360 |
outmsg.msg_iovlen = 1; |
| 361 |
outmsg.msg_control = NULL; |
| 362 |
outmsg.msg_controllen = 0; |
| 363 |
outmsg.msg_name = rp->ai_addr; |
| 364 |
outmsg.msg_namelen = rp->ai_addrlen; |
| 365 |
|
| 366 |
/* open the file */ |
| 367 |
if ( filename == NULL ) fd = 0; |
| 368 |
else if ( (fd = open(filename,O_RDONLY)) == -1 ) emsg(argv0,"open"); |
| 369 |
|
| 370 |
fprintf(stderr,"%s ready to send...\n", argv0); |
| 371 |
ct = 0; |
| 372 |
while ( (msglen = read(fd,buffer,buffer_size)) > 0 ) { |
| 373 |
/* Send to our neighbor. */ |
| 374 |
iov.iov_len = msglen; |
| 375 |
if ( sendmsg(sk, &outmsg, 0) != msglen ) emsg(argv0,"sendmsg"); |
| 376 |
if ( verbose ) fprintf(stderr,"%s-%d send %d bytes\n",argv0,++ct,msglen); |
| 377 |
if ( interactive && fd != 1 ) |
| 378 |
getchar(); |
| 379 |
// no flow control? no problem, make it slow |
| 380 |
else if ( delay > 0 ) |
| 381 |
usleep(delay); |
| 382 |
} |
| 383 |
|
| 384 |
close(fd); |
| 385 |
close(sk); |
| 386 |
} /* command_send() */ |
| 387 |
|
| 388 |
int main(int argc, char *argv[]) |
| 389 |
{ |
| 390 |
int ret; |
| 391 |
|
| 392 |
if (( ret = parse_arguments(argc, argv) )) return ret; |
| 393 |
|
| 394 |
switch(command) { |
| 395 |
case COMMAND_NONE: |
| 396 |
fprintf(stderr, "%s: Please specify a command.\n", argv[0]); |
| 397 |
break; |
| 398 |
case COMMAND_RECV: |
| 399 |
command_recv(argv[0],build_endpoint(argv[0])); |
| 400 |
break; |
| 401 |
case COMMAND_SEND: |
| 402 |
command_send(argv[0],build_endpoint(argv[0])); |
| 403 |
break; |
| 404 |
default: |
| 405 |
fprintf(stderr, "%s: illegal command %d\n", argv[0], command); |
| 406 |
} /* switch(command) */ |
| 407 |
|
| 408 |
return 0; |
| 409 |
} |