catalinux / ip2clue (public) (License: GPLv3) (since 2016-03-01) (hash sha1)
High-performance IPv4 and IPv6 daemon to retrieve IPv4/6 country information.
List of commits:
Subject Hash Author Date (UTC)
Initial version (0.0.90). 5807f4955d446127a7b873f5f3918f408ab93fff Catalin(ux) M. BOIE 2010-07-01 15:39:22
Commit 5807f4955d446127a7b873f5f3918f408ab93fff - Initial version (0.0.90).
Author: Catalin(ux) M. BOIE
Author date (UTC): 2010-07-01 15:39
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2010-07-01 15:39
Parent(s):
Signing key:
Tree: 6b815e1dcb8a1f780317489dbfd17a264efa8caa
File Lines added Lines deleted
.exclude 3 0
.gitignore 13 0
LICENSE 674 0
Makefile.in 42 0
README 38 0
TODO 15 0
clients/ip2clue.inc.php 75 0
clients/ip2clue.php 33 0
configure 3 0
crons/ip2c_ip-to-country 32 0
crons/ip2c_maxmind-ipv4 32 0
crons/ip2c_maxmind-ipv6 31 0
crons/ip2c_software77 48 0
duilder 566 0
duilder.conf 15 0
duilder_release 16 0
etc/download.conf 6 0
etc/ip2clued.conf 47 0
i_conf.c 113 0
i_conf.h 21 0
i_config.h 8 0
i_types.h 107 0
i_util.c 505 0
i_util.h 47 0
ip2clue.c 125 0
ip2clue.spec.in 52 0
ip2clued.c 298 0
my.conf 10 0
parser.c 360 0
parser.h 33 0
parser_ip2location.c 539 0
parser_ip2location.h 10 0
parser_text.c 236 0
parser_text.h 15 0
test1.c 154 0
test2.c 158 0
File .exclude added (mode: 100644) (index 0000000..5ac85a1)
1 samples
2 duilder_release
3 my.conf
File .gitignore added (mode: 100644) (index 0000000..54c1ff2)
1 *.out
2 *.log
3 *.o
4 cachegrind.*
5 ip2clued
6 *.BIN
7 *.csv
8 test1
9 test2
10 Changelog
11 *.spec
12 rpmlint.out
13 Makefile
File LICENSE added (mode: 100644) (index 0000000..94a9ed0)
1 GNU GENERAL PUBLIC LICENSE
2 Version 3, 29 June 2007
3
4 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5 Everyone is permitted to copy and distribute verbatim copies
6 of this license document, but changing it is not allowed.
7
8 Preamble
9
10 The GNU General Public License is a free, copyleft license for
11 software and other kinds of works.
12
13 The licenses for most software and other practical works are designed
14 to take away your freedom to share and change the works. By contrast,
15 the GNU General Public License is intended to guarantee your freedom to
16 share and change all versions of a program--to make sure it remains free
17 software for all its users. We, the Free Software Foundation, use the
18 GNU General Public License for most of our software; it applies also to
19 any other work released this way by its authors. You can apply it to
20 your programs, too.
21
22 When we speak of free software, we are referring to freedom, not
23 price. Our General Public Licenses are designed to make sure that you
24 have the freedom to distribute copies of free software (and charge for
25 them if you wish), that you receive source code or can get it if you
26 want it, that you can change the software or use pieces of it in new
27 free programs, and that you know you can do these things.
28
29 To protect your rights, we need to prevent others from denying you
30 these rights or asking you to surrender the rights. Therefore, you have
31 certain responsibilities if you distribute copies of the software, or if
32 you modify it: responsibilities to respect the freedom of others.
33
34 For example, if you distribute copies of such a program, whether
35 gratis or for a fee, you must pass on to the recipients the same
36 freedoms that you received. You must make sure that they, too, receive
37 or can get the source code. And you must show them these terms so they
38 know their rights.
39
40 Developers that use the GNU GPL protect your rights with two steps:
41 (1) assert copyright on the software, and (2) offer you this License
42 giving you legal permission to copy, distribute and/or modify it.
43
44 For the developers' and authors' protection, the GPL clearly explains
45 that there is no warranty for this free software. For both users' and
46 authors' sake, the GPL requires that modified versions be marked as
47 changed, so that their problems will not be attributed erroneously to
48 authors of previous versions.
49
50 Some devices are designed to deny users access to install or run
51 modified versions of the software inside them, although the manufacturer
52 can do so. This is fundamentally incompatible with the aim of
53 protecting users' freedom to change the software. The systematic
54 pattern of such abuse occurs in the area of products for individuals to
55 use, which is precisely where it is most unacceptable. Therefore, we
56 have designed this version of the GPL to prohibit the practice for those
57 products. If such problems arise substantially in other domains, we
58 stand ready to extend this provision to those domains in future versions
59 of the GPL, as needed to protect the freedom of users.
60
61 Finally, every program is threatened constantly by software patents.
62 States should not allow patents to restrict development and use of
63 software on general-purpose computers, but in those that do, we wish to
64 avoid the special danger that patents applied to a free program could
65 make it effectively proprietary. To prevent this, the GPL assures that
66 patents cannot be used to render the program non-free.
67
68 The precise terms and conditions for copying, distribution and
69 modification follow.
70
71 TERMS AND CONDITIONS
72
73 0. Definitions.
74
75 "This License" refers to version 3 of the GNU General Public License.
76
77 "Copyright" also means copyright-like laws that apply to other kinds of
78 works, such as semiconductor masks.
79
80 "The Program" refers to any copyrightable work licensed under this
81 License. Each licensee is addressed as "you". "Licensees" and
82 "recipients" may be individuals or organizations.
83
84 To "modify" a work means to copy from or adapt all or part of the work
85 in a fashion requiring copyright permission, other than the making of an
86 exact copy. The resulting work is called a "modified version" of the
87 earlier work or a work "based on" the earlier work.
88
89 A "covered work" means either the unmodified Program or a work based
90 on the Program.
91
92 To "propagate" a work means to do anything with it that, without
93 permission, would make you directly or secondarily liable for
94 infringement under applicable copyright law, except executing it on a
95 computer or modifying a private copy. Propagation includes copying,
96 distribution (with or without modification), making available to the
97 public, and in some countries other activities as well.
98
99 To "convey" a work means any kind of propagation that enables other
100 parties to make or receive copies. Mere interaction with a user through
101 a computer network, with no transfer of a copy, is not conveying.
102
103 An interactive user interface displays "Appropriate Legal Notices"
104 to the extent that it includes a convenient and prominently visible
105 feature that (1) displays an appropriate copyright notice, and (2)
106 tells the user that there is no warranty for the work (except to the
107 extent that warranties are provided), that licensees may convey the
108 work under this License, and how to view a copy of this License. If
109 the interface presents a list of user commands or options, such as a
110 menu, a prominent item in the list meets this criterion.
111
112 1. Source Code.
113
114 The "source code" for a work means the preferred form of the work
115 for making modifications to it. "Object code" means any non-source
116 form of a work.
117
118 A "Standard Interface" means an interface that either is an official
119 standard defined by a recognized standards body, or, in the case of
120 interfaces specified for a particular programming language, one that
121 is widely used among developers working in that language.
122
123 The "System Libraries" of an executable work include anything, other
124 than the work as a whole, that (a) is included in the normal form of
125 packaging a Major Component, but which is not part of that Major
126 Component, and (b) serves only to enable use of the work with that
127 Major Component, or to implement a Standard Interface for which an
128 implementation is available to the public in source code form. A
129 "Major Component", in this context, means a major essential component
130 (kernel, window system, and so on) of the specific operating system
131 (if any) on which the executable work runs, or a compiler used to
132 produce the work, or an object code interpreter used to run it.
133
134 The "Corresponding Source" for a work in object code form means all
135 the source code needed to generate, install, and (for an executable
136 work) run the object code and to modify the work, including scripts to
137 control those activities. However, it does not include the work's
138 System Libraries, or general-purpose tools or generally available free
139 programs which are used unmodified in performing those activities but
140 which are not part of the work. For example, Corresponding Source
141 includes interface definition files associated with source files for
142 the work, and the source code for shared libraries and dynamically
143 linked subprograms that the work is specifically designed to require,
144 such as by intimate data communication or control flow between those
145 subprograms and other parts of the work.
146
147 The Corresponding Source need not include anything that users
148 can regenerate automatically from other parts of the Corresponding
149 Source.
150
151 The Corresponding Source for a work in source code form is that
152 same work.
153
154 2. Basic Permissions.
155
156 All rights granted under this License are granted for the term of
157 copyright on the Program, and are irrevocable provided the stated
158 conditions are met. This License explicitly affirms your unlimited
159 permission to run the unmodified Program. The output from running a
160 covered work is covered by this License only if the output, given its
161 content, constitutes a covered work. This License acknowledges your
162 rights of fair use or other equivalent, as provided by copyright law.
163
164 You may make, run and propagate covered works that you do not
165 convey, without conditions so long as your license otherwise remains
166 in force. You may convey covered works to others for the sole purpose
167 of having them make modifications exclusively for you, or provide you
168 with facilities for running those works, provided that you comply with
169 the terms of this License in conveying all material for which you do
170 not control copyright. Those thus making or running the covered works
171 for you must do so exclusively on your behalf, under your direction
172 and control, on terms that prohibit them from making any copies of
173 your copyrighted material outside their relationship with you.
174
175 Conveying under any other circumstances is permitted solely under
176 the conditions stated below. Sublicensing is not allowed; section 10
177 makes it unnecessary.
178
179 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180
181 No covered work shall be deemed part of an effective technological
182 measure under any applicable law fulfilling obligations under article
183 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 similar laws prohibiting or restricting circumvention of such
185 measures.
186
187 When you convey a covered work, you waive any legal power to forbid
188 circumvention of technological measures to the extent such circumvention
189 is effected by exercising rights under this License with respect to
190 the covered work, and you disclaim any intention to limit operation or
191 modification of the work as a means of enforcing, against the work's
192 users, your or third parties' legal rights to forbid circumvention of
193 technological measures.
194
195 4. Conveying Verbatim Copies.
196
197 You may convey verbatim copies of the Program's source code as you
198 receive it, in any medium, provided that you conspicuously and
199 appropriately publish on each copy an appropriate copyright notice;
200 keep intact all notices stating that this License and any
201 non-permissive terms added in accord with section 7 apply to the code;
202 keep intact all notices of the absence of any warranty; and give all
203 recipients a copy of this License along with the Program.
204
205 You may charge any price or no price for each copy that you convey,
206 and you may offer support or warranty protection for a fee.
207
208 5. Conveying Modified Source Versions.
209
210 You may convey a work based on the Program, or the modifications to
211 produce it from the Program, in the form of source code under the
212 terms of section 4, provided that you also meet all of these conditions:
213
214 a) The work must carry prominent notices stating that you modified
215 it, and giving a relevant date.
216
217 b) The work must carry prominent notices stating that it is
218 released under this License and any conditions added under section
219 7. This requirement modifies the requirement in section 4 to
220 "keep intact all notices".
221
222 c) You must license the entire work, as a whole, under this
223 License to anyone who comes into possession of a copy. This
224 License will therefore apply, along with any applicable section 7
225 additional terms, to the whole of the work, and all its parts,
226 regardless of how they are packaged. This License gives no
227 permission to license the work in any other way, but it does not
228 invalidate such permission if you have separately received it.
229
230 d) If the work has interactive user interfaces, each must display
231 Appropriate Legal Notices; however, if the Program has interactive
232 interfaces that do not display Appropriate Legal Notices, your
233 work need not make them do so.
234
235 A compilation of a covered work with other separate and independent
236 works, which are not by their nature extensions of the covered work,
237 and which are not combined with it such as to form a larger program,
238 in or on a volume of a storage or distribution medium, is called an
239 "aggregate" if the compilation and its resulting copyright are not
240 used to limit the access or legal rights of the compilation's users
241 beyond what the individual works permit. Inclusion of a covered work
242 in an aggregate does not cause this License to apply to the other
243 parts of the aggregate.
244
245 6. Conveying Non-Source Forms.
246
247 You may convey a covered work in object code form under the terms
248 of sections 4 and 5, provided that you also convey the
249 machine-readable Corresponding Source under the terms of this License,
250 in one of these ways:
251
252 a) Convey the object code in, or embodied in, a physical product
253 (including a physical distribution medium), accompanied by the
254 Corresponding Source fixed on a durable physical medium
255 customarily used for software interchange.
256
257 b) Convey the object code in, or embodied in, a physical product
258 (including a physical distribution medium), accompanied by a
259 written offer, valid for at least three years and valid for as
260 long as you offer spare parts or customer support for that product
261 model, to give anyone who possesses the object code either (1) a
262 copy of the Corresponding Source for all the software in the
263 product that is covered by this License, on a durable physical
264 medium customarily used for software interchange, for a price no
265 more than your reasonable cost of physically performing this
266 conveying of source, or (2) access to copy the
267 Corresponding Source from a network server at no charge.
268
269 c) Convey individual copies of the object code with a copy of the
270 written offer to provide the Corresponding Source. This
271 alternative is allowed only occasionally and noncommercially, and
272 only if you received the object code with such an offer, in accord
273 with subsection 6b.
274
275 d) Convey the object code by offering access from a designated
276 place (gratis or for a charge), and offer equivalent access to the
277 Corresponding Source in the same way through the same place at no
278 further charge. You need not require recipients to copy the
279 Corresponding Source along with the object code. If the place to
280 copy the object code is a network server, the Corresponding Source
281 may be on a different server (operated by you or a third party)
282 that supports equivalent copying facilities, provided you maintain
283 clear directions next to the object code saying where to find the
284 Corresponding Source. Regardless of what server hosts the
285 Corresponding Source, you remain obligated to ensure that it is
286 available for as long as needed to satisfy these requirements.
287
288 e) Convey the object code using peer-to-peer transmission, provided
289 you inform other peers where the object code and Corresponding
290 Source of the work are being offered to the general public at no
291 charge under subsection 6d.
292
293 A separable portion of the object code, whose source code is excluded
294 from the Corresponding Source as a System Library, need not be
295 included in conveying the object code work.
296
297 A "User Product" is either (1) a "consumer product", which means any
298 tangible personal property which is normally used for personal, family,
299 or household purposes, or (2) anything designed or sold for incorporation
300 into a dwelling. In determining whether a product is a consumer product,
301 doubtful cases shall be resolved in favor of coverage. For a particular
302 product received by a particular user, "normally used" refers to a
303 typical or common use of that class of product, regardless of the status
304 of the particular user or of the way in which the particular user
305 actually uses, or expects or is expected to use, the product. A product
306 is a consumer product regardless of whether the product has substantial
307 commercial, industrial or non-consumer uses, unless such uses represent
308 the only significant mode of use of the product.
309
310 "Installation Information" for a User Product means any methods,
311 procedures, authorization keys, or other information required to install
312 and execute modified versions of a covered work in that User Product from
313 a modified version of its Corresponding Source. The information must
314 suffice to ensure that the continued functioning of the modified object
315 code is in no case prevented or interfered with solely because
316 modification has been made.
317
318 If you convey an object code work under this section in, or with, or
319 specifically for use in, a User Product, and the conveying occurs as
320 part of a transaction in which the right of possession and use of the
321 User Product is transferred to the recipient in perpetuity or for a
322 fixed term (regardless of how the transaction is characterized), the
323 Corresponding Source conveyed under this section must be accompanied
324 by the Installation Information. But this requirement does not apply
325 if neither you nor any third party retains the ability to install
326 modified object code on the User Product (for example, the work has
327 been installed in ROM).
328
329 The requirement to provide Installation Information does not include a
330 requirement to continue to provide support service, warranty, or updates
331 for a work that has been modified or installed by the recipient, or for
332 the User Product in which it has been modified or installed. Access to a
333 network may be denied when the modification itself materially and
334 adversely affects the operation of the network or violates the rules and
335 protocols for communication across the network.
336
337 Corresponding Source conveyed, and Installation Information provided,
338 in accord with this section must be in a format that is publicly
339 documented (and with an implementation available to the public in
340 source code form), and must require no special password or key for
341 unpacking, reading or copying.
342
343 7. Additional Terms.
344
345 "Additional permissions" are terms that supplement the terms of this
346 License by making exceptions from one or more of its conditions.
347 Additional permissions that are applicable to the entire Program shall
348 be treated as though they were included in this License, to the extent
349 that they are valid under applicable law. If additional permissions
350 apply only to part of the Program, that part may be used separately
351 under those permissions, but the entire Program remains governed by
352 this License without regard to the additional permissions.
353
354 When you convey a copy of a covered work, you may at your option
355 remove any additional permissions from that copy, or from any part of
356 it. (Additional permissions may be written to require their own
357 removal in certain cases when you modify the work.) You may place
358 additional permissions on material, added by you to a covered work,
359 for which you have or can give appropriate copyright permission.
360
361 Notwithstanding any other provision of this License, for material you
362 add to a covered work, you may (if authorized by the copyright holders of
363 that material) supplement the terms of this License with terms:
364
365 a) Disclaiming warranty or limiting liability differently from the
366 terms of sections 15 and 16 of this License; or
367
368 b) Requiring preservation of specified reasonable legal notices or
369 author attributions in that material or in the Appropriate Legal
370 Notices displayed by works containing it; or
371
372 c) Prohibiting misrepresentation of the origin of that material, or
373 requiring that modified versions of such material be marked in
374 reasonable ways as different from the original version; or
375
376 d) Limiting the use for publicity purposes of names of licensors or
377 authors of the material; or
378
379 e) Declining to grant rights under trademark law for use of some
380 trade names, trademarks, or service marks; or
381
382 f) Requiring indemnification of licensors and authors of that
383 material by anyone who conveys the material (or modified versions of
384 it) with contractual assumptions of liability to the recipient, for
385 any liability that these contractual assumptions directly impose on
386 those licensors and authors.
387
388 All other non-permissive additional terms are considered "further
389 restrictions" within the meaning of section 10. If the Program as you
390 received it, or any part of it, contains a notice stating that it is
391 governed by this License along with a term that is a further
392 restriction, you may remove that term. If a license document contains
393 a further restriction but permits relicensing or conveying under this
394 License, you may add to a covered work material governed by the terms
395 of that license document, provided that the further restriction does
396 not survive such relicensing or conveying.
397
398 If you add terms to a covered work in accord with this section, you
399 must place, in the relevant source files, a statement of the
400 additional terms that apply to those files, or a notice indicating
401 where to find the applicable terms.
402
403 Additional terms, permissive or non-permissive, may be stated in the
404 form of a separately written license, or stated as exceptions;
405 the above requirements apply either way.
406
407 8. Termination.
408
409 You may not propagate or modify a covered work except as expressly
410 provided under this License. Any attempt otherwise to propagate or
411 modify it is void, and will automatically terminate your rights under
412 this License (including any patent licenses granted under the third
413 paragraph of section 11).
414
415 However, if you cease all violation of this License, then your
416 license from a particular copyright holder is reinstated (a)
417 provisionally, unless and until the copyright holder explicitly and
418 finally terminates your license, and (b) permanently, if the copyright
419 holder fails to notify you of the violation by some reasonable means
420 prior to 60 days after the cessation.
421
422 Moreover, your license from a particular copyright holder is
423 reinstated permanently if the copyright holder notifies you of the
424 violation by some reasonable means, this is the first time you have
425 received notice of violation of this License (for any work) from that
426 copyright holder, and you cure the violation prior to 30 days after
427 your receipt of the notice.
428
429 Termination of your rights under this section does not terminate the
430 licenses of parties who have received copies or rights from you under
431 this License. If your rights have been terminated and not permanently
432 reinstated, you do not qualify to receive new licenses for the same
433 material under section 10.
434
435 9. Acceptance Not Required for Having Copies.
436
437 You are not required to accept this License in order to receive or
438 run a copy of the Program. Ancillary propagation of a covered work
439 occurring solely as a consequence of using peer-to-peer transmission
440 to receive a copy likewise does not require acceptance. However,
441 nothing other than this License grants you permission to propagate or
442 modify any covered work. These actions infringe copyright if you do
443 not accept this License. Therefore, by modifying or propagating a
444 covered work, you indicate your acceptance of this License to do so.
445
446 10. Automatic Licensing of Downstream Recipients.
447
448 Each time you convey a covered work, the recipient automatically
449 receives a license from the original licensors, to run, modify and
450 propagate that work, subject to this License. You are not responsible
451 for enforcing compliance by third parties with this License.
452
453 An "entity transaction" is a transaction transferring control of an
454 organization, or substantially all assets of one, or subdividing an
455 organization, or merging organizations. If propagation of a covered
456 work results from an entity transaction, each party to that
457 transaction who receives a copy of the work also receives whatever
458 licenses to the work the party's predecessor in interest had or could
459 give under the previous paragraph, plus a right to possession of the
460 Corresponding Source of the work from the predecessor in interest, if
461 the predecessor has it or can get it with reasonable efforts.
462
463 You may not impose any further restrictions on the exercise of the
464 rights granted or affirmed under this License. For example, you may
465 not impose a license fee, royalty, or other charge for exercise of
466 rights granted under this License, and you may not initiate litigation
467 (including a cross-claim or counterclaim in a lawsuit) alleging that
468 any patent claim is infringed by making, using, selling, offering for
469 sale, or importing the Program or any portion of it.
470
471 11. Patents.
472
473 A "contributor" is a copyright holder who authorizes use under this
474 License of the Program or a work on which the Program is based. The
475 work thus licensed is called the contributor's "contributor version".
476
477 A contributor's "essential patent claims" are all patent claims
478 owned or controlled by the contributor, whether already acquired or
479 hereafter acquired, that would be infringed by some manner, permitted
480 by this License, of making, using, or selling its contributor version,
481 but do not include claims that would be infringed only as a
482 consequence of further modification of the contributor version. For
483 purposes of this definition, "control" includes the right to grant
484 patent sublicenses in a manner consistent with the requirements of
485 this License.
486
487 Each contributor grants you a non-exclusive, worldwide, royalty-free
488 patent license under the contributor's essential patent claims, to
489 make, use, sell, offer for sale, import and otherwise run, modify and
490 propagate the contents of its contributor version.
491
492 In the following three paragraphs, a "patent license" is any express
493 agreement or commitment, however denominated, not to enforce a patent
494 (such as an express permission to practice a patent or covenant not to
495 sue for patent infringement). To "grant" such a patent license to a
496 party means to make such an agreement or commitment not to enforce a
497 patent against the party.
498
499 If you convey a covered work, knowingly relying on a patent license,
500 and the Corresponding Source of the work is not available for anyone
501 to copy, free of charge and under the terms of this License, through a
502 publicly available network server or other readily accessible means,
503 then you must either (1) cause the Corresponding Source to be so
504 available, or (2) arrange to deprive yourself of the benefit of the
505 patent license for this particular work, or (3) arrange, in a manner
506 consistent with the requirements of this License, to extend the patent
507 license to downstream recipients. "Knowingly relying" means you have
508 actual knowledge that, but for the patent license, your conveying the
509 covered work in a country, or your recipient's use of the covered work
510 in a country, would infringe one or more identifiable patents in that
511 country that you have reason to believe are valid.
512
513 If, pursuant to or in connection with a single transaction or
514 arrangement, you convey, or propagate by procuring conveyance of, a
515 covered work, and grant a patent license to some of the parties
516 receiving the covered work authorizing them to use, propagate, modify
517 or convey a specific copy of the covered work, then the patent license
518 you grant is automatically extended to all recipients of the covered
519 work and works based on it.
520
521 A patent license is "discriminatory" if it does not include within
522 the scope of its coverage, prohibits the exercise of, or is
523 conditioned on the non-exercise of one or more of the rights that are
524 specifically granted under this License. You may not convey a covered
525 work if you are a party to an arrangement with a third party that is
526 in the business of distributing software, under which you make payment
527 to the third party based on the extent of your activity of conveying
528 the work, and under which the third party grants, to any of the
529 parties who would receive the covered work from you, a discriminatory
530 patent license (a) in connection with copies of the covered work
531 conveyed by you (or copies made from those copies), or (b) primarily
532 for and in connection with specific products or compilations that
533 contain the covered work, unless you entered into that arrangement,
534 or that patent license was granted, prior to 28 March 2007.
535
536 Nothing in this License shall be construed as excluding or limiting
537 any implied license or other defenses to infringement that may
538 otherwise be available to you under applicable patent law.
539
540 12. No Surrender of Others' Freedom.
541
542 If conditions are imposed on you (whether by court order, agreement or
543 otherwise) that contradict the conditions of this License, they do not
544 excuse you from the conditions of this License. If you cannot convey a
545 covered work so as to satisfy simultaneously your obligations under this
546 License and any other pertinent obligations, then as a consequence you may
547 not convey it at all. For example, if you agree to terms that obligate you
548 to collect a royalty for further conveying from those to whom you convey
549 the Program, the only way you could satisfy both those terms and this
550 License would be to refrain entirely from conveying the Program.
551
552 13. Use with the GNU Affero General Public License.
553
554 Notwithstanding any other provision of this License, you have
555 permission to link or combine any covered work with a work licensed
556 under version 3 of the GNU Affero General Public License into a single
557 combined work, and to convey the resulting work. The terms of this
558 License will continue to apply to the part which is the covered work,
559 but the special requirements of the GNU Affero General Public License,
560 section 13, concerning interaction through a network will apply to the
561 combination as such.
562
563 14. Revised Versions of this License.
564
565 The Free Software Foundation may publish revised and/or new versions of
566 the GNU General Public License from time to time. Such new versions will
567 be similar in spirit to the present version, but may differ in detail to
568 address new problems or concerns.
569
570 Each version is given a distinguishing version number. If the
571 Program specifies that a certain numbered version of the GNU General
572 Public License "or any later version" applies to it, you have the
573 option of following the terms and conditions either of that numbered
574 version or of any later version published by the Free Software
575 Foundation. If the Program does not specify a version number of the
576 GNU General Public License, you may choose any version ever published
577 by the Free Software Foundation.
578
579 If the Program specifies that a proxy can decide which future
580 versions of the GNU General Public License can be used, that proxy's
581 public statement of acceptance of a version permanently authorizes you
582 to choose that version for the Program.
583
584 Later license versions may give you additional or different
585 permissions. However, no additional obligations are imposed on any
586 author or copyright holder as a result of your choosing to follow a
587 later version.
588
589 15. Disclaimer of Warranty.
590
591 THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599
600 16. Limitation of Liability.
601
602 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 SUCH DAMAGES.
611
612 17. Interpretation of Sections 15 and 16.
613
614 If the disclaimer of warranty and limitation of liability provided
615 above cannot be given local legal effect according to their terms,
616 reviewing courts shall apply local law that most closely approximates
617 an absolute waiver of all civil liability in connection with the
618 Program, unless a warranty or assumption of liability accompanies a
619 copy of the Program in return for a fee.
620
621 END OF TERMS AND CONDITIONS
622
623 How to Apply These Terms to Your New Programs
624
625 If you develop a new program, and you want it to be of the greatest
626 possible use to the public, the best way to achieve this is to make it
627 free software which everyone can redistribute and change under these terms.
628
629 To do so, attach the following notices to the program. It is safest
630 to attach them to the start of each source file to most effectively
631 state the exclusion of warranty; and each file should have at least
632 the "copyright" line and a pointer to where the full notice is found.
633
634 <one line to give the program's name and a brief idea of what it does.>
635 Copyright (C) <year> <name of author>
636
637 This program is free software: you can redistribute it and/or modify
638 it under the terms of the GNU General Public License as published by
639 the Free Software Foundation, either version 3 of the License, or
640 (at your option) any later version.
641
642 This program is distributed in the hope that it will be useful,
643 but WITHOUT ANY WARRANTY; without even the implied warranty of
644 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 GNU General Public License for more details.
646
647 You should have received a copy of the GNU General Public License
648 along with this program. If not, see <http://www.gnu.org/licenses/>.
649
650 Also add information on how to contact you by electronic and paper mail.
651
652 If the program does terminal interaction, make it output a short
653 notice like this when it starts in an interactive mode:
654
655 <program> Copyright (C) <year> <name of author>
656 This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 This is free software, and you are welcome to redistribute it
658 under certain conditions; type `show c' for details.
659
660 The hypothetical commands `show w' and `show c' should show the appropriate
661 parts of the General Public License. Of course, your program's commands
662 might be different; for a GUI interface, you would use an "about box".
663
664 You should also get your employer (if you work as a programmer) or school,
665 if any, to sign a "copyright disclaimer" for the program, if necessary.
666 For more information on this, and how to apply and follow the GNU GPL, see
667 <http://www.gnu.org/licenses/>.
668
669 The GNU General Public License does not permit incorporating your program
670 into proprietary programs. If your program is a subroutine library, you
671 may consider it more useful to permit linking proprietary applications with
672 the library. If this is what you want to do, use the GNU Lesser General
673 Public License instead of this License. But first, please read
674 <http://www.gnu.org/philosophy/why-not-lgpl.html>.
File Makefile.in added (mode: 100644) (index 0000000..f778de0)
1 export CC := gcc
2 export INCS += -I.
3 export LIBS +=
4 export CFLAGS += -ggdb3 -Wall -Wextra -pedantic -Wno-long-long -pipe $(INCS)
5 export OBJS += i_util.o parser_text.o parser_ip2location.o parser.o i_conf.o
6
7 .PHONY: all
8 all: ip2clued ip2clue
9
10 ip2clued: $(OBJS) ip2clued.c
11 $(CC) $(CFLAGS) ip2clued.c -o ip2clued $(OBJS) -lpthread -lConn
12
13 ip2clue: $(OBJS) ip2clue.c
14 $(CC) $(CFLAGS) ip2clue.c -o ip2clue $(OBJS) -lConn
15
16 i_util.o: i_util.c i_util.h i_types.h i_config.h
17 $(CC) $(CFLAGS) -c $<
18
19 parser_text.o: parser_text.c parser_text.h i_util.o
20 $(CC) $(CFLAGS) -c $<
21
22 parser_ip2location.o: parser_ip2location.c i_util.o
23 $(CC) $(CFLAGS) -c $<
24
25 parser.o: parser.c parser.h parser_text.o parser_ip2location.o
26 $(CC) $(CFLAGS) -c $<
27
28 i_conf.o: i_conf.c i_conf.h i_config.h
29 $(CC) $(CFLAGS) -c $<
30
31 .PHONY: clean
32 clean:
33 rm -f $(OBJS) ip2clued ip2clue
34
35 install: all
36 mkdir -p "${I_ETC}/${PRJ}"
37 cp -vd etc/* "${I_ETC}/${PRJ}/"
38 mkdir -p "${I_USR_BIN}"
39 cp -vd ip2clue "${I_USR_BIN}"
40 mkdir -p "${I_USR_SBIN}"
41 cp -vd ip2clued "${I_USR_SBIN}"
42 cp -vd crons/* "/etc/cron.monthly/"
File README added (mode: 100644) (index 0000000..e2c35e1)
1 Name: ip2clue
2 Description: High-performance IPv4 and IPv6 daemon to retrieve IP information
3 Author: Catalin(ux) M. BOIE
4 Web: http://kernel.embedromix.ro/us/
5
6
7 . Paid support
8 Call +40-745-048374
9
10
11 . About
12 This software product provides access to multiple IP databases (Maxmind,
13 Software 77, Webhosting.info and ip2location) to retrieve IP information
14 (country short, country long etc.).
15 It has a daemon that supports lots of concurrent connections thanks to Conn
16 library (also at http://kernel.embedromix.ro/us/), and a client binary.
17 Also, a PHP function and an example are provided.
18
19 Database supported:
20 - http://ip-to-country.webhosting.info/
21 - http://software77.net/
22 - http://www.maxmind.com/app/geolitecountry - both IPv4 and IPv6
23
24
25 . Configuration
26 - Edit /etc/ip2clue/download.conf to start automatically download the data.
27 - Edit /etc/ip2clue/ip2clued.conf to configure IPv4/IPv6 support, port etc.
28 - Start the ip2clued daemon (service ip2clued start, for example).
29 - Connect to it with "telnet localhost 9999" and type "S" and Enter:
30 statistics will be displayed. Depending on databases sizes, will take some time
31 for the daemon to return data.
32 - To test it, connect to it as above and issue command "R193.193.193.193" and
33 then press Enter: Something like "OK ip=193.193.193.193 cs=UA cl= tz= isp="
34 will appear.
35
36
37 . License
38 GPLv3 or later
File TODO added (mode: 100644) (index 0000000..faa07a1)
1 [ ] IPv6: do not forget about 2002:xxyy:zztt::/48, ::a.b.c.d and ::ffff:a.b.c.d.
2 [ ] endianess checks
3 [ ] Remove extra pointer from core structure and add a parallel list for
4 'extra' to be cache friendly. Do not forget that pointer is 8 bytes on x86_64!
5 [ ] We can store only first 64 bits of IPv6 addreses?
6 [ ] We always split in half, so we may store in a table the middle points and
7 to not compute them every time. But this means more cache misses. :(
8 [ ] elap_load is in seconds?! Should be in miliseconds!
9 [ ] UDP
10 [ ] Dependencies: unzip, gzip, wget
11 [ ] ip2clue_error is mixed between the threads!
12 [ ] Extend extra for 'text' parsers (long country etc.).
13 [ ] Allow client to specify the format.
14 [ ] Maxmind binary file?
15 [ ]
File clients/ip2clue.inc.php added (mode: 100644) (index 0000000..9a3df49)
1 <?php
2
3 /*
4 * ip2clue client function
5 * Site: http://kernel.embedromix.ro/us/
6 * Copyright: Catalin(ux) M. BOIE
7 * License: GPLv3 or later
8 */
9
10 $ip2clue_fd = -1;
11
12 /*
13 * Lookup by IP country and other information
14 */
15 function ip2clue_lookup($ip2clue_host, $ip2clue_port, $timeout, $ip)
16 {
17 global $ip2clue_fd;
18
19 $ret['ok'] = FALSE;
20
21 if ($ip2clue_fd == -1) {
22 $fd = @fsockopen($ip2clue_host, $ip2clue_port, $errno,
23 $strerr, $timeout);
24 if ($fd === FALSE) {
25 $ret['errmsg'] = "Cannot connect to $ip2clue_host"
26 . ":$ip2clue_port ($strerr)";
27 return $ret;
28 }
29
30 $ip2clue_fd = $fd;
31 }
32
33 $h = fwrite($ip2clue_fd, "R$ip\n");
34 if ($h === FALSE) {
35 $ret['errmsg'] = "Cannot write to $ip2clue_host"
36 . ":$ip2clue_port";
37 close($ip2clue_fd);
38 $ip2clue_fd = -1;
39 return $ret;
40 }
41
42 $buf = fgets($ip2clue_fd, 8192);
43 if ($buf === FALSE) {
44 $ret['errmsg'] = "Cannot read from $ip2clue_host"
45 . ":$ip2clue_port";
46 close($ip2clue_fd);
47 $ip2clue_fd = -1;
48 return $ret;
49 }
50
51 if (strncmp($buf, "OK", 2) != 0) {
52 $ret['errmsg'] = $buf;
53 return $ret;
54 }
55
56 $buf = substr($buf, 2);
57 $x = explode(" ", $buf);
58
59 foreach ($x as $val) {
60 $pair = explode("=", $val);
61 $var = trim($pair[0]);
62
63 if (strlen($var) == 0)
64 continue;
65
66 $val = trim($pair[1]);
67 $ret[$var] = $val;
68 }
69
70 $ret['ok'] = TRUE;
71
72 return $ret;
73 }
74
75 ?>
File clients/ip2clue.php added (mode: 100644) (index 0000000..9b76502)
1 <?php
2
3 include_once("ip2clue.inc.php");
4
5 /*
6 * ip2clue example
7 * Site: http://kernel.embedromix.ro/us/
8 * Copyright: Catalin(ux) M. BOIE
9 * License: GPLv3 or later
10 */
11
12 $host = "localhost";
13 $port = 9999;
14 $timeout = 1; /* 1 second */
15
16 $ips = array(
17 "193.193.193.193",
18 "2001:360::1234"
19 );
20
21 foreach ($ips as $ip) {
22 echo "$ip: ";
23
24 $ret = ip2clue_lookup($host, $port, $timeout, $ip);
25 if ($ret['ok'] == FALSE) {
26 echo "ERROR: " . $ret['errmsg'] . "\n";
27 continue;
28 }
29
30 echo print_r($ret, TRUE);
31 }
32
33 ?>
File configure added (mode: 100755) (index 0000000..d33bb6c)
1 #!/bin/bash
2
3 ./duilder
File crons/ip2c_ip-to-country added (mode: 100644) (index 0000000..6cb8a56)
1 #!/bin/bash
2
3 # ip2clue downloader
4 # Author: Catalin(ux) M. BOIE
5 # Home: http://ip-to-country.webhosting.info/
6 # TODO: Use ETag to not download again the data!
7
8 source /etc/ip2clue/download.conf
9 if [ "${ip_to_location}" = "0" ]; then
10 exit 0
11 fi
12
13 set -e
14
15 cd /var/cache/ip2clue
16
17 data_file="ip-to-country.csv.zip"
18
19 mtime=`stat --format="%Z" ${data_file}`
20 next=$[${mtime} + ${ip_to_location} * 24 * 3600]
21 echo "mtime=${mtime}, next=${next}, now=`date +%s`"
22 if [ "${next}" -ge "`date +%s`" ]; then
23 # do not update yet
24 exit 0
25 fi
26
27 # wait some random time...
28 sleep $[${RANDOM}%120]
29
30 wget "http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip" -O "${data_file}"
31 unzip "${data_file}" 1>/dev/null
32 rm -f "${data_file}
File crons/ip2c_maxmind-ipv4 added (mode: 100644) (index 0000000..ab09c3d)
1 #!/bin/bash
2
3 # ip2clue downloader
4 # Author: Catalin(ux) M. BOIE
5 # Home: http://www.maxmind.com/app/geolitecountry
6 # TODO: Use ETag to not download again the data!
7
8 source /etc/ip2clue/download.conf
9 if [ "${maxmind_ipv4}" = "0" ]; then
10 exit 0
11 fi
12
13 set -e
14
15 cd /var/cache/ip2clue
16
17 data_file="maxmind-ipv4.csv.zip"
18
19 mtime=`stat --format="%Z" ${data_file}`
20 next=$[${mtime} + ${maxmind_ipv4} * 24 * 3600]
21 echo "mtime=${mtime}, next=${next}, now=`date +%s`"
22 if [ "${next}" -ge "`date +%s`" ]; then
23 # do not update yet
24 exit 0
25 fi
26
27 # wait some random time...
28 sleep $[${RANDOM}%120]
29
30 wget "http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip" -O "${data_file}"
31 unzip "${data_file}" 1>/dev/null
32 rm -f "${data_file}"
File crons/ip2c_maxmind-ipv6 added (mode: 100644) (index 0000000..8a8ae6d)
1 #!/bin/bash
2
3 # ip2clue downloader
4 # Author: Catalin(ux) M. BOIE
5 # Home: http://www.maxmind.com/app/geolitecountry
6 # TODO: Use ETag to not download again the data!
7
8 source /etc/ip2clue/download.conf
9 if [ "${maxmind_ipv6}" = "0" ]; then
10 exit 0
11 fi
12
13 set -e
14
15 cd /var/cache/ip2clue
16
17 data_file="maxmind-ipv6.csv.gz"
18
19 mtime=`stat --format="%Z" ${data_file}`
20 next=$[${mtime} + ${maxmind_ipv6} * 24 * 3600]
21 echo "mtime=${mtime}, next=${next}, now=`date +%s`"
22 if [ "${next}" -ge "`date +%s`" ]; then
23 # do not update yet
24 exit 0
25 fi
26
27 # wait some random time...
28 sleep $[${RANDOM}%120]
29
30 wget "http://geolite.maxmind.com/download/geoip/database/GeoIPv6-20100330.csv.gz" -O "${data_file}"
31 gzip -d "${data_file}"
File crons/ip2c_software77 added (mode: 100644) (index 0000000..ba6ddd5)
1 #!/bin/bash
2
3 # ip2clue downloader
4 # Author: Catalin(ux) M. BOIE
5 # Home: http://software77.net/
6
7 source /etc/ip2clue/download.conf
8 if [ "${iptocountry}" = "0" ]; then
9 exit 0
10 fi
11
12 set -e
13
14 cd /var/cache/ip2clue
15
16 old_md5="IpToCountry.csv.md5"
17 new_md5="IpToCountry.csv.new.md5"
18 data_file="IpToCountry.csv.gz"
19
20 mtime=`stat --format="%Z" ${data_file}`
21 next=$[${mtime} + ${iptocountry} * 24 * 3600]
22 echo "mtime=${mtime}, next=${next}, now=`date +%s`"
23 if [ "${next}" -ge "`date +%s`" ]; then
24 # do not update yet
25 exit 0
26 fi
27
28 wget "http://software77.net/geo-ip?DL=3" -O "${new_md5}"
29
30 # wait some random time...
31 sleep $[${RANDOM}%120]
32
33 download=1
34 if [ -r "${old_md5}" ]; then
35 cmp "${new_md5}" "${old_md5}"
36 if [ "${?}" = "0" ]; then
37 download=0
38 fi
39 fi
40
41 if [ "${download}" = "0" ]; then
42 rm -f "${new_md5}"
43 exit 0
44 fi
45
46 wget "http://software77.net/geo-ip?DL=1" -O "${data_file}"
47 mv "${new_md5}" "${old_md5}"
48 gzip -d "${data_file}"
File duilder added (mode: 100755) (index 0000000..c0f3071)
1 #!/bin/bash
2
3 function git_last_changelog()
4 {
5 changelog=""
6
7 # get the list of tags
8 i=0
9 number_of_tags=0
10 for tag in `git tag -l`; do
11 if [ "${tag:0:1}" != "v" ]; then
12 # skip other kind of tags beside versions
13 continue
14 fi
15
16 tags[${i}]=${tag}
17 tags_commit[${i}]=`git show-ref ${tag} | cut -d' ' -f1`
18 number_of_tags=$[${number_of_tags}+1]
19
20 i=$[${i}+1]
21 done
22
23 # get the list of commits, test if is a tag and do the diff
24 prev=""
25 git log --pretty=oneline | cut -f1 | \
26 while read commit junk; do
27 # test if it is a tag
28 tag=""
29 i=0
30 while [ "${i}" -lt "${number_of_tags}" ]; do
31 if [ "${commit}" = "${tags_commit[${i}]}" ]; then
32 tag="${tags[${i}]}"
33 break
34 fi
35
36 i=$[${i}+1]
37 done
38
39 if [ -z "${tag}" ]; then
40 continue
41 fi
42
43 if [ ! -z "${prev}" ]; then
44 current=${tag}
45 git shortlog ${current}..${prev} | \
46 (IFS=""
47 while read line; do
48 echo " ${line}"
49 done)
50 echo
51 break
52 fi
53 prev=${tag}
54 done
55 }
56
57 function duilder_final()
58 {
59 PRJ="${1}"
60 VER="${2}"
61 RELEASE_SCRIPT="${3}"
62
63 # Run release script
64 if [ ! -z "${RELEASE_SCRIPT}" -a -x "${RELEASE_SCRIPT}" ]; then
65 echo "Running ${RELEASE_SCRIPT}..."
66 export CHANGELOG=`git_last_changelog`
67 ${RELEASE_SCRIPT}
68 fi
69 }
70
71 function duilder_docs()
72 {
73 PRJ="${1}"
74 VER="${2}"
75 EXPORT_PATH="${3}"
76
77 if [ ! -d "${EXPORT_PATH}" ]; then
78 echo "WARN: ${EXPORT_PATH} does not exists. Skipping..."
79 return
80 fi
81
82 echo "Copying docs to [${EXPORT_PATH}]..."
83 for f in README License LICENSE Changelog TODO FAQ INSTALL; do
84 if [ -r "${f}" ]; then
85 cp -vp "${f}" "${EXPORT_PATH}/"
86 fi
87 done
88
89 if [ -d "screenshot" ]; then
90 echo "Copying screenshots..."
91 mkdir -p "${EXPORT_PATH}"
92 cp -vp screenshot/* "${EXPORT_PATH}/"
93 fi
94 }
95
96 function duilder_git()
97 {
98 PRJ="${1}"
99 GIT_DEST="${2}"
100 EXPORT_GIT="${3}"
101 GIT_CHANGELOG="${4}"
102 GIT_PUSH="${5}"
103
104 if [ ! -x /usr/bin/git ]; then
105 echo "Warning: Git not found!"
106 exit 0
107 fi
108
109 if [ ! -d .git ]; then
110 echo "Warning: I cannot find .git directory!"
111 exit 0
112 fi
113
114 if [ "${EXPORT_GIT}" = "1" ]; then
115 echo "Generate GIT tree for HTTP transport..."
116 if [ ! -d "${GIT_DEST}/${PRJ}.git" ]; then
117 git clone --bare . "${GIT_DEST}/${PRJ}.git"
118
119 # Activate post-update hook
120 cp "${GIT_DEST}/${PRJ}.git/hooks/post-update.sample" \
121 "${GIT_DEST}/${PRJ}.git/hooks/post-update"
122 chmod a+x "${GIT_DEST}/${PRJ}.git/hooks/post-update"
123
124 # add project name and description
125 echo "${PRJ}" > "${GIT_DEST}/${PRJ}.git/description"
126
127 # allow export by git daemon?
128 #touch "${GIT_DEST}/${PRJ}.git/git-daemon-export-ok
129 else
130 # --force?
131 echo "Running git push -v --all \"${GIT_DEST}/${PRJ}.git\"..."
132 git push -v --all "${GIT_DEST}/${PRJ}.git"
133 echo "Running git push -v --tags \"${GIT_DEST}/${PRJ}.git\"..."
134 git push -v --tags "${GIT_DEST}/${PRJ}.git"
135 fi
136 (cd "${GIT_DEST}/${PRJ}.git" && git update-server-info)
137 fi
138
139 if [ "${GIT_PUSH}" = "1" ]; then
140 echo "[*] Git push..."
141 git push -v --all
142 fi
143
144 if [ "${GIT_CHANGELOG}" = "1" ]; then
145 echo "[*] Generating Changelog from git..."
146 echo -n > Changelog
147
148 # get the list of tags
149 i=0
150 number_of_tags=0
151 for tag in `git tag -l`; do
152 if [ "${tag:0:1}" != "v" ]; then
153 # skip other kind of tags beside versions
154 continue
155 fi
156
157 tags[${i}]=${tag}
158 tags_commit[${i}]=`git show-ref ${tag} | cut -d' ' -f1`
159 number_of_tags=$[${number_of_tags}+1]
160
161 i=$[${i}+1]
162 done
163
164 # get the list of commits, test if is a tag and do the diff
165 prev=""
166 git log --pretty=oneline | cut -f1 | \
167 while read commit junk; do
168 # test if it is a tag
169 tag=""
170 i=0
171 while [ "${i}" -lt "${number_of_tags}" ]; do
172 if [ "${commit}" = "${tags_commit[${i}]}" ]; then
173 tag="${tags[${i}]}"
174 break
175 fi
176
177 i=$[${i}+1]
178 done
179
180 if [ -z "${tag}" ]; then
181 continue
182 fi
183
184 if [ ! -z "${prev}" ]; then
185 echo "[*] Generating Changelog from ${tag} -> ${prev}..."
186 echo "[${tag} -> ${prev}]" >> Changelog
187 echo >> Changelog
188 git shortlog ${tag}..${prev} | \
189 (IFS=""
190 while read line; do
191 echo " ${line}"
192 done) \
193 >> Changelog
194 echo >> Changelog
195 fi
196 prev=${tag}
197 done
198 fi
199 }
200
201 function duilder_srpm()
202 {
203 PRJ="${1}"
204 VER="${2}"
205 EXPORT_PATH="${3}"
206 BUILD_SRPM="${4}"
207 SRPM_DEST="${5}"
208 SRPM_POST_RUN="${6}"
209
210 P="${PRJ}-${VER}"
211
212 if [ ! -d "${EXPORT_PATH}" ]; then
213 echo "WARN: ${EXPORT_PATH} does not exists. Skipping..."
214 return
215 fi
216
217 if [ "${BUILD_SRPM}" != "1" ]; then
218 exit 0
219 fi
220
221 echo "Building SRPM..."
222 rpmbuild -ts "${P}.tar.gz"
223
224 PKG="${RPMBUILD}/SRPMS/${P}-1.src.rpm"
225
226 # Run a rpmlint on it
227 if [ -x /usr/bin/rpmlint ]; then
228 echo "[*] RPMlinting..."
229 rpmlint -iv "${PKG}" > rpmlint.out
230 fi
231
232 if [ ! -z "${SRPM_DEST}" ]; then
233 echo "Copying [${PKG}] to [${SRPM_DEST}]..."
234 cp -vp "${PKG}" "${SRPM_DEST}/"
235 fi
236
237 echo "Copying to export dir [${EXPORT_PATH}]..."
238 mkdir -p "${EXPORT_PATH}"
239 cp -vp "${PKG}" "${EXPORT_PATH}/"
240
241 if [ -x "${SRPM_POST_RUN}" ]; then
242 echo "Running post SRPM build script [${SRPM_POST_RUN}]..."
243 ${SRPM_POST_RUN} "${PKG}"
244 fi
245 }
246
247 function duilder_tar()
248 {
249 PRJ="${1}"
250 VER="${2}"
251 EXPORT_PATH="${3}"
252 EXCLUDE="${4}"
253
254 P="${PRJ}-${VER}"
255
256 if [ ! -d "${EXPORT_PATH}" ]; then
257 echo "WARN: ${EXPORT_PATH} does not exists. Skipping..."
258 return
259 fi
260
261 echo "Generating tarball [${P}.tar.gz]..."
262 ADD_EXCLUDE=""
263 if [ ! -z "${EXCLUDE}" ]; then
264 ADD_EXCLUDE="--exclude-from ${P}/${EXCLUDE}"
265 fi
266
267 (cd .. \
268 && rm -rf "${P}" \
269 && cp -a --link "${PRJ}" "${P}" \
270 && tar czf "${PRJ}/${P}.tar.gz" \
271 --exclude-vcs \
272 --exclude ${P}/Makefile \
273 ${ADD_EXCLUDE} \
274 "${P}" \
275 && rm -rf "${P}"
276 )
277
278 echo "Copying source to ${EXPORT_PATH}/..."
279 mkdir -p "${EXPORT_PATH}"
280 cp -vp "${P}.tar.gz" "${EXPORT_PATH}/"
281 }
282
283 ####################################################################
284
285 # Variables
286 if [ -d "${HOME}/rpmbuild" ]; then
287 RPMBUILD="${HOME}/rpmbuild"
288 else
289 RPMBUILD="/usr/src/redhat"
290 fi
291
292
293 # Multiplexer
294 if [ "${1}" = "docs" ]; then
295 shift
296 duilder_docs "$@"
297 exit $?
298 fi
299
300 if [ "${1}" = "tar" ]; then
301 shift
302 duilder_tar "$@"
303 exit $?
304 fi
305
306 if [ "${1}" = "git" ]; then
307 shift
308 duilder_git "$@"
309 exit $?
310 fi
311
312 if [ "${1}" = "srpm" ]; then
313 shift
314 duilder_srpm "$@"
315 exit $?
316 fi
317
318 if [ "${1}" = "final" ]; then
319 shift
320 duilder_final "$@"
321 exit $?
322 fi
323
324 if [ ! -r duilder.conf ]; then
325 echo "You must build a duilder.conf file!"
326 exit 1
327 fi
328
329
330 ###### Main stuff
331
332 source ${PWD}/duilder.conf
333
334 # fixes
335 if [ -z "${GIT_DEST}" ]; then
336 GIT_DEST="${EXPORT_PATH}"
337 fi
338
339 # export variables - just in case a script cares
340 export PRJ VER REV EXPORT_PATH EXPORT_GIT GIT_PUSH GIT_DEST SRPMS_DEST LICENSE
341
342 if [ -z "${PRJ}" ]; then
343 echo "ERROR: PRJ= parameter is missing."
344 exit 1
345 fi
346
347 if [ -z "${VER}" ]; then
348 echo "ERROR: PRJ= parameter is missing."
349 exit 1
350 fi
351
352 if [ -z "${REV}" ]; then
353 echo "ERROR: REV= parameter is missing."
354 exit 1
355 fi
356
357 echo
358 echo "Duilder builder script"
359 echo "Copyright Catalin(ux) M BOIE"
360 echo
361 echo "PRJ=${PRJ}, VER=${VER}, REV=${REV}"
362 echo "System: `uname -a`"
363
364 ETC="/etc"
365 BIN="/bin"
366 USR_BIN="/usr/bin"
367 USR_SBIN="/usr/sbin"
368 USR_INCLUDE="/usr/include"
369 USR_LIB="/usr/lib"
370 USR_SHARE_DOC="/usr/share/doc/${PRJ}-${VER}"
371 SBIN="/usr/sbin"
372 VAR_LOG="/var/log/${PRJ}"
373
374 while [ "${1}" != "" ]; do
375 VAR="`echo ${1} | cut -d'=' -f1`"
376 VAL="`echo ${1} | cut -d'=' -f2`"
377 case ${VAR} in
378 --sysconfdir)
379 ETC="${VAL}"
380 ;;
381 --bindir)
382 USR_BIN="${VAL}"
383 ;;
384 --sbindir)
385 USR_SBIN="${VAL}"
386 ;;
387 --includedir)
388 USR_INCLUDE="${VAL}"
389 ;;
390 --libdir)
391 USR_LIB="${VAL}"
392 ;;
393 esac
394 shift
395 done
396
397 # Truncate future sed file
398 > tmp.sed
399
400 DB_SUPPORT=0
401
402 echo -n "Searching for PostgreSQL..."
403 set +e
404 PG_VERSION="`pg_config --version 2>/dev/null`"
405 set -e
406 if [ -z "${PG_VERSION}" ]; then
407 echo " not found."
408 PG_FOUND=0
409 else
410 echo " found version ${PG_VERSION}."
411 PG_FOUND=1
412 PG_INC="-I`pg_config --includedir`"
413 PG_LIB="-L`pg_config --libdir` -lpq"
414
415 echo "s#@PG_VERSION@#${PG_VERSION}#g" >> tmp.sed
416 echo "s#@PG_INC@#${PG_INC}#g" >> tmp.sed
417 echo "s#@PG_LIB@#${PG_LIB}#g" >> tmp.sed
418
419 DB_SUPPORT=1
420 echo "s#@DB_SUPPORT@#${DB_SUPPORT}#g" >> tmp.sed
421 fi
422 echo "s#@PG_FOUND@#${PG_FOUND}#g" >> tmp.sed
423
424
425 echo -n "Searching for MySQL..."
426 set +e
427 MYSQL_VERSION="`mysql_config --version 2>/dev/null`"
428 set -e
429 if [ -z "${MYSQL_VERSION}" ]; then
430 echo " not found."
431 MYSQL_FOUND=0
432 else
433 echo " found version ${MYSQL_VERSION}."
434 MYSQL_FOUND=1
435 MYSQL_INC="`mysql_config --include`"
436 MYSQL_LIB="`mysql_config --libs`"
437
438 echo "s#@MYSQL_VERSION@#${MYSQL_VERSION}#g" >> tmp.sed
439 echo "s#@MYSQL_INC@#${MYSQL_INC}#g" >> tmp.sed
440 echo "s#@MYSQL_LIB@#${MYSQL_LIB}#g" >> tmp.sed
441
442 DB_SUPPORT=1
443 echo "s#@DB_SUPPORT@#${DB_SUPPORT}#g" >> tmp.sed
444 fi
445 echo "s#@MYSQL_FOUND@#${MYSQL_FOUND}#g" >> tmp.sed
446
447 echo -n "Searching for poll..."
448 set +e
449 echo -e "#include <poll.h> \n int main(void) { return poll(0, 0, 0); }" | gcc -x c -pipe - -o /dev/null 2>/dev/null
450 E="${?}"
451 set -e
452 if [ "${E}" != "0" ]; then
453 echo " not found."
454 echo "s#@POLL_FOUND@#0#g" >> tmp.sed
455 else
456 echo " found."
457 echo "s#@POLL_FOUND@#1#g" >> tmp.sed
458 fi
459
460 echo -n "Searching for epoll..."
461 set +e
462 echo -e "#include <sys/epoll.h> \n int main(void) { return epoll_create(64); }" | gcc -x c -pipe - -o /dev/null 2>/dev/null
463 E="${?}"
464 set -e
465 if [ "${E}" != "0" ]; then
466 echo " not found."
467 echo "s#@EPOLL_FOUND@#0#g" >> tmp.sed
468 else
469 echo " found."
470 echo "s#@EPOLL_FOUND@#1#g" >> tmp.sed
471 fi
472
473 # generic stuff
474 echo "s#@PRJ@#${PRJ}#g" >> tmp.sed
475 echo "s#@VER@#${VER}#g" >> tmp.sed
476 echo "s#@REV@#${REV}#g" >> tmp.sed
477 echo "s#@DESCRIPTION@#${DESCRIPTION}#g" >> tmp.sed
478
479 echo "s#@ETC@#${ETC}#g" >> tmp.sed
480 echo "s#@BIN@#${BIN}#g" >> tmp.sed
481 echo "s#@USR_BIN@#${USR_BIN}#g" >> tmp.sed
482 echo "s#@SBIN@#${SBIN}#g" >> tmp.sed
483 echo "s#@USR_SBIN@#${USR_SBIN}#g" >> tmp.sed
484 echo "s#@VAR_LOG@#${VAR_LOG}#g" >> tmp.sed
485 echo "s#@USR_INCLUDE@#${USR_INCLUDE}#g" >> tmp.sed
486 echo "s#@USR_INC@#${USR_INCLUDE}#g" >> tmp.sed
487 echo "s#@USR_LIB@#${USR_LIB}#g" >> tmp.sed
488 echo "s#@USR_SHARE_DOC@#${USR_SHARE_DOC}#g" >> tmp.sed
489 # Export stuff
490 echo "s#@EXPORT_PATH@#${EXPORT_PATH}#g" >> tmp.sed
491
492
493
494 if [ -r Makefile.in ]; then
495 echo "Building Makefile..."
496 echo -n > Makefile
497 echo "# duilder header starts #" >> Makefile
498 echo "export PRJ := ${PRJ}" >> Makefile
499 echo "export VER := ${VER}" >> Makefile
500 echo "export REV := ${REV}" >> Makefile
501 echo "export DESTDIR" >> Makefile
502 echo >> Makefile
503 echo "export I_ETC := \$(DESTDIR)${ETC}" >> Makefile
504 echo "export I_BIN := \$(DESTDIR)${BIN}" >> Makefile
505 echo "export I_SBIN := \$(DESTDIR)${SBIN}" >> Makefile
506 echo "export I_USR_BIN := \$(DESTDIR)${USR_BIN}" >> Makefile
507 echo "export I_USR_SBIN := \$(DESTDIR)${USR_SBIN}" >> Makefile
508 echo "export I_USR_INCLUDE := \$(DESTDIR)${USR_INCLUDE}" >> Makefile
509 echo "export I_USR_INC := \$(DESTDIR)${USR_INCLUDE}" >> Makefile
510 echo "export I_USR_SHARE_DOC := \$(DESTDIR)${USR_SHARE_DOC}" >> Makefile
511 echo "export I_USR_LIB := \$(DESTDIR)${USR_LIB}" >> Makefile
512 echo "export I_LIB := \$(DESTDIR)${USR_LIB}" >> Makefile
513 echo "export I_VAR_LOG := \$(DESTDIR)${VAR_LOG}" >> Makefile
514 echo >> Makefile
515 echo "# DB stuff" >> Makefile
516 echo "export DB_SUPPORT := ${DB_SUPPORT}" >> Makefile
517 echo "# PG" >> Makefile
518 echo "export PG_FOUND := ${PG_FOUND}" >> Makefile
519 echo "export PG_INC := ${PG_INC}" >> Makefile
520 echo "export PG_LIB := ${PG_LIB}" >> Makefile
521 echo "# MySQL" >> Makefile
522 echo "export MYSQL_FOUND := ${MYSQL_FOUND}" >> Makefile
523 echo "export MYSQL_INC := ${MYSQL_INC}" >> Makefile
524 echo "export MYSQL_LIB := ${MYSQL_LIB}" >> Makefile
525 echo >> Makefile
526 echo "# duilder header ends #" >> Makefile
527 echo >> Makefile
528
529 sed -f tmp.sed Makefile.in >> Makefile
530
531 echo >> Makefile
532 echo "# duilder tail starts #" >> Makefile
533 echo >> Makefile
534 echo "# This is to allow exporting only the git tree" >> Makefile
535 echo "dist_git:" >> Makefile
536 echo " @./duilder git \"\$(PRJ)\" \"${GIT_DEST}\" \"${EXPORT_GIT}\" \"${EXPORT_PATH}\" \"${GIT_CHANGELOG}\"" >> Makefile
537 echo >> Makefile
538 echo ".PHONY: dist" >> Makefile
539 echo "dist: clean" >> Makefile
540 echo " @./duilder git \"\$(PRJ)\" \"${GIT_DEST}\" \"${EXPORT_GIT}\" \"${GIT_CHANGELOG}\"" \"${GIT_PUSH}\" >> Makefile
541 echo " @./duilder tar \"\$(PRJ)\" \"\$(VER)\" \"${EXPORT_PATH}\" \"${EXCLUDE}\"" >> Makefile
542 echo " @./duilder srpm \"\$(PRJ)\" \"\$(VER)\" \"${EXPORT_PATH}\" \"${BUILD_SRPM}\" \"${SRPM_DEST}\" \"${SRPM_POST_RUN}\"" >> Makefile
543 echo " @./duilder docs \"\$(PRJ)\" \"\$(VER)\" \"${EXPORT_PATH}\"" >> Makefile
544 echo " @./duilder final \"\$(PRJ)\" \"\$(VER)\" \"${RELEASE_SCRIPT}\"" >> Makefile
545 echo " @rm -f \"\$(PRJ)-\$(VER).tar.gz\"" >> Makefile
546 echo >> Makefile
547 fi
548
549 if [ -r "${PRJ}.spec.in" ]; then
550 echo "Generate .spec file..."
551 sed -f tmp.sed ${PRJ}.spec.in > ${PRJ}.spec
552 fi
553
554 if [ ! -z "${CONFIG_H}" ]; then
555 echo "Generating ${CONFIG_H} file..."
556 sed -f tmp.sed ${CONFIG_H}.in > ${CONFIG_H}
557 fi
558
559 rm -f tmp.sed
560
561 if [ "`basename ${0}`" = "duilderx" ]; then
562 echo "Clone myself to destination as 'duilder'..."
563 cp -vpf "${0}" ${PWD}/duilder
564 fi
565
566 echo "Done. Run make."
File duilder.conf added (mode: 100644) (index 0000000..89b5ccf)
1 PRJ="ip2clue"
2 VER="0.0.90"
3 REV="1"
4 EXCLUDE=".exclude"
5 EXPORT_PATH="/BIG1T/sync1/www/umbrella/kernel/us/ip2clue"
6 EXPORT_GIT="0"
7 GIT_PUSH="1"
8 GIT_CHANGELOG="1"
9 BUILD_SRPM="1"
10 SRPM_DEST="../../Dev/dinorepo/fedora/SRPMS"
11 SRPM_POST_RUN="/usr/local/bin/submit_package"
12 BUILD_TGZ="1"
13 BUILD_DEB="1"
14
15 RELEASE_SCRIPT="./duilder_release"
File duilder_release added (mode: 100755) (index 0000000..d17aa70)
1 #!/bin/bash
2
3 file="/BIG1T/sync1/www/umbrella/kernel/news/raw/`date +%Y-%m-%d`-${PRJ}.news"
4 >${file}
5
6 echo "${PRJ} ${VER} is out!" >> ${file}
7 echo "/us/" >> ${file}
8 echo "linux release userspace" >> ${file}
9 echo "Released ${VER} of ${PRJ}." >> ${file}
10
11 if [ ! -z "${CHANGELOG}" ]; then
12 echo "Changelog:<br />" >> ${file}
13 echo "<pre>" >> ${file}
14 echo "${CHANGELOG}" >> ${file}
15 echo "</pre>" >> ${file}
16 fi
File etc/download.conf added (mode: 100644) (index 0000000..f7cb311)
1 # This file controls the download frequency of the databases, in days
2 # Here, enable providers (0=no update, >0=number of days between updates)
3 maxmind_ipv4=0
4 maxmind_ipv6=0
5 iptolocation=0
6 ip_to_location=0
File etc/ip2clued.conf added (mode: 100644) (index 0000000..437e882)
1 # Where we can find input files.
2 # Do not use subdirectories.
3 datadir = /var/cache/ip2clue
4
5 # Now, follows the input files, in preference order (if the IP is not found in
6 # first file, it will be searched in the second and so on).
7 # The format is: type1:file1, type2:file2...
8 # Allowed formats: webhosting, maxmind, maxmind-v6, software77, ip2location.
9 # You can mix IPv4 and IPv6 files.
10 # Example: files = maxmind:maxmind.csv, webhosting:webhosting.csv, software77:software77.csv, ip2location:ip2location_ipv6.bin
11 files = ip2location:IPV6-COUNTRY.SAMPLE.BIN, maxmind:maxmind1.csv
12
13 # Format for answer
14 # %a - country short
15 # %P - IP
16 # %L - country long
17 # %r - region
18 # %c - city
19 # %i - ISP
20 # %x - latitude
21 # %y - longitude
22 # %z - ZIP code
23 # %d - domain
24 # %t - timezone
25 # %n - net speed
26 # %k - IDD
27 # %a - area code
28 # %w - WS code
29 # %W - WE name
30 format = OK ip=%P cs=%s cl=%L areacode=%a region=%r city=%c x=%x y=%y dom=%d tz=%t isp=%i
31
32 # How frequently we check to see if data files were changed, in seconds.
33 # Put 0 for never.
34 refresh = 60
35
36 # TCP/IP port to bind to
37 port = 9999
38
39 # allow ipv4 (0 or 1)?
40 ipv4 = 1
41
42 # allow ipv6 (0 or 1)?
43 ipv6 = 1
44
45 # debug (0-30)
46 debug = 1
47
File i_conf.c added (mode: 100644) (index 0000000..699b1df)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: Configuration file loader
4 */
5
6 #include <i_conf.h>
7
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11
12
13 struct ip2clue_conf *ip2clue_conf_load(const char *file)
14 {
15 FILE *f;
16 char *r;
17 struct ip2clue_conf *ret = NULL, *cur, *prev = NULL;
18 char buf[4096], *p, *q, *e;
19
20 f = fopen(file, "r");
21 if (!f)
22 return NULL;
23
24 while (1) {
25 r = fgets(buf, sizeof(buf), f);
26 if (!r)
27 break;
28
29 if (strlen(buf) == 0)
30 continue;
31
32 if (buf[0] == '#')
33 continue;
34
35 p = buf;
36 q = strchr(p, '=');
37 if (!q)
38 continue;
39 *q = '\0';
40 q += 1;
41 while ((*q == ' ') || (*q == '\t'))
42 q++;
43 e = q + strlen(q) - 1;
44 while ((*e == ' ') || (*e == '\t') || (*e == '\r') || (*e == '\n')) {
45 *e = '\0';
46 e--;
47 }
48
49 while ((*p == ' ') || (*p == '\t'))
50 p++;
51 e = p + strlen(p) - 1;
52 while ((*e == ' ') || (*e == '\t') || (*e == '\r') || (*e == '\n')) {
53 *e = '\0';
54 e--;
55 }
56
57 cur = (struct ip2clue_conf *) malloc(sizeof(struct ip2clue_conf));
58 if (!cur)
59 goto out_close;
60 if (ret == NULL)
61 ret = cur;
62
63 if (prev)
64 prev->next = cur;
65
66 cur->l = strdup(p);
67 cur->r = strdup(q);
68 cur->next = NULL;
69
70 prev = cur;
71 }
72
73 out_close:
74 fclose(f);
75
76 return ret;
77 }
78
79 char *ip2clue_conf_get(const struct ip2clue_conf *c, const char *l)
80 {
81 while (c) {
82 if (strcmp(l, c->l) == 0)
83 return c->r;
84 c = c->next;
85 }
86
87 return NULL;
88 }
89
90 unsigned long ip2clue_conf_get_ul(const struct ip2clue_conf *c, const char *l,
91 const unsigned int base)
92 {
93 char *r;
94
95 r = ip2clue_conf_get(c, l);
96 if (!r)
97 return 0;
98
99 return strtoul(r, NULL, base);
100 }
101
102 void ip2clue_conf_free(struct ip2clue_conf *c)
103 {
104 struct ip2clue_conf *next;
105
106 while (c) {
107 free(c->l);
108 free(c->r);
109 next = c->next;
110 free(c);
111 c = next;
112 }
113 }
File i_conf.h added (mode: 100644) (index 0000000..dd378e8)
1 #ifndef IP2CLUE_CONF_H
2 #define IP2CLUE_CONF_H 1
3
4 #include <i_config.h>
5
6 struct ip2clue_conf
7 {
8 char *l;
9 char *r;
10 struct ip2clue_conf *next;
11 };
12
13
14 extern struct ip2clue_conf *ip2clue_conf_load(const char *file);
15 extern char *ip2clue_conf_get(const struct ip2clue_conf *c,
16 const char *l);
17 extern unsigned long ip2clue_conf_get_ul(const struct ip2clue_conf *c,
18 const char *l, const unsigned int base);
19 extern void ip2clue_conf_free(struct ip2clue_conf *c);
20
21 #endif
File i_config.h added (mode: 100644) (index 0000000..6cd529f)
1 #ifndef IP2CLUE_I_CONFIG_H
2 #define IP2CLUE_I_CONFIG_H 1
3
4 #define _BSD_SOURCE
5 #define _GNU_SOURCE
6
7
8 #endif
File i_types.h added (mode: 100644) (index 0000000..e0051fa)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: types definition
4 */
5
6 #ifndef IP2CLUE_I_TYPES_H
7 #define IP2CLUE_I_TYPES_H 1
8
9 #include <i_config.h>
10
11 #include <time.h>
12
13 enum ip2clue_type
14 {
15 IP2CLUE_TYPE_V4 = 4,
16 IP2CLUE_TYPE_V6 = 6
17 };
18
19 enum ip2clue_format
20 {
21 IP2CLUE_FORMAT_WEBHOSTING = 1,
22 IP2CLUE_FORMAT_MAXMIND,
23 IP2CLUE_FORMAT_MAXMIND_V6,
24 IP2CLUE_FORMAT_SOFTWARE77,
25 IP2CLUE_FORMAT_IP2LOCATION
26 };
27
28 struct ip2clue_extra
29 {
30 char country_long[32];
31 char region[32];
32 char city[32];
33 char isp[64];
34 float latitude;
35 float longitude;
36 char zip[16];
37 char domain[64];
38 char timezone[16];
39 char netspeed[16];
40 char idd[16];
41 char areacode[32];
42 char ws_code[16];
43 char ws_name[32];
44 };
45
46 struct ip2clue_fields
47 {
48 unsigned char ip_start, ip_end;
49 unsigned char ip_bin_start, ip_bin_end;
50 unsigned char country_short, country_long;
51 unsigned char total; /* Total number of expected fields */
52 char comment_chars[8]; /* Put here the chars to ignore at the beggining of line */
53 unsigned char bin_file;
54 enum ip2clue_format format;
55 enum ip2clue_type v4_or_v6;
56 };
57
58 struct ip2clue_split
59 {
60 unsigned int count;
61 char fields[32][256];
62 };
63
64 /* common */
65 struct ip2clue_db
66 {
67 enum ip2clue_format format;
68 enum ip2clue_type v4_or_v6;
69 unsigned long long no_of_cells, current;
70 void *cells;
71 time_t ts; /* Db building time */
72 time_t ts_load; /* Time when the table was loaded. */
73 unsigned int elap_load_ms; /* How much time was needed for load */
74 char file[128]; /* Input file */
75 unsigned long long mem; /* How many bytes this table is using */
76 unsigned int usage_count; /* If 0, we can safely drop it */
77 unsigned long long lookup_ok;
78 unsigned long long lookup_notfound;
79 unsigned long long lookup_malformed;
80 };
81
82 /*
83 * This is a chain of ip2clue_db, ordered by preference
84 */
85 struct ip2clue_list
86 {
87 unsigned int number;
88 struct ip2clue_db **entries;
89 };
90
91 /* v4 */
92 struct ip2clue_cell_v4
93 {
94 unsigned int ip_start, ip_end;
95 char country_short[4];
96 struct ip2clue_extra *extra;
97 };
98
99 /* v6 */
100 struct ip2clue_cell_v6
101 {
102 unsigned int ip_start[4], ip_end[4];
103 char country_short[4];
104 struct ip2clue_extra *extra;
105 };
106
107 #endif
File i_util.c added (mode: 100644) (index 0000000..7288a53)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: parser for IP files
4 */
5
6 #include <i_config.h>
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <errno.h>
17
18 #include <i_util.h>
19
20 char ip2clue_error[256];
21
22 /*
23 * Returns ip2clue_error content
24 */
25 char *ip2clue_strerror(void)
26 {
27 return ip2clue_error;
28 }
29
30 /*
31 * Show a nice IPv6 address
32 * TODO: Replace with inet_ntop!
33 */
34 int ip2clue_addr_v6(char *out, size_t out_size, const unsigned int *a)
35 {
36 snprintf(out, out_size, "%x:%x:%x:%x:%x:%x:%x:%x",
37 a[0] >> 16, a[0] & 0xFFFF,
38 a[1] >> 16, a[1] & 0xFFFF,
39 a[2] >> 16, a[2] & 0xFFFF,
40 a[3] >> 16, a[3] & 0xFFFF);
41
42 return 0;
43 }
44
45 /*
46 * Compare two IPv6 addresses
47 */
48 int ip2clue_compare_v6(const unsigned int *a, const unsigned int *b)
49 {
50 unsigned int i;
51
52 for (i = 0; i < 4; i++) {
53 if (a[i] > b[i])
54 return 1;
55 else if (a[i] < b[i])
56 return -1;
57 }
58
59 return 0;
60 }
61
62 /*
63 * Split a line in fields.
64 * @sep is an string that contains all possible separators
65 * Returns number of fields.
66 */
67 int ip2clue_split(struct ip2clue_split *s, const char *line, const char *sep)
68 {
69 int len, i, j, inside_quote, separator_area;
70
71 len = strlen(line);
72 if (len == 0) {
73 snprintf(ip2clue_error, sizeof(ip2clue_error),
74 "empty string passed for splitting");
75 return -1;
76 }
77
78 inside_quote = 0;
79 separator_area = 0;
80 s->count = 1; /* There is at least one field! */
81 j = 0;
82 s->fields[s->count - 1][j] = '\0';
83 for (i = 0; i < len; i++) {
84 /* Parse only first 32 fields */
85 if (s->count == 32)
86 break;
87
88 if ((line[i] == '\r') || (line[i] == '\n'))
89 break;
90
91 if (line[i] == '"') {
92 if (inside_quote == 1) {
93 inside_quote = 0;
94 } else {
95 inside_quote = 1;
96 }
97 continue;
98 }
99
100 if ((inside_quote == 0) && (strchr(sep, line[i]))) {
101 /* Found separator */
102 if (separator_area == 1)
103 continue;
104
105 /* Terminate previous string */
106 s->fields[s->count - 1][j] = '\0';
107 /* Move to next field */
108 s->count++;
109 j = 0;
110 s->fields[s->count - 1][j] = '\0';
111 separator_area = 1;
112 continue;
113 }
114
115 separator_area = 0;
116
117 /* Field unusually large? */
118 if (j == sizeof(s->fields[s->count - 1]) - 2) {
119 snprintf(ip2clue_error, sizeof(ip2clue_error),
120 "field is too long");
121 return -1;
122 }
123
124 s->fields[s->count - 1][j++] = line[i];
125 s->fields[s->count - 1][j] = '\0';
126 }
127
128 /* Unterminated line? */
129 if (inside_quote == 1) {
130 snprintf(ip2clue_error, sizeof(ip2clue_error),
131 "invalid number of quotes");
132 return -1;
133 }
134
135 return s->count;
136 }
137
138 /*
139 * Returns the number of lines for a file
140 */
141 long ip2clue_file_lines(const char *file)
142 {
143 char buf[1024 * 1024];
144 int fd;
145 ssize_t n, i;
146 long ret = -1;
147
148 fd = open(file, O_RDONLY);
149 if (fd == -1) {
150 snprintf(ip2clue_error, sizeof(ip2clue_error),
151 "cannot open file [%s] (%s)",
152 file, strerror(errno));
153 return -1;
154 }
155
156 ret = 0;
157 while (1) {
158 n = read(fd, buf, sizeof(buf));
159 if (n == -1) {
160 snprintf(ip2clue_error, sizeof(ip2clue_error),
161 "cannot read (%s)", strerror(errno));
162 close(fd);
163 return -1;
164 } else if (n == 0) {
165 break;
166 }
167
168 for (i = 0; i < n; i++)
169 if (buf[i] == '\n')
170 ret++;
171 }
172
173 close(fd);
174
175 return ret;
176 }
177
178 /*
179 * Destroy data
180 */
181 void ip2clue_destroy(struct ip2clue_db *db)
182 {
183 unsigned int i;
184 struct ip2clue_cell_v4 *p_cell4, *cell4;
185 struct ip2clue_cell_v6 *p_cell6, *cell6;
186
187 if (db == NULL)
188 return;
189
190 db->usage_count--;
191 if (db->usage_count > 0)
192 return;
193
194 /* TODO: put 'for' under 'p_cell4 = ' */
195 for (i = 0; i < db->no_of_cells; i++) {
196 if (db->v4_or_v6 == IP2CLUE_TYPE_V4) {
197 p_cell4 = (struct ip2clue_cell_v4 *) db->cells;
198 cell4 = &p_cell4[i];
199 if (cell4->extra != NULL)
200 free(cell4->extra);
201 } else {
202 p_cell6 = (struct ip2clue_cell_v6 *) db->cells;
203 cell6 = &p_cell6[i];
204 if (cell6->extra != NULL)
205 free(cell6->extra);
206 }
207 }
208
209 if (db->cells != NULL)
210 free(db->cells);
211
212 free(db);
213 }
214
215
216 /*
217 * Search for an IPv4
218 */
219 struct ip2clue_cell_v4 *ip2clue_search_v4(struct ip2clue_db *db,
220 const char *s_ip)
221 {
222 unsigned int ip;
223 struct in_addr in;
224 long left, middle, right;
225 struct ip2clue_cell_v4 *cells;
226
227 if (inet_pton(AF_INET, s_ip, &in) != 1) {
228 snprintf(ip2clue_error, sizeof(ip2clue_error),
229 "malformed address");
230 db->lookup_malformed++;
231 return NULL;
232 }
233
234 ip = ntohl(in.s_addr);
235
236 left = 0;
237 right = db->no_of_cells - 1;
238 cells = (struct ip2clue_cell_v4 *) db->cells;
239 while (right >= left) {
240 middle = (right + left + 1) / 2;
241
242 /*
243 printf("ip=%u left=%ld (%u->%u), middle=%ld (%u->%u), right=%ld (%u->%u)\n",
244 ip,
245 left, db->cells[left].ip_start, db->cells[left].ip_end,
246 middle, db->cells[middle].ip_start, db->cells[middle].ip_end,
247 right, db->cells[right].ip_start, db->cells[right].ip_end);
248 */
249
250 if (ip > cells[middle].ip_end) {
251 left = middle + 1;
252 if (left == db->no_of_cells)
253 break;
254 } else if (ip < cells[middle].ip_start) {
255 right = middle - 1;
256 if (right == -1)
257 break;
258 } else {
259 db->lookup_ok++;
260 return &cells[middle];
261 }
262 }
263
264 snprintf(ip2clue_error, sizeof(ip2clue_error),
265 "cannot find address");
266 db->lookup_notfound++;
267
268 return NULL;
269 }
270
271 /*
272 * search for an IPv6
273 */
274 struct ip2clue_cell_v6 *ip2clue_search_v6(struct ip2clue_db *db, const char *s_ip)
275 {
276 unsigned int ip[4], i;
277 struct in6_addr in;
278 long left, middle, right;
279 struct ip2clue_cell_v6 *cells;
280 /*char a1[64], a2[64], a3[64];*/
281
282 if (inet_pton(AF_INET6, s_ip, &in) != 1) {
283 snprintf(ip2clue_error, sizeof(ip2clue_error),
284 "malformed address");
285 db->lookup_malformed++;
286 return NULL;
287 }
288
289 for (i = 0; i < 4; i++)
290 ip[i] = ntohl(in.s6_addr32[i]);
291
292 left = 0;
293 right = db->no_of_cells - 1;
294 cells = (struct ip2clue_cell_v6 *) db->cells;
295 while (right >= left) {
296 middle = (right + left + 1) / 2;
297
298 /*
299 ip2clue_addr_v6(a1, sizeof(a1), ip);
300 ip2clue_addr_v6(a2, sizeof(a2), cells[middle].ip_start);
301 ip2clue_addr_v6(a3, sizeof(a3), cells[middle].ip_end);
302 printf("Comparing ip=%s with ip_start=%s ip_end=%s...\n",
303 a1, a2, a3);
304 */
305 if (ip2clue_compare_v6(ip, cells[middle].ip_end) > 0) {
306 left = middle + 1;
307 if (left == db->no_of_cells)
308 break;
309 } else if (ip2clue_compare_v6(ip, cells[middle].ip_start) < 0) {
310 right = middle - 1;
311 if (right == -1)
312 break;
313 } else {
314 db->lookup_ok++;
315 return &cells[middle];
316 }
317 }
318
319 snprintf(ip2clue_error, sizeof(ip2clue_error),
320 "cannot find address");
321 db->lookup_notfound++;
322
323 return NULL;
324 }
325
326 /*
327 * Dump info about a cell
328 */
329 void ip2clue_dump_cell_v6(char *out, size_t out_size, struct ip2clue_cell_v6 *cell)
330 {
331 char s[64], e[64];
332
333 ip2clue_addr_v6(s, sizeof(s), cell->ip_start);
334 ip2clue_addr_v6(e, sizeof(e), cell->ip_end);
335 snprintf(out, out_size, "%s %s -> %s", cell->country_short, s, e);
336 /* TODO: dump extra! */
337 }
338
339 /*
340 * Print extra structure
341 */
342 void ip2clue_print_extra(char *out, size_t out_size, const struct ip2clue_extra *e)
343 {
344 snprintf(out, out_size, "country_long=%s region=%s city=%s isp=%s latitude=%f"
345 " longitude=%f zip=%s domain=%s timezone=%s netspeed=%s"
346 " idd=%s areacode=%s ws_code=%s ws_name=%s\n",
347 e->country_long, e->region, e->city, e->isp, e->latitude,
348 e->longitude, e->zip, e->domain, e->timezone, e->netspeed,
349 e->idd, e->areacode, e->ws_code, e->ws_name);
350 }
351
352 /*
353 * Search an IP in a list
354 */
355 struct ip2clue_cell_v4 *ip2clue_list_search_v4(struct ip2clue_list *list,
356 const char *ip)
357 {
358 unsigned int i;
359 struct ip2clue_db *db;
360 struct ip2clue_cell_v4 *ret = NULL;
361
362 for (i = 0; i < list->number; i++) {
363 db = list->entries[i];
364 if (db->v4_or_v6 != IP2CLUE_TYPE_V4)
365 continue;
366
367 ret = ip2clue_search_v4(db, ip);
368 if (ret != NULL)
369 break;
370 }
371
372 if (ret == NULL)
373 snprintf(ip2clue_error, sizeof(ip2clue_error),
374 "not found");
375
376 return ret;
377 }
378
379 /*
380 * Returns a IPv4 address from a special IPv6 address
381 * 2002:xxyy:zztt::/48, ::a.b.c.d and ::ffff:a.b.c.d
382 */
383 /* TODO */
384
385 /*
386 * Search an IP in a list
387 */
388 struct ip2clue_cell_v6 *ip2clue_list_search_v6(struct ip2clue_list *list,
389 const char *ip)
390 {
391 unsigned int i;
392 struct ip2clue_db *db;
393 struct ip2clue_cell_v6 *ret = NULL;
394
395 for (i = 0; i < list->number; i++) {
396 db = list->entries[i];
397 if (db->v4_or_v6 != IP2CLUE_TYPE_V6)
398 continue;
399
400 ret = ip2clue_search_v6(db, ip);
401 if (ret != NULL)
402 break;
403 }
404
405 if (ret == NULL) {
406 /* TODO: Now, try special IPv4 encapsulated in IPv6 addresses */
407 }
408
409 if (ret == NULL)
410 snprintf(ip2clue_error, sizeof(ip2clue_error),
411 "not found");
412
413 return ret;
414 }
415
416 /*
417 * Builds a string answer
418 * Returns 0 if address not found or other errors received, else 1.
419 */
420 int ip2clue_list_search(struct ip2clue_list *list, char *out,
421 const unsigned int out_size, const char *format, const char *ip)
422 {
423 struct ip2clue_cell_v4 *v4;
424 struct ip2clue_cell_v6 *v6;
425 struct ip2clue_extra *e;
426 char cs[4];
427 unsigned int rest, i, format_size, special, a_len;
428 char a[256];
429
430 if (strchr(ip, '.')) {
431 v4 = ip2clue_list_search_v4(list, ip);
432 if (v4 == NULL)
433 return 0;
434
435 strcpy(cs, v4->country_short);
436 e = v4->extra;
437 } else {
438 v6 = ip2clue_list_search_v6(list, ip);
439 if (v6 == NULL)
440 return 0;
441
442 strcpy(cs, v6->country_short);
443 e = v6->extra;
444 }
445
446 strcpy(out, "");
447 rest = out_size - 1;
448 format_size = strlen(format);
449 special = 0;
450 for (i = 0; i < format_size; i++) {
451 strcpy(a, "");
452
453 if (format[i] == '%') {
454 if (special == 1) {
455 strcpy(a, "%");
456 special = 0;
457 } else {
458 special = 1;
459 continue;
460 }
461 } else if (special == 1) {
462 if (format[i] == 's') {
463 snprintf(a, sizeof(a), "%s", cs);
464 } else if (format[i] == 'P') {
465 snprintf(a, sizeof(a), "%s", ip);
466 } else if (e != NULL) {
467 switch (format[i]) {
468 case 'L': snprintf(a, sizeof(a), "%s", e->country_long); break;
469 case 'r': snprintf(a, sizeof(a), "%s", e->region); break;
470 case 'c': snprintf(a, sizeof(a), "%s", e->city); break;
471 case 'i': snprintf(a, sizeof(a), "%s", e->isp); break;
472 case 'x': snprintf(a, sizeof(a), "%f", e->latitude); break;
473 case 'y': snprintf(a, sizeof(a), "%f", e->longitude); break;
474 case 'z': snprintf(a, sizeof(a), "%s", e->zip); break;
475 case 'd': snprintf(a, sizeof(a), "%s", e->domain); break;
476 case 't': snprintf(a, sizeof(a), "%s", e->timezone); break;
477 case 'n': snprintf(a, sizeof(a), "%s", e->netspeed); break;
478 case 'k': snprintf(a, sizeof(a), "%s", e->idd); break;
479 case 'a': snprintf(a, sizeof(a), "%s", e->areacode); break;
480 case 'w': snprintf(a, sizeof(a), "%s", e->ws_code); break;
481 case 'W': snprintf(a, sizeof(a), "%s", e->ws_name); break;
482 }
483 }
484 } else {
485 snprintf(a, 2, "%c", format[i]);
486 }
487
488 special = 0;
489
490 a_len = strlen(a);
491 if (a_len > rest) {
492 snprintf(ip2clue_error, sizeof(ip2clue_error),
493 "output buffer too short to add"
494 " more %u bytes (%s)",
495 a_len, a);
496 return 0;
497 }
498
499 strcat(out, a);
500 rest -= a_len;
501 }
502
503 return 1;
504 }
505
File i_util.h added (mode: 100644) (index 0000000..479062a)
1 #ifndef IP2CLUE_I_UTIL_H
2 #define IP2CLUE_I_UTIL_H 1
3
4 #include <i_config.h>
5
6 #include <stdlib.h>
7
8 #include <i_types.h>
9
10 extern char ip2clue_error[256];
11
12 extern char *ip2clue_strerror(void);
13
14 extern int ip2clue_addr_v6(char *out, size_t out_size,
15 const unsigned int *a);
16
17 extern int ip2clue_split(struct ip2clue_split *s, const char *line,
18 const char *sep);
19
20 extern long ip2clue_file_lines(const char *file);
21
22 extern void ip2clue_destroy(struct ip2clue_db *db);
23
24 extern void ip2clue_print_extra(char *out, size_t out_size,
25 const struct ip2clue_extra *e);
26
27 /* v4 */
28 extern struct ip2clue_cell_v4 *ip2clue_search_v4(struct ip2clue_db *db,
29 const char *ip);
30 extern struct ip2clue_cell_v4 *ip2clue_list_search_v4(struct ip2clue_list *list,
31 const char *ip);
32 extern void ip2clue_dump_cell_v4(char *out, size_t out_size,
33 struct ip2clue_cell_v4 *cell); /* TODO */
34
35 /* v6 */
36 extern struct ip2clue_cell_v6 *ip2clue_search_v6(struct ip2clue_db *db,
37 const char *ip);
38 extern struct ip2clue_cell_v6 *ip2clue_list_search_v6(struct ip2clue_list *list,
39 const char *ip);
40 extern void ip2clue_dump_cell_v6(char *out, size_t out_size,
41 struct ip2clue_cell_v6 *cell);
42
43 /* common */
44 extern int ip2clue_list_search(struct ip2clue_list *list,
45 char *out, const unsigned int out_size,
46 const char *format, const char *ip);
47 #endif
File ip2clue.c added (mode: 100644) (index 0000000..6b4dd0c)
1 #include <i_config.h>
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include <Conn.h>
8
9 #include <i_types.h>
10 #include <i_util.h>
11 #include <i_conf.h>
12 #include <parser.h>
13
14 static FILE *Logf = NULL;
15 static int conf_port = 9999;
16 static char cmd[128];
17
18
19 static void connected_cb(struct Conn *C)
20 {
21 Log(1, "Enqueue [%s]...\n", cmd);
22
23 if (Conn_enqueue(C, cmd, strlen(cmd)) == -1) {
24 Log(0, "Cannot enqueue (%s)!\n",
25 Conn_strerror());
26 }
27 }
28
29 static int data_cb(struct Conn *C, char *line)
30 {
31 printf("%s\n", line);
32 Conn_close(C);
33
34 return 0;
35 }
36
37 static void data(struct Conn *C)
38 {
39 Conn_for_every_line(C, data_cb);
40 }
41
42
43 static void error(struct Conn *C)
44 {
45 Log(0, "Error on id=%llu (%s).\n",
46 Conn_getid(C), Conn_strerror());
47 }
48
49 int main(int argc, char *argv[])
50 {
51 struct Conn *C;
52 int ret;
53
54 if (argc < 2) {
55 fprintf(stderr, "Usage: ip2clue <ip> [<ip2clue_daemon_port>]\n");
56 return 1;
57 }
58
59 /* prepare sending */
60 snprintf(cmd, sizeof(cmd), "R%s\n", argv[1]);
61
62 Conn_debug(Logf, 0);
63
64 if (argc >= 3)
65 conf_port = strtol(argv[2], NULL, 10);
66
67 ret = Conn_init(0);
68 if (ret == -1) {
69 Log(0, "Cannot init Conn (%s)!\n",
70 Conn_strerror());
71 return 1;
72 }
73
74 /* set callbacks */
75 Conn_data_cb = data;
76 Conn_error_cb = error;
77 Conn_connected_cb = connected_cb;
78
79 /* prefer IPv6 */
80 Log(1, "IPv6...\n");
81 C = Conn_alloc();
82 if (!C) {
83 Log(0, "Cannot alloc a Conn v6 structure (%s)!\n",
84 Conn_strerror());
85 return 1;
86 }
87
88 Conn_set_socket_domain(C, PF_INET6);
89 Conn_set_socket_type(C, SOCK_STREAM);
90 Conn_set_socket_addr(C, "::1");
91 Conn_set_socket_port(C, conf_port);
92 ret = Conn_commit(C);
93 if (ret != 0) {
94 Log(0, "Cannot commit v6 (%s)!\n",
95 Conn_strerror());
96
97 Log(1, "IPv4...\n");
98 Conn_set_socket_domain(C, PF_INET);
99 Conn_set_socket_type(C, SOCK_STREAM);
100 Conn_set_socket_addr(C, "127.0.0.1");
101 Conn_set_socket_port(C, conf_port);
102 ret = Conn_commit(C);
103 if (ret != 0) {
104 Log(0, "Cannot commit v4 (%s)!\n",
105 Conn_strerror());
106 return 1;
107 }
108 }
109
110 Log(1, "Start polling...\n");
111 while (1) {
112 ret = Conn_poll(-1);
113 if (ret == -1) {
114 Log(0, "Error in Conn_poll (%s)!\n",
115 Conn_strerror());
116 break;
117 } else if (ret == 0) {
118 break;
119 }
120 }
121
122 Conn_shutdown();
123
124 return 0;
125 }
File ip2clue.spec.in added (mode: 100644) (index 0000000..a1f518c)
1 Summary: IP to country (and other information) tools
2 Name: @PRJ@
3 Version: @VER@
4 Release: @REV@
5 License: GPLv3+
6 Group: Applications/Internet
7 Source: http://kernel.embedromix.ro/us/@PRJ@/%{name}-%{version}.tar.gz
8 URL: http://kernel.embedromix.ro/us/
9 BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
10 BuildRequires: Conn >= 1.0.31-1
11 Requires: Conn >= 1.0.31-1, gzip, wget, unzip
12
13 %description
14 A set of tools for IP 2 country look up operations. It has a fast daemon that
15 caches and searches for information and tools on the client side.
16
17 %prep
18 %setup -q
19
20 %build
21 %configure --sysconfdir=/etc
22 make
23
24 %install
25 rm -rf ${RPM_BUILD_ROOT}
26 mkdir -p ${RPM_BUILD_ROOT}
27 make install DESTDIR=${RPM_BUILD_ROOT}
28
29 %clean
30 rm -rf ${RPM_BUILD_ROOT}
31
32 %files
33 %defattr (-,root,root)
34 %{_sbindir}/ip2clued
35 %{_bindir}/ip2clue
36 %{_etcdir}/ip2clue/*
37 %{_etcdir}/rc.d/init.d/ip2clued
38 %{_etcdir}/cron.monthly/*
39 %dir /var/cache/ip2clue
40 %doc README Changelog TODO LICENSE clients
41
42 %post
43
44 %preun
45 if [ "$1" = "0" ]; then
46 /sbin/service ip2clued stop > /dev/null 2>&1
47 /sbin/chkconfig --del ip2clued
48 fi
49
50 %changelog
51 * Fri May 28 2010 <> - 0.0.90
52 - First version.
File ip2clued.c added (mode: 100644) (index 0000000..7092094)
1 #include <i_config.h>
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <pthread.h>
7
8 #include <Conn.h>
9
10 #include <i_types.h>
11 #include <i_util.h>
12 #include <i_conf.h>
13 #include <parser.h>
14
15 static FILE *Logf = NULL;
16 static char *log_file = "ip2clued.log";
17
18 /* conf stuff */
19 static char *conf_filename = "/etc/ip2clue/ip2clue.conf";
20 static char *conf_datadir;
21 static char *conf_files;
22 static char *conf_format;
23 static unsigned int conf_refresh;
24 static unsigned int conf_port;
25 static unsigned int conf_ipv4;
26 static unsigned int conf_ipv6;
27 static unsigned int conf_debug;
28
29 /* This will protect accesses to list 'list' */
30 static pthread_rwlock_t list_rwlock;
31
32 static struct ip2clue_list list;
33
34
35 static int data_cb(struct Conn *C, char *line)
36 {
37 int err;
38 char out[2048], *p;
39
40 switch (line[0]) {
41 case 'R':
42 line += 1;
43
44 p = strchr(line, ' ');
45 if (p)
46 *p = '\0';
47
48 pthread_rwlock_rdlock(&list_rwlock);
49
50 err = ip2clue_list_search(&list, out, sizeof(out) - 1,
51 conf_format, line);
52 if (err < 1)
53 snprintf(out, sizeof(out), "ER ip=%s errmsg=\"%s\"",
54 line, ip2clue_strerror());
55
56 pthread_rwlock_unlock(&list_rwlock);
57
58 break;
59
60 case 'S':
61 pthread_rwlock_rdlock(&list_rwlock);
62 ip2clue_list_stats(out, sizeof(out) - 1, &list);
63 pthread_rwlock_unlock(&list_rwlock);
64 break;
65
66 /* This is only for debug
67 case 'Q':
68 strcpy(out, "Bye!");
69 Conn_stop();
70 break;
71 */
72
73 default:
74 strcpy(out, "errmsg=\"Invalid command\"");
75 break;
76 }
77
78 strcat(out, "\n");
79
80 err = Conn_enqueue(C, out, strlen(out));
81 if (err == -1) {
82 Log(0, "Error in enqueue (%s)!\n",
83 Conn_strerror());
84 Conn_close(C);
85 }
86
87 return 0;
88 }
89
90 static void data(struct Conn *C)
91 {
92 Conn_for_every_line(C, data_cb);
93 }
94
95
96 static void error(struct Conn *C)
97 {
98 Log(1, "Error on id=%llu (%s).\n",
99 Conn_getid(C), Conn_strerror());
100 }
101
102 static void *worker_loader(void *arg)
103 {
104 int ret;
105 struct ip2clue_list list2;
106
107 Log(0, "Loader worker started...\n");
108 while (1) {
109 /* Test if we need to reload files */
110
111 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
112
113 ret = ip2clue_list_refresh(&list2, &list, conf_datadir, conf_files);
114 if (ret != 0) {
115 Log(0, "Cannot refresh list (%s)!"
116 " Sleeping and try again later...\n",
117 ip2clue_strerror());
118 } else {
119 pthread_rwlock_wrlock(&list_rwlock);
120 ret = ip2clue_list_replace(&list, &list2);
121 if (ret != 0)
122 Log(0, "Cannot replace list (%s)!\n",
123 ip2clue_strerror());
124 pthread_rwlock_unlock(&list_rwlock);
125 }
126
127 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
128
129 if (conf_refresh == 0)
130 break;
131
132 sleep(conf_refresh);
133 }
134
135 return NULL;
136 }
137
138 int main(int argc, char *argv[])
139 {
140 struct Conn *C4 = NULL, *C6 = NULL;
141 int ret;
142 pthread_t workers[1];
143 void *res;
144 struct ip2clue_conf *conf;
145
146 Logf = fopen(log_file, "w");
147 if (!Logf) {
148 fprintf(stderr, "Cannot open log file [%s] (%s)!\n",
149 log_file, strerror(errno));
150 return 1;
151 }
152 setlinebuf(Logf);
153
154
155 /* Load config file */
156 if (argc > 1)
157 conf_filename = argv[1];
158 Log(1, "Loading configuration from file [%s]...\n", conf_filename);
159 conf = ip2clue_conf_load(conf_filename);
160 if (conf == NULL) {
161 Log(0, "Cannot load conf file [%s]!\n", conf_filename);
162 return 1;
163 }
164
165 /* load variables */
166 conf_datadir = ip2clue_conf_get(conf, "datadir");
167 conf_files = ip2clue_conf_get(conf, "files");
168 conf_format = ip2clue_conf_get(conf, "format");
169 conf_refresh = ip2clue_conf_get_ul(conf, "refresh", 10);
170 conf_port = ip2clue_conf_get_ul(conf, "port", 10);
171 conf_ipv4 = ip2clue_conf_get_ul(conf, "ipv4", 10);
172 conf_ipv6 = ip2clue_conf_get_ul(conf, "ipv6", 10);
173 conf_debug = ip2clue_conf_get_ul(conf, "debug", 10);
174
175 if (!conf_datadir || !conf_files) {
176 Log(0, "ERROR: 'datadir = ' or 'files = ' missing from conf file!\n");
177 return 1;
178 }
179
180 if ((conf_ipv4 == 0) && (conf_ipv6 == 0)) {
181 Log(0, "ERROR: Both IPv4 and IPv6 are disabled!"
182 " Set ipv4 and/or ipv6 to '1'!\n");
183 return 1;
184 }
185
186 if (!conf_format)
187 conf_format = "OK ip=%P cs=%s tz=%t isp=%i";
188
189 if (conf_port == 0)
190 conf_port = 9999;
191
192 Log(1, "Parameters: datadir=[%s] files=[%s] format=[%s]"
193 " refresh=%lu port=%u ipv4=%u ipv6=%u"
194 " debug=%u",
195 conf_datadir, conf_files, conf_format,
196 conf_refresh, conf_port, conf_ipv4, conf_ipv6,
197 conf_debug);
198
199
200 ip2clue_list_init(&list);
201
202
203 Conn_debug(Logf, conf_debug);
204
205 Log(0, "Starting 'files reload' thread...\n");
206 ret = pthread_create(&workers[0], NULL, worker_loader, NULL);
207 if (ret != 0) {
208 Log(0, "Cannot start loader thread (%s)!\n",
209 strerror(errno));
210 return 1;
211 }
212
213
214 ret = Conn_init(0);
215 if (ret == -1) {
216 Log(0, "Cannot init Conn (%s)!\n",
217 Conn_strerror());
218 return 1;
219 }
220
221 /* set callbacks */
222 Conn_data_cb = data;
223 Conn_error_cb = error;
224
225 if (conf_ipv4 == 1) {
226 Log(0, "IPv4...\n");
227 C4 = Conn_alloc();
228 if (!C4) {
229 Log(0, "Cannot alloc a Conn v4 structure (%s)!\n",
230 Conn_strerror());
231 return 1;
232 }
233 Conn_set_socket_domain(C4, PF_INET);
234 Conn_set_socket_type(C4, SOCK_STREAM);
235 Conn_set_socket_bind_addr(C4, "0.0.0.0");
236 Conn_set_socket_bind_port(C4, conf_port);
237 ret = Conn_commit(C4);
238 if (ret != 0) {
239 Log(0, "Cannot commit (%s)!\n",
240 Conn_strerror());
241 return 1;
242 }
243 }
244
245 if (conf_ipv6 == 1) {
246 Log(0, "IPv6...\n");
247 C6 = Conn_alloc();
248 if (!C6) {
249 Log(0, "Cannot alloc a Conn v6 structure (%s)!\n",
250 Conn_strerror());
251 return 1;
252 }
253 Conn_set_socket_domain(C6, PF_INET6);
254 Conn_set_socket_type(C6, SOCK_STREAM);
255 Conn_set_socket_bind_addr(C6, "::");
256 Conn_set_socket_bind_port(C6, conf_port);
257 ret = Conn_commit(C6);
258 if (ret != 0) {
259 Log(0, "Cannot commit (%s)!\n",
260 Conn_strerror());
261 return 1;
262 }
263 }
264
265 Log(0, "Master starts polling...\n");
266 while (1) {
267 ret = Conn_poll(-1);
268 if (ret == -1) {
269 Log(0, "Error in Conn_poll (%s)!\n",
270 Conn_strerror());
271 break;
272 } else if (ret == 0) {
273 break;
274 }
275 }
276
277 Conn_shutdown();
278
279 ret = pthread_cancel(workers[0]);
280 if (ret != 0) {
281 Log(0, "Cannot cancel thread (%s)!\n",
282 strerror(errno));
283 }
284
285 ret = pthread_join(workers[0], &res);
286 if (ret != 0) {
287 Log(0, "Cannot join thread (%s)!\n",
288 strerror(errno));
289 }
290
291 ip2clue_list_destroy(&list);
292
293 ip2clue_conf_free(conf);
294
295 fclose(Logf);
296
297 return 0;
298 }
File my.conf added (mode: 100644) (index 0000000..173d9fa)
1 datadir = samples
2 #files = ip2location:IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-SAMPLE.BIN, ip2location:IPV6-COUNTRY.SAMPLE.BIN, maxmind:maxmind1.csv, maxmind-v6:maxmind-ipv6.csv
3 files = ip2location:IPV6-COUNTRY.SAMPLE.BIN, maxmind:maxmind1.csv, maxmind-v6:maxmind-ipv6.csv
4 format = OK ip=%P cs=%s cl=%L tz=%t isp=%i
5 refresh = 60
6 port = 9999
7 ipv4 = 1
8 ipv6 = 1
9 debug = 30
10
File parser.c added (mode: 100644) (index 0000000..2f42074)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: parser for IP files
4 */
5
6 #include <i_config.h>
7
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 #include <i_util.h>
16 #include <parser_text.h>
17 #include <parser_ip2location.h>
18
19 /*
20 * Parse file and store data in @db database
21 */
22 int ip2clue_parse_file(struct ip2clue_db *db, const char *file_name,
23 const char *format)
24 {
25 struct timeval ts, te;
26 int err;
27
28 gettimeofday(&ts, NULL);
29
30 db->ts_load = ts.tv_sec;
31 db->ts = 0;
32 snprintf(db->file, sizeof(db->file), "%s", file_name);
33
34 err = -1;
35
36 /* webhosting.info */
37 if (!strcasecmp(format, "webhosting")) {
38 db->format = IP2CLUE_FORMAT_WEBHOSTING;
39 err = ip2clue_parse_text(db);
40 } else if (!strcasecmp(format, "maxmind")) {
41 db->format = IP2CLUE_FORMAT_MAXMIND;
42 err = ip2clue_parse_text(db);
43 } else if (!strcasecmp(format, "maxmind-v6")) {
44 db->format = IP2CLUE_FORMAT_MAXMIND_V6;
45 err = ip2clue_parse_text(db);
46 } else if (!strcasecmp(format, "software77")) {
47 db->format = IP2CLUE_FORMAT_SOFTWARE77;
48 err = ip2clue_parse_text(db);
49 } else if (!strcasecmp(format, "ip2location")) {
50 db->format = IP2CLUE_FORMAT_IP2LOCATION;
51 err = ip2clue_parse_ip2location(db);
52 } else {
53 snprintf(ip2clue_error, sizeof(ip2clue_error),
54 "invalid file format [%s]", format);
55 return -1;
56 }
57
58 if (err == -1)
59 return -1;
60
61 gettimeofday(&te, NULL);
62
63 db->elap_load_ms = (te.tv_sec - ts.tv_sec) * 1000 +
64 (te.tv_usec - ts.tv_usec) / 1000;
65
66 db->usage_count = 0;
67 db->lookup_ok = 0;
68 db->lookup_notfound = 0;
69 db->lookup_malformed = 0;
70
71 return 0;
72 }
73
74 /*
75 * Returns the format based on code
76 */
77 char *ip2clue_format(enum ip2clue_format f)
78 {
79 switch (f) {
80 case IP2CLUE_FORMAT_WEBHOSTING: return "webhosting";
81 case IP2CLUE_FORMAT_MAXMIND: return "maxmind";
82 case IP2CLUE_FORMAT_MAXMIND_V6: return "maxmind-v6";
83 case IP2CLUE_FORMAT_SOFTWARE77: return "software77";
84 case IP2CLUE_FORMAT_IP2LOCATION: return "ip2location";
85 default: return "unknown";
86 }
87 }
88
89 /*
90 * Init a list of databases
91 */
92 void ip2clue_list_init(struct ip2clue_list *list)
93 {
94 list->number = 0;
95 list->entries = NULL;
96 }
97
98 /*
99 * Destroys a list of databases
100 */
101 void ip2clue_list_destroy(struct ip2clue_list *list)
102 {
103 unsigned int i;
104
105 if (list->entries == NULL)
106 return;
107
108 for (i = 0; i < list->number; i++)
109 ip2clue_destroy(list->entries[i]);
110 free(list->entries);
111
112 list->number = 0;
113 list->entries = NULL;
114 }
115
116 /*
117 * Output statistics about the databases
118 * TODO: Add number of lookups (successful or not) and number of reloads.
119 */
120 void ip2clue_list_stats(char *out, const size_t out_size, struct ip2clue_list *list)
121 {
122 size_t rest, line_size;
123 char line[512];
124 struct ip2clue_db *db;
125 unsigned int i;
126
127 rest = out_size;
128
129 strcpy(out, "");
130
131 line_size = snprintf(line, sizeof(line), "%u database(s)",
132 list->number);
133 if (rest < line_size)
134 return;
135
136 strcat(out, line);
137 rest -= line_size;
138
139 for (i = 0; i < list->number; i++) {
140 db = list->entries[i];
141 line_size = snprintf(line, sizeof(line),
142 "\n"
143 "db %u: format [%s], %s, entries=%llu"
144 ", build_ts=%ld, load_ts=%ld, load=%ums"
145 ", file=[%s], mem=%lluB"
146 " ok/notfound/malformed=%llu/%llu/%llu",
147 i, ip2clue_format(db->format),
148 db->v4_or_v6 == 4 ? "ipv4" : "ipv6",
149 db->no_of_cells,
150 db->ts, db->ts_load, db->elap_load_ms,
151 db->file, db->mem,
152 db->lookup_ok, db->lookup_notfound, db->lookup_malformed);
153 if (rest < line_size)
154 return;
155
156 strcat(out, line);
157 rest -= line_size;
158 }
159 }
160
161 /*
162 * Allocates a list structure
163 */
164 int ip2clue_list_alloc(struct ip2clue_list *a, const unsigned int number)
165 {
166 unsigned int mem;
167
168 mem = number * sizeof(void *);
169 a->entries = (struct ip2clue_db **) malloc(mem);
170 if (a->entries == NULL) {
171 snprintf(ip2clue_error, sizeof(ip2clue_error),
172 "cannot alloc %u bytes", mem);
173 return -1;
174 }
175
176 memset(a->entries, 0, mem);
177
178 a->number = number;
179
180 return 0;
181 }
182
183 /*
184 * Clone a list
185 */
186 int ip2clue_list_clone(struct ip2clue_list *dst, struct ip2clue_list *src)
187 {
188 unsigned int i;
189
190 ip2clue_list_destroy(dst);
191
192 if (ip2clue_list_alloc(dst, src->number) != 0)
193 return -1;
194
195 for (i = 0; i < src->number; i++) {
196 dst->entries[i] = src->entries[i];
197 src->entries[i]->usage_count++;
198 }
199
200 return 0;
201 }
202
203 /*
204 * Replaces a list with other
205 */
206 int ip2clue_list_replace(struct ip2clue_list *dst, struct ip2clue_list *src)
207 {
208 int ret;
209
210 ret = ip2clue_list_clone(dst, src);
211 if (ret != 0)
212 return ret;
213 ip2clue_list_destroy(src);
214
215 return 0;
216 }
217
218 /*
219 * Parse an option string
220 * @option can be something like 'maxmind:file1, ip2location:file2'.
221 */
222 static int ip2clue_list_load_one(struct ip2clue_db *db, const char *dir,
223 const char *option)
224 {
225 char *file;
226 char format[32];
227 unsigned int i, format_size;
228 char final_file[1024];
229
230 file = strchr(option, ':');
231 if (file == NULL) {
232 snprintf(ip2clue_error, sizeof(ip2clue_error),
233 "invalid format [%s]", option);
234 return -1;
235 }
236 file++;
237
238 i = 0;
239 format_size = sizeof(format);
240 while ((*option != '\0') && (*option != ':') && (i < format_size - 1)) {
241 format[i] = *option;
242 i++;
243 option++;
244 }
245 format[i] = '\0';
246
247 snprintf(final_file, sizeof(final_file), "%s/%s", dir, file);
248 return ip2clue_parse_file(db, final_file, format);
249 }
250
251 /*
252 * Searches a db by file name
253 */
254 static struct ip2clue_db *ip2clue_db_search(const struct ip2clue_list *l,
255 const char *file)
256 {
257 unsigned int i;
258 struct ip2clue_db *db;
259
260 if (l == NULL)
261 return NULL;
262
263 for (i = 0; i < l->number; i++) {
264 db = l->entries[i];
265 if (strcmp(db->file, file) == 0)
266 return db;
267 }
268
269 return NULL;
270 }
271
272 /*
273 * Refreshes a db list if files changed
274 * @options can be something like 'maxmind:file1, ip2location:file2'.
275 */
276 int ip2clue_list_refresh(struct ip2clue_list *dst, struct ip2clue_list *src,
277 const char *dir, const char *options)
278 {
279 struct ip2clue_db *db;
280 struct ip2clue_split s;
281 int files, err, force_load;
282 unsigned int i;
283 time_t mtime = 0;
284 struct stat S;
285 char *file;
286 char path[1024];
287 unsigned int mem;
288
289 files = ip2clue_split(&s, options, ", ");
290 if (files == -1)
291 return -1;
292
293 if (ip2clue_list_alloc(dst, files) != 0)
294 return -1;
295
296 for (i = 0; i < dst->number; i++) {
297 /* find the last change of the file */
298 file = strchr(s.fields[i], ':');
299 if (file == NULL) {
300 snprintf(ip2clue_error, sizeof(ip2clue_error),
301 "invalid option [%s] (no ':')", s.fields[i]);
302 goto out_free_dst; /* Invalid entry */
303 }
304 file++;
305
306 force_load = 1;
307
308 snprintf(path, sizeof(path), "%s/%s", dir, file);
309
310 /* See if we can find the db in the old list */
311 db = ip2clue_db_search(src, path);
312 if (db != NULL) {
313 /* is it still fresh? */
314 err = stat(path, &S);
315 if (err == 0)
316 mtime = S.st_mtime;
317
318 /* file changed? */
319 if (db->ts_load >= mtime)
320 force_load = 0;
321 }
322
323 if (force_load == 1) {
324 mem = sizeof(struct ip2clue_db);
325 db = (struct ip2clue_db *) malloc(mem);
326 if (db == NULL) {
327 snprintf(ip2clue_error, sizeof(ip2clue_error),
328 "cannot alloc %u bytes for db", mem);
329 goto out_free_dst;
330 }
331
332 /* TODO: s.fields[i] => get_value(&s, i) */
333 err = ip2clue_list_load_one(db, dir, s.fields[i]);
334 if (err != 0) {
335 free(db);
336 goto out_free_dst;
337 }
338 }
339 db->usage_count++;
340
341 dst->entries[i] = db;
342 }
343
344 return 0;
345
346 out_free_dst:
347 ip2clue_list_destroy(dst);
348
349 return -1;
350 }
351
352 /*
353 * Parse an option string
354 * @options can be something like 'maxmind:file1, ip2location:file2'.
355 */
356 int ip2clue_list_load(struct ip2clue_list *list, const char *dir,
357 const char *options)
358 {
359 return ip2clue_list_refresh(list, NULL, dir, options);
360 }
File parser.h added (mode: 100644) (index 0000000..21dceeb)
1 #ifndef IP2CLUE_PARSER_H
2 #define IP2CLUE_PARSER_H 1
3
4 #include <i_config.h>
5
6 #include <i_types.h>
7
8 extern int ip2clue_parse_file(struct ip2clue_db *db,
9 const char *file_name,
10 const char *format);
11
12 extern char *ip2clue_format(enum ip2clue_format f);
13
14 extern void ip2clue_list_init(struct ip2clue_list *list);
15 extern void ip2clue_list_destroy(struct ip2clue_list *db);
16
17 extern void ip2clue_list_stats(char *out,
18 const size_t out_size, struct ip2clue_list *list);
19
20 extern int ip2clue_list_clone(struct ip2clue_list *dst,
21 struct ip2clue_list *src);
22
23 extern int ip2clue_list_replace(struct ip2clue_list *dst,
24 struct ip2clue_list *src);
25
26 extern int ip2clue_list_refresh(struct ip2clue_list *dst,
27 struct ip2clue_list *src, const char *dir,
28 const char *options);
29
30 extern int ip2clue_list_load(struct ip2clue_list *list,
31 const char *dir, const char *options);
32
33 #endif
File parser_ip2location.c added (mode: 100644) (index 0000000..fbf48f0)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: parser for IP files
4 * ip2location stuff
5 */
6
7 #include <i_config.h>
8
9 #include <errno.h>
10 #include <time.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <error.h>
15 #include <string.h>
16
17 #include <i_types.h>
18 #include <i_util.h>
19 #include <parser_ip2location.h>
20
21 enum ip2clue_ip2location_item
22 {
23 IP2CLUE_IP2LOCATION_COUNTRY = 0,
24 IP2CLUE_IP2LOCATION_REGION,
25 IP2CLUE_IP2LOCATION_CITY,
26 IP2CLUE_IP2LOCATION_ISP,
27 IP2CLUE_IP2LOCATION_LAT,
28 IP2CLUE_IP2LOCATION_LON,
29 IP2CLUE_IP2LOCATION_DOMAIN,
30 IP2CLUE_IP2LOCATION_ZIPCODE,
31 IP2CLUE_IP2LOCATION_TZ,
32 IP2CLUE_IP2LOCATION_NETSPEED,
33 IP2CLUE_IP2LOCATION_IDD,
34 IP2CLUE_IP2LOCATION_AREACODE,
35 IP2CLUE_IP2LOCATION_WSC,
36 IP2CLUE_IP2LOCATION_WSN
37 };
38
39 #define IP2CLUE_IP2LOCATION_ITEMS 14
40 static unsigned char ip2clue_ip2location_lut[IP2CLUE_IP2LOCATION_ITEMS][19] =
41 {
42 {0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
43 {0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
44 {0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
45 {0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9},
46 {0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
47 {0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
48 {0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0,10, 0,10, 0,10, 0,10},
49 {0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7},
50 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8},
51 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8,11, 0,11, 8,11},
52 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,12, 0,12},
53 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,10,13, 0,13},
54 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,14},
55 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,10,15}
56 };
57
58 /*
59 * Returns position for a db_type and for an item
60 */
61 static unsigned char ip2clue_ip2location_pos(const unsigned db_type,
62 const enum ip2clue_ip2location_item item)
63 {
64 return ip2clue_ip2location_lut[item][db_type];
65 }
66
67 /*
68 * Read 8 bits
69 */
70 static int xread8(unsigned char *ret, const int fd, off_t off)
71 {
72 off_t x;
73 ssize_t n;
74
75 x = lseek(fd, off, SEEK_SET);
76 if (x != off) {
77 snprintf(ip2clue_error, sizeof(ip2clue_error),
78 "cannot search to position %lld (%s)",
79 (long long) off, strerror(errno));
80 return -1;
81 }
82
83 n = read(fd, ret, 1);
84 if (n != 1) {
85 snprintf(ip2clue_error, sizeof(ip2clue_error),
86 "cannot read (%s) n=%d at off %lld",
87 strerror(errno), n, (long long) off);
88 return -1;
89 }
90
91 /*
92 printf("%s: readed value %u at offset %lld!\n",
93 __FUNCTION__, *ret, (long long) off);
94 */
95
96 return 0;
97 }
98
99 /*
100 * Read 32 bits
101 */
102 static int xread32(unsigned int *ret, const int fd, off_t off)
103 {
104 off_t x;
105 ssize_t n;
106 unsigned char c[4];
107
108 x = lseek(fd, off, SEEK_SET);
109 if (x != off)
110 return -1;
111
112 n = read(fd, &c, 4);
113 if (n != 4)
114 return -1;
115
116 *ret = (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0];
117
118 return 0;
119 }
120
121 /*
122 * Read 128 bits
123 * Put in out 128 bits
124 */
125 static int xread128(unsigned int *out, const int fd, off_t off)
126 {
127 unsigned int v;
128
129 if (xread32(&v, fd, off) != 0)
130 return -1;
131 out[3] = v;
132
133 if (xread32(&v, fd, off + 4) != 0)
134 return -1;
135 out[2] = v;
136
137 if (xread32(&v, fd, off + 8) != 0)
138 return -1;
139 out[1] = v;
140
141 if (xread32(&v, fd, off + 12) != 0)
142 return -1;
143 out[0] = v;
144
145 return 0;
146 }
147 /*
148 * Read a float
149 */
150 static int xread_float(float *ret, const int fd, off_t off)
151 {
152 off_t x;
153 ssize_t n;
154
155 x = lseek(fd, off, SEEK_SET);
156 if (x != off)
157 return -1;
158
159 n = read(fd, ret, 4);
160 if (n != 4)
161 return -1;
162
163 /*
164 printf("Readed float %f from offset %lld!\n",
165 *ret, (long long) off);
166 */
167
168 return 0;
169 }
170
171 /*
172 * Read a string
173 */
174 static char *xread_str(char *out, const size_t out_len, const int fd,
175 const off_t off, unsigned int extra_add)
176 {
177 unsigned char len;
178 static char v[128];
179 ssize_t n;
180 unsigned int real_offset;
181
182 /* first, read offset */
183 if (xread32(&real_offset, fd, off) != 0)
184 return NULL;
185
186 real_offset += extra_add;
187
188 if (xread8(&len, fd, real_offset) != 0)
189 return NULL;
190
191 strcpy(v, "");
192
193 if (len == 0)
194 return v;
195
196 if (len > out_len - 1)
197 len = out_len - 1;
198
199 n = read(fd, out, len);
200 if (n != len) {
201 snprintf(ip2clue_error, sizeof(ip2clue_error),
202 "cannot read at offset %lld, len=%d, n=%d (%s)",
203 (long long) off, len, n, strerror(errno));
204 return NULL;
205 }
206 out[n] = '\0';
207
208 /*
209 printf("String at offset %lld, with len=%d is [%s]!\n",
210 (long long) off, len, out);
211 */
212
213 return 0;
214 }
215
216 /*
217 * Substract one from IPv6 address
218 */
219 void substract(unsigned int *a)
220 {
221 int i;
222
223 for (i = 3; i >= 0; i--)
224 if (a[i] == 0) {
225 a[i] = 0xFFFFFFFFUL;
226 } else {
227 a[i] = a[i] - 1;
228 break;
229 }
230 }
231
232 /*
233 * IP2country specific file
234 */
235 int ip2clue_parse_ip2location(struct ip2clue_db *db)
236 {
237 int fd;
238 unsigned int i, j;
239 unsigned char db_type, db_columns, db_year, db_month, db_day;
240 unsigned int db_count, db_addr, ip_version;
241 struct ip2clue_cell_v4 *p_cell4, *cell4 = NULL;
242 struct ip2clue_cell_v6 *p_cell6, *cell6 = NULL;
243 struct ip2clue_extra x;
244 unsigned int x_set = 0;
245 unsigned char pos;
246 unsigned int off, cur, next;
247 char *s;
248 unsigned int add, final, s_len;
249 struct ip2clue_extra *e;
250 /*char src[60], dst[60];*/
251 struct tm tm;
252 /*char dump[256];*/
253
254 fd = open(db->file, O_RDONLY);
255 if (fd == -1) {
256 snprintf(ip2clue_error, sizeof(ip2clue_error),
257 "cannot open [%s] (%s)",
258 db->file, strerror(errno));
259 return -1;
260 }
261
262 if (xread8(&db_type, fd, 0) != 0)
263 goto out_close;
264 if (xread8(&db_columns, fd, 1) != 0)
265 goto out_close;
266 if (xread8(&db_year, fd, 2) != 0)
267 goto out_close;
268 if (xread8(&db_month, fd, 3) != 0)
269 goto out_close;
270 if (xread8(&db_day, fd, 4) != 0)
271 goto out_close;
272 if (xread32(&db_count, fd, 5) != 0)
273 goto out_close;
274 db_count--;
275 if (xread32(&db_addr, fd, 9) != 0)
276 goto out_close;
277 db_addr--;
278 if (xread32(&ip_version, fd, 13) != 0)
279 goto out_close;
280
281 /*
282 printf("type=%u, columns=%u, year=%u, month=%u, day=%u, count=%u"
283 ", addr=%u, ip_version=%u.\n",
284 db_type, db_columns, db_year, db_month, db_day, db_count,
285 db_addr, ip_version);
286 */
287
288 memset(&tm, 0, sizeof(struct tm));
289 tm.tm_year = db_year + 2000 - 1900;
290 tm.tm_mon = db_month - 1;
291 tm.tm_mday = db_day;
292 db->ts = mktime(&tm);
293
294 if (ip_version == 0)
295 db->v4_or_v6 = 4;
296 else
297 db->v4_or_v6 = 6;
298 db->no_of_cells = db_count;
299
300 /* alloc memory for entries */
301 if (db->v4_or_v6 == IP2CLUE_TYPE_V6) {
302 db->mem = db->no_of_cells * sizeof(struct ip2clue_cell_v6);
303 } else {
304 db->mem = db->no_of_cells * sizeof(struct ip2clue_cell_v4);
305 }
306 db->cells = malloc(db->mem);
307 if (db->cells == NULL)
308 goto out_close;
309
310 /* parse file */
311 db->current = 0;
312 while (db->current < db->no_of_cells) {
313 /*
314 printf("Loading struct %llu/%llu...\r",
315 db->current + 1, db->no_of_cells);
316 */
317
318 x_set = 0; /* alloc or not extra structure? Default, not. */
319 memset(&x, 0, sizeof(struct ip2clue_extra));
320
321 if (db->v4_or_v6 == IP2CLUE_TYPE_V6) {
322 p_cell6 = (struct ip2clue_cell_v6 *) db->cells;
323 cell6 = &p_cell6[db->current];
324 memset(cell6, 0, sizeof(struct ip2clue_cell_v6));
325
326 cur = db_addr + db->current * (db_columns * 4 + 12);
327 next = db_addr + (db->current + 1) * (db_columns * 4 + 12);
328 /*printf("cur=%u, next=%u\n", cur, next);*/
329
330 if (xread128(cell6->ip_start, fd, cur) != 0)
331 goto out_parse_error;
332 if (xread128(cell6->ip_end, fd, next) != 0)
333 goto out_parse_error;
334
335 /* final? */
336 final = 1;
337 for (j = 0; j < 4; j++) {
338 if (cell6->ip_end[j] != 0xFFFFFFFFUL) {
339 final = 0;
340 break;
341 }
342 }
343
344 /* We need to substract 1, else the interval clashes on ends */
345 if (final == 0)
346 substract(cell6->ip_end);
347
348 /*
349 ip2clue_addr_v6(src, sizeof(src), cell6->ip_start);
350 ip2clue_addr_v6(dst, sizeof(src), cell6->ip_end);
351
352 printf("\tstart=%s, end=%s\n", src, dst);
353 */
354
355 add = 12;
356 } else {
357 p_cell4 = (struct ip2clue_cell_v4 *) db->cells;
358 cell4 = &p_cell4[db->current];
359 memset(cell4, 0, sizeof(struct ip2clue_cell_v4));
360
361 cur = db_addr + db->current * (db_columns * 4);
362 next = db_addr + (db->current + 1) * (db_columns * 4);
363 /*printf("cur=%u, next=%u\n", cur, next);*/
364
365 if (xread32(&cell4->ip_start, fd, cur) != 0)
366 goto out_parse_error;
367 if (xread32(&cell4->ip_end, fd, db_addr + (db->current + 1) * db_columns * 4) != 0)
368 goto out_parse_error;
369 cell4->ip_end--;
370
371 /* final? */
372 final = 1;
373 if (cell4->ip_end != 4294967295UL)
374 final = 0;
375
376 /*
377 printf("\tstart=%u, end=%u\n",
378 cell4->ip_start, cell4->ip_end);
379 */
380
381 add = 0;
382 }
383
384 for (i = 0; i < IP2CLUE_IP2LOCATION_ITEMS; i++) {
385 pos = ip2clue_ip2location_pos(db_type, i);
386 if (pos == 0)
387 continue;
388
389 /* default offset */
390 off = cur + add + 4 * (pos - 1);
391
392 switch (i) {
393 case IP2CLUE_IP2LOCATION_COUNTRY:
394 /* short */
395 if (db->v4_or_v6 == IP2CLUE_TYPE_V6) {
396 s = cell6->country_short;
397 s_len = sizeof(cell6->country_short);
398 } else {
399 s = cell4->country_short;
400 s_len = sizeof(cell4->country_short);
401 }
402 if (xread_str(s, s_len, fd, off, 0) != 0)
403 goto out_parse_error;
404 /*printf("Short: %s, ", s);*/
405
406 /* long */
407 if (xread_str(x.country_long,
408 sizeof(x.country_long), fd, off, 3) != 0)
409 goto out_parse_error;
410 x_set = 1;
411 break;
412
413 case IP2CLUE_IP2LOCATION_REGION:
414 if (xread_str(x.region, sizeof(x.region), fd, off, 0) != 0)
415 goto out_parse_error;
416 x_set = 1;
417 break;
418
419 case IP2CLUE_IP2LOCATION_CITY:
420 if (xread_str(x.city, sizeof(x.city), fd, off, 0) != 0)
421 goto out_parse_error;
422 x_set = 1;
423 break;
424
425 case IP2CLUE_IP2LOCATION_ISP:
426 if (xread_str(x.isp, sizeof(x.isp), fd, off, 0) != 0)
427 goto out_parse_error;
428 x_set = 1;
429 break;
430
431 case IP2CLUE_IP2LOCATION_LAT:
432 if (xread_float(&x.latitude, fd, off) != 0)
433 goto out_parse_error;
434 x_set = 1;
435 break;
436
437 case IP2CLUE_IP2LOCATION_LON:
438 if (xread_float(&x.longitude, fd, off) != 0)
439 goto out_parse_error;
440 x_set = 1;
441 break;
442
443 case IP2CLUE_IP2LOCATION_DOMAIN:
444 if (xread_str(x.domain, sizeof(x.domain), fd, off, 0) != 0)
445 goto out_parse_error;
446 x_set = 1;
447 break;
448
449 case IP2CLUE_IP2LOCATION_ZIPCODE:
450 if (xread_str(x.zip, sizeof(x.zip), fd, off, 0) != 0)
451 goto out_parse_error;
452 x_set = 1;
453 break;
454
455 case IP2CLUE_IP2LOCATION_TZ:
456 if (xread_str(x.timezone, sizeof(x.timezone), fd, off, 0) != 0)
457 goto out_parse_error;
458 x_set = 1;
459 break;
460
461 case IP2CLUE_IP2LOCATION_NETSPEED:
462 if (xread_str(x.netspeed, sizeof(x.netspeed), fd, off, 0) != 0)
463 goto out_parse_error;
464 x_set = 1;
465 break;
466
467 case IP2CLUE_IP2LOCATION_IDD:
468 if (xread_str(x.idd, sizeof(x.idd), fd, off, 0) != 0)
469 goto out_parse_error;
470 x_set = 1;
471 break;
472
473 case IP2CLUE_IP2LOCATION_AREACODE:
474 if (xread_str(x.areacode, sizeof(x.areacode), fd, off, 0) != 0)
475 goto out_parse_error;
476 x_set = 1;
477 break;
478
479 case IP2CLUE_IP2LOCATION_WSC:
480 if (xread_str(x.ws_code, sizeof(x.ws_code), fd, off, 0) != 0)
481 goto out_parse_error;
482 x_set = 1;
483 break;
484
485 case IP2CLUE_IP2LOCATION_WSN:
486 if (xread_str(x.ws_name, sizeof(x.ws_name), fd, off, 0) != 0)
487 goto out_parse_error;
488 x_set = 1;
489 break;
490
491 default:
492 snprintf(ip2clue_error, sizeof(ip2clue_error),
493 "unknown item type (%u)", i);
494 goto out_parse_error;
495 }
496 }
497
498 if (x_set == 1) {
499 e = (struct ip2clue_extra *) malloc(sizeof(struct ip2clue_extra));
500 if (e == NULL) {
501 snprintf(ip2clue_error, sizeof(ip2clue_error),
502 "cannot alloc memory");
503 goto out_parse_error;
504 }
505 memcpy(e, &x, sizeof(struct ip2clue_extra));
506 /*
507 ip2clue_print_extra(dump, sizeof(dump), e);
508 printf("Extra: %s.\n", dump);
509 */
510 } else {
511 e = NULL;
512 }
513
514 if (db->v4_or_v6 == IP2CLUE_TYPE_V6) {
515 cell6->extra = e;
516 } else {
517 cell4->extra = e;
518 }
519
520 db->current++;
521
522 /* just a safety */
523 if (final == 1)
524 break;
525 }
526
527 close(fd);
528
529 return 0;
530
531 out_parse_error:
532 ip2clue_destroy(db);
533 free(db);
534
535 out_close:
536 close(fd);
537
538 return -1;
539 }
File parser_ip2location.h added (mode: 100644) (index 0000000..f7b8100)
1 #ifndef IP2CLUE_PARSER_IP2LOCATION_H
2 #define IP2CLUE_PARSER_IP2LOCATION_H 1
3
4 #include <i_config.h>
5
6 #include <i_types.h>
7
8 extern int ip2clue_parse_ip2location(struct ip2clue_db *db);
9
10 #endif
File parser_text.c added (mode: 100644) (index 0000000..bcf58bf)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: parser for IP files
4 */
5
6 #include <i_config.h>
7
8 #include <errno.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <arpa/inet.h>
12
13 #include <i_types.h>
14 #include <i_util.h>
15 #include <parser_text.h>
16 #include <parser_ip2location.h>
17
18 static int ip2clue_set_fields(struct ip2clue_fields *f,
19 const enum ip2clue_type type)
20 {
21 /* defaults */
22 strcpy(f->comment_chars, "");
23 f->v4_or_v6 = IP2CLUE_TYPE_V4;
24
25 switch (type) {
26 case IP2CLUE_FORMAT_WEBHOSTING:
27 f->ip_bin_start = 0;
28 f->ip_bin_end = 1;
29 f->country_short = 2;
30 f->country_long = 4;
31 f->total = 5;
32 return 0;
33
34 case IP2CLUE_FORMAT_MAXMIND:
35 f->ip_bin_start = 2;
36 f->ip_bin_end = 3;
37 f->country_short = 4;
38 f->country_long = 5;
39 f->total = 6;
40 return 0;
41
42 case IP2CLUE_FORMAT_MAXMIND_V6:
43 f->v4_or_v6 = IP2CLUE_TYPE_V6;
44 f->ip_start = 0;
45 f->ip_end = 1;
46 f->country_short = 4;
47 f->country_long = 5;
48 f->total = 6;
49 return 0;
50
51 case IP2CLUE_FORMAT_SOFTWARE77:
52 f->ip_bin_start = 0;
53 f->ip_bin_end = 1;
54 f->country_short = 4;
55 f->country_long = 6;
56 f->total = 7;
57 return 0;
58
59 default:
60 return -1;
61 }
62 }
63
64 /*
65 * Add a cell to the db
66 */
67 static int ip2clue_add_cell(struct ip2clue_db *db, const struct ip2clue_split *s,
68 const struct ip2clue_fields *f)
69 {
70 struct ip2clue_cell_v4 *p_cell4, *cell4;
71 struct ip2clue_cell_v6 *p_cell6, *cell6;
72 int i;
73
74 if (db->v4_or_v6 == IP2CLUE_TYPE_V4) {
75 p_cell4 = (struct ip2clue_cell_v4 *) db->cells;
76 cell4 = &p_cell4[db->current];
77
78 cell4->extra = NULL;
79
80 cell4->ip_start = strtoul(s->fields[f->ip_bin_start], NULL, 10);
81
82 cell4->ip_end = strtoul(s->fields[f->ip_bin_end], NULL, 10);
83
84 if (f->country_short > 0)
85 snprintf(cell4->country_short, sizeof(cell4->country_short),
86 "%s",
87 s->fields[f->country_short]);
88 else
89 strcpy(cell4->country_short, "ZZ");
90 } else if (db->v4_or_v6 == IP2CLUE_TYPE_V6) {
91 struct in6_addr addr;
92
93 p_cell6 = (struct ip2clue_cell_v6 *) db->cells;
94 cell6 = &p_cell6[db->current];
95
96 cell6->extra = NULL;
97
98 if (inet_pton(AF_INET6, s->fields[f->ip_start], (void *) &addr) != 1) {
99 snprintf(ip2clue_error, sizeof(ip2clue_error),
100 "malformed address [%s]",
101 s->fields[f->ip_start]);
102 return -1;
103 }
104 for (i = 0; i < 4; i++)
105 cell6->ip_start[i] = ntohl(addr.s6_addr32[i]);
106
107 if (inet_pton(AF_INET6, s->fields[f->ip_end], (void *) &addr) != 1) {
108 snprintf(ip2clue_error, sizeof(ip2clue_error),
109 "malformed address [%s]",
110 s->fields[f->ip_end]);
111 return -1;
112 }
113 for (i = 0; i < 4; i++)
114 cell6->ip_end[i] = ntohl(addr.s6_addr32[i]);
115
116 if (f->country_short > 0)
117 snprintf(cell6->country_short, sizeof(cell6->country_short),
118 "%s",
119 s->fields[f->country_short]);
120 else
121 strcpy(cell6->country_short, "ZZ");
122 } else {
123 snprintf(ip2clue_error, sizeof(ip2clue_error),
124 "address is nor ipv4 neither ipv6 [%u]", db->v4_or_v6);
125 return -1;
126 }
127
128 return 0;
129 }
130
131 /*
132 * Parse a text file
133 */
134 int ip2clue_parse_text(struct ip2clue_db *db)
135 {
136 int err;
137 FILE *f;
138 char line[1024], *r;
139 struct ip2clue_split s;
140 unsigned long line_no, final_lines;
141 struct ip2clue_fields fields;
142 unsigned int pos;
143
144 err = ip2clue_set_fields(&fields, db->format);
145 if (err != 0)
146 return -1;
147
148 db->v4_or_v6 = fields.v4_or_v6;
149
150 /* First, count the number of lines to know how many memory to alloc */
151 db->no_of_cells = ip2clue_file_lines(db->file);
152 if (db->no_of_cells == -1)
153 return -1;
154
155 /* Second, alloc memory for whole data */
156 if (db->v4_or_v6 == IP2CLUE_TYPE_V4)
157 db->mem = db->no_of_cells * sizeof(struct ip2clue_cell_v4);
158 else if (db->v4_or_v6 == IP2CLUE_TYPE_V6)
159 db->mem = db->no_of_cells * sizeof(struct ip2clue_cell_v6);
160 else
161 return -1;
162 db->cells = malloc(db->mem);
163 if (db->cells == NULL) {
164 snprintf(ip2clue_error, sizeof(ip2clue_error),
165 "cannot alloc %llu bytes",
166 db->mem);
167 return -1;
168 }
169
170 f = fopen(db->file, "r");
171 if (f == NULL) {
172 snprintf(ip2clue_error, sizeof(ip2clue_error),
173 "cannot open [%s] (%s)",
174 db->file, strerror(errno));
175 goto out_free_db_cells;
176 }
177
178 /* Fourth, do the parsing */
179 line_no = 0;
180 final_lines = 0;
181 while (1) {
182 line_no++;
183
184 r = fgets(line, sizeof(line) - 1, f);
185 if (r == NULL)
186 break;
187
188 /* Remove comments */
189 if (strchr(fields.comment_chars, line[0]))
190 continue;
191
192 /* Remove \n\r */
193 pos = strlen(line) - 1;
194 while ((pos > 0) && ((line[pos] == '\n') || (line[pos] == '\r')))
195 line[pos--] = '\0';
196
197 err = ip2clue_split(&s, line, ",; ");
198 if (err < 0)
199 goto out_parse_error;
200
201 if (err != fields.total) {
202 snprintf(ip2clue_error, sizeof(ip2clue_error),
203 "error splitting line %lu [%s]:"
204 " too less fields; found %d needed %d",
205 line_no, line, err, fields.total);
206 goto out_parse_error;
207 }
208
209 if (final_lines == db->no_of_cells) {
210 snprintf(ip2clue_error, sizeof(ip2clue_error),
211 "seems that the file changed behind our back");
212 goto out_parse_error;
213 }
214
215 db->current = final_lines;
216
217 if (ip2clue_add_cell(db, &s, &fields) != 0)
218 goto out_parse_error;
219
220 final_lines++;
221 }
222 fclose(f);
223
224 db->no_of_cells = final_lines;
225
226 return 0;
227
228 out_parse_error:
229 fclose(f);
230
231 out_free_db_cells:
232 free(db->cells);
233 db->cells = NULL;
234
235 return -1;
236 }
File parser_text.h added (mode: 100644) (index 0000000..3ee773a)
1 /*
2 * Author: Catalin(ux) M. BOIE
3 * Description: parser for IP files
4 */
5
6 #ifndef IP2CLUE_PARSER_TEXT_H
7 #define IP2CLUE_PARSER_TEXT_H 1
8
9 #include <i_config.h>
10
11 #include <i_types.h>
12
13 extern int ip2clue_parse_text(struct ip2clue_db *db);
14
15 #endif
File test1.c added (mode: 100644) (index 0000000..d920b0b)
1 #include <i_config.h>
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include <i_types.h>
8 #include <i_util.h>
9 #include <parser.h>
10
11 int main(void)
12 {
13 int ret, loops;
14 struct ip2clue_db db;
15 struct ip2clue_cell_v4 *cell4;
16 struct ip2clue_cell_v6 *cell6;
17 struct timeval s, e;
18 long diff, total_loops;
19 char *test_ips[500000];
20 char sip[64];
21 char *file, *type;
22 char dump[256];
23 char *key;
24
25 /* v4 */
26 //file = "samples/maxmind1.csv"; type = "maxmind";
27 //file = "samples/ip-to-country.csv"; type = "webhosting";
28 //file = "samples/IpToCountry.csv"; type = "software77";
29 file = "samples/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-SAMPLE.BIN"; type = "ip2location";
30 //file = "samples/IP-COUNTRY-SAMPLE.BIN"; type = "ip2location";
31
32 /* v6 */
33 file = "samples/IPV6-COUNTRY.SAMPLE.BIN"; type = "ip2location";
34
35 ret = ip2clue_parse_file(&db, file, type);
36 if (ret != 0)
37 return 1;
38
39 printf("Loading took %ums.\n", db.elap_load_ms);
40 if (db.ts_load > 0)
41 printf("DB was loaded at %s", ctime(&db.ts_load));
42 if (db.ts > 0)
43 printf("DB was built by provider at %s", ctime(&db.ts));
44 printf("DB takes %llu bytes.\n", db.mem);
45
46 if (db.v4_or_v6 == IP2CLUE_TYPE_V4) {
47 total_loops = 500000;
48 for (loops = 0; loops < total_loops; loops++) {
49 snprintf(sip, sizeof(sip), "%ld.%ld.%ld.%ld",
50 random() % 255, random() % 255, random() % 255, random() % 255);
51 test_ips[loops] = strdup(sip);
52 }
53
54 gettimeofday(&s, NULL);
55 loops = 0;
56 while (loops < total_loops) {
57 cell4 = ip2clue_search_v4(&db, test_ips[loops]);
58 loops++;
59 }
60 gettimeofday(&e, NULL);
61 diff = (e.tv_sec - s.tv_sec) * 1000 + (e.tv_usec - s.tv_usec) / 1000;
62 printf("Took %ldms for %d loops (%ld reqs/ms).\n",
63 diff, loops, loops / (diff == 0 ? 1 : diff));
64
65 for (loops = 0; loops < total_loops; loops++)
66 free(test_ips[loops]);
67
68 cell4 = ip2clue_search_v4(&db, "2.6.190.56");
69 if (cell4 == NULL) {
70 printf("Error in lookup!\n");
71 return 2;
72 }
73 if (strcmp(cell4->country_short, "GB") != 0) {
74 printf("Invalid answer. Expected GB, got %s!\n", cell4->country_short);
75 abort();
76 }
77 printf("ip_start=%u, ip_end=%u, country_short=[%s]\n",
78 cell4->ip_start, cell4->ip_end, cell4->country_short);
79
80 cell4 = ip2clue_search_v4(&db, "222.252.0.1");
81 if (cell4 == NULL) {
82 printf("Error in lookup!\n");
83 return 2;
84 }
85 if (strcmp(cell4->country_short, "VN") != 0) {
86 printf("Invalid answer. Expected VN, got %s!\n", cell4->country_short);
87 abort();
88 }
89 printf("ip_start=%u, ip_end=%u, country_short=[%s]\n",
90 cell4->ip_start, cell4->ip_end, cell4->country_short);
91
92 /* Out of bounds */
93 cell4 = ip2clue_search_v4(&db, "0.0.0.0");
94 if (cell4 != NULL)
95 printf("ERROR: 0.0.0.0 din not returned NULL but [%s]!\n", cell4->country_short);
96
97 /* Out of bounds */
98 cell4 = ip2clue_search_v4(&db, "255.255.255.255");
99 if (cell4 != NULL)
100 printf("ERROR: 255.255.255.255 din not returned NULL but [%s]!\n", cell4->country_short);
101 } else if (db.v4_or_v6 == IP2CLUE_TYPE_V6) {
102 /* test 1 */
103 key = "2001:0960:0002:04D2:0000:0000:0000:0000";
104 printf("Looking for %s...\n", key);
105 cell6 = ip2clue_search_v6(&db, key);
106 if (cell6 == NULL) {
107 printf("Error in lookup!\n");
108 return 2;
109 }
110 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
111 printf("Got %s.\n", dump);
112 if (strcmp(cell6->country_short, "NL") != 0) {
113 printf("Invalid answer. Expected 'NL'!\n");
114 abort();
115 }
116
117 /* test 2 */
118 key = "2001:4830:00EA::";
119 printf("Looking for %s...\n", key);
120 cell6 = ip2clue_search_v6(&db, key);
121 if (cell6 == NULL) {
122 printf("Error in lookup!\n");
123 return 2;
124 }
125 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
126 printf("Got %s.\n", dump);
127 if (strcmp(cell6->country_short, "-") != 0) {
128 printf("Invalid answer. Expected '-'!\n");
129 abort();
130 }
131
132 /* test 3 */
133 key = "2001:ff8:1:0:0:0:0:0";
134 printf("Looking for %s...\n", key);
135 cell6 = ip2clue_search_v6(&db, key);
136 if (cell6 == NULL) {
137 printf("Error in lookup!\n");
138 return 2;
139 }
140 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
141 printf("Got %s.\n", dump);
142 if (strcmp(cell6->country_short, "MO") != 0) {
143 printf("Invalid answer. Expected 'MO'!\n");
144 abort();
145 }
146 } else {
147 printf("v4_or_v6 is wrong!\n");
148 abort();
149 }
150
151 ip2clue_destroy(&db);
152
153 return 0;
154 }
File test2.c added (mode: 100644) (index 0000000..94663e1)
1 #include <i_config.h>
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include <i_types.h>
8 #include <i_util.h>
9 #include <parser.h>
10
11 int main(void)
12 {
13 int ret, loops;
14 struct ip2clue_list list;
15 struct ip2clue_cell_v4 *cell4;
16 struct ip2clue_cell_v6 *cell6;
17 struct timeval s, e;
18 long diff, total_loops;
19 char *test_ips[500000];
20 char sip[64];
21 char *file, *type;
22 char dump[256];
23 char *key;
24
25 //file = "samples/ip-to-country.csv"; type = "webhosting";
26 //file = "samples/IpToCountry.csv"; type = "software77";
27 file = "samples/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-SAMPLE.BIN"; type = "ip2location";
28 //file = "samples/IP-COUNTRY-SAMPLE.BIN"; type = "ip2location";
29
30 ret = ip2clue_list_load(&list, "samples", "ip2location:IPV6-COUNTRY.SAMPLE.BIN, maxmind:maxmind1.csv");
31 if (ret != 0) {
32 printf("Cannot load list!\n");
33 return 1;
34 }
35
36 total_loops = 500000;
37 for (loops = 0; loops < total_loops; loops++) {
38 snprintf(sip, sizeof(sip), "%ld.%ld.%ld.%ld",
39 random() % 255, random() % 255, random() % 255, random() % 255);
40 test_ips[loops] = strdup(sip);
41 }
42
43 gettimeofday(&s, NULL);
44 loops = 0;
45 while (loops < total_loops) {
46 cell4 = ip2clue_list_search_v4(&list, test_ips[loops]);
47 loops++;
48 }
49 gettimeofday(&e, NULL);
50 diff = (e.tv_sec - s.tv_sec) * 1000 + (e.tv_usec - s.tv_usec) / 1000;
51 printf("Took %ldms for %d loops (%ld reqs/ms).\n",
52 diff, loops, loops / (diff == 0 ? 1 : diff));
53
54 for (loops = 0; loops < total_loops; loops++)
55 free(test_ips[loops]);
56
57 cell4 = ip2clue_list_search_v4(&list, "2.6.190.56");
58 if (cell4 == NULL) {
59 printf("Error in lookup!\n");
60 return 2;
61 }
62 if (strcmp(cell4->country_short, "GB") != 0) {
63 printf("Invalid answer. Expected GB, got %s!\n", cell4->country_short);
64 abort();
65 }
66
67 cell4 = ip2clue_list_search_v4(&list, "222.252.0.1");
68 if (cell4 == NULL) {
69 printf("Error in lookup!\n");
70 return 2;
71 }
72 if (strcmp(cell4->country_short, "VN") != 0) {
73 printf("Invalid answer. Expected VN, got %s!\n", cell4->country_short);
74 abort();
75 }
76
77 /* Out of bounds */
78 cell4 = ip2clue_list_search_v4(&list, "0.0.0.0");
79 if (cell4 != NULL)
80 printf("ERROR: 0.0.0.0 din not returned NULL but [%s]!\n", cell4->country_short);
81
82 /* Out of bounds */
83 cell4 = ip2clue_list_search_v4(&list, "255.255.255.255");
84 if (cell4 != NULL)
85 printf("ERROR: 255.255.255.255 din not returned NULL but [%s]!\n", cell4->country_short);
86
87 /* ipv6 */
88 total_loops = 500000;
89 for (loops = 0; loops < total_loops; loops++) {
90 snprintf(sip, sizeof(sip), "%lx:%lx:%lx:%lx:%lx:%lx:%lx:%lx",
91 random() % 65536, random() % 65536, random() % 65536, random() % 65536,
92 random() % 65536, random() % 65536, random() % 65536, random() % 65536);
93 test_ips[loops] = strdup(sip);
94 }
95
96 gettimeofday(&s, NULL);
97 loops = 0;
98 while (loops < total_loops) {
99 cell6 = ip2clue_list_search_v6(&list, test_ips[loops]);
100 loops++;
101 }
102 gettimeofday(&e, NULL);
103 diff = (e.tv_sec - s.tv_sec) * 1000 + (e.tv_usec - s.tv_usec) / 1000;
104 printf("Took %ldms for %d loops (%ld reqs/ms).\n",
105 diff, loops, loops / (diff == 0 ? 1 : diff));
106
107 for (loops = 0; loops < total_loops; loops++)
108 free(test_ips[loops]);
109
110 /* test 1 */
111 key = "2001:0960:0002:04D2:0000:0000:0000:0000";
112 printf("Looking for %s...\n", key);
113 cell6 = ip2clue_list_search_v6(&list, key);
114 if (cell6 == NULL) {
115 printf("Error in lookup!\n");
116 return 2;
117 }
118 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
119 printf("Got %s.\n", dump);
120 if (strcmp(cell6->country_short, "NL") != 0) {
121 printf("Invalid answer. Expected 'NL'!\n");
122 abort();
123 }
124
125 /* test 2 */
126 key = "2001:4830:00EA::";
127 printf("Looking for %s...\n", key);
128 cell6 = ip2clue_list_search_v6(&list, key);
129 if (cell6 == NULL) {
130 printf("Error in lookup!\n");
131 return 2;
132 }
133 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
134 printf("Got %s.\n", dump);
135 if (strcmp(cell6->country_short, "-") != 0) {
136 printf("Invalid answer. Expected '-'!\n");
137 abort();
138 }
139
140 /* test 3 */
141 key = "2001:ff8:1:0:0:0:0:0";
142 printf("Looking for %s...\n", key);
143 cell6 = ip2clue_list_search_v6(&list, key);
144 if (cell6 == NULL) {
145 printf("Error in lookup (%s)!\n", ip2clue_strerror());
146 return 2;
147 }
148 ip2clue_dump_cell_v6(dump, sizeof(dump), cell6);
149 printf("Got %s.\n", dump);
150 if (strcmp(cell6->country_short, "MO") != 0) {
151 printf("Invalid answer. Expected 'MO'!\n");
152 abort();
153 }
154
155 ip2clue_list_destroy(&list);
156
157 return 0;
158 }
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/ip2clue

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/ip2clue

Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/ip2clue

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main