diff --git a/coreapi/authentication.c b/coreapi/authentication.c index 6551b7121..24bed1c43 100644 --- a/coreapi/authentication.c +++ b/coreapi/authentication.c @@ -50,6 +50,18 @@ LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *useri return obj; } +static LinphoneAuthInfo *linphone_auth_info_clone(const LinphoneAuthInfo *ai){ + LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1); + if (ai->username) obj->username=ms_strdup(ai->username); + if (ai->userid) obj->userid=ms_strdup(ai->userid); + if (ai->passwd) obj->passwd=ms_strdup(ai->passwd); + if (ai->ha1) obj->ha1=ms_strdup(ai->ha1); + if (ai->realm) obj->realm=ms_strdup(ai->realm); + obj->works=FALSE; + obj->usecount=0; + return obj; +} + /** * Returns username. **/ @@ -193,7 +205,7 @@ static int realm_match(const char *realm1, const char *realm2){ /** * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore. **/ -LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username) +const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username) { MSList *elem; LinphoneAuthInfo *ret=NULL,*candidate=NULL; @@ -230,31 +242,25 @@ LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *rea * * This information will be used during all SIP transacations that require authentication. **/ -void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info) +void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info) { - MSList *elem; LinphoneAuthInfo *ai; + MSList *elem; /* find if we are attempting to modify an existing auth info */ - ai=linphone_core_find_auth_info(lc,info->realm,info->username); + ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username); if (ai!=NULL){ - elem=ms_list_find(lc->auth_info,ai); - if (elem==NULL){ - ms_error("AuthInfo list corruption ?"); - return; - } - linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data); - elem->data=(void *)info; - }else { - lc->auth_info=ms_list_append(lc->auth_info,(void *)info); + lc->auth_info=ms_list_remove(lc->auth_info,ai); + linphone_auth_info_destroy(ai); } + lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info)); /* retry pending authentication operations */ for(elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){ const char *username,*realm; SalOp *op=(SalOp*)elem->data; LinphoneAuthInfo *ai; sal_op_get_auth_requested(op,&realm,&username); - ai=linphone_core_find_auth_info(lc,realm,username); + ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username); if (ai){ SalAuthInfo sai; sai.username=ai->username; @@ -278,21 +284,20 @@ void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *inf /** * Removes an authentication information object. **/ -void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){ - int len=ms_list_size(lc->auth_info); - int newlen; +void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){ int i; MSList *elem; - lc->auth_info=ms_list_remove(lc->auth_info,info); - newlen=ms_list_size(lc->auth_info); - /*printf("len=%i newlen=%i\n",len,newlen);*/ - linphone_auth_info_destroy(info); - for (i=0;irealm,info->username); + if (r){ + lc->auth_info=ms_list_remove(lc->auth_info,r); + /*printf("len=%i newlen=%i\n",len,newlen);*/ + linphone_auth_info_destroy(r); + for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){ + linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i); + } linphone_auth_info_write_config(lc->config,NULL,i); } - for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){ - linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i); - } } /** diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index f6c5f034a..cb0c7b48e 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -328,7 +328,7 @@ static void call_failure(SalOp *op, SalError error, SalReason sr, const char *de static void auth_requested(SalOp *h, const char *realm, const char *username){ LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h)); - LinphoneAuthInfo *ai=linphone_core_find_auth_info(lc,realm,username); + LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username); if (ai && (ai->works || ai->usecount<3)){ SalAuthInfo sai; sai.username=ai->username; @@ -345,7 +345,7 @@ static void auth_requested(SalOp *h, const char *realm, const char *username){ static void auth_success(SalOp *h, const char *realm, const char *username){ LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(h)); - LinphoneAuthInfo *ai=linphone_core_find_auth_info(lc,realm,username); + LinphoneAuthInfo *ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username); if (ai) ai->works=TRUE; } diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 6ccac20b5..268e8f40f 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -3321,6 +3321,7 @@ static void linphone_core_uninit(LinphoneCore *lc) ui_config_uninit(lc); if (lp_config_needs_commit(lc->config)) lp_config_sync(lc->config); lp_config_destroy(lc->config); + lc->config = NULL; /* Mark the config as NULL to block further calls */ sip_setup_unregister_all(); linphone_core_free_payload_types(); diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index af5435769..7957747b0 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -530,13 +530,13 @@ void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index); int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config); -void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info); +void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info); -void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info); +void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info); const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc); -LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username); +const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username); void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info); diff --git a/coreapi/misc.c b/coreapi/misc.c index f0cf871fd..70c75d82a 100644 --- a/coreapi/misc.c +++ b/coreapi/misc.c @@ -200,7 +200,7 @@ int linphone_core_enable_payload_type(LinphoneCore *lc, PayloadType *pt, bool_t const char *linphone_core_get_payload_type_description(LinphoneCore *lc, PayloadType *pt){ if (ms_filter_codec_supported(pt->mime_type)){ MSFilterDesc *desc=ms_filter_get_encoder(pt->mime_type); - return desc->text; + return _(desc->text); } return NULL; } diff --git a/gtk-glade/logging.c b/gtk-glade/logging.c index a8211d1ea..4869beaac 100644 --- a/gtk-glade/logging.c +++ b/gtk-glade/logging.c @@ -19,6 +19,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "linphone.h" +#ifndef WIN32 +#include +#include +#endif + + static GtkWidget *log_window=NULL; static GStaticMutex log_mutex=G_STATIC_MUTEX_INIT; static GList *log_queue=NULL; @@ -28,6 +34,186 @@ typedef struct _LinphoneGtkLog{ gchar *msg; }LinphoneGtkLog; + +/****** + * Module to log to a file + ******/ + +/* Marker to insert as a line at the start of every log file */ +#define LOGFILE_MARKER_START "--------" +/* Marker to insert as a line at the end of every log file */ +#define LOGFILE_MARKER_STOP "--------" +/* Number of files to keep in history, log file rotation will be + performed. */ +#define LOGFILE_ROTATION 4 +/* Pointer to opened log file */ +static FILE *_logfile = NULL; + + +/* Called on exit, print out the marker, close the file and avoid to + continue logging. */ +static void linphone_gtk_log_uninit() +{ + if (_logfile != NULL) { + fprintf(_logfile, "%s\n", LOGFILE_MARKER_STOP); + fclose(_logfile); + _logfile = NULL; + } +} + +/* Called when we start logging, find a good place for the log files, + perform rotation, insert the start marker and return the pointer to + the file that should be used for logging, or NULL on errors or if + disabled. */ +static FILE *linphone_gtk_log_init() +{ + static char _logdir[1024]; + static char _logfname[1024]; + static gboolean _log_init = FALSE; + const char *dst_fname; + + dst_fname = linphone_gtk_get_ui_config("logfile",NULL); + /* For anything to happen, we need a logfile configuration variable, + this is our trigger */ + if (dst_fname) { + /* If we haven't initialised yet, arrange for _logdir to contain a + directory that has been created and _logfname to contain the + path to a file to which we will log */ + if (!_log_init) { +#ifdef WIN32 + const char *appdata=getenv("LOCALAPPDATA"); + if (appdata) { + snprintf(_logdir, sizeof(_logdir),"%s\\Linphone", appdata); + mkdir(_logdir); + } else { + _logdir[0] = '\0'; + } +#define PATH_SEPARATOR '\\' +#else + const char *home=getenv("HOME"); + if (home) { + snprintf(_logdir, sizeof(_logdir),"%s/.linphone", home); + mkdir(_logdir,S_IRUSR | S_IWUSR | S_IRGRP); + } else { + _logdir[0] = '\0'; + } +#define PATH_SEPARATOR '/' +#endif + /* We have a directory, fix the path to the log file in it and + open the file so that we will be appending to it. */ + if (_logdir[0] != '\0') { + snprintf(_logfname, sizeof(_logfname), "%s%c%s", + _logdir, PATH_SEPARATOR, dst_fname); + /* If the constant LOGFILE_ROTATION is greater than zero, then + we kick away a simple rotation that will ensure that there + are never more than LOGFILE_ROTATION+1 old copies of the + log file on the disk. The oldest file is always rotated + "away" as expected. Rotated files have the same name as + the main log file, though with a number 0..LOGFILE_ROTATION + at the end, where the greater the number is, the older the + file is. */ + if (ortp_file_exist(_logfname)==0 && LOGFILE_ROTATION > 0) { + int i; + char old_fname[1024]; + char new_fname[1024]; + + /* Rotate away existing files. We make sure to remove the + old files otherwise rename() would not work properly. We + have to loop in reverse here. */ + for (i=LOGFILE_ROTATION-1;i>=0;i--) { + snprintf(old_fname, sizeof(old_fname), "%s%c%s.%d", + _logdir, PATH_SEPARATOR, dst_fname, i); + snprintf(new_fname, sizeof(new_fname), "%s%c%s.%d", + _logdir, PATH_SEPARATOR, dst_fname, i+1); + if (ortp_file_exist(old_fname)==0) { + if (ortp_file_exist(new_fname)==0) + unlink(new_fname); + rename(old_fname, new_fname); + } + } + /* Move current log file as the first of the rotation. Make + sure to remove the old .0 also, since otherwise rename() + would not work as expected. */ + snprintf(new_fname, sizeof(new_fname), "%s%c%s.%d", + _logdir, PATH_SEPARATOR, dst_fname, 0); + if (ortp_file_exist(new_fname)==0) + unlink(new_fname); + rename(_logfname, new_fname); + } + /* Start a new log file and mark that we have now initialised */ + _logfile = fopen(_logfname, "w"); + fprintf(_logfile, "%s\n", LOGFILE_MARKER_START); + _log_init = TRUE; + } + } + } + + return _logfile; +} + +static void linphone_gtk_log_file(OrtpLogLevel lev, const char *msg) +{ + LinphoneCore *lc; + time_t now; + FILE *outlog; + + lc = linphone_gtk_get_core(); + /* Nothing to do until the core has initialised */ + if (lc == NULL) + return; + + /* lc->config will turn NULL at exit, close the file to flush and + return to stop logging */ + if (linphone_core_get_config(lc) == NULL) { + linphone_gtk_log_uninit(); + return; + } + + outlog = linphone_gtk_log_init(); + if (outlog != NULL) { + /* We have an opened file and we have initialised properly, it's + time to write all these log messages. We convert the log level + from oRTP into something readable and timestamp each log + message. The format of the timestamp can be controlled by + logfile_date_format in the GtkUi section of the config file, + but it defaults to something compact, but yet readable. */ + const char *lname="undef"; + const char *dateformat=linphone_gtk_get_ui_config("logfile_date_format", + "%Y%m%d-%H:%M:%S"); + char date[256]; + + /* Convert level constant to text */ + switch(lev){ + case ORTP_DEBUG: + lname="debug"; + break; + case ORTP_MESSAGE: + lname="message"; + break; + case ORTP_WARNING: + lname="warning"; + break; + case ORTP_ERROR: + lname="error"; + break; + case ORTP_FATAL: + lname="fatal"; + break; + default: + lname="undef"; + break; + } + /* Get current time and format it properly */ + now = time(NULL); + strftime(date, sizeof(date), dateformat, localtime(&now)); + /* Now print out the message to the logfile. We don't flush, + maybe we should do to ensure that we have all the messages in + case of a crash (which is one of the main reasons we have a + log facility in the first place). */ + fprintf(outlog, "[%s] [%s] %s\n", date, lname, msg); + } +} + void linphone_gtk_create_log_window(void){ GtkTextBuffer *b; log_window=linphone_gtk_create_window("log"); @@ -120,6 +306,7 @@ void linphone_gtk_log_push(OrtpLogLevel lev, const char *fmt, va_list args){ lgl->msg=msg; g_static_mutex_lock(&log_mutex); log_queue=g_list_append(log_queue,lgl); + linphone_gtk_log_file(lev, msg); g_static_mutex_unlock(&log_mutex); } diff --git a/gtk-glade/loginframe.c b/gtk-glade/loginframe.c index ff49c0b24..5b9d7f5c4 100644 --- a/gtk-glade/loginframe.c +++ b/gtk-glade/loginframe.c @@ -55,7 +55,7 @@ static gboolean do_login_noprompt(LinphoneProxyConfig *cfg){ void linphone_gtk_show_login_frame(LinphoneProxyConfig *cfg){ GtkWidget *mw=linphone_gtk_get_main_window(); GtkWidget *label=linphone_gtk_get_widget(mw,"login_label"); - LinphoneAuthInfo *ai; + const LinphoneAuthInfo *ai; gchar *str; LinphoneAddress *from; LinphoneCore *lc=linphone_gtk_get_core();