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 IP address. */
49 bool silc_net_is_ip(const char *addr)
52 int len = sizeof(tmp);
53 return silc_net_addr2bin(addr, (unsigned char *)&tmp.s_addr, len);
56 /* Performs lookups for remote name and IP address. This peforms reverse
57 lookup as well to verify that the IP has FQDN. */
59 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
61 struct sockaddr_in remote;
71 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
73 memset(&remote, 0, sizeof(remote));
75 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
79 host_ip = inet_ntoa(remote.sin_addr);
83 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
84 memcpy(*ip, host_ip, strlen(host_ip));
86 /* Get host by address */
87 dest = gethostbyaddr((char *)&remote.sin_addr,
88 sizeof(struct in_addr), AF_INET);
92 /* Get same host by name to see that the remote host really is
93 the who it says it is */
94 memset(host_name, 0, sizeof(host_name));
95 memcpy(host_name, dest->h_name, strlen(dest->h_name));
97 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
98 memcpy(*hostname, host_name, strlen(host_name));
99 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
101 dest = gethostbyname(host_name);
105 /* Find the address from list */
106 for (i = 0; dest->h_addr_list[i]; i++)
107 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
108 sizeof(struct in_addr)))
110 if (!dest->h_addr_list[i])
114 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
115 memcpy(*ip, host_ip, strlen(host_ip));
116 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
121 /* Performs lookups for local name and IP address. This peforms reverse
122 lookup as well to verify that the IP has FQDN. */
124 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
126 struct sockaddr_in local;
127 struct hostent *dest;
128 char *host_ip = NULL;
129 char host_name[1024];
136 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
138 memset(&local, 0, sizeof(local));
140 rval = getsockname(sock, (struct sockaddr *)&local, &len);
144 host_ip = inet_ntoa(local.sin_addr);
148 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
149 memcpy(*ip, host_ip, strlen(host_ip));
151 /* Get host by address */
152 dest = gethostbyaddr((char *)&local.sin_addr,
153 sizeof(struct in_addr), AF_INET);
157 /* Get same host by name to see that the local host really is
158 the who it says it is */
159 memset(host_name, 0, sizeof(host_name));
160 memcpy(host_name, dest->h_name, strlen(dest->h_name));
162 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
163 memcpy(*hostname, host_name, strlen(host_name));
164 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
166 dest = gethostbyname(host_name);
170 /* Find the address from list */
171 for (i = 0; dest->h_addr_list[i]; i++)
172 if (!memcmp(dest->h_addr_list[i], &local.sin_addr,
173 sizeof(struct in_addr)))
175 if (!dest->h_addr_list[i])
179 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
180 memcpy(*ip, host_ip, strlen(host_ip));
181 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
186 /* Return remote port by socket. */
188 uint16 silc_net_get_remote_port(int sock)
190 struct sockaddr_in remote;
193 memset(&remote, 0, sizeof(remote));
194 len = sizeof(remote);
195 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
198 return ntohs(remote.sin_port);
201 /* Return local port by socket. */
203 uint16 silc_net_get_local_port(int sock)
205 struct sockaddr_in local;
208 memset(&local, 0, sizeof(local));
210 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
213 return ntohs(local.sin_port);
216 /* Return name of localhost. */
218 char *silc_net_localhost(void)
221 struct hostent *dest;
223 if (gethostname(hostname, sizeof(hostname)))
226 dest = gethostbyname(hostname);
228 return strdup(hostname);
230 return strdup(dest->h_name);