Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

conversion.c

Go to the documentation of this file.
00001 /*! \file conversion.c
00002     This file contains various functions for converting data from one format to another.
00003  */
00004 
00005 #include "conversion.h"
00006 
00007 
00008 /* -------------------------------------------------------------------------- */
00009 /* DEBUG MANAGEMENT                                                           */ 
00010 /* -------------------------------------------------------------------------- */
00011 
00012 /*! \brief Debug flag. */
00013 
00014 static int debug = 0;
00015 
00016 /*! \brief Default value for the log file (an empty string).
00017  */
00018 
00019 static char default_log_file[] = "\0";
00020 
00021 /*! \brief Pointer to a given zero terminated string of characters that represents the path to the log file.
00022  */
00023 
00024 static char *log_file = default_log_file;
00025 
00026 /*! \brief This is a handler to a fake loggin service.
00027  */
00028 
00029 static int my_fake_syslog (const char *file, const char * fmt,...) { return 0; }
00030 
00031 /*! \brief Logging service. by default it is assigned to the fake logging service that does nothing.
00032  */
00033 
00034 static int (*my_syslog) (const char *file, const char * fmt,...) = my_fake_syslog;
00035 
00036 /*! \brief Assign a logging service to the conversion module.
00037     \author Denis BEURIVE
00038     \param dbg Verbosity level.
00039     \param logfile Pointer to a zero terminated string of characters that represents the path to the log file.
00040     \param logger Pointer to a guven logging service.
00041     \remark <UL>
00042               <LI>If 'logger' is set to NULL, then the logging service is not activated.
00043               <LI>If 'logfile' is set to NULL, then the logging service is not activated.
00044               <LI>If 'logfile' points to an empty string, then the logging service is not activated.
00045             </UL>
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 /* END OF DEBUG MANAGEMENT                                                    */ 
00060 /* -------------------------------------------------------------------------- */
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 /*! \brief Convert an integer (between 0 and 15 included) into the corresponding hexa character (from 0 to F).
00069     \author Denis BEURIVE
00070     \param c Integer value to convert (between 0 and 15 included).
00071     \return The function returns the corresponding hexa character.
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 /*! \brief Convert a given hexa character (from '0' to 'F') into the corresponding integer value
00100            (from 0 to 15 included).
00101     \author Denis BEURIVE
00102     \param c Hexa character to convert (from '0' to 'F'). Note the c could be [0-9a-fA-F].
00103     \return The function returns the corresponding integer value.
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 /*! \brief Convert a hardware address in standard ':' notation into its RAW representation.
00140     \author Denis BEURIVE
00141     \param haddr Pointer to a zero terminated string of characters that represents the hardware address in
00142            standard ':' notation.
00143     \param length Pointer to an integer that will be used to store the size (in bytes) of the RAW representation.
00144     \return Upon successful completion, the function returns a pointer to the RAW representation.
00145             The value pointed by 'lenght' is the number of bytes of the representation.
00146             Otherwize, the function returns the value NULL.
00147     \warning <UL>
00148                <LI>The buffer (which contains the RAW representation) is returned in a statically allocated
00149                    buffer,
00150                    which subsequent calls will overwrite. Do not try to free it!
00151                <LI>The maximum lenth for the RAW representation is 16 bytes.
00152              </UL>
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 /*! \brief Convert IP address in standard numbers-and-dots notation into binary data
00190            (4 bytes in network byte order).
00191     \author Denis BEURIVE
00192     \param ip Pointer to a zero terminated string of characters that represents the IP address to convert.
00193     \return The function returns the binary representation of the IP address (in network byte order).
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 /*! \brief Convert IP address in binary representation (4 bytes in network byte order)
00205            into standard numbers-and-dots notation.
00206     \author Denis BEURIVE
00207     \param ip Binary representation of the IP address to convert (4 bytes in network byte order).
00208     \return The function returns a zero terminated string that represents the IP address in standard
00209             numbers-and-dots notation.
00210     \warning The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
00211              Do not try to free it!
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 /*! \brief Convert a list of RAW IP addresses (4 bytes in network IP order) into a string representation
00226            (standard numbers-and-dots notation separated by characters ':').
00227     \author Denis BEURIVE
00228     \param buffin Input buffer that contains the list of RAW IP addresses.
00229     \param buffout Output buffer that will receive the string representation.
00230     \param lenin Size (in bytes) in the input buffer.
00231     \param lenout Size (in bytes) in the output buffer.
00232     \return Upon successful completion, the function returns the value 0.
00233             Otherwize, the function returns the value 1.
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 /*! \brief Convert a list of UINT32 into a succession of 4 bytes integers in network byte order.
00263     \author Denis BEURIVE
00264     \param buffer_in Zero terminated string of characters that represents the list of UINT32
00265            (the separator is the ':').
00266     \param buffer_out pointer to a buffer that will receive the 4 bytes integers in network byte order.
00267     \param len_out Size, in bytes, of the buffer buffer_out.
00268     \return Upon successful completion, the function returns a positive (>= 0) value. Otherwize,
00269             the function returns the value -1.
00270             If success, the returned value represents the number of UINT32.
00271     \remark If an error occured, look at the log file.
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   /* Split input string                                   */
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   /* If no value to convert, then return                  */
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   /* Make sure that len is a multiple of 4 bytes          */
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   /* Process buffer by groups of 4 bytes                  */
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 /*! \brief Convert a list of IP addresses into a succession of 4 bytes integers in network byte order.
00359     \author Denis BEURIVE
00360     \param buffer_in Zero terminated string of characters that represents the list of IP addresses
00361            (the separator is the ':').
00362     \param buffer_out pointer to a buffer that will receive the 4 bytes integers in network byte order.
00363     \param len_out Size, in bytes, of the buffer buffer_out.
00364     \return Upon successful completion, the function returns a positive (>= 0) value. Otherwize,
00365             the function returns the value -1.
00366             If success, the returned value represents the number of IP addresses.
00367     \remark If an error occured, look at the log file.
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   /* Split input string                                   */
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   /* If no value to convert, then return                  */
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   /* Make sure that len is a multiple of 4 bytes          */
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   /* Process buffer by groups of 4 bytes                  */
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 /*! \brief Convert hexa representation into bytes.
00445     \author Denis BEURIVE
00446     \param buffer_in Zero terminated string of characters that represents a succession of bytes in hexa.
00447     \param buffer_out Pointer to a buffer that will receive the binary representation.
00448     \param len_out Size, in bytes, of the buffer buffer_out.
00449     \return Upon successful completion, the function returns a positive (>= 0) value.
00450             Otherwize, the function returns the value -1.
00451             If success, the returned value represents the number of bytes.
00452     \remark If an error occured, look at the log file.
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   /* Sanity check                                         */
00463   /* Note: 2 hexa characters represent 1 byte.            */
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   /* Convert couples of hexa characters into binary       */ 
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 /*! \brief Convert ASCII representation into bytes.
00504     \author Denis BEURIVE
00505     \param buffer_in Zero terminated string of characters that represents a succession of bytes in ASCII.
00506     \param buffer_out Pointer to a buffer that will receive the binary representation.
00507     \param len_in Size, in bytes, of the buffer buffer_in.
00508            <UL>
00509                <LI>If you set the value of len_in to 0, then the function will consider the exact size
00510                    of the zero terminated string pointed by buffer_in. In other words, len_in will be set
00511                    to strlen(buffer_in).
00512                <LI>If you set the value of len_in to any positive value, then the function will consider
00513                    the exact number of characters specified by len_in. This feature can be used to add the
00514                    terminal zero.
00515            </UL> 
00516     \param len_out Size, in bytes, of the buffer buffer_out.
00517     \return Upon successful completion, the function returns a positive (>= 0) value.
00518             Otherwize, the function returns the value -1.
00519             If success, the returned value represents the number of bytes.
00520     \remark If an error occured, look at the log file.
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   /* Sanity check                                         */
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   /* Convert couples of hexa characters into binary       */
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 /*! \brief Convert a list of UINT16 into a succession of 2 bytes integers in network byte order.
00564     \author Denis BEURIVE
00565     \param buffer_in Zero terminated string of characters that represents the list of UINT16
00566            (the separator is the ':').
00567     \param buffer_out pointer to a buffer that will receive the 2 bytes integers in network byte order.
00568     \param len_out Size, in bytes, of the buffer buffer_out.
00569     \return Upon successful completion, the function returns a positive (>= 0) value.
00570             Otherwize, the function returns the value -1.
00571             If success, the returned value represents the number of UINT16.
00572     \remark If an error occured, look at the log file.
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   /* Split input string                                   */
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   /* If no value to convert, then return                  */
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   /* Make sure that len is a multiple of 2 bytes          */
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   /* Process buffer by groups of 2 bytes                  */
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 /*! \brief Convert a radius login into mydns ID. The conversion is :
00653            <ul>
00654              <li>s/-/--/g
00655              <li>s/@/-a/g
00656              <li>s/\./-p/g
00657              <li>s/_/-u/g
00658              <li>s/[^a-zA-Z0-9-]/-n/g
00659              <li>Keep only the 63 first characters.
00660            </ul>
00661     \author Denis BEURIVE
00662     \param radius Pointer to a zero terminated string of characters that represents the radius login to
00663                   convert into MyDns ID.
00664     \param mydns Pointer to a memory buffer used to store the converted radius login.
00665     \param mydns_size Number of bytes reserved for the buffer pointed by 'mydns'.
00666     \remark Make sure that the size of 'mydns' is long enough. You should have:
00667             mydns_size > 2*strlen(radius)
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     /* The following code is a "BRUTE force" conversion.  */ 
00689     /* It could have been MUCH shorter. But who cares?    */
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 

Generated on Mon Jun 19 12:31:05 2006 for MyDhcp_V2 by doxygen1.2.15