1
/*****************************************************************************/
2
/* "NetPIPE" -- Network Protocol Independent Performance Evaluator. */
3
/* Copyright 1997, 1998 Iowa State University Research Foundation, Inc. */
5
/* This program is free software; you can redistribute it and/or modify */
6
/* it under the terms of the GNU General Public License as published by */
7
/* the Free Software Foundation. You should have received a copy of the */
8
/* GNU General Public License along with this program; if not, write to the */
9
/* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
11
/* * sctp6.c ---- SCTP over IPv6 calls source */
12
/*****************************************************************************/
22
void Init(ArgStruct *p, int* pargc, char*** pargv)
24
p->reset_conn = 0; /* Default to not resetting connection */
25
p->prot.sndbufsz = p->prot.rcvbufsz = 0;
26
p->tr = 0; /* The transmitter will be set using the -h host flag. */
30
void Setup(ArgStruct *p)
35
struct sockaddr_in6 *lsin1, *lsin2; /* ptr to sockaddr_in6 in ArgStruct */
38
struct protoent *proto;
39
int send_size, recv_size, sizeofint = sizeof(int);
42
host = p->host; /* copy ptr to hostname */
44
lsin1 = &(p->prot.sin1);
45
lsin2 = &(p->prot.sin2);
47
bzero((char *) lsin1, sizeof(*lsin1));
48
bzero((char *) lsin2, sizeof(*lsin2));
50
if ( (sockfd = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP)) < 0){
51
printf("NetPIPE: can't open stream socket! errno=%d\n", errno);
55
if(!(proto = getprotobyname("sctp"))){
56
printf("NetPIPE: protocol 'sctp' unknown!\n");
60
/* Attempt to set SCTP_NODELAY */
62
if(setsockopt(sockfd, proto->p_proto, SCTP_NODELAY, &one, sizeof(one)) < 0)
64
printf("NetPIPE: setsockopt: SCTP_NODELAY failed! errno=%d\n", errno);
68
/* If requested, set the send and receive buffer sizes */
70
if(p->prot.sndbufsz > 0)
72
if(setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &(p->prot.sndbufsz),
73
sizeof(p->prot.sndbufsz)) < 0)
75
printf("NetPIPE: setsockopt: SO_SNDBUF failed! errno=%d\n", errno);
76
printf("You may have asked for a buffer larger than the system");
77
printf("can handle\n");
81
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &(p->prot.rcvbufsz),
82
sizeof(p->prot.rcvbufsz)) < 0)
84
printf("NetPIPE: setsockopt: SO_RCVBUF failed! errno=%d\n", errno);
85
printf("You may have asked for a buffer larger than the system");
86
printf("can handle\n");
90
getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
91
(char *) &send_size, (void *) &sizeofint);
92
getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
93
(char *) &recv_size, (void *) &sizeofint);
96
fprintf(stderr,"Send and receive buffers are %d and %d bytes\n",
97
send_size, recv_size);
99
"(A bug in Linux doubles the requested buffer sizes)\n");
102
if( p->tr ) { /* Primary transmitter */
104
lsin1->sin6_family = AF_INET6;
106
/* First attempt to convert the string to an IPv6 */
108
/* If the user supplied a real host name this will fail and */
109
/* we'll then do a name lookup. */
111
if (inet_pton(AF_INET6, host, &lsin1->sin6_addr) == 0)
113
if ((hp = gethostbyname2(host, AF_INET6)) == NULL)
115
printf("NetPIPE: invalid hostname '%s'\n", host);
119
if (hp->h_addrtype != AF_INET6)
121
printf("NetPIPE: invalid hostname '%s'\n", host);
124
bcopy(hp->h_addr, (char*) &(lsin1->sin6_addr),
128
lsin1->sin6_port = htons(p->port);
132
} else if( p->rcv ) { /* we are the receiver */
134
bzero((char *) lsin1, sizeof(*lsin1));
135
lsin1->sin6_family = AF_INET6;
136
lsin1->sin6_len = sizeof(*lsin1);
137
lsin1->sin6_port = htons(p->port);
138
/* Setting this to all 0 is the "ANY" address. */
139
bzero(&lsin1->sin6_addr, sizeof(lsin1->sin6_addr));
141
if (bind(sockfd, (struct sockaddr *) lsin1, sizeof(*lsin1)) < 0){
142
printf("NetPIPE: server: bind on local address failed! errno=%d",
147
p->servicefd = sockfd;
149
p->upper = send_size + recv_size;
151
establish(p); /* Establish connections */
156
readFully(int fd, void *obuf, int len)
159
char *buf = (char *) obuf;
162
while (bytesLeft > 0 &&
163
(bytesRead = read(fd, (void *) buf, bytesLeft)) > 0)
165
bytesLeft -= bytesRead;
168
if (bytesRead <= 0) return bytesRead;
172
void Sync(ArgStruct *p)
174
char s[] = "SyncMe", response[] = " ";
176
if (write(p->commfd, s, strlen(s)) < 0 || /* Write to nbor */
177
readFully(p->commfd, response, strlen(s)) < 0) /* Read from nbor */
179
perror("NetPIPE: error writing or reading synchronization string");
182
if (strncmp(s, response, strlen(s)))
184
fprintf(stderr, "NetPIPE: Synchronization string incorrect! |%s|\n",
190
void PrepareToReceive(ArgStruct *p)
193
The Berkeley sockets interface doesn't have a method to pre-post
194
a buffer for reception of data.
198
void SendData(ArgStruct *p)
200
int bytesWritten, bytesLeft;
203
bytesLeft = p->bufflen;
206
while (bytesLeft > 0 &&
207
(bytesWritten = write(p->commfd, q, bytesLeft)) > 0)
209
bytesLeft -= bytesWritten;
212
if (bytesWritten == -1)
214
printf("NetPIPE: write: error encountered, errno=%d\n", errno);
219
void RecvData(ArgStruct *p)
225
bytesLeft = p->bufflen;
228
while (bytesLeft > 0 &&
229
(bytesRead = read(p->commfd, q, bytesLeft)) > 0)
231
bytesLeft -= bytesRead;
234
if (bytesLeft > 0 && bytesRead == 0)
236
printf("NetPIPE: \"end of file\" encountered reading from socket\n");
238
else if (bytesRead == -1)
240
printf("NetPIPE: read: error encountered, errno=%d\n", errno);
245
/* uint32_t is used to insure that the integer size is the same even in tests
246
* between 64-bit and 32-bit architectures. */
248
void SendTime(ArgStruct *p, double *t)
250
uint32_t ltime, ntime;
253
Multiply the number of seconds by 1e8 to get time in 0.01 microseconds
254
and convert value to an unsigned 32-bit integer.
256
ltime = (uint32_t)(*t * 1.e8);
258
/* Send time in network order */
259
ntime = htonl(ltime);
260
if (write(p->commfd, (char *)&ntime, sizeof(uint32_t)) < 0)
262
printf("NetPIPE: write failed in SendTime: errno=%d\n", errno);
267
void RecvTime(ArgStruct *p, double *t)
269
uint32_t ltime, ntime;
272
bytesRead = readFully(p->commfd, (void *)&ntime, sizeof(uint32_t));
275
printf("NetPIPE: read failed in RecvTime: errno=%d\n", errno);
278
else if (bytesRead != sizeof(uint32_t))
280
fprintf(stderr, "NetPIPE: partial read in RecvTime of %d bytes\n",
284
ltime = ntohl(ntime);
286
/* Result is ltime (in microseconds) divided by 1.0e8 to get seconds */
288
*t = (double)ltime / 1.0e8;
291
void SendRepeat(ArgStruct *p, int rpt)
296
/* Send repeat count as a long in network order */
298
if (write(p->commfd, (void *) &nrpt, sizeof(uint32_t)) < 0)
300
printf("NetPIPE: write failed in SendRepeat: errno=%d\n", errno);
305
void RecvRepeat(ArgStruct *p, int *rpt)
310
bytesRead = readFully(p->commfd, (void *)&nrpt, sizeof(uint32_t));
313
printf("NetPIPE: read failed in RecvRepeat: errno=%d\n", errno);
316
else if (bytesRead != sizeof(uint32_t))
318
fprintf(stderr, "NetPIPE: partial read in RecvRepeat of %d bytes\n",
327
void establish(ArgStruct *p)
331
struct protoent *proto;
333
clen = (socklen_t) sizeof(p->prot.sin2);
337
while( connect(p->commfd, (struct sockaddr *) &(p->prot.sin1),
338
sizeof(p->prot.sin1)) < 0 ) {
340
/* If we are doing a reset and we get a connection refused from
341
* the connect() call, assume that the other node has not yet
342
* gotten to its corresponding accept() call and keep trying until
345
if(!doing_reset || errno != ECONNREFUSED) {
346
printf("Client: Cannot Connect! errno=%d\n",errno);
352
} else if( p->rcv ) {
355
listen(p->servicefd, 5);
356
p->commfd = accept(p->servicefd, (struct sockaddr *) &(p->prot.sin2),
360
printf("Server: Accept Failed! errno=%d\n",errno);
365
* Attempt to set SCTP_NODELAY. SCTP_NODELAY may or may not be
366
* propagated to accepted sockets.
368
if(!(proto = getprotobyname("sctp"))){
369
printf("unknown protocol!\n");
373
if(setsockopt(p->commfd, proto->p_proto, SCTP_NODELAY,
374
&one, sizeof(one)) < 0)
376
printf("setsockopt: SCTP_NODELAY failed! errno=%d\n", errno);
380
/* If requested, set the send and receive buffer sizes */
381
if(p->prot.sndbufsz > 0)
383
if(setsockopt(p->commfd, SOL_SOCKET, SO_SNDBUF,
385
sizeof(p->prot.sndbufsz)) < 0)
387
printf("setsockopt: SO_SNDBUF failed! errno=%d\n", errno);
390
if(setsockopt(p->commfd, SOL_SOCKET, SO_RCVBUF,
392
sizeof(p->prot.rcvbufsz)) < 0)
394
printf("setsockopt: SO_RCVBUF failed! errno=%d\n", errno);
401
void CleanUp(ArgStruct *p)
407
write(p->commfd,quit, 5);
408
read(p->commfd, quit, 5);
411
} else if( p->rcv ) {
413
read(p->commfd,quit, 5);
414
write(p->commfd,quit,5);
422
void Reset(ArgStruct *p)
431
/* Close the sockets */
435
/* Now open and connect new sockets */
443
void AfterAlignmentInit(ArgStruct *p)