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 /* XXX IPv6 support missing */
24 #include "silcincludes.h"
27 /* This function creates server or daemon or listener or what ever. This
28 does not fork a new process, it must be done by the caller if caller
29 wants to create a child process. This is used by the SILC server.
30 If argument `ip_addr' is NULL `any' address will be used. Returns
31 the created socket or -1 on error. */
33 int silc_net_create_server(int port, const char *ip_addr)
37 struct sockaddr_in server;
38 int len = sizeof(server.sin_addr);
40 SILC_LOG_DEBUG(("Creating a new server listener"));
42 /* Create the socket */
43 sock = socket(PF_INET, SOCK_STREAM, 0);
44 if (sock == INVALID_SOCKET) {
45 SILC_LOG_ERROR(("Cannot create socket"));
49 /* Set the socket options */
50 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
52 SILC_LOG_ERROR(("Cannot set socket options"));
57 /* Set the socket information for bind() */
58 memset(&server, 0, sizeof(server));
59 server.sin_family = AF_INET;
61 server.sin_port = htons(port);
63 /* Convert IP address to network byte order */
65 silc_net_addr2bin(ip_addr, (unsigned char *)&server.sin_addr.s_addr, len);
67 server.sin_addr.s_addr = INADDR_ANY;
69 /* Bind the server socket */
70 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
72 SILC_LOG_ERROR(("Cannot bind socket"));
77 /* Specify that we are listenning */
78 rval = listen(sock, 5);
80 SILC_LOG_ERROR(("Cannot set socket listenning"));
85 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
90 /* Closes the server by closing the socket connection. */
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(const char *local_ip, int port,
109 struct hostent *dest;
110 struct sockaddr_in desthost;
112 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
115 dest = gethostbyname(host);
117 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
118 "IP address", host));
122 /* Set socket information */
123 memset(&desthost, 0, sizeof(desthost));
124 desthost.sin_port = htons(port);
125 desthost.sin_family = AF_INET;
126 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
128 /* Create the connection socket */
129 sock = socket(AF_INET, SOCK_STREAM, 0);
130 if (sock == INVALID_SOCKET) {
131 SILC_LOG_ERROR(("Cannot create socket"));
135 /* Connect to the host */
136 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
137 err = WSAGetLastError();
138 if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) {
139 SILC_LOG_ERROR(("Cannot connect to remote host"));
145 /* Set appropriate options */
146 #if defined(TCP_NODELAY)
147 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
149 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
151 SILC_LOG_DEBUG(("Connection created"));
156 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
157 socket or -1 on error. This creates non-blocking socket hence the
158 connection returns directly. To get the result of the connect() one
159 must select() the socket and read the result after it's ready. */
161 int silc_net_create_connection_async(const char *local_ip, int port,
166 struct hostent *dest;
167 struct sockaddr_in desthost;
169 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
173 dest = gethostbyname(host);
175 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
176 "IP address", host));
180 /* Set socket information */
181 memset(&desthost, 0, sizeof(desthost));
182 desthost.sin_port = htons(port);
183 desthost.sin_family = AF_INET;
184 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
186 /* Create the connection socket */
187 sock = socket(AF_INET, SOCK_STREAM, 0);
188 if (sock == INVALID_SOCKET) {
189 SILC_LOG_ERROR(("Cannot create socket"));
193 /* Connect to the host */
194 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
195 err = WSAGetLastError();
196 if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) {
197 SILC_LOG_ERROR(("Cannot connect to remote host"));
203 /* Set socket to nonblocking mode */
204 silc_net_set_socket_nonblock(sock);
206 /* Set appropriate options */
207 #if defined(TCP_NODELAY)
208 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
210 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
212 SILC_LOG_DEBUG(("Connection created"));
217 /* Closes the connection by closing the socket connection. */
219 void silc_net_close_connection(int sock)
224 /* Converts the IP number string from numbers-and-dots notation to
227 bool silc_net_addr2bin(const char *addr, void *bin, uint32 bin_len)
231 ret = inet_addr(addr);
236 memcpy(bin, (unsigned char *)&ret, 4);
237 return ret != INADDR_NONE;
240 /* Set socket to non-blocking mode. */
242 int silc_net_set_socket_nonblock(int sock)
244 unsigned long on = 1;
245 return ioctlsocket(sock, FIONBIO, &on);
250 bool silc_net_win32_init(void)
252 int ret, sopt = SO_SYNCHRONOUS_NONALERT;
254 WORD ver = MAKEWORD(1, 1);
256 ret = WSAStartup(ver, &wdata);
260 /* Allow using the SOCKET's as file descriptors so that we can poll
261 them with SILC Scheduler. */
262 ret = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sopt,
270 /* Uninit Winsock2 */
272 void silc_net_win32_uninit(void)