sendfile.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "fspr_network_io.h"
  22. #include "fspr_errno.h"
  23. #include "fspr_general.h"
  24. #include "fspr_poll.h"
  25. #if !APR_HAS_SENDFILE
  26. int main(void)
  27. {
  28. fprintf(stderr,
  29. "This program won't work on this platform because there is no "
  30. "support for sendfile().\n");
  31. return 0;
  32. }
  33. #else /* !APR_HAS_SENDFILE */
  34. #define FILE_LENGTH 200000
  35. #define FILE_DATA_CHAR '0'
  36. #define HDR1 "1234567890ABCD\n"
  37. #define HDR2 "EFGH\n"
  38. #define HDR3_LEN 80000
  39. #define HDR3_CHAR '^'
  40. #define TRL1 "IJKLMNOPQRSTUVWXYZ\n"
  41. #define TRL2 "!@#$%&*()\n"
  42. #define TRL3_LEN 90000
  43. #define TRL3_CHAR '@'
  44. #define TESTSF_PORT 8021
  45. #define TESTFILE "testsf.dat"
  46. typedef enum {BLK, NONBLK, TIMEOUT} client_socket_mode_t;
  47. static void fspr_setup(fspr_pool_t **p, fspr_socket_t **sock, int *family)
  48. {
  49. char buf[120];
  50. fspr_status_t rv;
  51. rv = fspr_initialize();
  52. if (rv != APR_SUCCESS) {
  53. fprintf(stderr, "fspr_initialize()->%d/%s\n",
  54. rv,
  55. fspr_strerror(rv, buf, sizeof buf));
  56. exit(1);
  57. }
  58. atexit(fspr_terminate);
  59. rv = fspr_pool_create(p, NULL);
  60. if (rv != APR_SUCCESS) {
  61. fprintf(stderr, "fspr_pool_create()->%d/%s\n",
  62. rv,
  63. fspr_strerror(rv, buf, sizeof buf));
  64. exit(1);
  65. }
  66. *sock = NULL;
  67. rv = fspr_socket_create(sock, *family, SOCK_STREAM, 0, *p);
  68. if (rv != APR_SUCCESS) {
  69. fprintf(stderr, "fspr_socket_create()->%d/%s\n",
  70. rv,
  71. fspr_strerror(rv, buf, sizeof buf));
  72. exit(1);
  73. }
  74. if (*family == APR_UNSPEC) {
  75. fspr_sockaddr_t *localsa;
  76. rv = fspr_socket_addr_get(&localsa, APR_LOCAL, *sock);
  77. if (rv != APR_SUCCESS) {
  78. fprintf(stderr, "fspr_socket_addr_get()->%d/%s\n",
  79. rv,
  80. fspr_strerror(rv, buf, sizeof buf));
  81. exit(1);
  82. }
  83. *family = localsa->family;
  84. }
  85. }
  86. static void create_testfile(fspr_pool_t *p, const char *fname)
  87. {
  88. fspr_file_t *f = NULL;
  89. fspr_status_t rv;
  90. char buf[120];
  91. int i;
  92. fspr_finfo_t finfo;
  93. printf("Creating a test file...\n");
  94. rv = fspr_file_open(&f, fname,
  95. APR_CREATE | APR_WRITE | APR_TRUNCATE | APR_BUFFERED,
  96. APR_UREAD | APR_UWRITE, p);
  97. if (rv) {
  98. fprintf(stderr, "fspr_file_open()->%d/%s\n",
  99. rv, fspr_strerror(rv, buf, sizeof buf));
  100. exit(1);
  101. }
  102. buf[0] = FILE_DATA_CHAR;
  103. buf[1] = '\0';
  104. for (i = 0; i < FILE_LENGTH; i++) {
  105. /* exercise fspr_file_putc() and fspr_file_puts() on buffered files */
  106. if ((i % 2) == 0) {
  107. rv = fspr_file_putc(buf[0], f);
  108. if (rv) {
  109. fprintf(stderr, "fspr_file_putc()->%d/%s\n",
  110. rv, fspr_strerror(rv, buf, sizeof buf));
  111. exit(1);
  112. }
  113. }
  114. else {
  115. rv = fspr_file_puts(buf, f);
  116. if (rv) {
  117. fprintf(stderr, "fspr_file_puts()->%d/%s\n",
  118. rv, fspr_strerror(rv, buf, sizeof buf));
  119. exit(1);
  120. }
  121. }
  122. }
  123. rv = fspr_file_close(f);
  124. if (rv) {
  125. fprintf(stderr, "fspr_file_close()->%d/%s\n",
  126. rv, fspr_strerror(rv, buf, sizeof buf));
  127. exit(1);
  128. }
  129. rv = fspr_stat(&finfo, fname, APR_FINFO_NORM, p);
  130. if (rv != APR_SUCCESS && rv != APR_INCOMPLETE) {
  131. fprintf(stderr, "fspr_stat()->%d/%s\n",
  132. rv, fspr_strerror(rv, buf, sizeof buf));
  133. exit(1);
  134. }
  135. if (finfo.size != FILE_LENGTH) {
  136. fprintf(stderr,
  137. "test file %s should be %ld-bytes long\n"
  138. "instead it is %ld-bytes long\n",
  139. fname,
  140. (long int)FILE_LENGTH,
  141. (long int)finfo.size);
  142. exit(1);
  143. }
  144. }
  145. static int client(client_socket_mode_t socket_mode, char *host)
  146. {
  147. fspr_status_t rv, tmprv;
  148. fspr_socket_t *sock;
  149. fspr_pool_t *p;
  150. char buf[120];
  151. fspr_file_t *f = NULL;
  152. fspr_size_t len;
  153. fspr_size_t expected_len;
  154. fspr_off_t current_file_offset;
  155. fspr_hdtr_t hdtr;
  156. struct iovec headers[3];
  157. struct iovec trailers[3];
  158. fspr_size_t bytes_read;
  159. fspr_pollset_t *pset;
  160. fspr_int32_t nsocks;
  161. int i;
  162. int family;
  163. fspr_sockaddr_t *destsa;
  164. family = APR_INET;
  165. fspr_setup(&p, &sock, &family);
  166. create_testfile(p, TESTFILE);
  167. rv = fspr_file_open(&f, TESTFILE, APR_READ, 0, p);
  168. if (rv != APR_SUCCESS) {
  169. fprintf(stderr, "fspr_file_open()->%d/%s\n",
  170. rv,
  171. fspr_strerror(rv, buf, sizeof buf));
  172. exit(1);
  173. }
  174. if (!host) {
  175. host = "127.0.0.1";
  176. }
  177. rv = fspr_sockaddr_info_get(&destsa, host, family, TESTSF_PORT, 0, p);
  178. if (rv != APR_SUCCESS) {
  179. fprintf(stderr, "fspr_sockaddr_info_get()->%d/%s\n",
  180. rv,
  181. fspr_strerror(rv, buf, sizeof buf));
  182. exit(1);
  183. }
  184. rv = fspr_socket_connect(sock, destsa);
  185. if (rv != APR_SUCCESS) {
  186. fprintf(stderr, "fspr_socket_connect()->%d/%s\n",
  187. rv,
  188. fspr_strerror(rv, buf, sizeof buf));
  189. exit(1);
  190. }
  191. switch(socket_mode) {
  192. case BLK:
  193. /* leave it blocking */
  194. break;
  195. case NONBLK:
  196. /* set it non-blocking */
  197. rv = fspr_socket_opt_set(sock, APR_SO_NONBLOCK, 1);
  198. if (rv != APR_SUCCESS) {
  199. fprintf(stderr, "fspr_socket_opt_set(APR_SO_NONBLOCK)->%d/%s\n",
  200. rv,
  201. fspr_strerror(rv, buf, sizeof buf));
  202. exit(1);
  203. }
  204. break;
  205. case TIMEOUT:
  206. /* set a timeout */
  207. rv = fspr_socket_timeout_set(sock, 100 * APR_USEC_PER_SEC);
  208. if (rv != APR_SUCCESS) {
  209. fprintf(stderr, "fspr_socket_opt_set(APR_SO_NONBLOCK)->%d/%s\n",
  210. rv,
  211. fspr_strerror(rv, buf, sizeof buf));
  212. exit(1);
  213. }
  214. break;
  215. default:
  216. assert(1 != 1);
  217. }
  218. printf("Sending the file...\n");
  219. hdtr.headers = headers;
  220. hdtr.numheaders = 3;
  221. hdtr.headers[0].iov_base = HDR1;
  222. hdtr.headers[0].iov_len = strlen(hdtr.headers[0].iov_base);
  223. hdtr.headers[1].iov_base = HDR2;
  224. hdtr.headers[1].iov_len = strlen(hdtr.headers[1].iov_base);
  225. hdtr.headers[2].iov_base = malloc(HDR3_LEN);
  226. assert(hdtr.headers[2].iov_base);
  227. memset(hdtr.headers[2].iov_base, HDR3_CHAR, HDR3_LEN);
  228. hdtr.headers[2].iov_len = HDR3_LEN;
  229. hdtr.trailers = trailers;
  230. hdtr.numtrailers = 3;
  231. hdtr.trailers[0].iov_base = TRL1;
  232. hdtr.trailers[0].iov_len = strlen(hdtr.trailers[0].iov_base);
  233. hdtr.trailers[1].iov_base = TRL2;
  234. hdtr.trailers[1].iov_len = strlen(hdtr.trailers[1].iov_base);
  235. hdtr.trailers[2].iov_base = malloc(TRL3_LEN);
  236. memset(hdtr.trailers[2].iov_base, TRL3_CHAR, TRL3_LEN);
  237. assert(hdtr.trailers[2].iov_base);
  238. hdtr.trailers[2].iov_len = TRL3_LEN;
  239. expected_len =
  240. strlen(HDR1) + strlen(HDR2) + HDR3_LEN +
  241. strlen(TRL1) + strlen(TRL2) + TRL3_LEN +
  242. FILE_LENGTH;
  243. if (socket_mode == BLK) {
  244. current_file_offset = 0;
  245. len = FILE_LENGTH;
  246. rv = fspr_socket_sendfile(sock, f, &hdtr, &current_file_offset, &len, 0);
  247. if (rv != APR_SUCCESS) {
  248. fprintf(stderr, "fspr_socket_sendfile()->%d/%s\n",
  249. rv,
  250. fspr_strerror(rv, buf, sizeof buf));
  251. exit(1);
  252. }
  253. printf("fspr_socket_sendfile() updated offset with %ld\n",
  254. (long int)current_file_offset);
  255. printf("fspr_socket_sendfile() updated len with %ld\n",
  256. (long int)len);
  257. printf("bytes really sent: %" APR_SIZE_T_FMT "\n",
  258. expected_len);
  259. if (len != expected_len) {
  260. fprintf(stderr, "fspr_socket_sendfile() didn't report the correct "
  261. "number of bytes sent!\n");
  262. exit(1);
  263. }
  264. }
  265. else {
  266. /* non-blocking... wooooooo */
  267. fspr_size_t total_bytes_sent;
  268. fspr_pollfd_t pfd;
  269. pset = NULL;
  270. rv = fspr_pollset_create(&pset, 1, p, 0);
  271. assert(!rv);
  272. pfd.p = p;
  273. pfd.desc_type = APR_POLL_SOCKET;
  274. pfd.reqevents = APR_POLLOUT;
  275. pfd.rtnevents = 0;
  276. pfd.desc.s = sock;
  277. pfd.client_data = NULL;
  278. rv = fspr_pollset_add(pset, &pfd);
  279. assert(!rv);
  280. total_bytes_sent = 0;
  281. current_file_offset = 0;
  282. len = FILE_LENGTH;
  283. do {
  284. fspr_size_t tmplen;
  285. tmplen = len; /* bytes remaining to send from the file */
  286. printf("Calling fspr_socket_sendfile()...\n");
  287. printf("Headers (%d):\n", hdtr.numheaders);
  288. for (i = 0; i < hdtr.numheaders; i++) {
  289. printf("\t%ld bytes (%c)\n",
  290. (long)hdtr.headers[i].iov_len,
  291. *(char *)hdtr.headers[i].iov_base);
  292. }
  293. printf("File: %ld bytes from offset %ld\n",
  294. (long)tmplen, (long)current_file_offset);
  295. printf("Trailers (%d):\n", hdtr.numtrailers);
  296. for (i = 0; i < hdtr.numtrailers; i++) {
  297. printf("\t%ld bytes\n",
  298. (long)hdtr.trailers[i].iov_len);
  299. }
  300. rv = fspr_socket_sendfile(sock, f, &hdtr, &current_file_offset, &tmplen, 0);
  301. printf("fspr_socket_sendfile()->%d, sent %ld bytes\n", rv, (long)tmplen);
  302. if (rv) {
  303. if (APR_STATUS_IS_EAGAIN(rv)) {
  304. assert(tmplen == 0);
  305. nsocks = 1;
  306. tmprv = fspr_pollset_poll(pset, -1, &nsocks, NULL);
  307. assert(!tmprv);
  308. assert(nsocks == 1);
  309. /* continue; */
  310. }
  311. }
  312. total_bytes_sent += tmplen;
  313. /* Adjust hdtr to compensate for partially-written
  314. * data.
  315. */
  316. /* First, skip over any header data which might have
  317. * been written.
  318. */
  319. while (tmplen && hdtr.numheaders) {
  320. if (tmplen >= hdtr.headers[0].iov_len) {
  321. tmplen -= hdtr.headers[0].iov_len;
  322. --hdtr.numheaders;
  323. ++hdtr.headers;
  324. }
  325. else {
  326. hdtr.headers[0].iov_len -= tmplen;
  327. hdtr.headers[0].iov_base =
  328. (char*) hdtr.headers[0].iov_base + tmplen;
  329. tmplen = 0;
  330. }
  331. }
  332. /* Now, skip over any file data which might have been
  333. * written.
  334. */
  335. if (tmplen <= len) {
  336. current_file_offset += tmplen;
  337. len -= tmplen;
  338. tmplen = 0;
  339. }
  340. else {
  341. tmplen -= len;
  342. len = 0;
  343. current_file_offset = 0;
  344. }
  345. /* Last, skip over any trailer data which might have
  346. * been written.
  347. */
  348. while (tmplen && hdtr.numtrailers) {
  349. if (tmplen >= hdtr.trailers[0].iov_len) {
  350. tmplen -= hdtr.trailers[0].iov_len;
  351. --hdtr.numtrailers;
  352. ++hdtr.trailers;
  353. }
  354. else {
  355. hdtr.trailers[0].iov_len -= tmplen;
  356. hdtr.trailers[0].iov_base =
  357. (char *)hdtr.trailers[0].iov_base + tmplen;
  358. tmplen = 0;
  359. }
  360. }
  361. } while (total_bytes_sent < expected_len &&
  362. (rv == APR_SUCCESS ||
  363. (APR_STATUS_IS_EAGAIN(rv) && socket_mode != TIMEOUT)));
  364. if (total_bytes_sent != expected_len) {
  365. fprintf(stderr,
  366. "client problem: sent %ld of %ld bytes\n",
  367. (long)total_bytes_sent, (long)expected_len);
  368. exit(1);
  369. }
  370. if (rv) {
  371. fprintf(stderr,
  372. "client problem: rv %d\n",
  373. rv);
  374. exit(1);
  375. }
  376. }
  377. current_file_offset = 0;
  378. rv = fspr_file_seek(f, APR_CUR, &current_file_offset);
  379. if (rv != APR_SUCCESS) {
  380. fprintf(stderr, "fspr_file_seek()->%d/%s\n",
  381. rv,
  382. fspr_strerror(rv, buf, sizeof buf));
  383. exit(1);
  384. }
  385. printf("After fspr_socket_sendfile(), the kernel file pointer is "
  386. "at offset %ld.\n",
  387. (long int)current_file_offset);
  388. rv = fspr_socket_shutdown(sock, APR_SHUTDOWN_WRITE);
  389. if (rv != APR_SUCCESS) {
  390. fprintf(stderr, "fspr_socket_shutdown()->%d/%s\n",
  391. rv,
  392. fspr_strerror(rv, buf, sizeof buf));
  393. exit(1);
  394. }
  395. /* in case this is the non-blocking test, set socket timeout;
  396. * we're just waiting for EOF */
  397. rv = fspr_socket_timeout_set(sock, fspr_time_from_sec(3));
  398. if (rv != APR_SUCCESS) {
  399. fprintf(stderr, "fspr_socket_timeout_set()->%d/%s\n",
  400. rv,
  401. fspr_strerror(rv, buf, sizeof buf));
  402. exit(1);
  403. }
  404. bytes_read = 1;
  405. rv = fspr_socket_recv(sock, buf, &bytes_read);
  406. if (rv != APR_EOF) {
  407. fprintf(stderr, "fspr_socket_recv()->%d/%s (expected APR_EOF)\n",
  408. rv,
  409. fspr_strerror(rv, buf, sizeof buf));
  410. exit(1);
  411. }
  412. if (bytes_read != 0) {
  413. fprintf(stderr, "We expected to get 0 bytes read with APR_EOF\n"
  414. "but instead we read %ld bytes.\n",
  415. (long int)bytes_read);
  416. exit(1);
  417. }
  418. printf("client: fspr_socket_sendfile() worked as expected!\n");
  419. rv = fspr_file_remove(TESTFILE, p);
  420. if (rv != APR_SUCCESS) {
  421. fprintf(stderr, "fspr_file_remove()->%d/%s\n",
  422. rv,
  423. fspr_strerror(rv, buf, sizeof buf));
  424. exit(1);
  425. }
  426. return 0;
  427. }
  428. static int server(void)
  429. {
  430. fspr_status_t rv;
  431. fspr_socket_t *sock;
  432. fspr_pool_t *p;
  433. char buf[120];
  434. int i;
  435. fspr_socket_t *newsock = NULL;
  436. fspr_size_t bytes_read;
  437. fspr_sockaddr_t *localsa;
  438. int family;
  439. family = APR_UNSPEC;
  440. fspr_setup(&p, &sock, &family);
  441. rv = fspr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
  442. if (rv != APR_SUCCESS) {
  443. fprintf(stderr, "fspr_socket_opt_set()->%d/%s\n",
  444. rv,
  445. fspr_strerror(rv, buf, sizeof buf));
  446. exit(1);
  447. }
  448. rv = fspr_sockaddr_info_get(&localsa, NULL, family, TESTSF_PORT, 0, p);
  449. if (rv != APR_SUCCESS) {
  450. fprintf(stderr, "fspr_sockaddr_info_get()->%d/%s\n",
  451. rv,
  452. fspr_strerror(rv, buf, sizeof buf));
  453. exit(1);
  454. }
  455. rv = fspr_socket_bind(sock, localsa);
  456. if (rv != APR_SUCCESS) {
  457. fprintf(stderr, "fspr_socket_bind()->%d/%s\n",
  458. rv,
  459. fspr_strerror(rv, buf, sizeof buf));
  460. exit(1);
  461. }
  462. rv = fspr_socket_listen(sock, 5);
  463. if (rv != APR_SUCCESS) {
  464. fprintf(stderr, "fspr_socket_listen()->%d/%s\n",
  465. rv,
  466. fspr_strerror(rv, buf, sizeof buf));
  467. exit(1);
  468. }
  469. printf("Waiting for a client to connect...\n");
  470. rv = fspr_socket_accept(&newsock, sock, p);
  471. if (rv != APR_SUCCESS) {
  472. fprintf(stderr, "fspr_socket_accept()->%d/%s\n",
  473. rv,
  474. fspr_strerror(rv, buf, sizeof buf));
  475. exit(1);
  476. }
  477. printf("Processing a client...\n");
  478. assert(sizeof buf > strlen(HDR1));
  479. bytes_read = strlen(HDR1);
  480. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  481. if (rv != APR_SUCCESS) {
  482. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  483. rv,
  484. fspr_strerror(rv, buf, sizeof buf));
  485. exit(1);
  486. }
  487. if (bytes_read != strlen(HDR1)) {
  488. fprintf(stderr, "wrong data read (1)\n");
  489. exit(1);
  490. }
  491. if (memcmp(buf, HDR1, strlen(HDR1))) {
  492. fprintf(stderr, "wrong data read (2)\n");
  493. fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",
  494. (int)bytes_read, buf, HDR1);
  495. exit(1);
  496. }
  497. assert(sizeof buf > strlen(HDR2));
  498. bytes_read = strlen(HDR2);
  499. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  500. if (rv != APR_SUCCESS) {
  501. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  502. rv,
  503. fspr_strerror(rv, buf, sizeof buf));
  504. exit(1);
  505. }
  506. if (bytes_read != strlen(HDR2)) {
  507. fprintf(stderr, "wrong data read (3)\n");
  508. exit(1);
  509. }
  510. if (memcmp(buf, HDR2, strlen(HDR2))) {
  511. fprintf(stderr, "wrong data read (4)\n");
  512. fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",
  513. (int)bytes_read, buf, HDR2);
  514. exit(1);
  515. }
  516. for (i = 0; i < HDR3_LEN; i++) {
  517. bytes_read = 1;
  518. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  519. if (rv != APR_SUCCESS) {
  520. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  521. rv,
  522. fspr_strerror(rv, buf, sizeof buf));
  523. exit(1);
  524. }
  525. if (bytes_read != 1) {
  526. fprintf(stderr, "fspr_socket_recv()->%ld bytes instead of 1\n",
  527. (long int)bytes_read);
  528. exit(1);
  529. }
  530. if (buf[0] != HDR3_CHAR) {
  531. fprintf(stderr,
  532. "problem with data read (byte %d of hdr 3):\n",
  533. i);
  534. fprintf(stderr, "read `%c' (0x%x) from client; expected "
  535. "`%c'\n",
  536. buf[0], buf[0], HDR3_CHAR);
  537. exit(1);
  538. }
  539. }
  540. for (i = 0; i < FILE_LENGTH; i++) {
  541. bytes_read = 1;
  542. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  543. if (rv != APR_SUCCESS) {
  544. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  545. rv,
  546. fspr_strerror(rv, buf, sizeof buf));
  547. exit(1);
  548. }
  549. if (bytes_read != 1) {
  550. fprintf(stderr, "fspr_socket_recv()->%ld bytes instead of 1\n",
  551. (long int)bytes_read);
  552. exit(1);
  553. }
  554. if (buf[0] != FILE_DATA_CHAR) {
  555. fprintf(stderr,
  556. "problem with data read (byte %d of file):\n",
  557. i);
  558. fprintf(stderr, "read `%c' (0x%x) from client; expected "
  559. "`%c'\n",
  560. buf[0], buf[0], FILE_DATA_CHAR);
  561. exit(1);
  562. }
  563. }
  564. assert(sizeof buf > strlen(TRL1));
  565. bytes_read = strlen(TRL1);
  566. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  567. if (rv != APR_SUCCESS) {
  568. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  569. rv,
  570. fspr_strerror(rv, buf, sizeof buf));
  571. exit(1);
  572. }
  573. if (bytes_read != strlen(TRL1)) {
  574. fprintf(stderr, "wrong data read (5)\n");
  575. exit(1);
  576. }
  577. if (memcmp(buf, TRL1, strlen(TRL1))) {
  578. fprintf(stderr, "wrong data read (6)\n");
  579. fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",
  580. (int)bytes_read, buf, TRL1);
  581. exit(1);
  582. }
  583. assert(sizeof buf > strlen(TRL2));
  584. bytes_read = strlen(TRL2);
  585. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  586. if (rv != APR_SUCCESS) {
  587. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  588. rv,
  589. fspr_strerror(rv, buf, sizeof buf));
  590. exit(1);
  591. }
  592. if (bytes_read != strlen(TRL2)) {
  593. fprintf(stderr, "wrong data read (7)\n");
  594. exit(1);
  595. }
  596. if (memcmp(buf, TRL2, strlen(TRL2))) {
  597. fprintf(stderr, "wrong data read (8)\n");
  598. fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",
  599. (int)bytes_read, buf, TRL2);
  600. exit(1);
  601. }
  602. for (i = 0; i < TRL3_LEN; i++) {
  603. bytes_read = 1;
  604. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  605. if (rv != APR_SUCCESS) {
  606. fprintf(stderr, "fspr_socket_recv()->%d/%s\n",
  607. rv,
  608. fspr_strerror(rv, buf, sizeof buf));
  609. exit(1);
  610. }
  611. if (bytes_read != 1) {
  612. fprintf(stderr, "fspr_socket_recv()->%ld bytes instead of 1\n",
  613. (long int)bytes_read);
  614. exit(1);
  615. }
  616. if (buf[0] != TRL3_CHAR) {
  617. fprintf(stderr,
  618. "problem with data read (byte %d of trl 3):\n",
  619. i);
  620. fprintf(stderr, "read `%c' (0x%x) from client; expected "
  621. "`%c'\n",
  622. buf[0], buf[0], TRL3_CHAR);
  623. exit(1);
  624. }
  625. }
  626. bytes_read = 1;
  627. rv = fspr_socket_recv(newsock, buf, &bytes_read);
  628. if (rv != APR_EOF) {
  629. fprintf(stderr, "fspr_socket_recv()->%d/%s (expected APR_EOF)\n",
  630. rv,
  631. fspr_strerror(rv, buf, sizeof buf));
  632. exit(1);
  633. }
  634. if (bytes_read != 0) {
  635. fprintf(stderr, "We expected to get 0 bytes read with APR_EOF\n"
  636. "but instead we read %ld bytes (%c).\n",
  637. (long int)bytes_read, buf[0]);
  638. exit(1);
  639. }
  640. printf("server: fspr_socket_sendfile() worked as expected!\n");
  641. return 0;
  642. }
  643. int main(int argc, char *argv[])
  644. {
  645. #ifdef SIGPIPE
  646. signal(SIGPIPE, SIG_IGN);
  647. #endif
  648. /* Gee whiz this is goofy logic but I wanna drive sendfile right now,
  649. * not dork around with the command line!
  650. */
  651. if (argc >= 3 && !strcmp(argv[1], "client")) {
  652. char *host = 0;
  653. if (argv[3]) {
  654. host = argv[3];
  655. }
  656. if (!strcmp(argv[2], "blocking")) {
  657. return client(BLK, host);
  658. }
  659. else if (!strcmp(argv[2], "timeout")) {
  660. return client(TIMEOUT, host);
  661. }
  662. else if (!strcmp(argv[2], "nonblocking")) {
  663. return client(NONBLK, host);
  664. }
  665. }
  666. else if (argc == 2 && !strcmp(argv[1], "server")) {
  667. return server();
  668. }
  669. fprintf(stderr,
  670. "Usage: %s client {blocking|nonblocking|timeout}\n"
  671. " %s server\n",
  672. argv[0], argv[0]);
  673. return -1;
  674. }
  675. #endif /* !APR_HAS_SENDFILE */