5ac83f95cec1763d27490512c4cfa98d1bf594a9
[ccan] / ccan / net / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * net - simple IPv4/IPv6 socket library
7  *
8  * This code makes it simple to support IPv4 and IPv6 without speed penalty
9  * in clients by using non-blocking simultaneous connect, and using
10  * a convenience function to create both IPv4 and IPv6 sockets for servers.
11  *
12  * License: MIT
13  *
14  * Author: Rusty Russell <rusty@rustcorp.com.au>
15  *
16  * Example:
17  *      #include <ccan/net/net.h>
18  *      #include <sys/types.h>
19  *      #include <sys/socket.h>
20  *      #include <netinet/in.h>
21  *      #include <stdio.h>
22  *      #include <err.h>
23  *
24  *      int main(int argc, char *argv[])
25  *      {
26  *              struct addrinfo *addr;
27  *              const char *dest, *port;
28  *              int fd;
29  *              union {
30  *                      struct sockaddr s;
31  *                      struct sockaddr_in v4;
32  *                      struct sockaddr_in6 v6;
33  *              } u;
34  *              socklen_t slen = sizeof(u);
35  *      
36  *              if (argc == 2) {
37  *                      dest = argv[1];
38  *                      port = "http";
39  *              } else if (argc == 3) {
40  *                      dest = argv[1];
41  *                      port = argv[2];
42  *              } else
43  *                      errx(1, "Usage: test-net <target> [<port>]");
44  *      
45  *              addr = net_client_lookup(dest, port, AF_UNSPEC, SOCK_STREAM);
46  *              if (!addr)
47  *                      err(1, "Failed to look up %s", dest);
48  *      
49  *              fd = net_connect(addr);
50  *              if (fd < 0)
51  *                      err(1, "Failed to connect to %s", dest);
52  *      
53  *              if (getsockname(fd, &u.s, &slen) == 0)
54  *                      printf("Connected via %s\n",
55  *                             u.s.sa_family == AF_INET6 ? "IPv6"
56  *                             : u.s.sa_family == AF_INET ? "IPv4"
57  *                             : "UNKNOWN??");
58  *              else
59  *                      err(1, "Failed to get socket type for connection");
60  *              return 0;
61  *      }
62  */
63 int main(int argc, char *argv[])
64 {
65         /* Expect exactly one argument */
66         if (argc != 2)
67                 return 1;
68
69         if (strcmp(argv[1], "depends") == 0) {
70                 printf("ccan/noerr\n");
71                 return 0;
72         }
73
74         return 1;
75 }