dict.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2005, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_DICT
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #ifdef HAVE_SYS_TYPES_H
  32. #include <sys/types.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #include <errno.h>
  38. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  39. #include <time.h>
  40. #include <io.h>
  41. #else
  42. #ifdef HAVE_SYS_SOCKET_H
  43. #include <sys/socket.h>
  44. #endif
  45. #include <netinet/in.h>
  46. #include <sys/time.h>
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50. #include <netdb.h>
  51. #ifdef HAVE_ARPA_INET_H
  52. #include <arpa/inet.h>
  53. #endif
  54. #ifdef HAVE_NET_IF_H
  55. #include <net/if.h>
  56. #endif
  57. #include <sys/ioctl.h>
  58. #include <signal.h>
  59. #ifdef HAVE_SYS_PARAM_H
  60. #include <sys/param.h>
  61. #endif
  62. #ifdef HAVE_SYS_SELECT_H
  63. #include <sys/select.h>
  64. #endif
  65. #endif
  66. #include "urldata.h"
  67. #include <curl/curl.h>
  68. #include "transfer.h"
  69. #include "sendf.h"
  70. #include "progress.h"
  71. #include "strequal.h"
  72. #include "dict.h"
  73. #define _MPRINTF_REPLACE /* use our functions only */
  74. #include <curl/mprintf.h>
  75. CURLcode Curl_dict(struct connectdata *conn, bool *done)
  76. {
  77. char *word;
  78. char *ppath;
  79. char *database = NULL;
  80. char *strategy = NULL;
  81. char *nthdef = NULL; /* This is not part of the protocol, but required
  82. by RFC 2229 */
  83. CURLcode result=CURLE_OK;
  84. struct SessionHandle *data=conn->data;
  85. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  86. char *path = conn->path;
  87. curl_off_t *bytecount = &conn->bytecount;
  88. *done = TRUE; /* unconditionally */
  89. if(conn->bits.user_passwd) {
  90. /* AUTH is missing */
  91. }
  92. if (strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
  93. strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
  94. strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
  95. word = strchr(path, ':');
  96. if (word) {
  97. word++;
  98. database = strchr(word, ':');
  99. if (database) {
  100. *database++ = (char)0;
  101. strategy = strchr(database, ':');
  102. if (strategy) {
  103. *strategy++ = (char)0;
  104. nthdef = strchr(strategy, ':');
  105. if (nthdef) {
  106. *nthdef++ = (char)0;
  107. }
  108. }
  109. }
  110. }
  111. if ((word == NULL) || (*word == (char)0)) {
  112. failf(data, "lookup word is missing");
  113. }
  114. if ((database == NULL) || (*database == (char)0)) {
  115. database = (char *)"!";
  116. }
  117. if ((strategy == NULL) || (*strategy == (char)0)) {
  118. strategy = (char *)".";
  119. }
  120. result = Curl_sendf(sockfd, conn,
  121. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  122. "MATCH "
  123. "%s " /* database */
  124. "%s " /* strategy */
  125. "%s\r\n" /* word */
  126. "QUIT\r\n",
  127. database,
  128. strategy,
  129. word
  130. );
  131. if(result)
  132. failf(data, "Failed sending DICT request");
  133. else
  134. result = Curl_Transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  135. -1, NULL); /* no upload */
  136. if(result)
  137. return result;
  138. }
  139. else if (strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
  140. strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
  141. strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
  142. word = strchr(path, ':');
  143. if (word) {
  144. word++;
  145. database = strchr(word, ':');
  146. if (database) {
  147. *database++ = (char)0;
  148. nthdef = strchr(database, ':');
  149. if (nthdef) {
  150. *nthdef++ = (char)0;
  151. }
  152. }
  153. }
  154. if ((word == NULL) || (*word == (char)0)) {
  155. failf(data, "lookup word is missing");
  156. }
  157. if ((database == NULL) || (*database == (char)0)) {
  158. database = (char *)"!";
  159. }
  160. result = Curl_sendf(sockfd, conn,
  161. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  162. "DEFINE "
  163. "%s " /* database */
  164. "%s\r\n" /* word */
  165. "QUIT\r\n",
  166. database,
  167. word);
  168. if(result)
  169. failf(data, "Failed sending DICT request");
  170. else
  171. result = Curl_Transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  172. -1, NULL); /* no upload */
  173. if(result)
  174. return result;
  175. }
  176. else {
  177. ppath = strchr(path, '/');
  178. if (ppath) {
  179. int i;
  180. ppath++;
  181. for (i = 0; ppath[i]; i++) {
  182. if (ppath[i] == ':')
  183. ppath[i] = ' ';
  184. }
  185. result = Curl_sendf(sockfd, conn,
  186. "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
  187. "%s\r\n"
  188. "QUIT\r\n", ppath);
  189. if(result)
  190. failf(data, "Failed sending DICT request");
  191. else
  192. result = Curl_Transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
  193. -1, NULL);
  194. if(result)
  195. return result;
  196. }
  197. }
  198. return CURLE_OK;
  199. }
  200. #endif /*CURL_DISABLE_DICT*/