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 /* 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, const char *ip_addr)
34 struct sockaddr_in server;
35 int len = sizeof(server.sin_addr);
37 SILC_LOG_DEBUG(("Creating a new server listener"));
39 /* Create the socket */
40 sock = socket(AF_INET, SOCK_STREAM, 0);
42 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
46 /* Set the socket options */
47 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
49 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
53 /* Set the socket information for bind() */
54 memset(&server, 0, sizeof(server));
55 server.sin_family = AF_INET;
57 server.sin_port = htons(port);
59 /* Convert IP address to network byte order */
61 silc_net_addr2bin(ip_addr, (unsigned char *)&server.sin_addr.s_addr, len);
64 server.sin_addr.s_addr = INADDR_ANY;
66 /* Bind the server socket */
67 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
69 SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
73 /* Specify that we are listenning */
74 rval = listen(sock, 5);
76 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
80 /* Set the server socket to non-blocking mode */
81 silc_net_set_socket_nonblock(sock);
83 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
88 /* Closes the server by closing the socket connection. */
90 void silc_net_close_server(int sock)
95 SILC_LOG_DEBUG(("Server socket closed"));
98 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
99 socket or -1 on error. This blocks the process while trying to create
102 int silc_net_create_connection(const char *local_ip, int port,
106 struct hostent *dest;
107 struct sockaddr_in desthost;
109 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
112 dest = gethostbyname(host);
114 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
115 "IP address", host));
119 /* Set socket information */
120 memset(&desthost, 0, sizeof(desthost));
121 desthost.sin_port = htons(port);
122 desthost.sin_family = AF_INET;
123 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
125 /* Create the connection socket */
126 sock = socket(AF_INET, SOCK_STREAM, 0);
128 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
132 /* Bind to the local address if provided */
134 struct sockaddr_in local;
135 int local_len = sizeof(local.sin_addr);
137 /* Set the socket information for bind() */
138 memset(&local, 0, sizeof(local));
139 local.sin_family = AF_INET;
141 /* Convert IP address to network byte order */
142 silc_net_addr2bin(local_ip, (unsigned char *)&local.sin_addr.s_addr,
145 /* Bind the local socket */
146 rval = bind(sock, (struct sockaddr *)&local, sizeof(local));
148 SILC_LOG_ERROR(("Cannot connect to remote host: "
149 "cannot bind socket: %s", strerror(errno)));
154 /* Connect to the host */
155 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
157 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
163 /* Set appropriate options */
164 #if defined(TCP_NODELAY)
165 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
167 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
169 SILC_LOG_DEBUG(("Connection created"));
174 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
175 socket or -1 on error. This creates non-blocking socket hence the
176 connection returns directly. To get the result of the connect() one
177 must select() the socket and read the result after it's ready. */
179 int silc_net_create_connection_async(const char *local_ip, int port,
183 struct hostent *dest;
184 struct sockaddr_in desthost;
186 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
190 dest = gethostbyname(host);
192 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
193 "IP address", host));
197 /* Set socket information */
198 memset(&desthost, 0, sizeof(desthost));
199 desthost.sin_port = htons(port);
200 desthost.sin_family = AF_INET;
201 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
203 /* Create the connection socket */
204 sock = socket(AF_INET, SOCK_STREAM, 0);
206 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
210 /* Bind to the local address if provided */
212 struct sockaddr_in local;
213 int local_len = sizeof(local.sin_addr);
215 /* Set the socket information for bind() */
216 memset(&local, 0, sizeof(local));
217 local.sin_family = AF_INET;
219 /* Convert IP address to network byte order */
220 silc_net_addr2bin(local_ip, (unsigned char *)&local.sin_addr.s_addr,
223 /* Bind the local socket */
224 rval = bind(sock, (struct sockaddr *)&local, sizeof(local));
226 SILC_LOG_ERROR(("Cannot connect to remote host: "
227 "cannot bind socket: %s", strerror(errno)));
232 /* Set the socket to non-blocking mode */
233 silc_net_set_socket_nonblock(sock);
235 /* Connect to the host */
236 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
238 if (errno != EINPROGRESS) {
239 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
246 /* Set appropriate options */
247 #if defined(TCP_NODELAY)
248 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
250 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
252 SILC_LOG_DEBUG(("Connection operation in progress"));
257 /* Closes the connection by closing the socket connection. */
259 void silc_net_close_connection(int sock)
264 /* Set's the socket to non-blocking mode. */
266 int silc_net_set_socket_nonblock(int sock)
268 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
271 /* Converts the IP number string from numbers-and-dots notation to
274 bool silc_net_addr2bin(const char *addr, void *bin, uint32 bin_len)
279 ret = inet_aton(addr, &tmp);
284 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);