Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

modem.c

00001 /*! \brief modem.c
00002      This file contains all functionn associated with the modem's manipulation.
00003  */
00004 
00005 #include <string.h>
00006 #include "modem.h"
00007 
00008 #ifndef USE_PLUGINS
00009     #include "hitachi.h"
00010 #endif
00011 
00012 /*! \brief Debug mode. */
00013 
00014 static int DEBUG = 0;
00015 
00016 /*! \brief Pointer to the log file used by this module.
00017  */
00018 
00019 static char    *log_file = NULL;
00020 
00021 /*! \brief Fake logging service (used if logging is not activated).
00022     \author Denis BEURIVE
00023  */
00024 
00025 static int my_fake_syslog (const char *file, const char * fmt,...) { return 0; }
00026 
00027 /*! \brief Pointer to the log routine used by this module.
00028     \remark By default, the module uses a fake syslog routine that does nothing.
00029             You must call the function init_modem_syslog() to assign a new syslog routine.
00030  */
00031 
00032 static int     (*my_syslog) (const char *file, const char * fmt,...) = my_fake_syslog;
00033 
00034 /*! \author Denis BEURIVE
00035     \brief Initialize the syslog service.
00036     \param syslog Pointer to a function used to log messages. The function signature must be :<P>
00037            int syslog (const char *file, const char * fmt,...)<P>
00038            Upon successful completion, the function returns the value 0.<P>
00039            To desactivate logging, just set this pointer to NULL.<P>
00040            If NULL, logging service is not activated (no matter the value of 'log').
00041     \param log Pointer to the log file.
00042            If NULL, logging service is not activated (no matter the value of 'syslog').
00043     \param debug Integer that set the debug level (0: no debug).
00044  */
00045 
00046 void init_modem_syslog (int (*syslog)(const char *file, const char * fmt,...),
00047                         char *log,
00048                         int debug)
00049 {
00050   if ((syslog != NULL) && (log != NULL))
00051   {
00052      my_syslog = syslog;
00053      log_file  = log;
00054      DEBUG     = debug;
00055   }
00056   else { my_syslog = my_fake_syslog; }
00057 
00058   return;
00059 }
00060 
00061 /*! \author Denis BEURIVE
00062     \brief Find the manufacturor of a modem (looking at the DHCP packet).
00063     \param packet Pointer to a 'dhcp_packet' data structure that represents the incoming packet.
00064            associated with the interace.
00065     \return The function returns an integer value that represents the modem's manufacturor.
00066             The list of possible values is defined in the file 'modem.h'.
00067     \remark The returned value MODEM_UNKNOWN means that the modem is not in the list of 
00068             selected modems. This is not an error. 
00069  */
00070 
00071 int get_modem_manufacturor (struct dhcp_packet *packet)
00072 {
00073   char    *haddr;
00074 
00075   /* ------------------------------------------------------------------ */
00076   /* Lookup MAC address                                                 */
00077   /* ------------------------------------------------------------------ */
00078 
00079   haddr = packets_get_chaddr_no_separator(packet);
00080 
00081   #ifdef USE_PLUGINS
00082 
00083        return (modem_conf_get_id(haddr));
00084 
00085   #else
00086 
00087        if (strncmp(haddr, HITACHI_MARK1, strlen(HITACHI_MARK1)) == 0) { return MODEM_HITACHI; }
00088        if (strncmp(haddr, HITACHI_MARK2, strlen(HITACHI_MARK2)) == 0) { return MODEM_HITACHI; }
00089        if (strncmp(haddr, SAGEM_MARK1,   strlen(SAGEM_MARK1))   == 0) { return MODEM_SAGEM;   }
00090        if (strncmp(haddr, SAGEM_MARK2,   strlen(SAGEM_MARK2))   == 0) { return MODEM_SAGEM;   }
00091        return MODEM_UNKNOWN;
00092 
00093   #endif
00094 }
00095 
00096 /*! \author Denis BEURIVE
00097     \brief Calculate the modem's network MAC address from the modem mother board MAC address.
00098     \param packet Pointer to a 'dhcp_packet' data structure that represents the incoming packet.
00099     \param manufacturor Integer value that represents the modem's manufacturor (as returnrd by the
00100                         function get_modem_manufacturor()).
00101     \return The function returns a pointer to a statically allocated string of characters that represents
00102             the mac address in hexadecimal notation (without ':').
00103  */
00104 
00105 char *calculate_modem_mac_address (struct dhcp_packet *packet, int manufacturor)
00106 {
00107   char *real_haddr;
00108   static char   haddr_hexa[DHCP_P_STR_HADDR_SIZE_EXACT];
00109 
00110   #ifndef USE_PLUGINS
00111   #else
00112       char *new_haddr;
00113       char* (*calculate_mac_address)(char*);
00114   #endif
00115 
00116   real_haddr = packets_get_chaddr_no_separator(packet);
00117   memset ((void*)haddr_hexa, 0, DHCP_P_STR_HADDR_SIZE_EXACT);
00118   strncpy (haddr_hexa, real_haddr, DHCP_P_STR_HADDR_SIZE_EXACT-1);
00119 
00120   #ifndef USE_PLUGINS
00121     
00122       /* -------------------------------------------------------------- */
00123       /* HITACHI - substract 4 from the MAC address                     */
00124       /*                                                                */
00125       /* NOTE: sizeof(BigNumDigit)=4 bytes !!!                          */
00126       /* -------------------------------------------------------------- */
00127         
00128       if (manufacturor == MODEM_HITACHI)
00129       {
00130         calculate_mac (haddr_hexa);
00131     
00132         if (DEBUG > 1)
00133         {
00134           my_syslog (log_file, "[DEBUG] [%s:%d] calculate_modem_mac_address: HITACHI MAC address [%s] => [%s]",
00135                      __FILE__, __LINE__, real_haddr, haddr_hexa); 
00136         }
00137     
00138         return haddr_hexa;
00139       }
00140     
00141       /* -------------------------------------------------------------- */
00142       /* Other modem => return the MAC address "as is"                  */
00143       /* -------------------------------------------------------------- */
00144     
00145       if (DEBUG > 1)
00146       {
00147         my_syslog (log_file, "[DEBUG] [%s:%d] calculate_modem_mac_address: UNKNOWN MAC address [%s] => [%s]",
00148                               __FILE__, __LINE__, real_haddr, haddr_hexa);
00149       }
00150     
00151       return haddr_hexa;
00152 
00153   #else
00154 
00155       calculate_mac_address = (char*(*)(char*))modem_conf_get_function(manufacturor);
00156       new_haddr = (*calculate_mac_address)(haddr_hexa);
00157 
00158       if (DEBUG > 1)
00159       {
00160         my_syslog (log_file, "[DEBUG] [%s:%d] calculate_modem_mac_address: MAC [%s] => [%s]",
00161                    __FILE__, __LINE__, real_haddr, new_haddr); 
00162       }
00163 
00164       return new_haddr;
00165 
00166   #endif
00167 }
00168 
00169 /*! \brief Return a string that represents the modem's manufacturor.
00170     \author Denis BEURIVE
00171     \param id Integer that represents the manufacturor (see file 'modem.h' for the list of IDs).
00172     \return The function returns a pointer to a zero terminated string of characters that represents
00173             the modem's manufacturor.
00174  */
00175 
00176 char *id_manufacturor_to_string (int id)
00177 {
00178   #ifndef USE_PLUGINS
00179 
00180       static char m_hitachi[] = "HITACHI";
00181       static char m_sagem[]   = "SAGEM";
00182       static char m_unknown[] = "UNKNOWN";
00183     
00184       switch (id)
00185       {
00186         case MODEM_HITACHI: return m_hitachi;
00187         case MODEM_SAGEM:   return m_sagem; 
00188         default: return m_unknown;
00189       }
00190     
00191       return m_unknown;
00192 
00193   #else
00194 
00195       return modem_conf_get_tag_name(id);
00196 
00197   #endif
00198 }
00199 
00200 
00201 
00202 

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