1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 PORT_DICT, /* defport */
111 PROT_DICT /* protocol */
114 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
123 newp = curl_easy_unescape(data, inputbuff, 0, &len);
127 dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
129 /* According to RFC2229 section 2.2, these letters need to be escaped with
134 if((byte <= 32) || (byte == 127) ||
135 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
136 dictp[olen++] = '\\';
138 dictp[olen++] = byte;
147 static CURLcode dict_do(struct connectdata *conn, bool *done)
152 char *database = NULL;
153 char *strategy = NULL;
154 char *nthdef = NULL; /* This is not part of the protocol, but required
156 CURLcode result=CURLE_OK;
157 struct SessionHandle *data=conn->data;
158 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
160 char *path = data->state.path;
161 curl_off_t *bytecount = &data->req.bytecount;
163 *done = TRUE; /* unconditionally */
165 if(conn->bits.user_passwd) {
166 /* AUTH is missing */
169 if(Curl_raw_nequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
170 Curl_raw_nequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
171 Curl_raw_nequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
173 word = strchr(path, ':');
176 database = strchr(word, ':');
178 *database++ = (char)0;
179 strategy = strchr(database, ':');
181 *strategy++ = (char)0;
182 nthdef = strchr(strategy, ':');
190 if((word == NULL) || (*word == (char)0)) {
191 infof(data, "lookup word is missing");
192 word=(char *)"default";
194 if((database == NULL) || (*database == (char)0)) {
195 database = (char *)"!";
197 if((strategy == NULL) || (*strategy == (char)0)) {
198 strategy = (char *)".";
201 eword = unescape_word(data, word);
203 return CURLE_OUT_OF_MEMORY;
205 result = Curl_sendf(sockfd, conn,
206 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
221 failf(data, "Failed sending DICT request");
224 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
225 -1, NULL); /* no upload */
227 else if(Curl_raw_nequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
228 Curl_raw_nequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
229 Curl_raw_nequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
231 word = strchr(path, ':');
234 database = strchr(word, ':');
236 *database++ = (char)0;
237 nthdef = strchr(database, ':');
244 if((word == NULL) || (*word == (char)0)) {
245 infof(data, "lookup word is missing");
246 word=(char *)"default";
248 if((database == NULL) || (*database == (char)0)) {
249 database = (char *)"!";
252 eword = unescape_word(data, word);
254 return CURLE_OUT_OF_MEMORY;
256 result = Curl_sendf(sockfd, conn,
257 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
268 failf(data, "Failed sending DICT request");
271 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
272 -1, NULL); /* no upload */
276 ppath = strchr(path, '/');
281 for (i = 0; ppath[i]; i++) {
285 result = Curl_sendf(sockfd, conn,
286 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
290 failf(data, "Failed sending DICT request");
294 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
300 #endif /*CURL_DISABLE_DICT*/