00001
00002
00003
00004
00005 #include "conversion.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014 static int debug = 0;
00015
00016
00017
00018
00019 static char default_log_file[] = "\0";
00020
00021
00022
00023
00024 static char *log_file = default_log_file;
00025
00026
00027
00028
00029 static int my_fake_syslog (const char *file, const char * fmt,...) { return 0; }
00030
00031
00032
00033
00034 static int (*my_syslog) (const char *file, const char * fmt,...) = my_fake_syslog;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 void conversion_logging_service (int dbg,
00049 char *logfile,
00050 int (*logger) (const char *file, const char * fmt,...))
00051 {
00052 if ((logger != NULL) && (logfile != NULL))
00053 { if (logfile[0] != 0) { my_syslog = logger; log_file = logfile; } }
00054
00055 debug = dbg;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 char int_to_char (unsigned char c)
00075 {
00076 switch (c)
00077 {
00078 case 0: return '0';
00079 case 1: return '1';
00080 case 2: return '2';
00081 case 3: return '3';
00082 case 4: return '4';
00083 case 5: return '5';
00084 case 6: return '6';
00085 case 7: return '7';
00086 case 8: return '8';
00087 case 9: return '9';
00088 case 10: return 'A';
00089 case 11: return 'B';
00090 case 12: return 'C';
00091 case 13: return 'D';
00092 case 14: return 'E';
00093 case 15: return 'F';
00094 }
00095
00096 return 0;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106 unsigned char char_to_int (char c)
00107 {
00108 switch (c)
00109 {
00110 case '0': return 0;
00111 case '1': return 1;
00112 case '2': return 2;
00113 case '3': return 3;
00114 case '4': return 4;
00115 case '5': return 5;
00116 case '6': return 6;
00117 case '7': return 7;
00118 case '8': return 8;
00119 case '9': return 9;
00120
00121 case 'A': return 10;
00122 case 'B': return 11;
00123 case 'C': return 12;
00124 case 'D': return 13;
00125 case 'E': return 14;
00126 case 'F': return 15;
00127
00128 case 'a': return 10;
00129 case 'b': return 11;
00130 case 'c': return 12;
00131 case 'd': return 13;
00132 case 'e': return 14;
00133 case 'f': return 15;
00134 }
00135
00136 return 0xFF;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 unsigned char* haddr_to_hexa (char *haddr, int *length)
00156 {
00157 int len, hex, i;
00158 static unsigned char hexa[17];
00159
00160
00161 memset ((void*)hexa, 0, 17);
00162 len = strlen(haddr) + 1;
00163
00164 if ((len % 3) != 0)
00165 {
00166 if (debug > 0)
00167 { my_syslog (log_file,
00168 "[DEBUG] [%s:%d] haddr_to_hexa: '%s' not multiple of 3!", __FILE__, __LINE__, haddr); }
00169 return NULL;
00170 }
00171
00172 hex = len / 3;
00173
00174 if (hex >= *length)
00175 {
00176 if (debug > 0)
00177 { my_syslog (log_file,
00178 "[DEBUG] [%s:%d] haddr_to_hexa: '%s' too long! (len = %d)", __FILE__, __LINE__, haddr, len); }
00179 return NULL;
00180 }
00181
00182 *length = hex;
00183
00184 for (i=0; i<hex; i++) { hexa[i] = (char_to_int(haddr[3*i]) << 4) | char_to_int(haddr[3*i+1]); }
00185
00186 return hexa;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196 unsigned long int get_network_ip (char *ip)
00197 {
00198 struct in_addr inp;
00199
00200 inet_aton (ip, &inp);
00201 return inp.s_addr;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 char *get_doted_ip (unsigned long int ip)
00215 {
00216 struct in_addr add;
00217 static char sip[16];
00218
00219 add.s_addr = ip;
00220 memset ((void*)sip, 0, 16);
00221 strncpy (sip, inet_ntoa(add), 15);
00222 return sip;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 int raw_ips_to_string (unsigned char *buffin, char *buffout, int lenin, int lenout)
00237 {
00238 unsigned long int *ip;
00239 int nip, i;
00240 char *sip;
00241
00242 memset ((void*)buffout, 0, lenout);
00243
00244 if (lenin % 4 != 0) { return 1; }
00245 nip = lenin / 4;
00246
00247 if (lenout < 16 * nip) { return 1; }
00248
00249 ip = (unsigned long int*)buffin;
00250
00251 for (i=0; i<nip; i++)
00252 {
00253 sip = get_doted_ip (*ip);
00254 strcat (buffout, sip);
00255 if (i != nip-1) { strcat (buffout, ":"); }
00256 ip += 1;
00257 }
00258
00259 return 0;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 int uint32_to_network_byte_order (char* buffer_in, char *buffer_out, int len_out)
00275 {
00276 char *pt_out;
00277 struct fields fd;
00278 unsigned long int v;
00279 int n, i, err;
00280
00281
00282
00283
00284
00285 n = s_split(buffer_in, ":", &fd);
00286 if (n == -1)
00287 {
00288 my_syslog (log_file,
00289 "[ERROR] [%s:%d] uint32_to_network_byte_order: Could not allocate memory", __FILE__, __LINE__);
00290 return -1;
00291 }
00292
00293
00294
00295
00296
00297 if (n == 0)
00298 {
00299 my_syslog (log_file,
00300 "[WARNING] [%s:%d] uint32_to_network_byte_order: No value to convert", __FILE__, __LINE__);
00301 return 0;
00302 }
00303
00304 if (debug > 1)
00305 { my_syslog (log_file,
00306 "[DEBUG] [%s:%d] uint32_to_network_byte_order: Number of integer to convert is %d",
00307 __FILE__, __LINE__, n); }
00308
00309
00310
00311
00312
00313 if (n*4 > len_out)
00314 {
00315 free_fields(&fd);
00316 my_syslog (log_file,
00317 "[ERROR] [%s:%d] uint32_to_network_byte_order: Size of ouput buffer is too short! %d > %d",
00318 __FILE__, __LINE__,
00319 n*4,
00320 len_out);
00321 return -1;
00322 }
00323
00324 memset ((void*)buffer_out, 0, len_out);
00325
00326
00327
00328
00329
00330 pt_out = buffer_out;
00331
00332 for (i=0; i<fd.number_of_fields; i++)
00333 {
00334 v = string2unsigned_int (fd.tabs[i], &err);
00335 if (err)
00336 {
00337 my_syslog (log_file,
00338 "[WARNING] [%s:%d] uint32_to_network_byte_order: Invalid integer (%s) - skip",
00339 __FILE__, __LINE__, fd.tabs[i]);
00340 n -= 1;
00341 continue;
00342 }
00343
00344 v = htonl (v);
00345
00346 if (debug > 1)
00347 { my_syslog (log_file,
00348 "[DEBUG] [%s:%d] uint32_to_network_byte_order: %s -> %X", __FILE__, __LINE__, fd.tabs[i], v); }
00349
00350 memcpy ((void*)pt_out, (void*)&v, 4);
00351 pt_out += 4;
00352 }
00353
00354 free_fields(&fd);
00355 return n;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 int ip_to_network_byte_order (char* buffer_in, char *buffer_out, int len_out)
00371 {
00372 char *pt_out;
00373 struct fields fd;
00374 unsigned long int v;
00375 int n, i;
00376
00377
00378
00379
00380
00381 n = s_split(buffer_in, ":", &fd);
00382 if (n == -1)
00383 {
00384 my_syslog (log_file,
00385 "[ERROR] [%s:%d] ip_to_network_byte_order: Could not allocate memory", __FILE__, __LINE__);
00386 return -1;
00387 }
00388
00389
00390
00391
00392
00393 if (n == 0)
00394 {
00395 my_syslog (log_file,
00396 "[WARNING] [%s:%d] ip_to_network_byte_order: No value to convert", __FILE__, __LINE__);
00397 return 0;
00398 }
00399
00400 if (debug > 1)
00401 { my_syslog (log_file,
00402 "[DEBUG] [%s:%d] ip_to_network_byte_order: Number of IP addresses to convert is %d",
00403 __FILE__, __LINE__, n); }
00404
00405
00406
00407
00408
00409 if (n*4 > len_out)
00410 {
00411 free_fields(&fd);
00412 my_syslog (log_file,
00413 "[ERROR] [%s:%d] ip_to_network_byte_order: Size of ouput buffer is too short! %d > %d",
00414 __FILE__, __LINE__,
00415 n*4,
00416 len_out);
00417 return -1;
00418 }
00419
00420 memset ((void*)buffer_out, 0, len_out);
00421
00422
00423
00424
00425
00426 pt_out = buffer_out;
00427
00428 for (i=0; i<fd.number_of_fields; i++)
00429 {
00430 v = get_network_ip (fd.tabs[i]);
00431 memcpy ((void*)pt_out, (void*)&v, 4);
00432
00433 if (debug > 1)
00434 { my_syslog (log_file,
00435 "[DEBUG] [%s:%d] ip_to_network_byte_order: %s -> %X", __FILE__, __LINE__, fd.tabs[i], v); }
00436
00437 pt_out += 4;
00438 }
00439
00440 free_fields(&fd);
00441 return n;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 int hexa_to_binary (char *buffer_in, char *buffer_out, int len_out)
00456 {
00457 int len_in, i;
00458
00459 len_in = strlen(buffer_in);
00460
00461
00462
00463
00464
00465
00466 if (len_in == 0)
00467 {
00468 my_syslog (log_file,
00469 "[WARNING] [%s:%d] hexa_to_binary: No value to convert", __FILE__, __LINE__);
00470 return 0;
00471 }
00472
00473 if (len_in % 2 != 0)
00474 {
00475 my_syslog (log_file,
00476 "[ERROR] [%s:%d] hexa_to_binary: Invalid size for input buffer (%d not multiple of 2)",
00477 __FILE__, __LINE__,
00478 len_in);
00479 return -1;
00480 }
00481
00482 if (len_in/2 > len_out)
00483 {
00484 my_syslog (log_file,
00485 "[ERROR] [%s:%d] hexa_to_binary: Size of output buffer is too short! %d > %d",
00486 __FILE__, __LINE__,
00487 len_in/2,
00488 len_out);
00489 return -1;
00490 }
00491
00492
00493
00494
00495
00496 memset ((void*)buffer_out, 0, len_out);
00497
00498 for (i=0; i<len_in/2; i++) { buffer_out[i] = (char_to_int(buffer_in[2*i]) << 4) | char_to_int(buffer_in[2*i+1]); }
00499
00500 return len_in/2;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 int ascii_to_binary (char *buffer_in, char *buffer_out, int len_in, int len_out)
00524 {
00525 if (len_in <= 0) { len_in = strlen(buffer_in); }
00526
00527
00528
00529
00530
00531 if (len_in == 0)
00532 {
00533 my_syslog (log_file,
00534 "[WARNING] [%s:%d] ascii_to_binary: No value to convert", __FILE__, __LINE__);
00535 return 0;
00536 }
00537
00538 if (len_in >= len_out)
00539 {
00540 my_syslog (log_file,
00541 "[ERROR] [%s:%d] ascii_to_binary: Size of output buffer is too short! %d > %d",
00542 __FILE__, __LINE__,
00543 len_in,
00544 len_out);
00545 return -1;
00546 }
00547
00548 if (debug > 1)
00549 { my_syslog (log_file,
00550 "[DEBUG] [%s:%d] ascii_to_binary: <%s> -> %d bytes", __FILE__, __LINE__, buffer_in, len_in); }
00551
00552
00553
00554
00555
00556 memset ((void*)buffer_out, 0, len_out);
00557 memcpy ((void*)buffer_out, buffer_in, len_in);
00558
00559 return len_in;
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 int uint16_to_network_byte_order (char* buffer_in, char *buffer_out, int len_out)
00576 {
00577 char *pt_out;
00578 struct fields fd;
00579 unsigned short int v;
00580 int n, i, err;
00581
00582
00583
00584
00585
00586 n = s_split(buffer_in, ":", &fd);
00587 if (n == -1)
00588 {
00589 my_syslog (log_file,
00590 "[ERROR] [%s:%d] uint16_to_network_byte_order: Could not allocate memory", __FILE__, __LINE__);
00591 return -1;
00592 }
00593
00594
00595
00596
00597
00598 if (n == 0)
00599 {
00600 my_syslog (log_file,
00601 "[WARNING] [%s:%d] uint16_to_network_byte_order: No value to convert", __FILE__, __LINE__);
00602 return 0;
00603 }
00604
00605 if (debug > 1)
00606 { my_syslog (log_file,
00607 "[DEBUG] [%s:%d] uint16_to_network_byte_order: Number of UINT16 to convert is %d",
00608 __FILE__, __LINE__,
00609 n);
00610 }
00611
00612
00613
00614
00615
00616 if (n*2 > len_out)
00617 {
00618 free_fields(&fd);
00619 my_syslog (log_file,
00620 "[ERROR] [%s:%d] uint16_to_network_byte_order: Size of ouput buffer is too short! %d > %d",
00621 __FILE__, __LINE__,
00622 n*2,
00623 len_out);
00624 return -1;
00625 }
00626
00627 memset ((void*)buffer_out, 0, len_out);
00628
00629
00630
00631
00632
00633 pt_out = buffer_out;
00634
00635 for (i=0; i<fd.number_of_fields; i++)
00636 {
00637 v = (unsigned short int) ((string2unsigned_int (fd.tabs[i], &err)) & 0x0000FFFF);
00638 v = htons(v);
00639 memcpy ((void*)pt_out, (void*)&v, 2);
00640
00641 if (debug > 1)
00642 { my_syslog (log_file,
00643 "[DEBUG] [%s:%d] uint16_to_network_byte_order: %s -> %d", __FILE__, __LINE__, fd.tabs[i], v); }
00644
00645 pt_out += 2;
00646 }
00647
00648 free_fields(&fd);
00649 return n;
00650 }
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671 #ifdef MYDNS_CONVERSION_BIN2HEX
00672
00673 void radius2mydns (char *radius, char *mydns, int mydns_size)
00674 { bin2hex (radius, mydns, mydns_size); }
00675
00676 #endif
00677
00678 #ifdef MYDNS_CONVERSION_SPECIFIC
00679
00680 void radius2mydns (char *radius, char *mydns, int mydns_size)
00681 {
00682 char c;
00683 int i;
00684
00685 memset ((void*)mydns, 0, mydns_size);
00686
00687
00688
00689
00690
00691
00692 for (i=0; i<strlen(radius); i++)
00693 {
00694 c = radius[i];
00695
00696 switch (c)
00697 {
00698
00699 case '-': strcat (mydns, "--"); break;
00700 case '@': strcat (mydns, "-a"); break;
00701 case '.': strcat (mydns, "-p"); break;
00702 case '_': strcat (mydns, "-u"); break;
00703
00704 case 'a': strncat (mydns, radius+i, 1); break;
00705 case 'b': strncat (mydns, radius+i, 1); break;
00706 case 'c': strncat (mydns, radius+i, 1); break;
00707 case 'd': strncat (mydns, radius+i, 1); break;
00708 case 'e': strncat (mydns, radius+i, 1); break;
00709 case 'f': strncat (mydns, radius+i, 1); break;
00710 case 'g': strncat (mydns, radius+i, 1); break;
00711 case 'h': strncat (mydns, radius+i, 1); break;
00712 case 'i': strncat (mydns, radius+i, 1); break;
00713 case 'j': strncat (mydns, radius+i, 1); break;
00714 case 'k': strncat (mydns, radius+i, 1); break;
00715 case 'l': strncat (mydns, radius+i, 1); break;
00716 case 'm': strncat (mydns, radius+i, 1); break;
00717 case 'n': strncat (mydns, radius+i, 1); break;
00718 case 'o': strncat (mydns, radius+i, 1); break;
00719 case 'p': strncat (mydns, radius+i, 1); break;
00720 case 'q': strncat (mydns, radius+i, 1); break;
00721 case 'r': strncat (mydns, radius+i, 1); break;
00722 case 's': strncat (mydns, radius+i, 1); break;
00723 case 't': strncat (mydns, radius+i, 1); break;
00724 case 'u': strncat (mydns, radius+i, 1); break;
00725 case 'v': strncat (mydns, radius+i, 1); break;
00726 case 'w': strncat (mydns, radius+i, 1); break;
00727 case 'x': strncat (mydns, radius+i, 1); break;
00728 case 'y': strncat (mydns, radius+i, 1); break;
00729 case 'z': strncat (mydns, radius+i, 1); break;
00730
00731 case 'A': strncat (mydns, radius+i, 1); break;
00732 case 'B': strncat (mydns, radius+i, 1); break;
00733 case 'C': strncat (mydns, radius+i, 1); break;
00734 case 'D': strncat (mydns, radius+i, 1); break;
00735 case 'E': strncat (mydns, radius+i, 1); break;
00736 case 'F': strncat (mydns, radius+i, 1); break;
00737 case 'G': strncat (mydns, radius+i, 1); break;
00738 case 'H': strncat (mydns, radius+i, 1); break;
00739 case 'I': strncat (mydns, radius+i, 1); break;
00740 case 'J': strncat (mydns, radius+i, 1); break;
00741 case 'K': strncat (mydns, radius+i, 1); break;
00742 case 'L': strncat (mydns, radius+i, 1); break;
00743 case 'M': strncat (mydns, radius+i, 1); break;
00744 case 'N': strncat (mydns, radius+i, 1); break;
00745 case 'O': strncat (mydns, radius+i, 1); break;
00746 case 'P': strncat (mydns, radius+i, 1); break;
00747 case 'Q': strncat (mydns, radius+i, 1); break;
00748 case 'R': strncat (mydns, radius+i, 1); break;
00749 case 'S': strncat (mydns, radius+i, 1); break;
00750 case 'T': strncat (mydns, radius+i, 1); break;
00751 case 'U': strncat (mydns, radius+i, 1); break;
00752 case 'V': strncat (mydns, radius+i, 1); break;
00753 case 'W': strncat (mydns, radius+i, 1); break;
00754 case 'X': strncat (mydns, radius+i, 1); break;
00755 case 'Y': strncat (mydns, radius+i, 1); break;
00756 case 'Z': strncat (mydns, radius+i, 1); break;
00757
00758 case '0': strncat (mydns, radius+i, 1); break;
00759 case '1': strncat (mydns, radius+i, 1); break;
00760 case '2': strncat (mydns, radius+i, 1); break;
00761 case '3': strncat (mydns, radius+i, 1); break;
00762 case '4': strncat (mydns, radius+i, 1); break;
00763 case '5': strncat (mydns, radius+i, 1); break;
00764 case '6': strncat (mydns, radius+i, 1); break;
00765 case '7': strncat (mydns, radius+i, 1); break;
00766 case '8': strncat (mydns, radius+i, 1); break;
00767 case '9': strncat (mydns, radius+i, 1); break;
00768
00769 default: strcat (mydns, "-n");
00770 }
00771 }
00772
00773 mydns[63] = 0;
00774 }
00775
00776 #endif
00777
00778 #ifdef MYDNS_CONVERSION_NOTHING
00779
00780 void radius2mydns (char *radius, char *mydns, int mydns_size)
00781 {
00782 char c;
00783 int i;
00784
00785 memset ((void*)mydns, 0, mydns_size);
00786
00787 for (i=0; i<strlen(radius); i++)
00788 {
00789 c = radius[i];
00790
00791 switch (c)
00792 {
00793
00794 case '-': strcat (mydns, "--"); break;
00795 case '@': strcat (mydns, "."); break;
00796 case '_': strcat (mydns, "-."); break;
00797 default : strncat (mydns, radius+i, 1);
00798 }
00799 }
00800 }
00801
00802 #endif
00803
00804
00805