author | hallski <hallski> |
Mon, 08 Nov 2004 13:21:52 +0000 | |
changeset 104 | f43fde0f89d4 |
parent 99 | 81e9d035de02 |
child 121 | 612603ba6d90 |
permissions | -rw-r--r-- |
83 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
99
81e9d035de02
2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
83
diff
changeset
|
3 |
* Copyright (C) 2003 Imendio AB |
83 | 4 |
* Copyright (C) 2003 Mikael Hallendal <micke@imendio.com> |
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Lesser General Public License as |
|
8 |
* published by the Free Software Foundation; either version 2 of the |
|
9 |
* License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Lesser General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Lesser General Public |
|
17 |
* License along with this program; if not, write to the |
|
18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
19 |
* Boston, MA 02111-1307, USA. |
|
20 |
*/ |
|
21 |
||
22 |
#include <config.h> |
|
23 |
||
24 |
#include <glib.h> |
|
25 |
#include <string.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <loudmouth/loudmouth.h> |
|
28 |
#ifdef __WIN32__ |
|
29 |
#include <winsock2.h> |
|
30 |
#endif |
|
31 |
||
32 |
typedef struct { |
|
33 |
gchar *name; |
|
34 |
gchar *passwd; |
|
35 |
} UserInfo; |
|
36 |
||
37 |
static void |
|
38 |
free_user_info (UserInfo *info) |
|
39 |
{ |
|
40 |
g_free (info->name); |
|
41 |
g_free (info->passwd); |
|
42 |
||
43 |
g_free (info); |
|
44 |
} |
|
45 |
||
46 |
static unsigned char expected_fingerprint[20]; |
|
47 |
||
48 |
static void |
|
49 |
print_finger (const unsigned char *fpr, unsigned int size) |
|
50 |
{ |
|
51 |
gint i; |
|
52 |
for (i = 0; i < size-1; i++) |
|
53 |
g_print ("%02X:", (unsigned char) fpr[i]); |
|
54 |
g_print ("%02X", (unsigned char) fpr[size-1]); |
|
55 |
} |
|
56 |
||
57 |
static LmSSLResponse |
|
58 |
ssl_cb (LmSSL *ssl, LmSSLStatus status, gpointer ud) |
|
59 |
{ |
|
60 |
g_print ("SSL status: %d\n", status); |
|
61 |
switch (status) { |
|
62 |
case LM_SSL_STATUS_NO_CERT_FOUND: |
|
63 |
g_print ("No certificate found!\n"); |
|
64 |
break; |
|
65 |
case LM_SSL_STATUS_UNTRUSTED_CERT: |
|
66 |
g_print ("Certificate is not trusted!\n"); |
|
67 |
break; |
|
68 |
case LM_SSL_STATUS_CERT_EXPIRED: |
|
69 |
g_print ("Certificate has expired!\n"); |
|
70 |
break; |
|
71 |
case LM_SSL_STATUS_CERT_NOT_ACTIVATED: |
|
72 |
g_print ("Certificate has not been activated!\n"); |
|
73 |
break; |
|
74 |
case LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH: |
|
75 |
g_print ("Certificate hostname does not match expected hostname!\n"); |
|
76 |
break; |
|
77 |
case LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH: { |
|
78 |
const unsigned char *fpr = lm_ssl_get_fingerprint (ssl); |
|
79 |
g_print ("Certificate fingerprint does not match expected fingerprint!\n"); |
|
80 |
g_print ("Remote fingerprint: "); |
|
81 |
print_finger (fpr, 16); |
|
82 |
g_print ("\nExpected fingerprint: "); |
|
83 |
print_finger (expected_fingerprint, 16); |
|
84 |
g_print ("\n"); |
|
85 |
break; |
|
86 |
} |
|
87 |
case LM_SSL_STATUS_GENERIC_ERROR: |
|
88 |
g_print ("Generic SSL error!\n"); |
|
89 |
break; |
|
90 |
} |
|
91 |
||
92 |
return LM_SSL_RESPONSE_CONTINUE; |
|
93 |
} |
|
94 |
||
95 |
static void |
|
96 |
authentication_cb (LmConnection *connection, gboolean result, gpointer ud) |
|
97 |
{ |
|
98 |
g_print ("Auth: %d\n", result); |
|
99 |
||
100 |
if (result == TRUE) { |
|
101 |
LmMessage *m; |
|
102 |
||
103 |
m = lm_message_new_with_sub_type (NULL, |
|
104 |
LM_MESSAGE_TYPE_PRESENCE, |
|
105 |
LM_MESSAGE_SUB_TYPE_AVAILABLE); |
|
106 |
g_print (":: %s\n", lm_message_node_to_string (m->node)); |
|
107 |
||
108 |
lm_connection_send (connection, m, NULL); |
|
109 |
lm_message_unref (m); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
static void |
|
114 |
connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) |
|
115 |
{ |
|
116 |
g_print ("Connected callback\n"); |
|
117 |
lm_connection_authenticate (connection, |
|
118 |
info->name, info->passwd, "TestLM", |
|
119 |
authentication_cb, NULL,FALSE, NULL); |
|
120 |
g_print ("Sent auth message\n"); |
|
121 |
} |
|
122 |
||
123 |
static LmHandlerResult |
|
124 |
handle_messages (LmMessageHandler *handler, |
|
125 |
LmConnection *connection, |
|
126 |
LmMessage *m, |
|
127 |
gpointer user_data) |
|
128 |
{ |
|
129 |
g_print ("Incoming message from: %s\n", |
|
130 |
lm_message_node_get_attribute (m->node, "from")); |
|
131 |
||
132 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
133 |
} |
|
134 |
||
135 |
int |
|
136 |
main (int argc, char **argv) |
|
137 |
{ |
|
138 |
GMainLoop *main_loop; |
|
139 |
LmConnection *connection; |
|
140 |
LmMessageHandler *handler; |
|
141 |
gboolean result; |
|
142 |
UserInfo *info; |
|
143 |
#ifdef __WIN32__ |
|
144 |
WORD wVersionRequested; |
|
145 |
WSADATA wsaData; |
|
146 |
int err; |
|
147 |
#endif |
|
148 |
||
149 |
if (argc < 4) { |
|
150 |
g_print ("Usage: test-lm <server> <username> <password>\n" |
|
151 |
" test-lm <server> <username> <password> <fingerprint>\n"); |
|
152 |
return 1; |
|
153 |
} |
|
154 |
||
155 |
/* Needed to build on Win32. */ |
|
156 |
#ifdef __WIN32__ |
|
157 |
wVersionRequested = MAKEWORD( 2, 2 ); |
|
158 |
||
159 |
err = WSAStartup( wVersionRequested, &wsaData ); |
|
160 |
if ( err != 0 ) { |
|
161 |
/* Tell the user that we could not find a usable */ |
|
162 |
/* WinSock DLL. */ |
|
163 |
return; |
|
164 |
} |
|
165 |
||
166 |
/* Confirm that the WinSock DLL supports 2.2.*/ |
|
167 |
/* Note that if the DLL supports versions greater */ |
|
168 |
/* than 2.2 in addition to 2.2, it will still return */ |
|
169 |
/* 2.2 in wVersion since that is the version we */ |
|
170 |
/* requested. */ |
|
171 |
||
172 |
if ( LOBYTE( wsaData.wVersion ) != 2 || |
|
173 |
HIBYTE( wsaData.wVersion ) != 2 ) { |
|
174 |
/* Tell the user that we could not find a usable */ |
|
175 |
/* WinSock DLL. */ |
|
176 |
WSACleanup( ); |
|
177 |
return; |
|
178 |
} |
|
179 |
#endif |
|
180 |
||
181 |
connection = lm_connection_new (argv[1]); |
|
182 |
||
183 |
if (argc > 4 && !lm_ssl_is_supported ()) { |
|
184 |
g_error ("No SSL support!"); |
|
185 |
exit (1); |
|
186 |
} |
|
187 |
||
188 |
||
189 |
handler = lm_message_handler_new (handle_messages, NULL, NULL); |
|
190 |
lm_connection_register_message_handler (connection, handler, |
|
191 |
LM_MESSAGE_TYPE_MESSAGE, |
|
192 |
LM_HANDLER_PRIORITY_NORMAL); |
|
193 |
||
194 |
lm_message_handler_unref (handler); |
|
195 |
||
196 |
info = g_new0 (UserInfo, 1); |
|
197 |
info->name = g_strdup (argv[2]); |
|
198 |
info->passwd = g_strdup (argv[3]); |
|
199 |
||
200 |
if (argc > 4) { |
|
201 |
int i; |
|
202 |
char *p; |
|
203 |
LmSSL *ssl; |
|
204 |
||
205 |
lm_connection_set_port (connection, |
|
206 |
LM_CONNECTION_DEFAULT_PORT_SSL); |
|
207 |
||
208 |
for (i = 0, p = argv[4]; *p && *(p+1); i++, p += 3) |
|
209 |
expected_fingerprint[i] = (unsigned char) g_ascii_strtoull (p, NULL, 16); |
|
210 |
||
211 |
ssl = lm_ssl_new (expected_fingerprint, |
|
212 |
(LmSSLFunction) ssl_cb, |
|
213 |
info, |
|
214 |
(GDestroyNotify) free_user_info); |
|
215 |
||
216 |
lm_connection_set_ssl (connection, ssl); |
|
217 |
||
218 |
lm_ssl_unref (ssl); |
|
219 |
} |
|
220 |
||
221 |
result = lm_connection_open (connection, |
|
222 |
(LmResultFunction) connection_open_cb, |
|
223 |
info, NULL, NULL); |
|
224 |
||
225 |
if (!result) { |
|
226 |
g_print ("Opening connection failed: %d\n", result); |
|
227 |
} else { |
|
228 |
g_print ("Returned from the connection_open\n"); |
|
229 |
} |
|
230 |
||
231 |
main_loop = g_main_loop_new (NULL, FALSE); |
|
232 |
g_main_loop_run (main_loop); |
|
233 |
||
234 |
return 0; |
|
235 |
} |