00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <sys/types.h>
00020 #include <unistd.h>
00021 #include <time.h>
00022 #include <sys/syslog.h>
00023 #include <libdhcp.h>
00024 #include <stdio.h>
00025 #include <malloc.h>
00026
00027 LIBDHCP_Control
00028 *libdhcp_control_new
00029 ( LIBDHCP_Callback callback,
00030 LIBDHCP_Capability dhc_cap,
00031 time_t timeout,
00032 void *arg,
00033 LIBDHCP_Error_Handler error_handler,
00034 uint8_t log_level
00035 )
00036 {
00037 LIBDHCP_Control *dhc = calloc( 1, sizeof( LIBDHCP_Control ) );
00038 if ( dhc == 0L )
00039 return 0L;
00040 dhc->capability = dhc_cap;
00041 dhc->callback = callback;
00042 dhc->timeout = timeout;
00043 dhc->arg = arg;
00044 dhc->eh = error_handler;
00045 dhc->log_level = log_level;
00046 return dhc;
00047 }
00048
00049 void libdhcp_control_free(LIBDHCP_Control *dhc) {
00050 free(dhc);
00051 }
00052
00053 extern char **environ;
00054
00055 int libdhcp_call_client
00056 ( LIBDHCP_Control *control,
00057 DHCP_Client client,
00058 ...
00059 )
00060 {
00061 va_list ap;
00062 int argc=0;
00063 char *argv [ 32 ], **argvp=argv, *p;
00064
00065 va_start(ap, client);
00066
00067 while( (argc < 32) && ((p = va_arg(ap, char*) ) != 0 ) )
00068 {
00069 *(argvp++)=p;
00070 argc++;
00071 }
00072 *argvp = 0;
00073 va_end(ap);
00074
00075 return (*client)(control, argc, argv, environ);
00076 }
00077
00078
00079 char *libdhcp_state_string( DHCP_State s, char *buf )
00080 {
00081 static char sbuf[32];
00082 char *p = buf ? &(buf[0]) : &(sbuf[0]);
00083
00084 sprintf
00085 ( p, "%s",
00086 (s == DHC4_NBI)
00087 ?"DHC4_NBI"
00088 :(s == DHC4_PREINIT)
00089 ?"DHC4_PREINIT"
00090 :(s == DHC4_BOUND)
00091 ?"DHC4_BOUND"
00092 :(s == DHC4_RENEW)
00093 ?"DHC4_RENEW"
00094 :(s == DHC4_REBOOT)
00095 ?"DHC4_REBOOT"
00096 :(s == DHC4_REBIND)
00097 ?"DHC4_REBIND"
00098 :(s == DHC4_STOP)
00099 ?"DHC4_STOP"
00100 :(s == DHC4_MEDIUM)
00101 ?"DHC4_MEDIUM"
00102 :(s == DHC4_TIMEOUT)
00103 ?"DHC4_TIMEOUT"
00104 :(s == DHC4_FAIL)
00105 ?"DHC4_FAIL"
00106 :(s == DHC4_EXPIRE)
00107 ?"DHC4_EXPIRE"
00108 :(s == DHC4_RELEASE)
00109 ?"DHC4_RELEASE"
00110 :(s == DHC_TIMEDOUT)
00111 ?"DHC_TIMEDOUT"
00112 :(s == DHC6_BOUND)
00113 ?"DHC6_BOUND"
00114 :(s == DHC6_REBIND)
00115 ?"DHC6_REBIND"
00116 :(s == DHC6_RELEASE)
00117 ?"DHC6_RELEASE"
00118 :"DHC_INVALID"
00119 );
00120
00121 return p;
00122 }
00123
00124 int
00125 libdhcp_stderr_logger
00126 ( struct libdhcp_control_s *ctl,
00127 int priority,
00128 const char *fmt,
00129 va_list ap
00130 )
00131 {
00132 struct tm tm;
00133 int len=0;
00134 if ( (priority != LOG_FATAL) && ctl && (priority > ctl->log_level) )
00135 return 0;
00136 time_t t=time(0);
00137 tm=*localtime(&t);
00138 fprintf(stderr, "%.4u-%.2u-%.2u_%.2u:%.2u:%.2u libdhcp [%d] %s:",
00139 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
00140 getpid(),
00141 (priority == LOG_FATAL)
00142 ? "FATAL"
00143 : (priority == LOG_ERR)
00144 ? "ERROR"
00145 : (priority == LOG_INFO)
00146 ? "INFO"
00147 : (priority == LOG_DEBUG)
00148 ? "DBG"
00149 : ""
00150 );
00151 if(priority == LOG_FATAL)
00152 if( ctl )
00153 ctl->finished = 1;
00154 len+=vprintf(fmt,ap);
00155 printf("\n");
00156 fflush(stderr);
00157 return len;
00158 }
00159
00160 int
00161 libdhcp_syslogger
00162 ( struct libdhcp_control_s *ctl,
00163 int priority,
00164 const char *fmt,
00165 va_list ap
00166 )
00167 {
00168 if ( (priority != LOG_FATAL) && ctl && (priority > ctl->log_level) )
00169 return 0;
00170 if(priority == LOG_FATAL)
00171 {
00172 if(ctl)
00173 ctl->finished = 1;
00174 priority = LOG_ERR;
00175 }
00176 vsyslog( priority, fmt, ap );
00177 return 1;
00178 }