gsasl  1.8.0
suggest.c
Go to the documentation of this file.
00001 /* suggest.c --- Suggest client mechanism to use, from a set of mechanisms.
00002  * Copyright (C) 2002-2012 Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License License along with GNU SASL Library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "internal.h"
00024 
00037 const char *
00038 gsasl_client_suggest_mechanism (Gsasl * ctx, const char *mechlist)
00039 {
00040   size_t mechlist_len, target_mech, i;
00041 
00042   mechlist_len = mechlist ? strlen (mechlist) : 0;
00043   target_mech = ctx->n_client_mechs;    /* ~ no target */
00044 
00045   for (i = 0; i < mechlist_len;)
00046     {
00047       size_t len;
00048 
00049       len = strspn (mechlist + i, GSASL_VALID_MECHANISM_CHARACTERS);
00050       if (!len)
00051         ++i;
00052       else
00053         {
00054           size_t j;
00055 
00056           /* Assumption: the mechs array is sorted by preference
00057            * from low security to high security. */
00058           for (j = (target_mech < ctx->n_client_mechs ? target_mech + 1 : 0);
00059                j < ctx->n_client_mechs; ++j)
00060             {
00061               if (strncmp (ctx->client_mechs[j].name, mechlist + i, len) == 0)
00062                 {
00063                   Gsasl_session *sctx;
00064 
00065                   if (gsasl_client_start (ctx, ctx->client_mechs[j].name,
00066                                           &sctx) == GSASL_OK)
00067                     {
00068                       gsasl_finish (sctx);
00069                       target_mech = j;
00070                     }
00071 
00072                   break;
00073                 }
00074             }
00075           i += len + 1;
00076         }
00077     }
00078 
00079   return target_mech < ctx->n_client_mechs ?
00080     ctx->client_mechs[target_mech].name : NULL;
00081 }