Update tag value for tizen 2.0 build
[external/curl.git] / lib / dict.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifndef CURL_DISABLE_DICT
26
27 /* -- WIN32 approved -- */
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #ifdef WIN32
35 #include <time.h>
36 #include <io.h>
37 #else
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <netdb.h>
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
51 #endif
52 #ifdef HAVE_NET_IF_H
53 #include <net/if.h>
54 #endif
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
58
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
62
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
66
67
68 #endif
69
70 #include "urldata.h"
71 #include <curl/curl.h>
72 #include "transfer.h"
73 #include "sendf.h"
74
75 #include "progress.h"
76 #include "strequal.h"
77 #include "dict.h"
78 #include "rawstr.h"
79
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
82
83 /* The last #include file should be: */
84 #include "memdebug.h"
85
86
87 /*
88  * Forward declarations.
89  */
90
91 static CURLcode dict_do(struct connectdata *conn, bool *done);
92
93 /*
94  * DICT protocol handler.
95  */
96
97 const struct Curl_handler Curl_handler_dict = {
98   "DICT",                               /* scheme */
99   ZERO_NULL,                            /* setup_connection */
100   dict_do,                              /* do_it */
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 */
112 };
113
114 static char *unescape_word(struct SessionHandle *data, const char *inputbuff)
115 {
116   char *newp;
117   char *dictp;
118   char *ptr;
119   int len;
120   char byte;
121   int olen=0;
122
123   newp = curl_easy_unescape(data, inputbuff, 0, &len);
124   if(!newp)
125     return NULL;
126
127   dictp = malloc(((size_t)len)*2 + 1); /* add one for terminating zero */
128   if(dictp) {
129     /* According to RFC2229 section 2.2, these letters need to be escaped with
130        \[letter] */
131     for(ptr = newp;
132         (byte = *ptr) != 0;
133         ptr++) {
134       if((byte <= 32) || (byte == 127) ||
135           (byte == '\'') || (byte == '\"') || (byte == '\\')) {
136         dictp[olen++] = '\\';
137       }
138       dictp[olen++] = byte;
139     }
140     dictp[olen]=0;
141
142     free(newp);
143   }
144   return dictp;
145 }
146
147 static CURLcode dict_do(struct connectdata *conn, bool *done)
148 {
149   char *word;
150   char *eword;
151   char *ppath;
152   char *database = NULL;
153   char *strategy = NULL;
154   char *nthdef = NULL; /* This is not part of the protocol, but required
155                           by RFC 2229 */
156   CURLcode result=CURLE_OK;
157   struct SessionHandle *data=conn->data;
158   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
159
160   char *path = data->state.path;
161   curl_off_t *bytecount = &data->req.bytecount;
162
163   *done = TRUE; /* unconditionally */
164
165   if(conn->bits.user_passwd) {
166     /* AUTH is missing */
167   }
168
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)) {
172
173     word = strchr(path, ':');
174     if(word) {
175       word++;
176       database = strchr(word, ':');
177       if(database) {
178         *database++ = (char)0;
179         strategy = strchr(database, ':');
180         if(strategy) {
181           *strategy++ = (char)0;
182           nthdef = strchr(strategy, ':');
183           if(nthdef) {
184             *nthdef = (char)0;
185           }
186         }
187       }
188     }
189
190     if((word == NULL) || (*word == (char)0)) {
191       infof(data, "lookup word is missing");
192       word=(char *)"default";
193     }
194     if((database == NULL) || (*database == (char)0)) {
195       database = (char *)"!";
196     }
197     if((strategy == NULL) || (*strategy == (char)0)) {
198       strategy = (char *)".";
199     }
200
201     eword = unescape_word(data, word);
202     if(!eword)
203       return CURLE_OUT_OF_MEMORY;
204
205     result = Curl_sendf(sockfd, conn,
206                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
207                         "MATCH "
208                         "%s "    /* database */
209                         "%s "    /* strategy */
210                         "%s\r\n" /* word */
211                         "QUIT\r\n",
212
213                         database,
214                         strategy,
215                         eword
216                         );
217
218     free(eword);
219
220     if(result) {
221       failf(data, "Failed sending DICT request");
222       return result;
223     }
224     Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
225                         -1, NULL); /* no upload */
226   }
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)) {
230
231     word = strchr(path, ':');
232     if(word) {
233       word++;
234       database = strchr(word, ':');
235       if(database) {
236         *database++ = (char)0;
237         nthdef = strchr(database, ':');
238         if(nthdef) {
239           *nthdef = (char)0;
240         }
241       }
242     }
243
244     if((word == NULL) || (*word == (char)0)) {
245       infof(data, "lookup word is missing");
246       word=(char *)"default";
247     }
248     if((database == NULL) || (*database == (char)0)) {
249       database = (char *)"!";
250     }
251
252     eword = unescape_word(data, word);
253     if(!eword)
254       return CURLE_OUT_OF_MEMORY;
255
256     result = Curl_sendf(sockfd, conn,
257                         "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
258                         "DEFINE "
259                         "%s "     /* database */
260                         "%s\r\n"  /* word */
261                         "QUIT\r\n",
262                         database,
263                         eword);
264
265     free(eword);
266
267     if(result) {
268       failf(data, "Failed sending DICT request");
269       return result;
270     }
271     Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
272                         -1, NULL); /* no upload */
273   }
274   else {
275
276     ppath = strchr(path, '/');
277     if(ppath) {
278       int i;
279
280       ppath++;
281       for (i = 0; ppath[i]; i++) {
282         if(ppath[i] == ':')
283           ppath[i] = ' ';
284       }
285       result = Curl_sendf(sockfd, conn,
286                           "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
287                           "%s\r\n"
288                           "QUIT\r\n", ppath);
289       if(result) {
290         failf(data, "Failed sending DICT request");
291         return result;
292       }
293
294       Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount, -1, NULL);
295     }
296   }
297
298   return CURLE_OK;
299 }
300 #endif /*CURL_DISABLE_DICT*/