summaryrefslogtreecommitdiffstats
path: root/test/test-writev.c
blob: 4740738f451fcf9249a7605569c85061074177a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
    test-writev: use this to figure out if your writev() does intelligent
    things on the network.  Some writev()s when given multiple buffers
    will break them up into multiple packets, which is a waste.

    Linux prior to 2.0.31 has this problem.

    Solaris 2.5, 2.5.1 doesn't appear to, 2.6 hasn't been tested.

    IRIX 5.3 doesn't have this problem.

    To use this you want to snoop the wire with tcpdump, and then run
    "test-writev a.b.c.d port#" ... against some TCP service on another
    box.  For example you can run it against port 80 on another server.
    You want to look to see how many data packets are sent, you're hoping
    only one of size 300 is sent.
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <errno.h>

#ifndef INADDR_NONE
#define INADDR_NONE (-1ul)
#endif

void main( int argc, char **argv )
{
    struct sockaddr_in server_addr;
    int s;
    struct iovec vector[3];
    char buf[100];
    int i;
    const int just_say_no = 1;

    if( argc != 3 ) {
usage:
        fprintf( stderr, "usage: test-writev a.b.c.d port#\n" );
        exit( 1 );
    }
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr( argv[1] );
    if( server_addr.sin_addr.s_addr == INADDR_NONE ) {
        fprintf( stderr, "bogus address\n" );
        goto usage;
    }
    server_addr.sin_port = htons( atoi( argv[2] ) );

    s = socket( AF_INET, SOCK_STREAM, 0 );
    if( s < 0 ) {
        perror("socket");
        exit(1);
    }
    if( connect( s, (struct sockaddr *)&server_addr, sizeof( server_addr ) )
        != 0 ) {
        perror("connect");
        exit(1);
    }

    if( setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&just_say_no,
        sizeof(just_say_no)) != 0 ) {
        perror( "TCP_NODELAY" );
        exit(1);
    }
    /* now build up a two part writev and write it out */
    for( i = 0; i < sizeof( buf ); ++i ) {
        buf[i] = 'x';
    }
    vector[0].iov_base = buf;
    vector[0].iov_len = sizeof(buf);
    vector[1].iov_base = buf;
    vector[1].iov_len = sizeof(buf);
    vector[2].iov_base = buf;
    vector[2].iov_len = sizeof(buf);

    i = writev( s, &vector[0], 3 );
    fprintf( stdout, "i=%d, errno=%d\n", i, errno );
    exit(0);
}