5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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 /* This function creates server or daemon or listener or what ever. This
26 does not fork a new process, it must be done by the caller if caller
27 wants to create a child process. This is used by the SILC server.
28 If argument `ip_addr' is NULL `any' address will be used. Returns
29 the created socket or -1 on error. */
31 int silc_net_create_server(int port, char *ip_addr)
34 struct sockaddr_in server;
36 SILC_LOG_DEBUG(("Creating a new server listener"));
38 /* Create the socket */
39 sock = socket(PF_INET, SOCK_STREAM, 0);
41 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
45 /* Set the socket options */
46 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
48 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
52 /* Set the socket information for bind() */
53 memset(&server, 0, sizeof(server));
54 server.sin_family = PF_INET;
56 server.sin_port = htons(port);
58 /* Convert IP address to network byte order */
60 inet_aton(ip_addr, &server.sin_addr);
62 server.sin_addr.s_addr = INADDR_ANY;
64 /* Bind the server socket */
65 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
67 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
71 /* Specify that we are listenning */
72 rval = listen(sock, 5);
74 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
78 /* Set the server socket to non-blocking mode */
79 silc_net_set_socket_nonblock(sock);
81 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
86 void silc_net_close_server(int sock)
91 SILC_LOG_DEBUG(("Server socket closed"));
94 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
95 socket or -1 on error. This blocks the process while trying to create
98 int silc_net_create_connection(int port, char *host)
101 struct hostent *dest;
102 struct sockaddr_in desthost;
104 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
107 dest = gethostbyname(host);
109 SILC_LOG_ERROR(("Network (%s) unreachable", host));
113 /* Set socket information */
114 memset(&desthost, 0, sizeof(desthost));
115 desthost.sin_port = htons(port);
116 desthost.sin_family = PF_INET;
117 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
119 /* Create the connection socket */
120 sock = socket(PF_INET, SOCK_STREAM, 0);
122 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
126 /* Connect to the host */
127 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
129 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
135 /* Set appropriate options */
136 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
137 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
139 SILC_LOG_DEBUG(("Connection created"));
144 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
145 socket or -1 on error. This creates non-blocking socket hence the
146 connection returns directly. To get the result of the connect() one
147 must select() the socket and read the result after it's ready. */
149 int silc_net_create_connection_async(int port, char *host)
152 struct hostent *dest;
153 struct sockaddr_in desthost;
155 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
159 dest = gethostbyname(host);
161 SILC_LOG_ERROR(("Network (%s) unreachable", host));
165 /* Set socket information */
166 memset(&desthost, 0, sizeof(desthost));
167 desthost.sin_port = htons(port);
168 desthost.sin_family = PF_INET;
169 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
171 /* Create the connection socket */
172 sock = socket(PF_INET, SOCK_STREAM, 0);
174 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
178 /* Set the socket to non-blocking mode */
179 silc_net_set_socket_nonblock(sock);
181 /* Connect to the host */
182 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
184 if (errno != EINPROGRESS) {
185 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
192 /* Set appropriate options */
193 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
194 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
196 SILC_LOG_DEBUG(("Connection operation in progress"));
201 /* Closes the connection */
203 void silc_net_close_connection(int sock)
208 /* Accepts a connection from a particular socket */
210 int silc_net_accept_connection(int sock)
212 return accept(sock, 0, 0);
215 /* Set's the socket to non-blocking mode. */
217 int silc_net_set_socket_nonblock(int sock)
219 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
222 /* Sets a option for a socket. */
224 int silc_net_set_socket_opt(int sock, int level, int option, int on)
226 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
229 /* Checks whether IP address sent as argument is valid IP address. */
231 int silc_net_is_ip(const char *addr)
234 return inet_aton(addr, &tmp);
237 /* Performs lookups for remote name and IP address. This peforms reverse
238 lookup as well to verify that the IP has FQDN. */
240 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
242 struct sockaddr_in remote;
243 struct hostent *dest;
244 char *host_ip = NULL;
245 char host_name[1024];
252 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
254 memset(&remote, 0, sizeof(remote));
255 len = sizeof(remote);
256 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
260 /* Get host by address */
261 dest = gethostbyaddr((char *)&remote.sin_addr,
262 sizeof(struct in_addr), AF_INET);
266 /* Get same host by name to see that the remote host really is
267 the who it says it is */
268 memset(host_name, 0, sizeof(host_name));
269 memcpy(host_name, dest->h_name, strlen(dest->h_name));
271 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
272 memcpy(*hostname, host_name, strlen(host_name));
273 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
275 dest = gethostbyname(host_name);
279 /* Find the address from list */
280 for (i = 0; dest->h_addr_list[i]; i++)
281 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
282 sizeof(struct in_addr)))
284 if (!dest->h_addr_list[i])
287 host_ip = inet_ntoa(remote.sin_addr);
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));
296 /* Return remote port by socket. */
298 uint16 silc_net_get_remote_port(int sock)
300 struct sockaddr_in remote;
303 memset(&remote, 0, sizeof(remote));
304 len = sizeof(remote);
305 if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
308 return ntohs(remote.sin_port);
311 /* Return local port by socket. */
313 uint16 silc_net_get_local_port(int sock)
315 struct sockaddr_in local;
318 memset(&local, 0, sizeof(local));
320 if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
323 return ntohs(local.sin_port);
326 /* Return name of localhost. */
328 char *silc_net_localhost()
331 if (!gethostname(hostname, sizeof(hostname)))
332 return strdup(hostname);