1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #ifndef CURL_DISABLE_DICT
27 /* -- WIN32 approved -- */
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
71 #include <curl/curl.h>
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
83 /* The last #include file should be: */
88 * Forward declarations.
91 static CURLcode dict_do(struct connectdata *conn, bool *done);
94 * DICT protocol handler.
97 const struct Curl_handler Curl_handler_dict = {
99 ZERO_NULL, /* setup_connection */
101 ZERO_NULL, /* done */
102 ZERO_NULL, /* do_more */
103 ZERO_NULL, /* connect_it */
104 ZERO_NULL, /* connecting */
105 ZERO_NULL, /* doing */
106 ZERO_NULL, /* proto_getsock */
107 ZERO_NULL, /* doing_getsock */
108 ZERO_NULL, /* perform_getsock */
109 ZERO_NULL, /* disconnect */
110 ZERO_NULL, /* readwrite */
111 PORT_DICT, /* defport */
112 CURLPROTO_DICT, /* protocol */
113 PROTOPT_NONE /* flags */
116 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
125 newp = curl_easy_unescape(data, inputbuff, 0, &len);
129 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
131 /* According to RFC2229 section 2.2, these letters need to be escaped with
136 if((byte <= 32) || (byte == 127) ||
137 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
138 dictp[olen++] = '\\';
140 dictp[olen++] = byte;
149 static CURLcode dict_do(struct connectdata *conn, bool *done)
154 char *database = NULL;
155 char *strategy = NULL;
156 char *nthdef = NULL; /* This is not part of the protocol, but required
158 CURLcode result=CURLE_OK;
159 struct SessionHandle *data=conn->data;
160 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
162 char *path = data->state.path;
163 curl_off_t *bytecount = &data->req.bytecount;
165 *done = TRUE; /* unconditionally */
167 if(conn->bits.user_passwd) {
168 /* AUTH is missing */
171 if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
172 Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
173 Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
175 word = strchr(path, ':');
178 database = strchr(word, ':');
180 *database++ = (char)0;
181 strategy = strchr(database, ':');
183 *strategy++ = (char)0;
184 nthdef = strchr(strategy, ':');
192 if((word == NULL) || (*word == (char)0)) {
193 infof(data, "lookup word is missing");
194 word=(char *)"default";
196 if((database == NULL) || (*database == (char)0)) {
197 database = (char *)"!";
199 if((strategy == NULL) || (*strategy == (char)0)) {
200 strategy = (char *)".";
203 eword = unescape_word(data, word);
205 return CURLE_OUT_OF_MEMORY;
207 result = Curl_sendf(sockfd, conn,
208 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
223 failf(data, "Failed sending DICT request");
226 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
227 -1, NULL); /* no upload */
229 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
230 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
231 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
233 word = strchr(path, ':');
236 database = strchr(word, ':');
238 *database++ = (char)0;
239 nthdef = strchr(database, ':');
246 if((word == NULL) || (*word == (char)0)) {
247 infof(data, "lookup word is missing");
248 word=(char *)"default";
250 if((database == NULL) || (*database == (char)0)) {
251 database = (char *)"!";
254 eword = unescape_word(data, word);
256 return CURLE_OUT_OF_MEMORY;
258 result = Curl_sendf(sockfd, conn,
259 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
270 failf(data, "Failed sending DICT request");
273 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
274 -1, NULL); /* no upload */
278 ppath = strchr(path, '/');
283 for(i = 0; ppath[i]; i++) {
287 result = Curl_sendf(sockfd, conn,
288 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
292 failf(data, "Failed sending DICT request");
296 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
302 #endif /*CURL_DISABLE_DICT*/