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 |
#include <netdb.h> |
23 |
|
24 |
#include "gettext.h" |
25 |
|
26 |
#include "sockinfo.h" |
27 |
#include "slog.h" |
28 |
|
29 |
void sockinfo::set (const sockaddr_in *sa, u8 prot_) |
30 |
{ |
31 |
host = sa->sin_addr.s_addr; |
32 |
port = prot_ == PROT_IPv4 ? 0 : sa->sin_port; |
33 |
prot = prot_; |
34 |
} |
35 |
|
36 |
void |
37 |
sockinfo::set (const conf_node *conf) |
38 |
{ |
39 |
host = 0; |
40 |
port = htons (conf->udp_port); |
41 |
|
42 |
if (conf->hostname) |
43 |
{ |
44 |
struct hostent *he = gethostbyname (conf->hostname); |
45 |
|
46 |
if (he |
47 |
&& he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
48 |
{ |
49 |
//sa->sin_family = he->h_addrtype; |
50 |
memcpy (&host, he->h_addr_list[0], 4); |
51 |
|
52 |
prot = PROT_UDPv4 | PROT_IPv4; |
53 |
} |
54 |
else |
55 |
slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
56 |
} |
57 |
} |
58 |
|
59 |
|
60 |
const sockaddr * |
61 |
sockinfo::sav4() const |
62 |
{ |
63 |
static sockaddr_in sa; |
64 |
|
65 |
sa.sin_family = AF_INET; |
66 |
sa.sin_port = port; |
67 |
sa.sin_addr.s_addr = host; |
68 |
|
69 |
return (const sockaddr *)&sa; |
70 |
} |
71 |
|
72 |
static char hostport[15 + 1 + 5 + 1]; // IPv4 : port |
73 |
|
74 |
const char * |
75 |
sockinfo::ntoa () const |
76 |
{ |
77 |
in_addr ia = { host }; |
78 |
|
79 |
sprintf (hostport, "%.15s", inet_ntoa (ia)); |
80 |
|
81 |
return hostport; |
82 |
} |
83 |
|
84 |
sockinfo::operator const char *() const |
85 |
{ |
86 |
in_addr ia = { host }; |
87 |
|
88 |
sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
89 |
|
90 |
return hostport; |
91 |
} |
92 |
|