5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcruntime.h"
22 /* Returns bound port from listener */
24 SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
25 SilcUInt32 *port_count)
30 ports = silc_calloc(listener->socks_count, sizeof(*ports));
34 for (i = 0; i < listener->socks_count; i++)
35 ports[i] = silc_net_get_local_port(listener->socks[i]);
38 *port_count = listener->socks_count;
43 /* Return bound IP from listener */
45 char **silc_net_listener_get_ip(SilcNetListener listener,
48 char **ips = NULL, *ip;
51 ips = silc_calloc(listener->socks_count, sizeof(*ips));
55 for (i = 0, k = 0; i < listener->socks_count; i++) {
56 if (silc_net_check_local_by_sock(listener->socks[i], NULL, &ip))
66 /* Return bound hostname from listener */
68 char **silc_net_listener_get_hostname(SilcNetListener listener,
69 SilcUInt32 *hostname_count)
74 hs = silc_calloc(listener->socks_count, sizeof(*hs));
78 for (i = 0, k = 0; i < listener->socks_count; i++) {
79 if (silc_net_check_local_by_sock(listener->socks[i], &h, NULL))
89 /* Accepts a connection from a particular socket */
91 int silc_net_accept_connection(int sock)
93 int ret = accept(sock, 0, 0);
95 silc_set_errno_posix(errno);
99 /* Sets a option for a socket. */
101 int silc_net_set_socket_opt(int sock, int level, int option, int on)
103 int ret = setsockopt(sock, level, option, (void *)&on, sizeof(on));
105 silc_set_errno_posix(errno);
109 /* Get socket options */
111 int silc_net_get_socket_opt(int sock, int level, int option,
112 void *optval, int *opt_len)
114 int ret = getsockopt(sock, level, option, optval, (unsigned int *)opt_len);
116 silc_set_errno_posix(errno);
120 /* Checks whether IP address sent as argument is valid IPv4 address. */
122 SilcBool silc_net_is_ip4(const char *addr)
127 if (*addr != '.' && !isdigit((int)*addr))
140 /* Checks whether IP address sent as argument is valid IPv6 address. */
142 SilcBool silc_net_is_ip6(const char *addr)
144 /* XXX does this work with all kinds of IPv6 addresses? */
146 if (*addr != ':' && !isxdigit((int)*addr))
154 /* Checks whether IP address sent as argument is valid IP address. */
156 SilcBool silc_net_is_ip(const char *addr)
158 if (silc_net_is_ip4(addr))
160 return silc_net_is_ip6(addr);
163 /* Internal context for async resolving */
165 SilcNetResolveCallback completion;
167 SilcBool prefer_ipv6;
168 SilcSchedule schedule;
171 } *SilcNetResolveContext;
173 SILC_TASK_CALLBACK(silc_net_resolve_completion)
175 SilcNetResolveContext r = (SilcNetResolveContext)context;
177 /* Call the completion callback */
179 (*r->completion)(r->result, r->context);
182 silc_free(r->result);
186 /* Thread function to resolve the address for hostname. */
188 static void *silc_net_gethostbyname_thread(void *context)
190 SilcNetResolveContext r = (SilcNetResolveContext)context;
191 SilcSchedule schedule = r->schedule;
194 if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
195 r->result = silc_strdup(tmp);
197 silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
199 silc_schedule_wakeup(schedule);
203 /* Thread function to resolve the hostname for address. */
205 static void *silc_net_gethostbyaddr_thread(void *context)
207 SilcNetResolveContext r = (SilcNetResolveContext)context;
208 SilcSchedule schedule = r->schedule;
211 if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
212 r->result = silc_strdup(tmp);
214 silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
216 silc_schedule_wakeup(schedule);
220 /* Resolves IP address for hostname. */
222 SilcBool silc_net_gethostbyname(const char *name,
223 SilcBool prefer_ipv6, char *address,
224 SilcUInt32 address_len)
227 struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL;
229 memset(&hints, 0, sizeof(hints));
230 hints.ai_socktype = SOCK_STREAM;
231 if (getaddrinfo(name, NULL, &hints, &ai))
234 for (tmp = ai; tmp; tmp = tmp->ai_next) {
235 if (tmp->ai_family == AF_INET6) {
241 if (tmp->ai_family == AF_INET) {
249 tmp = (prefer_ipv6 ? (ip6 ? ip6 : ip4) : (ip4 ? ip4 : ip6));
255 if (getnameinfo(tmp->ai_addr, tmp->ai_addrlen, address,
256 address_len, NULL, 0, NI_NUMERICHOST)) {
267 if (silc_net_is_ip4(name)) {
268 memset(address, 0, address_len);
269 if (address_len < strlen(name))
271 strncpy(address, name, strlen(name));
275 hp = gethostbyname(name);
279 memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
283 if (address_len < strlen(tmp))
285 memset(address, 0, address_len);
286 strncpy(address, tmp, strlen(tmp));
292 /* Resolves IP address for hostname async. */
294 void silc_net_gethostbyname_async(const char *name,
295 SilcBool prefer_ipv6,
296 SilcSchedule schedule,
297 SilcNetResolveCallback completion,
300 SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
303 schedule = silc_schedule_get_global();
304 SILC_VERIFY(schedule);
307 r->completion = completion;
308 r->context = context;
309 r->prefer_ipv6 = prefer_ipv6;
310 r->schedule = schedule;
311 r->input = silc_strdup(name);
313 silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
316 /* Resolves hostname by IP address. */
318 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
322 struct addrinfo req, *ai;
324 memset(&req, 0, sizeof(req));
325 req.ai_socktype = SOCK_STREAM;
326 req.ai_flags = AI_CANONNAME;
328 if (getaddrinfo(addr, NULL, &req, &ai))
330 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, name, name_len, NULL, 0, 0)) {
339 if (!silc_net_addr2bin(addr, a, sizeof(a)))
342 hp = gethostbyaddr(a, 4, AF_INET);
345 if (name_len < strlen(hp->h_name))
347 memset(name, 0, name_len);
348 strncpy(name, hp->h_name, strlen(hp->h_name));
354 /* Resolves hostname by IP address async. */
356 void silc_net_gethostbyaddr_async(const char *addr,
357 SilcSchedule schedule,
358 SilcNetResolveCallback completion,
361 SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
364 schedule = silc_schedule_get_global();
365 SILC_VERIFY(schedule);
368 r->completion = completion;
369 r->context = context;
370 r->schedule = schedule;
371 r->input = silc_strdup(addr);
373 silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
378 /* Performs lookups for remote name and IP address. This peforms reverse
379 lookup as well to verify that the IP has FQDN. */
381 SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
389 struct sockaddr_storage remote;
396 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
398 memset(&remote, 0, sizeof(remote));
399 memset(&s, 0, sizeof(s));
400 len = sizeof(remote);
401 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
405 if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0,
409 *ip = silc_memdup(s, strlen(s));
413 struct sockaddr_in remote;
420 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
422 memset(&remote, 0, sizeof(remote));
423 len = sizeof(remote);
424 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
428 host_ip = inet_ntoa(remote.sin_addr);
432 *ip = silc_memdup(host_ip, strlen(host_ip));
437 /* Do reverse lookup if we want hostname too. */
439 /* Get host by address */
440 if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
443 *hostname = silc_memdup(host, strlen(host));
444 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
447 if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
450 if (strcmp(*ip, host))
454 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
458 /* Performs lookups for local name and IP address. This peforms reverse
459 lookup as well to verify that the IP has FQDN. */
461 SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
469 struct sockaddr_storage local;
476 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
478 memset(&local, 0, sizeof(local));
479 memset(&s, 0, sizeof(s));
481 rval = getsockname(sock, (struct sockaddr *)&local, &len);
485 if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
489 *ip = silc_memdup(s, strlen(s));
493 struct sockaddr_in local;
500 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
502 memset(&local, 0, sizeof(local));
504 rval = getsockname(sock, (struct sockaddr *)&local, &len);
508 host_ip = inet_ntoa(local.sin_addr);
512 *ip = silc_memdup(host_ip, strlen(host_ip));
517 /* Do reverse lookup if we want hostname too. */
519 /* Get host by address */
520 if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
523 *hostname = silc_memdup(host, strlen(host));
524 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
527 if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
530 if (strcmp(*ip, host))
534 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
538 /* Return remote port by socket. */
540 SilcUInt16 silc_net_get_remote_port(SilcSocket sock)
543 struct sockaddr_storage remote;
547 memset(&remote, 0, sizeof(remote));
548 len = sizeof(remote);
549 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
552 if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
558 struct sockaddr_in remote;
561 memset(&remote, 0, sizeof(remote));
562 len = sizeof(remote);
563 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
566 return ntohs(remote.sin_port);
570 /* Return local port by socket. */
572 SilcUInt16 silc_net_get_local_port(SilcSocket sock)
575 struct sockaddr_storage local;
579 memset(&local, 0, sizeof(local));
581 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
584 if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
590 struct sockaddr_in local;
593 memset(&local, 0, sizeof(local));
595 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
598 return ntohs(local.sin_port);
601 #endif /* !SILC_SYMBIAN */
603 /* Return name of localhost. */
605 char *silc_net_localhost(void)
607 char hostname[256], ip_addr[64];
609 if (gethostname(hostname, sizeof(hostname)))
612 if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
613 return silc_strdup(hostname);
615 silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
616 return silc_strdup(hostname);
619 /* Returns local IP address */
621 char *silc_net_localip(void)
623 char hostname[256], ip_addr[64];
625 if (gethostname(hostname, sizeof(hostname)))
628 if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
631 return silc_strdup(ip_addr);
634 /* Convert network byte order IP to string */
636 SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len,
637 char *addr, SilcUInt32 addr_size)
639 if (!addr || !addr_size)
644 struct sockaddr_in6 ipv6;
645 memset(&ipv6, 0, sizeof(ipv6));
646 ipv6.sin6_family = AF_INET6;
647 memcpy(&ipv6.sin6_addr, bin, sizeof(ipv6.sin6_addr));
648 if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
649 addr, addr_size, NULL, 0, NI_NUMERICHOST))
653 #endif /* HAVE_IPV6 */
654 } else if (bin_len == 4) {
658 memcpy(&ipv4.s_addr, bin, 4);
663 silc_snprintf(addr, addr_size, a);
670 /* Host to network byte order */
672 SilcUInt32 silc_htonl(SilcUInt32 host)
674 #ifdef WORDS_BIGENDIAN
677 return SILC_SWAB_32(host);
678 #endif /* WORDS_BIGENDIAN */
681 /* Network to host byte order */
683 SilcUInt32 silc_ntohl(SilcUInt32 net)
685 #ifdef WORDS_BIGENDIAN
688 return SILC_SWAB_32(net);
689 #endif /* WORDS_BIGENDIAN */
692 /* Host to network byte order */
694 SilcUInt16 silc_htons(SilcUInt16 host)
696 #ifdef WORDS_BIGENDIAN
699 return SILC_SWAB_16(host);
700 #endif /* WORDS_BIGENDIAN */
703 /* Network to host byte order */
705 SilcUInt16 silc_ntohs(SilcUInt16 net)
707 #ifdef WORDS_BIGENDIAN
710 return SILC_SWAB_16(net);
711 #endif /* WORDS_BIGENDIAN */