1 /* myftp - simple file transfer over sctp testing tool.
2 * Copyright (c) 2002 Intel Corp.
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.
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)
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.
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.
25 * Please send any bug reports or fixes you make to the
27 * lksctp developers <sctp-developers-list@cig.mot.com>
29 * Or submit a bug report through the following website:
30 * http://www.sf.net/projects/lksctp
32 * Written or modified by:
33 * Xingang Guo <xingang.guo@intel.com>
34 * Jon Grimm <jgrimm@us.ibm.com>
36 * Any bugs reported given to us we will try to fix... any fixes shared will
37 * be incorporated into the next SCTP release.
49 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h> /* for sockaddr_in */
54 #include <sys/errno.h>
56 #include <netinet/sctp.h>
59 static char buffer[BUFSIZE];
60 #define DUMP_CORE { char *diediedie = 0; *diediedie = 0; }
62 typedef enum { COMMAND_NONE, COMMAND_RECV, COMMAND_SEND } command_t;
64 /* These are the global options. */
65 #define MAX_NUM_HOST 5
66 static char *local_host[MAX_NUM_HOST];
67 static int num_local_host = 0;
68 static int local_port = 4444;
70 static int buffer_size = BUFSIZE;
71 static char *remote_host = NULL;
72 static int remote_port = 4444;
73 static command_t command = COMMAND_NONE;
74 static char *filename = NULL;
75 static int interactive = 0;
76 static unsigned long delay = 0;
77 static int verbose = 0;
82 fprintf(stderr, "Usage: %s [options]\n",argv0);
83 fprintf(stderr, "Options:\n");
84 fprintf(stderr, "\t--local, -H <hostname> Specify local interface\n");
85 fprintf(stderr, "\t--local-port, -P <port> Specify local port (default 4444)\n");
86 fprintf(stderr, "\t--remote, -h <hostname> Specify interface on remote host\n");
87 fprintf(stderr, "\t--remote-port, -p <port> Specify remote port (default 4444)\n");
88 fprintf(stderr, "\t--listen, -l Work in receiving mode\n");
89 fprintf(stderr, "\t--send, -s Work in sending mode\n");
90 fprintf(stderr, "\t--file, -f <filename> File to read or write,\n");
91 fprintf(stderr, "\t--buffer, -b <size> Buffer size. (default 1024 bytes)\n");
92 fprintf(stderr, "\t by default use standard input/output.\n");
93 fprintf(stderr, "\t--quick, -q Send packets continueously,\n");
94 fprintf(stderr, "\t do not wait for <ENTER> key. Default wait.\n");
95 fprintf(stderr, "\t--delay, -d <usec> Delay between consecutive sends (see --quick)\n");
96 fprintf(stderr, "\t--verbose, -v In verbose mode, display the progress.\n");
97 fprintf(stderr, "\n\t--help, Print this message.\n\n");
100 static int parse_arguments(int argc, char *argv[])
102 int option_index = 0;
104 static struct option long_options[] = {
106 {"local-port", 1, 0, 2},
108 {"remote-port", 1, 0, 4},
112 {"listen", 0, 0, 10},
115 {"verbose", 0, 0, 13},
120 /* Parse the arguments. */
122 c = getopt_long(argc, argv, "H:P:h:p:f:d:b:qlsv",long_options,&option_index);
127 printf ("option %s", long_options[option_index].name);
128 if (optarg) printf (" with arg %s", optarg);
131 case 1: /* local host */
133 local_host[num_local_host++] = optarg;
135 case 2: /* local port */
137 local_port = atoi(optarg);
139 case 3: /* remote host */
141 remote_host = optarg;
143 case 4: /* remote port */
145 remote_port = atoi(optarg);
154 delay = strtoul(optarg,NULL,10);
155 printf("delay is %ld usec\n",delay);
160 buffer_size = atoi(optarg);
161 if ( buffer_size > BUFSIZE ) {
162 buffer_size = BUFSIZE;
163 fprintf(stderr,"Warning, buffer size too large, set to %d\n",buffer_size);
166 case 'q': interactive = 0; break;
169 case 'v': verbose = 1; break;
171 case 10: /* listen */
174 fprintf(stderr, "%s: pick ONE of listen or send\n", argv[0]);
177 else command = COMMAND_RECV;
183 fprintf(stderr, "%s: pick ONE of listen or send\n", argv[0]);
185 } else command = COMMAND_SEND;
195 printf ("%s: unrecognized option 0%c\n", argv[0], c);
202 fprintf(stderr, "%s: non-option arguments are illegal: ", argv[0]);
203 while (optind < argc) fprintf(stderr, "%s ", argv[optind++]);
204 fprintf(stderr, "\n");
210 if (0 == num_local_host) {
211 fprintf(stderr, "%s: You MUST provide a local host.\n", argv[0]);
216 if ( filename == NULL && command == COMMAND_SEND)
217 fprintf(stderr,"%s: Use standard input to send\n",argv[0]);
219 if ( filename == NULL && command == COMMAND_RECV )
220 fprintf(stderr,"%s: Use standard output to write\n",argv[0]);
223 } /* parse_arguments() */
226 emsg(char *prog,char *s)
228 if ( prog != NULL ) fprintf(stderr,"%s: ",prog);
236 static int build_endpoint(char *argv0)
240 /* Create the local endpoint. */
241 if ( (retval = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0 ) {
242 emsg(argv0,"socket");
246 for ( i = 0;i < num_local_host;i++ ) {
248 struct sockaddr_in laddr;
250 /* Get the transport address for the local host name. */
251 fprintf(stderr,"Hostname %d is %s\n",i+1,local_host[i]);
252 if ( (hst = gethostbyname(local_host[i])) == NULL ) {
253 fprintf(stderr, "%s: bad hostname: %s\n", argv0, local_host[i]);
256 memcpy(&laddr.sin_addr, hst->h_addr_list[0],sizeof(laddr.sin_addr));
257 laddr.sin_port = htons(local_port);
258 laddr.sin_family = AF_INET;
260 /* Bind this socket to the test port. */
261 if ( bind(retval, (struct sockaddr *)&laddr, sizeof(laddr)) ) {
267 fprintf(stderr,"Endpoint built.\n");
270 } /* build_endpoint() */
272 /* Convenience structure to determine space needed for cmsg. */
274 struct sctp_initmsg init;
275 struct sctp_sndrcvinfo sndrcvinfo;
279 /* Listen on the socket, printing out anything that arrives. */
281 command_recv(char *argv0, int sk)
283 struct msghdr inmessage;
284 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
291 /* Initialize inmessage with enough space for DATA... */
292 memset(&inmessage, 0, sizeof(inmessage));
293 iov.iov_base = buffer;
294 iov.iov_len = buffer_size;
295 inmessage.msg_iov = &iov;
296 inmessage.msg_iovlen = 1;
297 /* or a control message. */
298 inmessage.msg_control = incmsg;
299 inmessage.msg_controllen = sizeof(incmsg);
302 if ( filename == NULL ) fd = 1;
303 else if ( (fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE)) == -1 )
306 fprintf(stderr,"%s Receiving...\n", argv0);
307 /* Get the messages sent */
309 while ( (ret = recvmsg(sk, &inmessage, MSG_WAITALL)) >= 0 ) {
311 fprintf(stderr,"%s-%d received %d bytes\n",
313 if ( !(inmessage.msg_flags & MSG_NOTIFICATION) ) {
314 //printf("%s write %d bytes\n",argv0,ret);
315 if ( write(fd,buffer,ret) != ret ) emsg(argv0,"write");
317 union sctp_notification *sn;
318 sn = (union sctp_notification *)iov.iov_base;
319 if ((sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) &&
320 (sn->sn_assoc_change.sac_state
321 == SCTP_SHUTDOWN_COMP))
327 if ( ret < 0 ) emsg(argv0,"recvmsg");
331 } /* command_recv() */
333 /* Read lines from stdin and send them to the socket. */
335 command_send(char *argv0, int sk)
337 struct msghdr outmsg;
340 struct sockaddr_in remote_addr;
345 /* Set up the destination. */
346 hst = gethostbyname(remote_host);
347 if (hst == NULL || hst->h_length < 1) {
348 fprintf(stderr, "%s: bad hostname: %s\n", argv0, remote_host);
351 memcpy(&remote_addr.sin_addr, hst->h_addr_list[0], sizeof(remote_addr.sin_addr));
352 remote_addr.sin_port = htons(remote_port);
353 remote_addr.sin_family = AF_INET;
355 /* Initialize the message struct we use to pass messages to
358 iov.iov_base = buffer;
359 iov.iov_len = buffer_size;
360 outmsg.msg_iov = &iov;
361 outmsg.msg_iovlen = 1;
362 outmsg.msg_control = NULL;
363 outmsg.msg_controllen = 0;
364 outmsg.msg_name = &remote_addr;
365 outmsg.msg_namelen = sizeof(remote_addr);
368 if ( filename == NULL ) fd = 0;
369 else if ( (fd = open(filename,O_RDONLY)) == -1 ) emsg(argv0,"open");
371 fprintf(stderr,"%s ready to send...\n", argv0);
373 while ( (msglen = read(fd,buffer,buffer_size)) > 0 ) {
374 /* Send to our neighbor. */
375 iov.iov_len = msglen;
376 if ( sendmsg(sk, &outmsg, 0) != msglen ) emsg(argv0,"sendmsg");
377 if ( verbose ) fprintf(stderr,"%s-%d send %d bytes\n",argv0,++ct,msglen);
378 if ( interactive && fd != 1 )
380 // no flow control? no problem, make it slow
381 else if ( delay > 0 )
383 } while ( msglen > 0 );
387 } /* command_send() */
389 int main(int argc, char *argv[])
393 if (( ret = parse_arguments(argc, argv) )) return ret;
397 fprintf(stderr, "%s: Please specify a command.\n", argv[0]);
400 command_recv(argv[0],build_endpoint(argv[0]));
403 command_send(argv[0],build_endpoint(argv[0]));
406 fprintf(stderr, "%s: illegal command %d\n", argv[0], command);
407 } /* switch(command) */