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);
46 /* Checks whether IP address sent as argument is valid IP address. */
48 bool silc_net_is_ip(const char *addr)
51 int len = sizeof(tmp);
52 return silc_net_addr2bin(addr, (unsigned char *)&tmp.s_addr, len);
55 /* Performs lookups for remote name and IP address. This peforms reverse
56 lookup as well to verify that the IP has FQDN. */
58 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
60 struct sockaddr_in remote;
70 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
72 memset(&remote, 0, sizeof(remote));
74 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
78 host_ip = inet_ntoa(remote.sin_addr);
82 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
83 memcpy(*ip, host_ip, strlen(host_ip));
85 /* Get host by address */
86 dest = gethostbyaddr((char *)&remote.sin_addr,
87 sizeof(struct in_addr), AF_INET);
91 /* Get same host by name to see that the remote host really is
92 the who it says it is */
93 memset(host_name, 0, sizeof(host_name));
94 memcpy(host_name, dest->h_name, strlen(dest->h_name));
96 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
97 memcpy(*hostname, host_name, strlen(host_name));
98 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
100 dest = gethostbyname(host_name);
104 /* Find the address from list */
105 for (i = 0; dest->h_addr_list[i]; i++)
106 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
107 sizeof(struct in_addr)))
109 if (!dest->h_addr_list[i])
113 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
114 memcpy(*ip, host_ip, strlen(host_ip));
115 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
120 /* Performs lookups for local name and IP address. This peforms reverse
121 lookup as well to verify that the IP has FQDN. */
123 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
125 struct sockaddr_in local;
126 struct hostent *dest;
127 char *host_ip = NULL;
128 char host_name[1024];
135 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
137 memset(&local, 0, sizeof(local));
139 rval = getsockname(sock, (struct sockaddr *)&local, &len);
143 host_ip = inet_ntoa(local.sin_addr);
147 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
148 memcpy(*ip, host_ip, strlen(host_ip));
150 /* Get host by address */
151 dest = gethostbyaddr((char *)&local.sin_addr,
152 sizeof(struct in_addr), AF_INET);
156 /* Get same host by name to see that the local host really is
157 the who it says it is */
158 memset(host_name, 0, sizeof(host_name));
159 memcpy(host_name, dest->h_name, strlen(dest->h_name));
161 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
162 memcpy(*hostname, host_name, strlen(host_name));
163 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
165 dest = gethostbyname(host_name);
169 /* Find the address from list */
170 for (i = 0; dest->h_addr_list[i]; i++)
171 if (!memcmp(dest->h_addr_list[i], &local.sin_addr,
172 sizeof(struct in_addr)))
174 if (!dest->h_addr_list[i])
178 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
179 memcpy(*ip, host_ip, strlen(host_ip));
180 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
185 /* Return remote port by socket. */
187 uint16 silc_net_get_remote_port(int sock)
189 struct sockaddr_in remote;
192 memset(&remote, 0, sizeof(remote));
193 len = sizeof(remote);
194 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
197 return ntohs(remote.sin_port);
200 /* Return local port by socket. */
202 uint16 silc_net_get_local_port(int sock)
204 struct sockaddr_in local;
207 memset(&local, 0, sizeof(local));
209 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
212 return ntohs(local.sin_port);
215 /* Return name of localhost. */
217 char *silc_net_localhost()
220 struct hostent *dest;
222 if (gethostname(hostname, sizeof(hostname)))
225 dest = gethostbyname(hostname);
227 return strdup(hostname);
229 return strdup(dest->h_name);