1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 * $Id: dict.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_DICT
28 /* -- WIN32 approved -- */
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.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>
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
82 /* The last #include file should be: */
87 * Forward declarations.
90 static CURLcode
dict_do(struct connectdata
*conn
, bool *done
);
93 * DICT protocol handler.
96 const struct Curl_handler Curl_handler_dict
= {
98 ZERO_NULL
, /* setup_connection */
100 ZERO_NULL
, /* done */
101 ZERO_NULL
, /* do_more */
102 ZERO_NULL
, /* connect_it */
103 ZERO_NULL
, /* connecting */
104 ZERO_NULL
, /* doing */
105 ZERO_NULL
, /* proto_getsock */
106 ZERO_NULL
, /* doing_getsock */
107 ZERO_NULL
, /* disconnect */
108 PORT_DICT
, /* defport */
109 PROT_DICT
/* protocol */
112 static char *unescape_word(struct SessionHandle
*data
, const char *inp
)
121 newp
= curl_easy_unescape(data
, inp
, 0, &len
);
125 dictp
= malloc(len
*2 + 1); /* add one for terminating zero */
127 /* According to RFC2229 section 2.2, these letters need to be escaped with
132 if((byte
<= 32) || (byte
== 127) ||
133 (byte
== '\'') || (byte
== '\"') || (byte
== '\\')) {
134 dictp
[olen
++] = '\\';
136 dictp
[olen
++] = byte
;
145 static CURLcode
dict_do(struct connectdata
*conn
, bool *done
)
150 char *database
= NULL
;
151 char *strategy
= NULL
;
152 char *nthdef
= NULL
; /* This is not part of the protocol, but required
154 CURLcode result
=CURLE_OK
;
155 struct SessionHandle
*data
=conn
->data
;
156 curl_socket_t sockfd
= conn
->sock
[FIRSTSOCKET
];
158 char *path
= data
->state
.path
;
159 curl_off_t
*bytecount
= &data
->req
.bytecount
;
161 *done
= TRUE
; /* unconditionally */
163 if(conn
->bits
.user_passwd
) {
164 /* AUTH is missing */
167 if(strnequal(path
, DICT_MATCH
, sizeof(DICT_MATCH
)-1) ||
168 strnequal(path
, DICT_MATCH2
, sizeof(DICT_MATCH2
)-1) ||
169 strnequal(path
, DICT_MATCH3
, sizeof(DICT_MATCH3
)-1)) {
171 word
= strchr(path
, ':');
174 database
= strchr(word
, ':');
176 *database
++ = (char)0;
177 strategy
= strchr(database
, ':');
179 *strategy
++ = (char)0;
180 nthdef
= strchr(strategy
, ':');
188 if((word
== NULL
) || (*word
== (char)0)) {
189 infof(data
, "lookup word is missing");
190 word
=(char *)"default";
192 if((database
== NULL
) || (*database
== (char)0)) {
193 database
= (char *)"!";
195 if((strategy
== NULL
) || (*strategy
== (char)0)) {
196 strategy
= (char *)".";
199 eword
= unescape_word(data
, word
);
201 return CURLE_OUT_OF_MEMORY
;
203 result
= Curl_sendf(sockfd
, conn
,
204 "CLIENT " LIBCURL_NAME
" " LIBCURL_VERSION
"\r\n"
219 failf(data
, "Failed sending DICT request");
221 result
= Curl_setup_transfer(conn
, FIRSTSOCKET
, -1, FALSE
, bytecount
,
222 -1, NULL
); /* no upload */
226 else if(strnequal(path
, DICT_DEFINE
, sizeof(DICT_DEFINE
)-1) ||
227 strnequal(path
, DICT_DEFINE2
, sizeof(DICT_DEFINE2
)-1) ||
228 strnequal(path
, DICT_DEFINE3
, sizeof(DICT_DEFINE3
)-1)) {
230 word
= strchr(path
, ':');
233 database
= strchr(word
, ':');
235 *database
++ = (char)0;
236 nthdef
= strchr(database
, ':');
243 if((word
== NULL
) || (*word
== (char)0)) {
244 infof(data
, "lookup word is missing");
245 word
=(char *)"default";
247 if((database
== NULL
) || (*database
== (char)0)) {
248 database
= (char *)"!";
251 eword
= unescape_word(data
, word
);
253 return CURLE_OUT_OF_MEMORY
;
255 result
= Curl_sendf(sockfd
, conn
,
256 "CLIENT " LIBCURL_NAME
" " LIBCURL_VERSION
"\r\n"
267 failf(data
, "Failed sending DICT request");
269 result
= Curl_setup_transfer(conn
, FIRSTSOCKET
, -1, FALSE
, bytecount
,
270 -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");
294 result
= Curl_setup_transfer(conn
, FIRSTSOCKET
, -1, FALSE
, bytecount
,
303 #endif /*CURL_DISABLE_DICT*/