|
13870
|
1 /**
|
|
|
2 * The QQ2003C protocol plugin
|
|
|
3 *
|
|
|
4 * for gaim
|
|
|
5 *
|
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
|
7 *
|
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
|
11 * (at your option) any later version.
|
|
|
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 * You should have received a copy of the GNU General Public License
|
|
|
19 * along with this program; if not, write to the Free Software
|
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
21 *
|
|
|
22 * This code is based on
|
|
|
23 * * qqwry.c, by lvxiang <srm2003@163.com>
|
|
|
24 * * IpChecker.m, by Jeff_Ye
|
|
|
25 */
|
|
|
26
|
|
|
27 // START OF FILE
|
|
|
28 /*****************************************************************************/
|
|
|
29 #include "internal.h"
|
|
|
30 #include <string.h> // memset
|
|
|
31 #include "debug.h"
|
|
|
32 #include "prefs.h" // gaim_prefs_get_string
|
|
|
33
|
|
|
34 #include "utils.h"
|
|
|
35 #include "ip_location.h"
|
|
|
36
|
|
|
37 #define DEFAULT_IP_LOCATION_FILE "gaim/QQWry.dat"
|
|
|
38
|
|
|
39 typedef struct _ip_finder ip_finder;
|
|
|
40
|
|
|
41 // all offset is based the begining of the file
|
|
|
42 struct _ip_finder {
|
|
|
43 guint32 offset_first_start_ip; // first abs offset of start ip
|
|
|
44 guint32 offset_last_start_ip; // last abs offset of start ip
|
|
|
45 guint32 cur_start_ip; // start ip of current search range
|
|
|
46 guint32 cur_end_ip; // end ip of current search range
|
|
|
47 guint32 offset_cur_end_ip; // where is the current end ip saved
|
|
|
48 GIOChannel *io; // IO Channel to read file
|
|
|
49 }; // struct _ip_finder
|
|
|
50
|
|
|
51 /*****************************************************************************/
|
|
|
52 // convert 1-4 bytes array to integer.
|
|
|
53 // Small endian (higher bytes in lower place)
|
|
|
54 static guint32 _byte_array_to_int(guint8 * ip, gint count)
|
|
|
55 {
|
|
|
56 guint32 ret, i;
|
|
|
57 g_return_val_if_fail(count >= 1 && count <= 4, 0);
|
|
|
58 ret = ip[0];
|
|
|
59 for (i = 0; i < count; i++)
|
|
|
60 ret |= ((guint32) ip[i]) << (8 * i);
|
|
|
61 return ret;
|
|
|
62 } // _byte_array_to_int
|
|
|
63
|
|
|
64 /*****************************************************************************/
|
|
|
65 // read len of bytes to buf, from io at offset
|
|
|
66 static void _read_from(GIOChannel * io, guint32 offset, guint8 * buf, gint len)
|
|
|
67 {
|
|
|
68 GError *err;
|
|
|
69 GIOStatus status;
|
|
|
70
|
|
|
71 err = NULL;
|
|
|
72 status = g_io_channel_seek_position(io, offset, G_SEEK_SET, &err);
|
|
|
73 if (err != NULL) {
|
|
|
74 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail seek file @offset[%d]: %s", offset, err->message);
|
|
|
75 g_error_free(err);
|
|
|
76 memset(buf, 0, len);
|
|
|
77 return;
|
|
|
78 } // if err
|
|
|
79
|
|
|
80 status = g_io_channel_read_chars(io, buf, len, NULL, &err);
|
|
|
81 if (err != NULL) {
|
|
|
82 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail read %d bytes from file: %s", len, err->message);
|
|
|
83 g_error_free(err);
|
|
|
84 memset(buf, 0, len);
|
|
|
85 return;
|
|
|
86 } // if err
|
|
|
87 } // _read_from
|
|
|
88
|
|
|
89 /*****************************************************************************/
|
|
|
90 // read len of bytes to buf, from io at offset
|
|
|
91 static gsize _read_line_from(GIOChannel * io, guint32 offset, gchar ** ret_str)
|
|
|
92 {
|
|
|
93 gsize bytes_read;
|
|
|
94 GError *err;
|
|
|
95 GIOStatus status;
|
|
|
96
|
|
|
97 err = NULL;
|
|
|
98 status = g_io_channel_seek_position(io, offset, G_SEEK_SET, &err);
|
|
|
99 if (err != NULL) {
|
|
|
100 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail seek file @offset[%d]: %s", offset, err->message);
|
|
|
101 g_error_free(err);
|
|
|
102 ret_str = NULL;
|
|
|
103 return -1;
|
|
|
104 } // if err
|
|
|
105
|
|
|
106 status = g_io_channel_read_line(io, ret_str, &bytes_read, NULL, &err);
|
|
|
107 if (err != NULL) {
|
|
|
108 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail read from file: %s", err->message);
|
|
|
109 g_error_free(err);
|
|
|
110 ret_str = NULL;
|
|
|
111 return -1;
|
|
|
112 } // if err
|
|
|
113
|
|
|
114 return bytes_read;
|
|
|
115 } // _read_from
|
|
|
116
|
|
|
117 /*****************************************************************************/
|
|
|
118 // read the string from io, at offset, it may jump several times
|
|
|
119 // returns the offset of next readable string for area
|
|
|
120 static guint32 _get_string(GIOChannel * io, guint32 offset, gchar ** ret)
|
|
|
121 {
|
|
|
122 guint8 *buf;
|
|
|
123 g_return_val_if_fail(io != NULL, 0);
|
|
|
124
|
|
|
125 buf = g_new0(guint8, 3);
|
|
|
126 _read_from(io, offset, buf, 1);
|
|
|
127
|
|
|
128 switch (buf[0]) { /* fixed by bluestar11 at gmail dot com, 04/12/20 */
|
|
|
129 case 0x01: /* jump together */
|
|
|
130 _read_from(io, offset + 1, buf, 3);
|
|
|
131 return _get_string(io, _byte_array_to_int(buf, 3), ret);
|
|
|
132 case 0x02: /* jump separately */
|
|
|
133 _read_from(io, offset + 1, buf, 3);
|
|
|
134 _get_string(io, _byte_array_to_int(buf, 3), ret);
|
|
|
135 return offset + 4;
|
|
|
136 default:
|
|
|
137 _read_line_from(io, offset, ret);
|
|
|
138 return offset + strlen(*ret) + 1;
|
|
|
139 } /* switch */
|
|
|
140 } // _get_string
|
|
|
141
|
|
|
142 /*****************************************************************************/
|
|
|
143 // extract country and area starting from offset
|
|
|
144 static void _get_country_city(GIOChannel * io, guint32 offset, gchar ** country, gchar ** area)
|
|
|
145 {
|
|
|
146 guint32 next_offset;
|
|
|
147 g_return_if_fail(io != NULL);
|
|
|
148
|
|
|
149 next_offset = _get_string(io, offset, country);
|
|
|
150 if (next_offset == 0)
|
|
|
151 *area = g_strdup("");
|
|
|
152 else
|
|
|
153 _get_string(io, next_offset, area);
|
|
|
154 } // _get_country_city
|
|
|
155
|
|
|
156 /*****************************************************************************/
|
|
|
157 // set start_ip and end_ip of current range
|
|
|
158 static void _set_ip_range(gint rec_no, ip_finder * f)
|
|
|
159 {
|
|
|
160 guint8 *buf;
|
|
|
161 guint32 offset;
|
|
|
162
|
|
|
163 g_return_if_fail(f != NULL);
|
|
|
164
|
|
|
165 buf = g_newa(guint8, 7);
|
|
|
166 offset = f->offset_first_start_ip + rec_no * 7;
|
|
|
167
|
|
|
168 _read_from(f->io, offset, buf, 7);
|
|
|
169 f->cur_start_ip = _byte_array_to_int(buf, 4);
|
|
|
170 f->offset_cur_end_ip = _byte_array_to_int(buf + 4, 3);
|
|
|
171
|
|
|
172 _read_from(f->io, f->offset_cur_end_ip, buf, 4);
|
|
|
173 f->cur_end_ip = _byte_array_to_int(buf, 4);
|
|
|
174
|
|
|
175 } // _set_ip_range
|
|
|
176
|
|
|
177 /*****************************************************************************/
|
|
|
178 // set the country and area for given IP.
|
|
|
179 // country and area needs to be freed later
|
|
|
180 gboolean qq_ip_get_location(guint32 ip, gchar ** country, gchar ** area)
|
|
|
181 {
|
|
|
182 gint rec, record_count, B, E;
|
|
|
183 guint8 *buf;
|
|
|
184 gchar *addr_file;
|
|
|
185 ip_finder *f;
|
|
|
186 GError *err;
|
|
|
187 const char *ip_fn;
|
|
|
188
|
|
|
189 if (ip == 0)
|
|
|
190 return FALSE;
|
|
|
191
|
|
|
192 f = g_new0(ip_finder, 1);
|
|
|
193 err = NULL;
|
|
|
194 ip_fn = gaim_prefs_get_string("/plugins/prpl/qq/ipfile");
|
|
|
195 if (ip_fn == NULL || strlen(ip_fn) == 0 || strncmp(ip_fn, "(null)", strlen("(null)")) == 0) {
|
|
|
196 addr_file = g_build_filename(DATADIR, DEFAULT_IP_LOCATION_FILE, NULL);
|
|
|
197 } else {
|
|
|
198 addr_file = g_build_filename(ip_fn, NULL);
|
|
|
199 }
|
|
|
200
|
|
|
201 f->io = g_io_channel_new_file(addr_file, "r", &err);
|
|
|
202 g_free(addr_file);
|
|
|
203 if (err != NULL) {
|
|
|
204 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Unable to open (%s): %s\n", addr_file, err->message);
|
|
|
205 g_error_free(err);
|
|
|
206 return FALSE;
|
|
|
207 } else
|
|
|
208 g_io_channel_set_encoding(f->io, NULL, NULL); // set binary
|
|
|
209
|
|
|
210 buf = g_newa(guint8, 4);
|
|
|
211
|
|
|
212 _read_from(f->io, 0, buf, 4);
|
|
|
213 f->offset_first_start_ip = _byte_array_to_int(buf, 4);
|
|
|
214 _read_from(f->io, 4, buf, 4);
|
|
|
215 f->offset_last_start_ip = _byte_array_to_int(buf, 4);
|
|
|
216
|
|
|
217 record_count = (f->offset_last_start_ip - f->offset_first_start_ip) / 7;
|
|
|
218 if (record_count <= 1) {
|
|
|
219 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "File data error, no records found\n");
|
|
|
220 g_io_channel_shutdown(f->io, FALSE, NULL);
|
|
|
221 return FALSE;;
|
|
|
222 }
|
|
|
223 // search for right range
|
|
|
224 B = 0;
|
|
|
225 E = record_count;
|
|
|
226 while (B < E - 1) {
|
|
|
227 rec = (B + E) / 2;
|
|
|
228 _set_ip_range(rec, f);
|
|
|
229 if (ip == f->cur_start_ip) {
|
|
|
230 B = rec;
|
|
|
231 break;
|
|
|
232 }
|
|
|
233 if (ip > f->cur_start_ip)
|
|
|
234 B = rec;
|
|
|
235 else
|
|
|
236 E = rec;
|
|
|
237 } // while
|
|
|
238 _set_ip_range(B, f);
|
|
|
239
|
|
|
240 if (f->cur_start_ip <= ip && ip <= f->cur_end_ip) {
|
|
|
241 _get_country_city(f->io, f->offset_cur_end_ip + 4, country, area);
|
|
|
242 } else { // not in this range... miss
|
|
|
243 *country = g_strdup("unkown");
|
|
|
244 *area = g_strdup(" ");
|
|
|
245 } // if ip_start<=ip<=ip_end
|
|
|
246
|
|
|
247 g_io_channel_shutdown(f->io, FALSE, NULL);
|
|
|
248 return TRUE;
|
|
|
249
|
|
|
250 } // qq_ip_get_location
|
|
|
251
|
|
|
252 /*****************************************************************************/
|
|
|
253 // END OF FILE
|