Date: Mon, 4 Dec 2006 15:11:25 +0200 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: freebsd-questions@freebsd.org Subject: Re: Is there a standard function for converting IP address to number? Message-ID: <20061204131125.GA60955@kobe.laptop> In-Reply-To: <20061204124550.GA1766@host.my.domain> References: <20061204124550.GA1766@host.my.domain>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2006-12-04 14:45, a@zeos.net wrote: > Is there a standard function converting four numbers to one 32-bit > IP address? > > I mean a function like > > f(i, j, k, l) { > return (((((i << 8) | j) << 8) | k) << 8) | l; > } That's not even a complete, usable function, but if you are looking for a 'standard' function as in "part of libc already", the answer is no. Try something like the in_addr_t makeaddr(uint8_t, uint8_t, uint8_t, uint8_t); function from this sample program: http://people.freebsd.org/~keramida/files/addr.tar.gz which looks like this: #include <sys/types.h> #include <netinet/in.h> #include "addr.h" in_addr_t makeaddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { return (htonl(((in_addr_t)a << 24) + ((in_addr_t)b << 16) + ((in_addr_t)c << 8) + (in_addr_t)d)); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061204131125.GA60955>