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)
35 struct sockaddr_in server;
36 int len = sizeof(server.sin_addr);
38 SILC_LOG_DEBUG(("Creating a new server listener"));
40 /* Create the socket */
41 sock = socket(PF_INET, SOCK_STREAM, 0);
42 if (sock == INVALID_SOCKET) {
43 SILC_LOG_ERROR(("Cannot create socket"));
47 /* Set the socket options */
48 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
50 SILC_LOG_ERROR(("Cannot set socket options"));
55 /* Set the socket information for bind() */
56 memset(&server, 0, sizeof(server));
57 server.sin_family = AF_INET;
59 server.sin_port = htons(port);
61 /* Convert IP address to network byte order */
63 silc_net_addr2bin(ip_addr, (unsigned char *)&server.sin_addr.s_addr, len);
65 server.sin_addr.s_addr = INADDR_ANY;
67 /* Bind the server socket */
68 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
70 SILC_LOG_ERROR(("Cannot bind socket"));
75 /* Specify that we are listenning */
76 rval = listen(sock, 5);
78 SILC_LOG_ERROR(("Cannot set socket listenning"));
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(int port, char *host)
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);
127 if (sock == INVALID_SOCKET) {
128 SILC_LOG_ERROR(("Cannot create socket"));
132 /* Connect to the host */
133 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
134 err = WSAGetLastError();
135 if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) {
136 SILC_LOG_ERROR(("Cannot connect to remote host"));
142 /* Set appropriate options */
143 #if defined(TCP_NODELAY)
144 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
146 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
148 SILC_LOG_DEBUG(("Connection created"));
153 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
154 socket or -1 on error. This creates non-blocking socket hence the
155 connection returns directly. To get the result of the connect() one
156 must select() the socket and read the result after it's ready. */
158 int silc_net_create_connection_async(int port, char *host)
162 struct hostent *dest;
163 struct sockaddr_in desthost;
165 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
169 dest = gethostbyname(host);
171 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
172 "IP address", host));
176 /* Set socket information */
177 memset(&desthost, 0, sizeof(desthost));
178 desthost.sin_port = htons(port);
179 desthost.sin_family = AF_INET;
180 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
182 /* Create the connection socket */
183 sock = socket(AF_INET, SOCK_STREAM, 0);
184 if (sock == INVALID_SOCKET) {
185 SILC_LOG_ERROR(("Cannot create socket"));
189 /* Connect to the host */
190 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
191 err = WSAGetLastError();
192 if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) {
193 SILC_LOG_ERROR(("Cannot connect to remote host"));
199 /* Set socket to nonblocking mode */
200 silc_net_set_socket_nonblock(sock);
202 /* Set appropriate options */
203 #if defined(TCP_NODELAY)
204 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
206 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
208 SILC_LOG_DEBUG(("Connection created"));
213 /* Closes the connection by closing the socket connection. */
215 void silc_net_close_connection(int sock)
220 /* Converts the IP number string from numbers-and-dots notation to
223 bool silc_net_addr2bin(const char *addr, unsigned char *bin,
228 ret = inet_addr(addr);
233 SILC_PUT32_LSB(ret, bin);
235 return ret != INADDR_NONE;
238 /* Set socket to non-blocking mode. */
240 int silc_net_set_socket_nonblock(int sock)
242 unsigned long on = 1;
243 return ioctlsocket(sock, FIONBIO, &on);
248 bool silc_net_win32_init(void)
250 int ret, sopt = SO_SYNCHRONOUS_NONALERT;
252 WORD ver = MAKEWORD(1, 1);
254 ret = WSAStartup(ver, &wdata);
258 /* Allow using the SOCKET's as file descriptors so that we can poll
259 them with SILC Scheduler. */
260 ret = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sopt,
268 /* Uninit Winsock2 */
270 void silc_net_win32_uninit(void)