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, 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(PF_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 = PF_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);
63 server.sin_addr.s_addr = INADDR_ANY;
65 /* Bind the server socket */
66 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
68 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
72 /* Specify that we are listenning */
73 rval = listen(sock, 5);
75 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
79 /* Set the server socket to non-blocking mode */
80 silc_net_set_socket_nonblock(sock);
82 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
87 /* Closes the server by closing the socket connection. */
89 void silc_net_close_server(int sock)
94 SILC_LOG_DEBUG(("Server socket closed"));
97 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
98 socket or -1 on error. This blocks the process while trying to create
101 int silc_net_create_connection(int port, char *host)
104 struct hostent *dest;
105 struct sockaddr_in desthost;
107 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
110 dest = gethostbyname(host);
112 SILC_LOG_ERROR(("Network (%s) unreachable", host));
116 /* Set socket information */
117 memset(&desthost, 0, sizeof(desthost));
118 desthost.sin_port = htons(port);
119 desthost.sin_family = PF_INET;
120 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
122 /* Create the connection socket */
123 sock = socket(PF_INET, SOCK_STREAM, 0);
125 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
129 /* Connect to the host */
130 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
132 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
138 /* Set appropriate options */
139 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
140 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
142 SILC_LOG_DEBUG(("Connection created"));
147 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
148 socket or -1 on error. This creates non-blocking socket hence the
149 connection returns directly. To get the result of the connect() one
150 must select() the socket and read the result after it's ready. */
152 int silc_net_create_connection_async(int port, char *host)
155 struct hostent *dest;
156 struct sockaddr_in desthost;
158 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
162 dest = gethostbyname(host);
164 SILC_LOG_ERROR(("Network (%s) unreachable", host));
168 /* Set socket information */
169 memset(&desthost, 0, sizeof(desthost));
170 desthost.sin_port = htons(port);
171 desthost.sin_family = PF_INET;
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 options */
196 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
197 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
199 SILC_LOG_DEBUG(("Connection operation in progress"));
204 /* Closes the connection by closing the socket connection. */
206 void silc_net_close_connection(int sock)
211 /* Set's the socket to non-blocking mode. */
213 int silc_net_set_socket_nonblock(int sock)
215 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
218 /* Converts the IP number string from numbers-and-dots notation to
221 bool silc_net_addr2bin(const char *addr, unsigned char *bin,
227 ret = inet_aton(addr, &tmp);
232 SILC_PUT32_LSB(tmp.s_addr, bin);