Ticket #1824: ipv6probe.c

File ipv6probe.c, 1.8 KB (added by jkeenan, 11 years ago)

kurahaupo's patch converted to a file (no c++ style comments)

Line 
1/*
2Copyright (C) 2005-2009, Parrot Foundation.
3$Id: test.in 49532 2010-10-14 00:47:36Z jkeenan $
4
5*/
6
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <stdio.h>
10#include <errno.h>
11
12int main(int c,char**v) {
13    int x = socket( PF_INET6, SOCK_DGRAM, 0 );
14    if ( x < 0 ) {
15        perror("Not OK - socket failed");
16        return 2;
17    }
18
19    /*
20    By now we have (a) PF_INET6 defined, and (b) in-principle kernel
21    support for v6; however we don't yet know if we have any v6
22    interfaces.
23    */
24
25    struct in6_addr A = IN6ADDR_LOOPBACK_INIT;  /* IN6ADDR_ANY_INIT; */
26    struct sockaddr_in6 S = { AF_INET6 };  /* family is always first field in sockaddr_* */
27    S.sin6_addr = A;
28    S.sin6_port = 32760;  /* a pseudorandom 15-bit number */
29
30    /*
31    By now we have struct in6_addr, struct sockaddr_in6 and AF_INET6 and
32    IN6ADDR_LOOPBACK_INIT. But we still don't know about interfaces.
33    */
34
35    #ifdef linux  /* might not be supported elsewhere */
36    S.sin6_flowinfo = 0;
37    S.sin6_scope_id = 0;
38    #endif
39
40    /*
41    Set the SO_REUSEADDR option so as not to interfere with anyone else
42    using the port, including running this test again within the
43    re-use timeout period.
44    */
45    int reuse_address = 1;
46    if ( setsockopt(x, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0 ) {
47        perror("Not OK - setsockopt failed");
48        return 1;
49    }
50
51    if ( bind( x, (void*) &S, sizeof(S) ) < 0 && errno != EADDRINUSE ) {
52        perror("Not OK - bind failed");
53        return 1;
54    }
55
56    /*
57    By now we know we can bind to "::1", which means we truly do have
58    IPv6 support. Of course, we might not have any useful routes off
59    this host, but that wasn't the question.
60    */
61    puts("OK");
62    close(x);
63    return 0;
64}
65
66/*
67 * Local variables:
68 *   c-file-style: "parrot"
69 * End:
70 * vim: expandtab shiftwidth=4:
71 */