~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/vnet/libxutil/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002 - 2004 Mike Wray <mike.wray@hp.com>.
 
3
 *
 
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.
 
12
 *
 
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
 
16
 */
 
17
 
 
18
#include "sys_net.h"
 
19
#include "sys_string.h"
 
20
 
 
21
#ifndef __KERNEL__
 
22
#  include <grp.h>   
 
23
#  include <pwd.h>  
 
24
#endif
 
25
 
 
26
#include "util.h"
 
27
 
 
28
 
 
29
/** @file Various utility functions.
 
30
 */
 
31
 
 
32
/** Print an address (in network order) as an IPv4 address string
 
33
 * in dot notation.
 
34
 *
 
35
 * @param io where to print address
 
36
 * @param address to print (in network order)
 
37
 * @return bytes printed
 
38
 */
 
39
int print_address(IOStream *io, unsigned long address){
 
40
#ifdef __KERNEL__
 
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));
 
47
#else
 
48
    struct in_addr inaddr = { s_addr: address };
 
49
    return IOStream_print(io, inet_ntoa(inaddr));
 
50
#endif
 
51
}
 
52
 
 
53
/** Get the protocol number for a protocol.
 
54
 *
 
55
 * @param name protocol name
 
56
 * @param protocol where to put the protocol number
 
57
 * @return 0 if OK, error otherwise
 
58
 */  
 
59
int get_protocol_number(char *name, unsigned long *protocol){
 
60
#ifdef __KERNEL__
 
61
    return -1;
 
62
#else
 
63
    struct protoent *proto = getprotobyname(name);
 
64
    if(!proto){
 
65
        return -1;
 
66
    }
 
67
    *protocol = proto->p_proto;
 
68
    return 0;
 
69
#endif
 
70
}
 
71
 
 
72
/** Get the protocol name for a protocol number.
 
73
 *
 
74
 * @param protocol number
 
75
 * @return name or null
 
76
 */
 
77
char *get_protocol_name(unsigned long protocol){
 
78
#ifdef __KERNEL__
 
79
    return 0;
 
80
#else
 
81
    struct protoent *proto = getprotobynumber(protocol);
 
82
    if(!proto){
 
83
        return 0;
 
84
    }
 
85
    return proto->p_name;
 
86
#endif
 
87
}
 
88
 
 
89
/** Get the host name for an address.
 
90
 *
 
91
 * @param addr address
 
92
 * @return host name or null
 
93
 */
 
94
char *get_host_name(unsigned long addr){
 
95
#ifdef __KERNEL__
 
96
    return 0;
 
97
#else
 
98
    struct in_addr inaddr;
 
99
    struct hostent *host = 0;
 
100
 
 
101
    inaddr.s_addr = addr;
 
102
    host = gethostbyaddr((char*)&inaddr, sizeof(inaddr), AF_INET);
 
103
    if(!host) return NULL;
 
104
    return host->h_name;
 
105
#endif
 
106
}