Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcclient / client_listener.c
1 /*
2
3   client_listener.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 - 2008 Pekka Riikonen
8
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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcclient.h"
22 #include "client_internal.h"
23
24 /************************** Types and definitions ***************************/
25
26 /* Listener context */
27 struct SilcClientListenerStruct {
28   SilcClient client;                      /* Client */
29   SilcSchedule schedule;                  /* Scheduler */
30   SilcClientConnectCallback callback;     /* Connection callback */
31   void *context;                          /* User context */
32   SilcClientConnectionParams params;      /* Connection parameters */
33   SilcPublicKey public_key;               /* Responder public key */
34   SilcPrivateKey private_key;             /* Responder private key */
35   SilcNetListener tcp_listener;           /* TCP listener */
36   SilcPacketStream udp_listener;          /* UDP listener */
37 };
38
39 /************************ Static utility functions **************************/
40
41 /* Called after application has verified remote host's public key. */
42
43 static void silc_client_listener_verify_key_cb(SilcBool success, void *context)
44 {
45   SilcVerifyKeyContext verify = context;
46
47   /* Call the completion callback back to the SKE */
48   verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK :
49                      SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
50                      verify->completion_context);
51
52   silc_free(verify);
53 }
54
55 /* Verify remote host's public key. */
56
57 static void
58 silc_client_listener_verify_key(SilcSKE ske,
59                                 SilcPublicKey public_key,
60                                 void *context,
61                                 SilcSKEVerifyCbCompletion completion,
62                                 void *completion_context)
63 {
64   SilcClientConnection conn = context;
65   SilcClient client = conn->client;
66   SilcVerifyKeyContext verify;
67
68   /* If we provided repository for SKE and we got here the key was not
69      found from the repository. */
70   if (conn->internal->params.repository &&
71       !conn->internal->params.verify_notfound) {
72     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
73                completion_context);
74     return;
75   }
76
77   SILC_LOG_DEBUG(("Verify remote public key"));
78
79   verify = silc_calloc(1, sizeof(*verify));
80   if (!verify) {
81     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
82                completion_context);
83     return;
84   }
85   verify->ske = ske;
86   verify->completion = completion;
87   verify->completion_context = completion_context;
88
89   /* Verify public key in application */
90   client->internal->ops->verify_public_key(client, conn,
91                                            SILC_CONN_CLIENT, public_key,
92                                            silc_client_listener_verify_key_cb,
93                                            verify);
94 }
95
96 /* Key exchange protocol completion callback. */
97
98 static void silc_client_listener_completion(SilcSKE ske,
99                                             SilcSKEStatus status,
100                                             SilcSKESecurityProperties prop,
101                                             SilcSKEKeyMaterial keymat,
102                                             SilcSKERekeyMaterial rekey,
103                                             void *context)
104 {
105   SilcClientConnection conn = context;
106   SilcCipher send_key, receive_key;
107   SilcHmac hmac_send, hmac_receive;
108
109   SILC_LOG_DEBUG(("Key exchange completed"));
110
111   if (status != SILC_SKE_STATUS_OK) {
112     /* Key exchange failed */
113     conn->callback(conn->client, conn,
114                    status == SILC_SKE_STATUS_TIMEOUT ?
115                    SILC_CLIENT_CONN_ERROR_TIMEOUT :
116                    SILC_CLIENT_CONN_ERROR_KE, conn->internal->error,
117                    conn->internal->disconnect_message,
118                    conn->callback_context);
119     return;
120   }
121
122   /* Allocate the cipher and HMAC contexts */
123   if (!silc_ske_set_keys(ske, keymat, prop, &send_key, &receive_key,
124                          &hmac_send, &hmac_receive, &conn->internal->hash)) {
125     conn->callback(conn->client, conn,
126                    SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
127                    conn->callback_context);
128     return;
129   }
130
131   /* Set the keys into the packet stream.  After this call packets will be
132      encrypted with these keys. */
133   if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
134                             hmac_receive, FALSE)) {
135     conn->callback(conn->client, conn,
136                    SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
137                    conn->callback_context);
138     return;
139   }
140
141   /* Key exchange successful */
142   conn->callback(conn->client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
143                  conn->callback_context);
144 }
145
146 /* Starts key agreement as responder. */
147
148 static void
149 silc_client_listener_new_connection(SilcClientListener listener,
150                                     SilcPacketStream stream)
151 {
152   SilcClient client = listener->client;
153   SilcClientConnection conn;
154   SilcSKEParamsStruct params;
155   const char *hostname = NULL, *ip = NULL;
156   SilcUInt16 port;
157
158   /* Get remote information */
159   silc_socket_stream_get_info(silc_packet_stream_get_stream(stream),
160                               NULL, &hostname, &ip, &port);
161   if (!ip || !port) {
162     listener->callback(client, NULL, SILC_CLIENT_CONN_ERROR, 0, NULL,
163                        listener->context);
164     silc_packet_stream_destroy(stream);
165     return;
166   }
167   if (!hostname)
168     hostname = ip;
169
170   /* Add new connection */
171   conn = silc_client_add_connection(client, SILC_CONN_CLIENT, FALSE,
172                                     &listener->params,
173                                     listener->public_key,
174                                     listener->private_key,
175                                     (char *)hostname, port,
176                                     listener->callback, listener->context);
177   if (!conn) {
178     listener->callback(client, NULL, SILC_CLIENT_CONN_ERROR, 0, NULL,
179                        listener->context);
180     silc_packet_stream_destroy(stream);
181     return;
182   }
183   conn->stream = stream;
184   conn->socket_stream = silc_packet_stream_get_stream(stream);
185   silc_socket_stream_get_info(conn->socket_stream, &conn->sock, NULL,
186                               NULL, NULL);
187   conn->internal->schedule = listener->schedule;
188   silc_packet_set_context(conn->stream, conn);
189
190   SILC_LOG_DEBUG(("Processing new incoming connection %p", conn));
191
192   /* Allocate SKE */
193   conn->internal->ske =
194     silc_ske_alloc(client->rng, conn->internal->schedule,
195                    listener->params.repository, listener->public_key,
196                    listener->private_key, listener);
197   if (!conn->internal->ske) {
198     conn->callback(conn->client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
199                    conn->callback_context);
200     return;
201   }
202
203   /* Set SKE parameters */
204   params.version = client->internal->silc_client_version;
205   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
206   if (listener->params.udp) {
207     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
208     params.session_port = listener->params.local_port;
209   }
210
211   silc_ske_set_callbacks(conn->internal->ske, silc_client_listener_verify_key,
212                          silc_client_listener_completion, conn);
213
214   /* Start key exchange as responder */
215   conn->internal->op = silc_ske_responder(conn->internal->ske,
216                                           conn->stream, &params);
217   if (!conn->internal->op)
218     conn->callback(conn->client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
219                    conn->callback_context);
220 }
221
222 /* TCP network listener callback.  Accepts new key agreement connection.
223    Responder function. */
224
225 static void silc_client_listener_tcp_accept(SilcResult status,
226                                             SilcStream stream,
227                                             void *context)
228 {
229   SilcClientListener listener = context;
230   SilcPacketStream packet_stream;
231
232   SILC_LOG_DEBUG(("New incoming TCP connection"));
233
234   /* Create packet stream */
235   packet_stream =
236     silc_packet_stream_create(listener->client->internal->packet_engine,
237                               listener->schedule, stream);
238   if (!packet_stream) {
239     silc_stream_destroy(stream);
240     return;
241   }
242
243   /* Process session */
244   silc_client_listener_new_connection(listener, packet_stream);
245 }
246
247 /* UDP network listener callback.  Accepts new key agreement session.
248    Responder function. */
249
250 static SilcBool silc_client_udp_accept(SilcPacketEngine engine,
251                                        SilcPacketStream stream,
252                                        SilcPacket packet,
253                                        void *callback_context,
254                                        void *stream_context)
255 {
256   SilcClientListener listener = callback_context;
257   SilcPacketStream packet_stream;
258   SilcUInt16 port;
259   const char *ip;
260
261   SILC_LOG_DEBUG(("New incoming UDP connection"));
262
263   /* We want only key exchange packet.  Eat other packets so that default
264      packet callback doesn't get them. */
265   if (packet->type != SILC_PACKET_KEY_EXCHANGE) {
266     silc_packet_free(packet);
267     return TRUE;
268   }
269
270   /* Create packet stream for this remote UDP session */
271   if (!silc_packet_get_sender(packet, &ip, &port)) {
272     silc_packet_free(packet);
273     return TRUE;
274   }
275   packet_stream = silc_packet_stream_add_remote(stream, ip, port, packet);
276   if (!packet_stream) {
277     silc_packet_free(packet);
278     return TRUE;
279   }
280
281   /* Process session */
282   silc_client_listener_new_connection(listener, packet_stream);
283   return TRUE;
284 }
285
286 /* Packet stream callbacks */
287 static SilcPacketCallbacks silc_client_listener_stream_cb =
288 {
289   silc_client_udp_accept, NULL, NULL
290 };
291
292 /***************************** Listner routines *****************************/
293
294 /* Adds network listener.  The `callback' will be called after new conection
295    has arrived and key exchange protocol has been completed. */
296
297 SilcClientListener
298 silc_client_listener_add(SilcClient client,
299                          SilcSchedule schedule,
300                          SilcClientConnectionParams *params,
301                          SilcPublicKey public_key,
302                          SilcPrivateKey private_key,
303                          SilcClientConnectCallback callback,
304                          void *context)
305 {
306   SilcClientListener listener;
307   SilcStream stream;
308
309   if (!client || !schedule ||
310       !params || (!params->local_ip && !params->bind_ip))
311     return NULL;
312
313   SILC_LOG_DEBUG(("Adding new listener"));
314
315   listener = silc_calloc(1, sizeof(*listener));
316   if (!listener)
317     return NULL;
318   listener->client = client;
319   listener->schedule = schedule;
320   listener->callback = callback;
321   listener->context = context;
322   listener->params = *params;
323   listener->public_key = public_key;
324   listener->private_key = private_key;
325
326   /* Create network listener */
327   if (params->udp) {
328     /* UDP listener */
329     stream = silc_net_udp_connect(params->bind_ip ? params->bind_ip :
330                                   params->local_ip, params->local_port,
331                                   NULL, 0, schedule);
332     listener->udp_listener =
333       silc_packet_stream_create(client->internal->packet_engine,
334                                 schedule, stream);
335     if (!listener->udp_listener) {
336       client->internal->ops->say(
337                      client, NULL, SILC_CLIENT_MESSAGE_ERROR,
338                      "Cannot create UDP listener on %s on port %d: %s",
339                      params->bind_ip ? params->bind_ip :
340                      params->local_ip, params->local_port,
341                      silc_errno_string(silc_errno));
342       silc_client_listener_free(listener);
343       if (stream)
344         silc_stream_destroy(stream);
345       return NULL;
346     }
347     silc_packet_stream_link(listener->udp_listener,
348                             &silc_client_listener_stream_cb, listener,
349                             1000000, SILC_PACKET_ANY, -1);
350
351     if (!params->local_port) {
352       /* Get listener port */
353       SilcSocket sock;
354       silc_socket_stream_get_info(stream, &sock, NULL, NULL, NULL);
355       listener->params.local_port = silc_net_get_local_port(sock);
356     }
357   } else {
358     /* TCP listener */
359     listener->tcp_listener =
360       silc_net_tcp_create_listener(params->bind_ip ?
361                                    (const char **)&params->bind_ip :
362                                    (const char **)&params->local_ip,
363                                    1, params->local_port, TRUE, FALSE,
364                                    schedule, silc_client_listener_tcp_accept,
365                                    listener);
366     if (!listener->tcp_listener) {
367       client->internal->ops->say(
368                      client, NULL, SILC_CLIENT_MESSAGE_ERROR,
369                      "Cannot create listener on %s on port %d: %s",
370                      params->bind_ip ? params->bind_ip :
371                      params->local_ip, params->local_port,
372                      silc_errno_string(silc_errno));
373
374       silc_client_listener_free(listener);
375       return NULL;
376     }
377
378     if (!params->local_port) {
379       /* Get listener port */
380       SilcUInt16 *ports;
381       ports = silc_net_listener_get_port(listener->tcp_listener, NULL);
382       listener->params.local_port = ports[0];
383       silc_free(ports);
384     }
385   }
386
387   SILC_LOG_DEBUG(("Bound listener to %s:%d",
388                   params->bind_ip ? params->bind_ip : params->local_ip,
389                   listener->params.local_port));
390
391   return listener;
392 }
393
394 /* Close and free listner */
395
396 void silc_client_listener_free(SilcClientListener listener)
397 {
398   if (listener->tcp_listener)
399     silc_net_close_listener(listener->tcp_listener);
400   silc_packet_stream_destroy(listener->udp_listener);
401   silc_free(listener);
402 }
403
404 /* Return listner bound port */
405
406 SilcUInt16 silc_client_listener_get_local_port(SilcClientListener listener)
407 {
408   return listener->params.local_port;
409 }