1 |
/* |
2 |
sockinfo.c -- socket address management |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2 of the License, or |
7 |
(at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
*/ |
18 |
|
19 |
#include "config.h" |
20 |
|
21 |
#include "arpa/inet.h" |
22 |
|
23 |
#include "gettext.h" |
24 |
|
25 |
#include "sockinfo.h" |
26 |
#include "slog.h" |
27 |
|
28 |
void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
29 |
{ |
30 |
host = sa->sin_addr.s_addr; |
31 |
port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
32 |
prot = prot_; |
33 |
} |
34 |
|
35 |
void |
36 |
sockinfo::set (const conf_node *conf) |
37 |
{ |
38 |
host = 0; |
39 |
port = htons (conf->udp_port); |
40 |
|
41 |
if (conf->hostname) |
42 |
{ |
43 |
struct hostent *he = gethostbyname (conf->hostname); |
44 |
|
45 |
if (he |
46 |
&& he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
47 |
{ |
48 |
//sa->sin_family = he->h_addrtype; |
49 |
memcpy (&host, he->h_addr_list[0], 4); |
50 |
|
51 |
prot = PROT_UDPv4 | PROT_IPv4; |
52 |
} |
53 |
else |
54 |
slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
55 |
} |
56 |
} |
57 |
|
58 |
|
59 |
const sockaddr * |
60 |
sockinfo::sav4() const |
61 |
{ |
62 |
static sockaddr_in sa; |
63 |
|
64 |
sa.sin_family = AF_INET; |
65 |
sa.sin_port = port; |
66 |
sa.sin_addr.s_addr = host; |
67 |
|
68 |
return (const sockaddr *)&sa; |
69 |
} |
70 |
|
71 |
static char hostport[15 + 1 + 5 + 1]; // IPv4 : port |
72 |
|
73 |
const char * |
74 |
sockinfo::ntoa () const |
75 |
{ |
76 |
in_addr ia = { host }; |
77 |
|
78 |
sprintf (hostport, "%.15s", inet_ntoa (ia)); |
79 |
|
80 |
return hostport; |
81 |
} |
82 |
|
83 |
sockinfo::operator const char *() const |
84 |
{ |
85 |
in_addr ia = { host }; |
86 |
|
87 |
sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
88 |
|
89 |
return hostport; |
90 |
} |
91 |
|