2
* Copyright (C) 2002 - 2004 Mike Wray <mike.wray@hp.com>.
4
* This library is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as
6
* published by the Free Software Foundation; either version 2.1 of the
7
* License, or (at your option) any later version. This library is
8
* distributed in the hope that it will be useful, but WITHOUT ANY
9
* WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE.
11
* See the GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this library; if not, write to the Free Software Foundation,
15
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "sys_string.h"
29
/** @file Various utility functions.
32
/** Print an address (in network order) as an IPv4 address string
35
* @param io where to print address
36
* @param address to print (in network order)
37
* @return bytes printed
39
int print_address(IOStream *io, unsigned long address){
41
address = ntohl(address);
42
return IOStream_print(io, "%u.%u.%u.%u",
43
(unsigned)((address >> 24) & 0xff),
44
(unsigned)((address >> 16) & 0xff),
45
(unsigned)((address >> 8) & 0xff),
46
(unsigned)((address ) & 0xff));
48
struct in_addr inaddr = { s_addr: address };
49
return IOStream_print(io, inet_ntoa(inaddr));
53
/** Get the protocol number for a protocol.
55
* @param name protocol name
56
* @param protocol where to put the protocol number
57
* @return 0 if OK, error otherwise
59
int get_protocol_number(char *name, unsigned long *protocol){
63
struct protoent *proto = getprotobyname(name);
67
*protocol = proto->p_proto;
72
/** Get the protocol name for a protocol number.
74
* @param protocol number
75
* @return name or null
77
char *get_protocol_name(unsigned long protocol){
81
struct protoent *proto = getprotobynumber(protocol);
89
/** Get the host name for an address.
92
* @return host name or null
94
char *get_host_name(unsigned long addr){
98
struct in_addr inaddr;
99
struct hostent *host = 0;
101
inaddr.s_addr = addr;
102
host = gethostbyaddr((char*)&inaddr, sizeof(inaddr), AF_INET);
103
if(!host) return NULL;