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.
23 * Revision 1.1 2000/06/27 11:36:55 priikone
29 #include "silcincludes.h"
32 /* This function creates server or daemon or listener or what ever. This
33 does not fork a new process, it must be done by the caller if caller
34 wants to create a child process. This is used by the SILC server.
35 If argument `ip_addr' is NULL `any' address will be used. Returns
36 the created socket or -1 on error. */
38 int silc_net_create_server(int port, char *ip_addr)
41 struct sockaddr_in server;
43 SILC_LOG_DEBUG(("Creating a new server listener"));
45 /* Create the socket */
46 sock = socket(PF_INET, SOCK_STREAM, 0);
48 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
52 /* Set the socket options */
53 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
55 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
59 /* Set the socket information for bind() */
60 memset(&server, 0, sizeof(server));
61 server.sin_family = PF_INET;
62 server.sin_port = htons(port);
64 /* Convert IP address to network byte order */
66 inet_aton(ip_addr, &server.sin_addr);
68 server.sin_addr.s_addr = INADDR_ANY;
70 /* Bind the server socket */
71 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
73 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
77 /* Specify that we are listenning */
78 rval = listen(sock, 5);
80 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
84 /* Set the server socket to non-blocking mode */
85 silc_net_set_socket_nonblock(sock);
87 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
92 void silc_net_close_server(int sock)
97 SILC_LOG_DEBUG(("Server socket closed"));
100 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
101 socket or -1 on error. This blocks the process while trying to create
104 int silc_net_create_connection(int port, char *host)
107 struct hostent *dest;
108 struct sockaddr_in desthost;
110 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
113 dest = gethostbyname(host);
115 SILC_LOG_ERROR(("Network (%s) unreachable", host));
119 /* Set socket information */
120 memset(&desthost, 0, sizeof(desthost));
121 desthost.sin_port = htons(port);
122 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
124 /* Create the connection socket */
125 sock = socket(PF_INET, SOCK_STREAM, 0);
127 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
131 /* Connect to the host */
132 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
134 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
140 /* Set appropriate option */
141 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
143 SILC_LOG_DEBUG(("Connection created"));
148 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
149 socket or -1 on error. This creates non-blocking socket hence the
150 connection returns directly. To get the result of the connect() one
151 must select() the socket and read the result after it's ready. */
153 int silc_net_create_connection_async(int port, char *host)
156 struct hostent *dest;
157 struct sockaddr_in desthost;
159 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
163 dest = gethostbyname(host);
165 SILC_LOG_ERROR(("Network (%s) unreachable", host));
169 /* Set socket information */
170 memset(&desthost, 0, sizeof(desthost));
171 desthost.sin_port = htons(port);
172 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
174 /* Create the connection socket */
175 sock = socket(PF_INET, SOCK_STREAM, 0);
177 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
181 /* Set the socket to non-blocking mode */
182 silc_net_set_socket_nonblock(sock);
184 /* Connect to the host */
185 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
187 if (errno != EINPROGRESS) {
188 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
195 /* Set appropriate option */
196 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
198 SILC_LOG_DEBUG(("Connection operation in progress"));
203 /* Closes the connection */
205 void silc_net_close_connection(int sock)
210 /* Accepts a connection from a particular socket */
212 int silc_net_accept_connection(int sock)
214 return accept(sock, 0, 0);
217 /* Set's the socket to non-blocking mode. */
219 int silc_net_set_socket_nonblock(int sock)
221 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
224 /* Sets a option for a socket. */
226 int silc_net_set_socket_opt(int sock, int level, int option, int on)
228 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
231 /* Checks whether IP address sent as argument is valid IP address. */
233 int silc_net_is_ip(const char *addr)
236 return inet_aton(addr, &tmp);
239 /* Performs lookups for remote name and IP address. */
241 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
243 struct sockaddr_in remote;
244 struct hostent *dest;
245 char *host_ip = NULL;
246 char host_name[1024];
253 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
255 memset(&remote, 0, sizeof(remote));
256 len = sizeof(remote);
257 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
261 /* Get host by address */
262 dest = gethostbyaddr((char *)&remote.sin_addr,
263 sizeof(struct in_addr), AF_INET);
267 /* Get same hsot by name to see that the remote host really is
268 the who it says it is */
269 memset(host_name, 0, sizeof(host_name));
270 memcpy(host_name, dest->h_name, strlen(dest->h_name));
271 dest = gethostbyname(host_name);
275 /* Find the address from list */
276 for (i = 0; dest->h_addr_list[i]; i++)
277 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
278 sizeof(struct in_addr)))
280 if (!dest->h_addr_list[i])
283 host_ip = inet_ntoa(remote.sin_addr);
287 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
288 memcpy(*hostname, host_name, strlen(host_name));
289 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
290 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
291 memcpy(*ip, host_ip, strlen(host_ip));
292 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));