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