updates.
[runtime.git] / lib / silcclient / silcclient_using.html
1 <br />
2 <font size="+2">Using SILC Client Library</font>
3
4 <br />&nbsp;<br />&nbsp;<br />
5 <b>1.0 Introduction</b>
6
7 <br />&nbsp;<br />
8 SILC Client library is a full featured SILC Client protocol implementation.
9 The library has been designed to be complete SILC client without actual
10 user interface.  The library provides the API for the appliation which
11 it can use to implement generally whatever user interface it wants.  The
12 SILC Client Library recides in the lib/silcclient/ directory.  It uses
13 common and core compomnent of SILC protocol from the lib/silccore, SKE
14 from lib/silcske and general utility routines from lib/silcutil.
15
16 <br />&nbsp;<br />
17 The `silcapi.h' file defines the function prototypes that application
18 must implement in order to be able to create the user interface with the
19 library.  The idea is that the application can implement whatever user
20 interface routines in the functions and display the data whatever way
21 it wants.  The library is entirely transparent to the user interface and
22 it does not include any user interface specific issues such as window
23 handling or item handling on the screen etc.  These does not interest
24 the library.  The `silcapi.h' also defines the client libary interface
25 the application can call.  The interface includes for example functions
26 for sending channel and private messages, client and channel retrieval
27 and other utility functions.
28
29 <br />&nbsp;<br />&nbsp;<br />
30 <b>1.0.1 Including Library Headers</b>
31
32 <br />&nbsp;<br />
33 Your application must include the following includes in your sources to 
34 get access all SILC Client Library routines:
35
36 <br />&nbsp;<br />
37 <tt>
38 #include "silcincludes.h"<br />
39 #include "clientlibincludes.h"
40 </tt>
41
42 <br />&nbsp;<br />
43 If you are compiling with C++ compiler then you need to include the 
44 headers as follows:
45
46 <br />&nbsp;<br />
47 <tt>
48 extern "C" {<br />
49 #include "silcincludes.h"<br />
50 #include "clientlibincludes.h"<br &/>
51 }
52 </tt>
53
54
55 <br />&nbsp;<br />&nbsp;<br />
56 <b>1.1 Creating Client</b>
57
58 <br />&nbsp;<br />
59 The client is context or entity based, so several client entitites can
60 be created in the application if needed.  However, it should be noted
61 that they are completely independent from each other and can be seen
62 as different applications.  Usually only one client entity is needed
63 per application.
64
65 <br />&nbsp;<br />
66 The client object is SilcClient which is usually allocated in following
67 manner:
68
69 <br />&nbsp;<br />
70 <tt>&nbsp;&nbsp;SilcClient client = silc_client_alloc(&ops, params, context, version);</tt>
71
72 <br />&nbsp;<br />
73 `ops' is the static structure of client operations that library will call.
74 `context' can be some application specific context that will be saved into
75 the SilcClient object.  It is up to the caller to free this context.
76 SilcClient is always passed to the application thus the application
77 specific context can be retrieved from the SilcClient object.  See 
78 `client.h' file for detailed definition of SilcClient object.
79
80 <br />&nbsp;<br />
81 `ops' can be defined for example as follows:
82
83 <br />&nbsp;<br />
84 <tt>
85 SilcClientOperations ops = {<br />
86 &nbsp;&nbsp;  silc_say,<br />
87 &nbsp;&nbsp;  silc_channel_message,<br />
88 &nbsp;&nbsp;  silc_private_message,<br />
89 &nbsp;&nbsp;  silc_notify,<br />
90 &nbsp;&nbsp;  silc_command,<br />
91 &nbsp;&nbsp;  silc_command_reply,<br />
92 &nbsp;&nbsp;  silc_connect,<br />
93 &nbsp;&nbsp;  silc_disconnect,<br />
94 &nbsp;&nbsp;  silc_get_auth_method,<br />
95 &nbsp;&nbsp;  silc_verify_public_key,<br />
96 &nbsp;&nbsp;  silc_ask_passphrase,<br />
97 &nbsp;&nbsp;  silc_failure,<br />
98 &nbsp;&nbsp;  silc_key_agreement,<br />
99 };<br />
100 </tt>
101
102 <br />&nbsp;<br />
103 Please see the `client_ops_example.c' source file in lib/silcclient/
104 directory for predefined structure and stub functions for your
105 convenience.  It is provided for programmers so that they can copy
106 it and use it directly in their application.
107
108
109 <br />&nbsp;<br />&nbsp;<br />
110 <b>1.2 Initializing the Client</b>
111
112 <br />&nbsp;<br />
113 The client must be initialized before running.  However, there are also
114 some other tasks that must be done before initializing the client.
115 The following pointers must be set by the application  before calling
116 the initializing function:
117
118 <br />&nbsp;<br />
119 <tt>
120 &nbsp;&nbsp;client->username<br />
121 &nbsp;&nbsp;client->hostname<br />
122 &nbsp;&nbsp;client->realname<br />
123 &nbsp;&nbsp;client->pkcs<br />
124 &nbsp;&nbsp;client->public_key<br />
125 &nbsp;&nbsp;client->private_key
126 </tt>
127
128 <br />&nbsp;<br />
129 You may also set client->nickname if you want.  If it is set then the
130 library will change the nickname to that one after the client is connected
131 to the server.  If not set, then server will initially give the nickname
132 which is same as the username.
133
134 <br />&nbsp;<br />
135 After setting the pointers one must call:
136
137 <br />&nbsp;<br />
138 <tt>&nbsp;&nbsp;silc_client_init(client);</tt>
139
140 <br />&nbsp;<br />
141 which then initializes the client library for the `client'.  If the
142 pointers mentioned above are not initialized the silc_client_init will
143 fail.  The application should check the return value of the silc_client_init
144 function.
145
146
147 <br />&nbsp;<br />&nbsp;<br />
148 <b>1.3 Running the Client</b>
149
150 <br />&nbsp;<br />
151 The client is run by calling silc_client_run.  The function will call
152 the scheduler from utility library that will be run until the program is
153 ended.  When silc_client_run returns the application is ended.  Thus,
154 to run the client, call:
155
156 <br />&nbsp;<br />
157 <tt>&nbsp;&nbsp;silc_client_run(client);</tt>
158
159 <br />&nbsp;<br />
160 Usually application may do some other initializations before calling
161 this function.  For example before calling this function application
162 should initialize the user interface.
163
164
165 <br />&nbsp;<br />&nbsp;<br />
166 <b>1.3.1 Running the Client in GUI application</b>
167
168 <br />&nbsp;<br />
169 Many GUI applications has their own main loop or event loop, which they 
170 would like to use or are forced to use by the underlaying system.  If you 
171 are developing for example GUI application on Unix system, and you are 
172 using GTK+ or QT as GUI library you would probably like to use their own 
173 main loop.  SILC Client can be run under external main loop as well.  The 
174 interface provides a function silc_client_run_one which will run the 
175 client library once, and returns immediately.  During that running it can 
176 process incoming data and send outgoing data, but it is guaranteed that it 
177 will not block the calling process.
178
179 <br />&nbsp;<br />
180 It is suggested that you would call this function as many times in a 
181 second as possible to provide smooth action for the client library.  You 
182 can use an timeout task, or an idle task provided by your GUI library to 
183 accomplish this.  After you have initialized the client library with 
184 silc_client_init, you should register the timeout task or idle task that 
185 will call the silc_client_run_one periodically.  In the Toolkit package 
186 there is GTK-- GUI example in silcer/ directory.  That example calls the 
187 silc_client_run_one every 50 milliseconds, and it should be sufficient for 
188 smooth working.
189
190 <br />&nbsp;<br />
191 For Win32 the silc_client_run can be used instead of using the Windows's 
192 own event loop.  However, if you would like to use the silc_client_run_one 
193 also on Win32 systems it is possible.
194
195
196 <br />&nbsp;<br />&nbsp;<br />
197 <b>1.3.1.1 Running Client in GTK--</b>
198
199 <br />&nbsp;<br />
200 Here is a short example how to run the SILC Client libary under the 
201 Gnome/GTK--'s main loop:
202
203 <br />&nbsp;<br />
204 <tt>
205 gint YourClass::silc_scheduler()<br />
206 {<br />
207 &nbsp;&nbsp;  // Run the SILC client once, and return immediately.  This function<br />
208 &nbsp;&nbsp;  // is called every 50 milliseconds by the Gnome main loop, to process<br />
209 &nbsp;&nbsp;  // SILC stuff.  This function will read data, and write data to network,<br />
210 &nbsp;&nbsp;  // etc.  Makes the client library tick! :)<br />
211 &nbsp;&nbsp;  silc_client_run_one(silc_client);<br />
212 &nbsp;&nbsp;  return 1;<br />
213 }<br />
214 </tt>
215
216 <br />&nbsp;<br />
217 then, during initialization of the SILC Client call:
218
219 <br />&nbsp;<br />
220 <tt>
221 // Setup SILC scheduler as timeout task. This will handle the SILC<br />
222 // client library every 50 milliseconds.  It will actually make the<br />
223 // SILC client work on background.<br />
224 Gnome::Main::timeout.connect(slot(this, &YourClass::silc_scheduler), 50);<br />
225 </tt>
226
227 <br />&nbsp;<br />
228 This will call the function silc_scheduler every 50 millisecconds, which 
229 on the otherhand will call silc_client_run_one, which will make the SILC 
230 Client library work on the background of the GUI application.
231
232
233 <br />&nbsp;<br />&nbsp;<br />
234 <b>1.4 Creating Connection to Server</b>
235
236 <br />&nbsp;<br />
237 Connection to remote SILC server is done by calling:
238
239 <br />&nbsp;<br />
240 <tt>&nbsp;&nbsp;silc_client_connect_to_server(client, port, hostname, context);</tt>
241
242 <br />&nbsp;<br />
243 The function will create the connection asynchronously to the server, ie.
244 the function will return before the actual connection is created.  After
245 the connection is created the client->ops->connect operation is called.
246
247 <br />&nbsp;<br />
248 Generally speaking the connections are associated with windows' on the
249 screen.  IRC is usually implemented this way, however it is not the
250 necessary way to associate the client's connections.  SilcClientConnection
251 object is provided by the library (and is always passed to the application)
252 that can be used in the application to associate the connection from the
253 library.  Application specific context can be saved to the 
254 SilcClientConnection object which then can be retrieved in the application,
255 thus perhaps associate the connection with what ever object in 
256 application (window or something else).
257
258
259 <br />&nbsp;<br />&nbsp;<br />
260 <b>1.4.1 Using Own Connecting</b>
261
262 <br />&nbsp;<br />
263 Application might not want to use silc_client_connect_to_server function
264 if it wants to perform its own connecting for some reason.  In this case
265 application must call function silc_client_start_key_exchange after it
266 has created the connection by itself.  This function starts the key
267 exhange protocol between the client and server and the library takes care
268 of everything after that.
269
270 <br />&nbsp;<br />
271 After connection has been created application must call:
272
273 <br />&nbsp;<br />
274 <tt>
275 &nbsp;&nbsp;SilcClientConnection conn;
276
277 <br />&nbsp;<br />
278 &nbsp;&nbsp;/* Add new connection to client */<br />
279 &nbsp;&nbsp;conn = silc_client_add_connection(client, hostname, port, context);
280
281 <br />&nbsp;<br />
282 &nbsp;&nbsp;/* Start key exchange and let the library handle everything<br />
283 &nbsp;&nbsp;   after this point on. */<br />
284 &nbsp;&nbsp;silc_client_start_key_exchange(client, conn, sock);
285 </tt>
286
287 <br />&nbsp;<br />
288 NOTE: These calls are performed only and only if application did not call
289 silc_client_connect_to_server function, but performed the connecting 
290 process manually.
291
292
293 <br />&nbsp;<br />&nbsp;<br />
294 <b>1.5 Example Client</b>
295
296 <br />&nbsp;<br />
297 This section includes an example SILC client implementation in pseudo-like
298 C code.  It creates and initializes the client and sets up an imaginary
299 user interface.  The user will use the user interface then to create
300 the connections.  The SilcClientOperations are expected to be implemented.
301
302 <br />&nbsp;<br />
303 <pre>
304 #include "silcincludes.h"
305 #include "silcapi.h"
306
307 int main()
308 {
309         SilcClientOperations ops = {
310           silc_say,
311           silc_channel_message,
312           silc_private_message,
313           silc_notify,
314           silc_command,
315           silc_command_reply,
316           silc_connect,
317           silc_disconnect,
318           silc_get_auth_method,
319           silc_verify_public_key,
320           silc_ask_passphrase,
321           silc_failure,
322           silc_key_agreement,
323         };
324
325         SilcClient client;
326
327         /* Allocate SILC client. The `silc_version_string' is defined
328            in includes/version.h file. */
329         client = silc_client_alloc(&ops, NULL, silc_version_string);
330
331         /* Register default ciphers, pkcs, hash funtions and hmacs. */
332         silc_cipher_register_default();
333         silc_pkcs_register_default();
334         silc_hash_register_default();
335         silc_hmac_register_default();
336
337         /* Set the mandatory pointers, read public and private key from
338            files (or somewhere) and return pointers and PKCS context. */
339         client->username = silc_get_username();
340         client->hostname = silc_net_localhost();
341         client->realname = silc_get_real_name();
342         client->pkcs = get_public_and_private_key(&client->public_key,
343                                                   &client->private_key);
344
345         /* If the keys does not exist, create a key pair since we must
346            provide key pair to the library. */
347         if (!client->pkcs)
348           generate_key_new_key_pair(client);
349
350         /* Iinitialize client */
351         if (!silc_client_init(client))
352           fatal_error("Could not initialize client");
353
354         /* Initialize user interface. The user interface can be generally
355            initialized at any phase, including before actually allocating
356            and initializing the client, if wished. */
357         InitUserInterface();
358         DoCoolThings();
359
360         /* Start the client. This will start the scheduler. At this phase
361            the user might have the user interface in front of him already.
362            He will use the user interface to create the connection to the
363            server for example. When this function returns the program is 
364           ended. */
365         silc_client_run(client);
366
367         /* Client is ended */
368         return 0;
369 }
370 </pre>