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