dict.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #ifndef CURL_DISABLE_DICT
00026 
00027 #ifdef HAVE_NETINET_IN_H
00028 #include <netinet/in.h>
00029 #endif
00030 #ifdef HAVE_NETDB_H
00031 #include <netdb.h>
00032 #endif
00033 #ifdef HAVE_ARPA_INET_H
00034 #include <arpa/inet.h>
00035 #endif
00036 #ifdef HAVE_NET_IF_H
00037 #include <net/if.h>
00038 #endif
00039 #ifdef HAVE_SYS_IOCTL_H
00040 #include <sys/ioctl.h>
00041 #endif
00042 
00043 #ifdef HAVE_SYS_PARAM_H
00044 #include <sys/param.h>
00045 #endif
00046 
00047 #ifdef HAVE_SYS_SELECT_H
00048 #include <sys/select.h>
00049 #endif
00050 
00051 #include "urldata.h"
00052 #include <curl/curl.h>
00053 #include "transfer.h"
00054 #include "sendf.h"
00055 #include "escape.h"
00056 #include "progress.h"
00057 #include "dict.h"
00058 #include "strcase.h"
00059 #include "curl_memory.h"
00060 /* The last #include file should be: */
00061 #include "memdebug.h"
00062 
00063 /*
00064  * Forward declarations.
00065  */
00066 
00067 static CURLcode dict_do(struct connectdata *conn, bool *done);
00068 
00069 /*
00070  * DICT protocol handler.
00071  */
00072 
00073 const struct Curl_handler Curl_handler_dict = {
00074   "DICT",                               /* scheme */
00075   ZERO_NULL,                            /* setup_connection */
00076   dict_do,                              /* do_it */
00077   ZERO_NULL,                            /* done */
00078   ZERO_NULL,                            /* do_more */
00079   ZERO_NULL,                            /* connect_it */
00080   ZERO_NULL,                            /* connecting */
00081   ZERO_NULL,                            /* doing */
00082   ZERO_NULL,                            /* proto_getsock */
00083   ZERO_NULL,                            /* doing_getsock */
00084   ZERO_NULL,                            /* domore_getsock */
00085   ZERO_NULL,                            /* perform_getsock */
00086   ZERO_NULL,                            /* disconnect */
00087   ZERO_NULL,                            /* readwrite */
00088   PORT_DICT,                            /* defport */
00089   CURLPROTO_DICT,                       /* protocol */
00090   PROTOPT_NONE | PROTOPT_NOURLQUERY      /* flags */
00091 };
00092 
00093 static char *unescape_word(struct Curl_easy *data, const char *inputbuff)
00094 {
00095   char *newp;
00096   char *dictp;
00097   char *ptr;
00098   size_t len;
00099   char ch;
00100   int olen=0;
00101 
00102   CURLcode result = Curl_urldecode(data, inputbuff, 0, &newp, &len, FALSE);
00103   if(!newp || result)
00104     return NULL;
00105 
00106   dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
00107   if(dictp) {
00108     /* According to RFC2229 section 2.2, these letters need to be escaped with
00109        \[letter] */
00110     for(ptr = newp;
00111         (ch = *ptr) != 0;
00112         ptr++) {
00113       if((ch <= 32) || (ch == 127) ||
00114           (ch == '\'') || (ch == '\"') || (ch == '\\')) {
00115         dictp[olen++] = '\\';
00116       }
00117       dictp[olen++] = ch;
00118     }
00119     dictp[olen]=0;
00120   }
00121   free(newp);
00122   return dictp;
00123 }
00124 
00125 static CURLcode dict_do(struct connectdata *conn, bool *done)
00126 {
00127   char *word;
00128   char *eword;
00129   char *ppath;
00130   char *database = NULL;
00131   char *strategy = NULL;
00132   char *nthdef = NULL; /* This is not part of the protocol, but required
00133                           by RFC 2229 */
00134   CURLcode result=CURLE_OK;
00135   struct Curl_easy *data=conn->data;
00136   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
00137 
00138   char *path = data->state.path;
00139   curl_off_t *bytecount = &data->req.bytecount;
00140 
00141   *done = TRUE; /* unconditionally */
00142 
00143   if(conn->bits.user_passwd) {
00144     /* AUTH is missing */
00145   }
00146 
00147   if(strncasecompare(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
00148      strncasecompare(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
00149      strncasecompare(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
00150 
00151     word = strchr(path, ':');
00152     if(word) {
00153       word++;
00154       database = strchr(word, ':');
00155       if(database) {
00156         *database++ = (char)0;
00157         strategy = strchr(database, ':');
00158         if(strategy) {
00159           *strategy++ = (char)0;
00160           nthdef = strchr(strategy, ':');
00161           if(nthdef) {
00162             *nthdef = (char)0;
00163           }
00164         }
00165       }
00166     }
00167 
00168     if((word == NULL) || (*word == (char)0)) {
00169       infof(data, "lookup word is missing\n");
00170       word=(char *)"default";
00171     }
00172     if((database == NULL) || (*database == (char)0)) {
00173       database = (char *)"!";
00174     }
00175     if((strategy == NULL) || (*strategy == (char)0)) {
00176       strategy = (char *)".";
00177     }
00178 
00179     eword = unescape_word(data, word);
00180     if(!eword)
00181       return CURLE_OUT_OF_MEMORY;
00182 
00183     result = Curl_sendf(sockfd, conn,
00184                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
00185                         "MATCH "
00186                         "%s "    /* database */
00187                         "%s "    /* strategy */
00188                         "%s\r\n" /* word */
00189                         "QUIT\r\n",
00190 
00191                         database,
00192                         strategy,
00193                         eword
00194                         );
00195 
00196     free(eword);
00197 
00198     if(result) {
00199       failf(data, "Failed sending DICT request");
00200       return result;
00201     }
00202     Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
00203                         -1, NULL); /* no upload */
00204   }
00205   else if(strncasecompare(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
00206           strncasecompare(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
00207           strncasecompare(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
00208 
00209     word = strchr(path, ':');
00210     if(word) {
00211       word++;
00212       database = strchr(word, ':');
00213       if(database) {
00214         *database++ = (char)0;
00215         nthdef = strchr(database, ':');
00216         if(nthdef) {
00217           *nthdef = (char)0;
00218         }
00219       }
00220     }
00221 
00222     if((word == NULL) || (*word == (char)0)) {
00223       infof(data, "lookup word is missing\n");
00224       word=(char *)"default";
00225     }
00226     if((database == NULL) || (*database == (char)0)) {
00227       database = (char *)"!";
00228     }
00229 
00230     eword = unescape_word(data, word);
00231     if(!eword)
00232       return CURLE_OUT_OF_MEMORY;
00233 
00234     result = Curl_sendf(sockfd, conn,
00235                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
00236                         "DEFINE "
00237                         "%s "     /* database */
00238                         "%s\r\n"  /* word */
00239                         "QUIT\r\n",
00240                         database,
00241                         eword);
00242 
00243     free(eword);
00244 
00245     if(result) {
00246       failf(data, "Failed sending DICT request");
00247       return result;
00248     }
00249     Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
00250                         -1, NULL); /* no upload */
00251   }
00252   else {
00253 
00254     ppath = strchr(path, '/');
00255     if(ppath) {
00256       int i;
00257 
00258       ppath++;
00259       for(i = 0; ppath[i]; i++) {
00260         if(ppath[i] == ':')
00261           ppath[i] = ' ';
00262       }
00263       result = Curl_sendf(sockfd, conn,
00264                           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
00265                           "%s\r\n"
00266                           "QUIT\r\n", ppath);
00267       if(result) {
00268         failf(data, "Failed sending DICT request");
00269         return result;
00270       }
00271 
00272       Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
00273     }
00274   }
00275 
00276   return CURLE_OK;
00277 }
00278 #endif /*CURL_DISABLE_DICT*/


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02