Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Added network interfaces section
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilm committed Sep 18, 2012
1 parent 9f63904 commit 084042b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions code/interfaces/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <uv.h>

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;
}
14 changes: 13 additions & 1 deletion source/networking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 084042b

Please sign in to comment.