dict.c 5.9 KB

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