From 084042b1a83874734960f358cd2c1d428b79bbe1 Mon Sep 17 00:00:00 2001 From: Nikhil Marathe Date: Tue, 18 Sep 2012 19:23:22 +0530 Subject: [PATCH] Added network interfaces section --- code/Makefile | 3 ++- code/interfaces/main.c | 33 +++++++++++++++++++++++++++++++++ source/networking.rst | 14 +++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 code/interfaces/main.c diff --git a/code/Makefile b/code/Makefile index 787ef25..9dcb868 100644 --- a/code/Makefile +++ b/code/Makefile @@ -19,7 +19,8 @@ examples=\ pipe-echo-server\ multi-echo-server\ tty\ - tty-gravity + tty-gravity\ + interfaces UV_PATH=$(shell pwd)/../libuv CFLAGS=-g -Wall -I$(UV_PATH)/include $(UV_PATH)/uv.a diff --git a/code/interfaces/main.c b/code/interfaces/main.c new file mode 100644 index 0000000..cac12c2 --- /dev/null +++ b/code/interfaces/main.c @@ -0,0 +1,33 @@ +#include +#include + +int main() { + char buf[512]; + uv_interface_address_t *info; + int count, i; + + uv_interface_addresses(&info, &count); + i = count; + + printf("Number of interfaces: %d\n", count); + while (i--) { + uv_interface_address_t interface = info[i]; + + printf("Name: %s\n", interface.name); + printf("Internal? %s\n", interface.is_internal ? "Yes" : "No"); + + if (interface.address.address4.sin_family == AF_INET) { + uv_ip4_name(&interface.address.address4, buf, sizeof(buf)); + printf("IPv4 address: %s\n", buf); + } + else if (interface.address.address4.sin_family == AF_INET6) { + uv_ip6_name(&interface.address.address6, buf, sizeof(buf)); + printf("IPv6 address: %s\n", buf); + } + + printf("\n"); + } + + uv_free_interface_addresses(info, count); + return 0; +} diff --git a/source/networking.rst b/source/networking.rst index da29a42..85c36c5 100644 --- a/source/networking.rst +++ b/source/networking.rst @@ -193,7 +193,19 @@ call ``uv_freeaddrinfo`` in the callback. Network interfaces ------------------ -TODO +Information about the system's network interfaces can be obtained through libuv +using ``uv_interface_addresses``. This simple program just prints out all the +interface details so you get an idea of the fields that are available. This is +useful to allow your service to bind to IP addresses when it starts. + +.. rubric:: interfaces/main.c +.. literalinclude:: ../code/interfaces/main.c + :linenos: + :emphasize-lines: 9,17 + +``is_internal`` is true for loopback interfaces. Note that if a physical +interface has multiple IPv4/IPv6 addresses, the name will be reported multiple +times, with each address being reported once. .. _c-ares: http://c-ares.haxx.se .. _getaddrinfo: http://www.kernel.org/doc/man-pages/online/pages/man3/getaddrinfo.3.html