Page MenuHomeFreeBSD

libifconfig: First baby steps towards libification of ifconfig

Authored by on Aug 16 2016, 12:54 PM.



Libifconfig will implement most things ifconfig does today, and it is my intention to reduce complexity of future ifconfig maintenance and development by refactoring ifconfig to use it once the library stabilizes more. (Some work is done on this already)

This is an initial patch to get feedback on the first baby steps, and maybe get it into the tree, which would hopefully make it easier to experiment with and increase amount of feedback.

The API is not currently stable. It may change entirely at any time, but it's safe to experiment with.
See for basic usage examples.

The manpage is nonexistent at the moment, but between the examples and documentation in the header files you should find your way around.

Diff Detail

rS FreeBSD src repository - subversion
Lint Skipped
Unit Tests Skipped

Event Timeline retitled this revision from to libifconfig: First baby steps towards libification of ifconfig. updated this object. edited the test plan for this revision. (Show Details) set the repository for this revision to rS FreeBSD src repository - subversion.

Removed things that are not related to FreeBSD base.

A few points about how we put code into FreeBSD.

1 ↗(On Diff #19341)

I don't think this file is supposed to be here?

1 ↗(On Diff #19341)

We don't normally have a LICENSE file. Just put the appropriate license into the C and include files.

1 ↗(On Diff #19341)

Please move the examples into the examples directory in share.

Very quick first pass remarks.

The source and headers should probably be moved directly into lib/libifconfig.

Bikeshed: libifconfig or libifc?


This (and SRCS/INCS/INCSDIR) should be indented with tabs.


This should be indented with a tab.

Typo fixes in comments.

51 ↗(On Diff #19347)

superfluous space after "do".

77 ↗(On Diff #19347)


51 ↗(On Diff #19347)

Superfluous space after "do".

48 ↗(On Diff #19347)

Superfluous space after "do".

52 ↗(On Diff #19347)

Superfluous space after "do".

135 ↗(On Diff #19347)

This will likely be easier to read if the conditions are inverted, i.e. check for error, not success.

Something like:

if ((desc = realloc(desc, descrlen)) == NULL) {
    h->error.errtype = OTHER;
    h->error.errcode = ENOMEM;
    return (-1);

ifr.ifr_buffer.buffer = descr;
ifr.ifr_buffer.length = descrlen;

if (libifc_ioctlwrap(h, AF_LOCAL, SIOCGIFHESCR, &ifr) != 0)
    return (-1);

if (ifr.ifr_buffer.length > descrlen) {
    descrlen = ifr.ifr_buffer.length;

196 ↗(On Diff #19347)

The maximum length is configured through the net.ifdescr_maxlen sysctl.
If the name is longer the ioctl() will return ENAMETOOLONG.

I don't think you need to check for this before the ioctl() call. Just handle the error.

206 ↗(On Diff #19347)

It's safe to call free(NULL). You don't need this check.
Also, I don't think it can be NULL at this point. You can trust the kernel to not remove the buffer for you.

244 ↗(On Diff #19347)

The kernel doesn't truncate, and if the name is longer than it likes you'll get an error.

87 ↗(On Diff #19347)

The maximum number of address families (looking at /usr/include/sys/socket.h) seems to be low enough that this shouldn't be required. AF_MAX is 42 (a pleasing number).

Just allocating a fixed array of AF_MAX will waste very little memory, simplify things, and get rid of the lookup loop in libifc_socket(). edited edge metadata. marked 9 inline comments as done.

Addressed feedback

135 ↗(On Diff #19347)

Chop all the x-mas trees. :)

87 ↗(On Diff #19347)

I agree that allocating a static amount of memory (sizeof(...)*AF_MAX) should incur an insignificant amount of wasted memory, and that the reduction in code complexity is desired.

It does however not remove the need for the lookup loop, as this is required to find the proper socket and create it if it doesn't exist.

87 ↗(On Diff #19347)

Yes, but if you allocate fds[AF_MAX] you can index based on address family and the lookup becomes a simple array access, rather than an iteration of the array.
(so fds[AF_INET], rather than a for loop). marked 8 inline comments as done.

Addressed feedback.

Now assumes addressfamily will be a number less than AF_MAX and allocates an array of sockets with AF_MAX elements. All lookups and assignments of sockets are now done via array access.


You may want to initialise the sockets array elements to -1, because '0' can be a valid file descriptor.


So != -1 here.


You could assign directly to h->sockets[addressfamily] here. marked 3 inline comments as done.

Addressed comments.

Also adjusted sockets array from AF_MAX to AF_MAX+1 as AF_MAX describes the highest ID of currently available AF's, and not number of available AFs. (1-based vs 0-based counting, in other words.)

The only change in this revision is the library has been renamed from "libifconfig" to "libifc", to be in sync with the prefixes and what people are actually calling it. Uploading it so it will be easier to track changes between revisions here.


I think you missed a rename here. marked an inline comment as done.

Fixed a few missed renames.

Removed libifc_ioctlwrap_caddrt as it was not necessary.
'struct ifreq ifr' is now always initialized by overwriting it with 0's.
Makefile now defines WARN?=6 to fault on all warnings. (Warnings are future "gifts" we don't want)

Now tells lib/Makefile to build libifc, so it actually gets built with 'buildworld'
Disabled building of shared library for now, to further state this is not production ready.

This revision was automatically updated to reflect the committed changes.