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 /* Checks whether IP address sent as argument is valid IP address. */
41 bool silc_net_is_ip(const char *addr)
44 int len = sizeof(tmp);
45 return silc_net_addr2bin(addr, (unsigned char *)&tmp.s_addr, len);
48 /* Performs lookups for remote name and IP address. This peforms reverse
49 lookup as well to verify that the IP has FQDN. */
51 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
53 struct sockaddr_in remote;
63 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
65 memset(&remote, 0, sizeof(remote));
67 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
71 host_ip = inet_ntoa(remote.sin_addr);
75 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
76 memcpy(*ip, host_ip, strlen(host_ip));
78 /* Get host by address */
79 dest = gethostbyaddr((char *)&remote.sin_addr,
80 sizeof(struct in_addr), AF_INET);
84 /* Get same host by name to see that the remote host really is
85 the who it says it is */
86 memset(host_name, 0, sizeof(host_name));
87 memcpy(host_name, dest->h_name, strlen(dest->h_name));
89 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
90 memcpy(*hostname, host_name, strlen(host_name));
91 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
93 dest = gethostbyname(host_name);
97 /* Find the address from list */
98 for (i = 0; dest->h_addr_list[i]; i++)
99 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
100 sizeof(struct in_addr)))
102 if (!dest->h_addr_list[i])
106 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
107 memcpy(*ip, host_ip, strlen(host_ip));
108 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
113 /* Performs lookups for local name and IP address. This peforms reverse
114 lookup as well to verify that the IP has FQDN. */
116 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
118 struct sockaddr_in local;
119 struct hostent *dest;
120 char *host_ip = NULL;
121 char host_name[1024];
128 SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
130 memset(&local, 0, sizeof(local));
132 rval = getsockname(sock, (struct sockaddr *)&local, &len);
136 host_ip = inet_ntoa(local.sin_addr);
140 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
141 memcpy(*ip, host_ip, strlen(host_ip));
143 /* Get host by address */
144 dest = gethostbyaddr((char *)&local.sin_addr,
145 sizeof(struct in_addr), AF_INET);
149 /* Get same host by name to see that the local host really is
150 the who it says it is */
151 memset(host_name, 0, sizeof(host_name));
152 memcpy(host_name, dest->h_name, strlen(dest->h_name));
154 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
155 memcpy(*hostname, host_name, strlen(host_name));
156 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
158 dest = gethostbyname(host_name);
162 /* Find the address from list */
163 for (i = 0; dest->h_addr_list[i]; i++)
164 if (!memcmp(dest->h_addr_list[i], &local.sin_addr,
165 sizeof(struct in_addr)))
167 if (!dest->h_addr_list[i])
171 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
172 memcpy(*ip, host_ip, strlen(host_ip));
173 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
178 /* Return remote port by socket. */
180 uint16 silc_net_get_remote_port(int sock)
182 struct sockaddr_in remote;
185 memset(&remote, 0, sizeof(remote));
186 len = sizeof(remote);
187 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
190 return ntohs(remote.sin_port);
193 /* Return local port by socket. */
195 uint16 silc_net_get_local_port(int sock)
197 struct sockaddr_in local;
200 memset(&local, 0, sizeof(local));
202 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
205 return ntohs(local.sin_port);
208 /* Return name of localhost. */
210 char *silc_net_localhost()
213 struct hostent *dest;
215 if (gethostname(hostname, sizeof(hostname)))
218 dest = gethostbyname(hostname);
220 return strdup(hostname);
222 return strdup(dest->h_name);