5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
25 /* Accepts a connection from a particular socket */
27 int silc_net_accept_connection(int sock)
29 return accept(sock, 0, 0);
32 /* Sets a option for a socket. */
34 int silc_net_set_socket_opt(int sock, int level, int option, int on)
36 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
39 /* Get socket options */
41 int silc_net_get_socket_opt(int sock, int level, int option,
42 void *optval, int *opt_len)
44 return getsockopt(sock, level, option, optval, opt_len);
47 /* Checks whether IP address sent as argument is valid IPv4 address. */
49 bool silc_net_is_ip4(const char *addr)
54 if (*addr != '.' && !isdigit(*addr))
67 /* Checks whether IP address sent as argument is valid IPv6 address. */
69 bool silc_net_is_ip6(const char *addr)
71 /* XXX does this work with all kinds of IPv6 addresses? */
73 if (*addr != ':' && !isxdigit(*addr))
81 /* Checks whether IP address sent as argument is valid IP address. */
83 bool silc_net_is_ip(const char *addr)
85 if (silc_net_is_ip4(addr))
87 return silc_net_is_ip6(addr);
90 /* Resolves IP address for hostname. */
92 bool silc_net_gethostbyname(const char *name, char *address,
96 struct addrinfo hints, *ai;
97 char hbuf[NI_MAXHOST];
99 memset(&hints, 0, sizeof(hints));
100 hints.ai_socktype = SOCK_STREAM;
101 if (getaddrinfo(name, NULL, &hints, &ai))
104 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
105 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
108 if (!inet_ntop(ai->ai_family, ai->ai_addr, address, address_len))
117 hp = gethostbyname(name);
121 memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
125 if (address_len < strlen(tmp))
127 memset(address, 0, address_len);
128 strncpy(address, tmp, strlen(tmp));
134 /* Resolves hostname by IP address. */
136 bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 name_len)
139 struct addrinfo req, *ai;
141 memset(&req, 0, sizeof(req));
142 req.ai_socktype = SOCK_STREAM;
143 req.ai_flags = AI_CANONNAME;
145 if (getaddrinfo(addr, NULL, &req, &ai))
147 if (name_len < strlen(ai->ai_canonname))
149 memset(name, 0, name_len);
150 strncpy(name, ai->ai_canonname, strlen(ai->ai_canonname));
156 hp = gethostbyaddr(addr, strlen(addr), AF_INET);
159 if (name_len < strlen(hp->h_name))
161 memset(name, 0, name_len);
162 strncpy(name, hp->h_name, strlen(hp->h_name));
168 /* Performs lookups for remote name and IP address. This peforms reverse
169 lookup as well to verify that the IP has FQDN. */
171 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
173 struct sockaddr_in remote;
174 struct hostent *dest;
175 char *host_ip = NULL;
176 char host_name[1024];
183 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
185 memset(&remote, 0, sizeof(remote));
186 len = sizeof(remote);
187 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
191 host_ip = inet_ntoa(remote.sin_addr);
195 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
196 memcpy(*ip, host_ip, strlen(host_ip));
198 /* Get host by address */
199 dest = gethostbyaddr((char *)&remote.sin_addr,
200 sizeof(struct in_addr), AF_INET);
204 /* Get same host by name to see that the remote host really is
205 the who it says it is */
206 memset(host_name, 0, sizeof(host_name));
207 memcpy(host_name, dest->h_name, strlen(dest->h_name));
209 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
210 memcpy(*hostname, host_name, strlen(host_name));
211 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
213 dest = gethostbyname(host_name);
217 /* Find the address from list */
218 for (i = 0; dest->h_addr_list[i]; i++)
219 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
220 sizeof(struct in_addr)))
222 if (!dest->h_addr_list[i])
226 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
227 memcpy(*ip, host_ip, strlen(host_ip));
228 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
233 /* Performs lookups for local name and IP address. This peforms reverse
234 lookup as well to verify that the IP has FQDN. */
236 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
238 struct sockaddr_in local;
239 struct hostent *dest;
240 char *host_ip = NULL;
241 char host_name[1024];
248 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
250 memset(&local, 0, sizeof(local));
252 rval = getsockname(sock, (struct sockaddr *)&local, &len);
256 host_ip = inet_ntoa(local.sin_addr);
260 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
261 memcpy(*ip, host_ip, strlen(host_ip));
263 /* Get host by address */
264 dest = gethostbyaddr((char *)&local.sin_addr,
265 sizeof(struct in_addr), AF_INET);
269 /* Get same host by name to see that the local host really is
270 the who it says it is */
271 memset(host_name, 0, sizeof(host_name));
272 memcpy(host_name, dest->h_name, strlen(dest->h_name));
274 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
275 memcpy(*hostname, host_name, strlen(host_name));
276 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
278 dest = gethostbyname(host_name);
282 /* Find the address from list */
283 for (i = 0; dest->h_addr_list[i]; i++)
284 if (!memcmp(dest->h_addr_list[i], &local.sin_addr,
285 sizeof(struct in_addr)))
287 if (!dest->h_addr_list[i])
291 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
292 memcpy(*ip, host_ip, strlen(host_ip));
293 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
298 /* Return remote port by socket. */
300 uint16 silc_net_get_remote_port(int sock)
302 struct sockaddr_in remote;
305 memset(&remote, 0, sizeof(remote));
306 len = sizeof(remote);
307 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
310 return ntohs(remote.sin_port);
313 /* Return local port by socket. */
315 uint16 silc_net_get_local_port(int sock)
317 struct sockaddr_in local;
320 memset(&local, 0, sizeof(local));
322 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
325 return ntohs(local.sin_port);
328 /* Return name of localhost. */
330 char *silc_net_localhost(void)
332 char hostname[256], ip_addr[64];
334 if (gethostname(hostname, sizeof(hostname)))
337 if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
338 return strdup(hostname);
340 silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
341 return strdup(hostname);
344 /* Returns local IP address */
346 char *silc_net_localip(void)
348 char hostname[256], ip_addr[64];
350 if (gethostname(hostname, sizeof(hostname)))
353 if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
356 return strdup(ip_addr);