sockbuf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /* sockbuf.c - i/o routines with support for adding i/o layers. */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2024 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. #include "portable.h"
  17. #include <stdio.h>
  18. #include <ac/stdlib.h>
  19. #include <ac/ctype.h>
  20. #include <ac/errno.h>
  21. #include <ac/socket.h>
  22. #include <ac/string.h>
  23. #include <ac/unistd.h>
  24. #ifdef HAVE_IO_H
  25. #include <io.h>
  26. #endif /* HAVE_IO_H */
  27. #if defined( HAVE_FCNTL_H )
  28. #include <fcntl.h>
  29. #endif
  30. #if defined( HAVE_SYS_FILIO_H )
  31. #include <sys/filio.h>
  32. #elif defined( HAVE_SYS_IOCTL_H )
  33. #include <sys/ioctl.h>
  34. #endif
  35. #include "lber-int.h"
  36. #ifndef LBER_MIN_BUFF_SIZE
  37. #define LBER_MIN_BUFF_SIZE 4096
  38. #endif
  39. #ifndef LBER_MAX_BUFF_SIZE
  40. #define LBER_MAX_BUFF_SIZE (65536*256)
  41. #endif
  42. #ifndef LBER_DEFAULT_READAHEAD
  43. #define LBER_DEFAULT_READAHEAD 16384
  44. #endif
  45. Sockbuf *
  46. ber_sockbuf_alloc( void )
  47. {
  48. Sockbuf *sb;
  49. sb = LBER_CALLOC( 1, sizeof( Sockbuf ) );
  50. if( sb == NULL ) return NULL;
  51. ber_int_sb_init( sb );
  52. return sb;
  53. }
  54. void
  55. ber_sockbuf_free( Sockbuf *sb )
  56. {
  57. assert( sb != NULL );
  58. assert( SOCKBUF_VALID( sb ) );
  59. ber_int_sb_close( sb );
  60. ber_int_sb_destroy( sb );
  61. LBER_FREE( sb );
  62. }
  63. /* Return values: -1: error, 0: no operation performed or the answer is false,
  64. * 1: successful operation or the answer is true
  65. */
  66. int
  67. ber_sockbuf_ctrl( Sockbuf *sb, int opt, void *arg )
  68. {
  69. Sockbuf_IO_Desc *p;
  70. int ret = 0;
  71. assert( sb != NULL );
  72. assert( SOCKBUF_VALID( sb ) );
  73. switch ( opt ) {
  74. case LBER_SB_OPT_HAS_IO:
  75. p = sb->sb_iod;
  76. while ( p && p->sbiod_io != (Sockbuf_IO *)arg ) {
  77. p = p->sbiod_next;
  78. }
  79. if ( p ) {
  80. ret = 1;
  81. }
  82. break;
  83. case LBER_SB_OPT_GET_FD:
  84. if ( arg != NULL ) {
  85. *((ber_socket_t *)arg) = sb->sb_fd;
  86. }
  87. ret = ( sb->sb_fd == AC_SOCKET_INVALID ? -1 : 1);
  88. break;
  89. case LBER_SB_OPT_SET_FD:
  90. sb->sb_fd = *((ber_socket_t *)arg);
  91. ret = 1;
  92. break;
  93. case LBER_SB_OPT_SET_NONBLOCK:
  94. ret = ber_pvt_socket_set_nonblock( sb->sb_fd, arg != NULL)
  95. ? -1 : 1;
  96. break;
  97. case LBER_SB_OPT_DRAIN: {
  98. /* Drain the data source to enable possible errors (e.g.
  99. * TLS) to be propagated to the upper layers
  100. */
  101. char buf[LBER_MIN_BUFF_SIZE];
  102. do {
  103. ret = ber_int_sb_read( sb, buf, sizeof( buf ) );
  104. } while ( ret == sizeof( buf ) );
  105. ret = 1;
  106. } break;
  107. case LBER_SB_OPT_NEEDS_READ:
  108. ret = ( sb->sb_trans_needs_read ? 1 : 0 );
  109. break;
  110. case LBER_SB_OPT_NEEDS_WRITE:
  111. ret = ( sb->sb_trans_needs_write ? 1 : 0 );
  112. break;
  113. case LBER_SB_OPT_GET_MAX_INCOMING:
  114. if ( arg != NULL ) {
  115. *((ber_len_t *)arg) = sb->sb_max_incoming;
  116. }
  117. ret = 1;
  118. break;
  119. case LBER_SB_OPT_SET_MAX_INCOMING:
  120. sb->sb_max_incoming = *((ber_len_t *)arg);
  121. ret = 1;
  122. break;
  123. case LBER_SB_OPT_UNGET_BUF:
  124. #ifdef LDAP_PF_LOCAL_SENDMSG
  125. sb->sb_ungetlen = ((struct berval *)arg)->bv_len;
  126. if ( sb->sb_ungetlen <= sizeof( sb->sb_ungetbuf )) {
  127. AC_MEMCPY( sb->sb_ungetbuf, ((struct berval *)arg)->bv_val,
  128. sb->sb_ungetlen );
  129. ret = 1;
  130. } else {
  131. sb->sb_ungetlen = 0;
  132. ret = -1;
  133. }
  134. #endif
  135. break;
  136. default:
  137. ret = sb->sb_iod->sbiod_io->sbi_ctrl( sb->sb_iod, opt, arg );
  138. break;
  139. }
  140. return ret;
  141. }
  142. int
  143. ber_sockbuf_add_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer, void *arg )
  144. {
  145. Sockbuf_IO_Desc *d, *p, **q;
  146. assert( sb != NULL );
  147. assert( SOCKBUF_VALID( sb ) );
  148. if ( sbio == NULL ) {
  149. return -1;
  150. }
  151. q = &sb->sb_iod;
  152. p = *q;
  153. while ( p && p->sbiod_level > layer ) {
  154. q = &p->sbiod_next;
  155. p = *q;
  156. }
  157. d = LBER_MALLOC( sizeof( *d ) );
  158. if ( d == NULL ) {
  159. return -1;
  160. }
  161. d->sbiod_level = layer;
  162. d->sbiod_sb = sb;
  163. d->sbiod_io = sbio;
  164. memset( &d->sbiod_pvt, '\0', sizeof( d->sbiod_pvt ) );
  165. d->sbiod_next = p;
  166. *q = d;
  167. if ( sbio->sbi_setup != NULL && ( sbio->sbi_setup( d, arg ) < 0 ) ) {
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. int
  173. ber_sockbuf_remove_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer )
  174. {
  175. Sockbuf_IO_Desc *p, **q;
  176. assert( sb != NULL );
  177. assert( SOCKBUF_VALID( sb ) );
  178. if ( sb->sb_iod == NULL ) {
  179. return -1;
  180. }
  181. q = &sb->sb_iod;
  182. while ( *q != NULL ) {
  183. p = *q;
  184. if ( layer == p->sbiod_level && p->sbiod_io == sbio ) {
  185. if ( p->sbiod_io->sbi_remove != NULL &&
  186. p->sbiod_io->sbi_remove( p ) < 0 )
  187. {
  188. return -1;
  189. }
  190. *q = p->sbiod_next;
  191. LBER_FREE( p );
  192. break;
  193. }
  194. q = &p->sbiod_next;
  195. }
  196. return 0;
  197. }
  198. void
  199. ber_pvt_sb_buf_init( Sockbuf_Buf *buf )
  200. {
  201. buf->buf_base = NULL;
  202. buf->buf_ptr = 0;
  203. buf->buf_end = 0;
  204. buf->buf_size = 0;
  205. }
  206. void
  207. ber_pvt_sb_buf_destroy( Sockbuf_Buf *buf )
  208. {
  209. assert( buf != NULL);
  210. if (buf->buf_base) {
  211. LBER_FREE( buf->buf_base );
  212. }
  213. ber_pvt_sb_buf_init( buf );
  214. }
  215. int
  216. ber_pvt_sb_grow_buffer( Sockbuf_Buf *buf, ber_len_t minsize )
  217. {
  218. ber_len_t pw;
  219. char *p;
  220. assert( buf != NULL );
  221. for ( pw = LBER_MIN_BUFF_SIZE; pw < minsize; pw <<= 1 ) {
  222. if (pw > LBER_MAX_BUFF_SIZE) return -1;
  223. }
  224. if ( buf->buf_size < pw ) {
  225. p = LBER_REALLOC( buf->buf_base, pw );
  226. if ( p == NULL ) return -1;
  227. buf->buf_base = p;
  228. buf->buf_size = pw;
  229. }
  230. return 0;
  231. }
  232. ber_len_t
  233. ber_pvt_sb_copy_out( Sockbuf_Buf *sbb, char *buf, ber_len_t len )
  234. {
  235. ber_len_t max;
  236. assert( buf != NULL );
  237. assert( sbb != NULL );
  238. #if 0
  239. assert( sbb->buf_size > 0 );
  240. #endif
  241. max = sbb->buf_end - sbb->buf_ptr;
  242. max = ( max < len) ? max : len;
  243. if ( max ) {
  244. AC_MEMCPY( buf, sbb->buf_base + sbb->buf_ptr, max );
  245. sbb->buf_ptr += max;
  246. if ( sbb->buf_ptr >= sbb->buf_end ) {
  247. sbb->buf_ptr = sbb->buf_end = 0;
  248. }
  249. }
  250. return max;
  251. }
  252. ber_slen_t
  253. ber_pvt_sb_do_write( Sockbuf_IO_Desc *sbiod, Sockbuf_Buf *buf_out )
  254. {
  255. ber_len_t to_go;
  256. ber_slen_t ret;
  257. assert( sbiod != NULL );
  258. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  259. to_go = buf_out->buf_end - buf_out->buf_ptr;
  260. assert( to_go > 0 );
  261. for(;;) {
  262. ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf_out->buf_base +
  263. buf_out->buf_ptr, to_go );
  264. #ifdef EINTR
  265. if ((ret<0) && (errno==EINTR)) continue;
  266. #endif
  267. break;
  268. }
  269. if ( ret <= 0 ) return ret;
  270. buf_out->buf_ptr += ret;
  271. if (buf_out->buf_ptr == buf_out->buf_end) {
  272. buf_out->buf_end = buf_out->buf_ptr = 0;
  273. }
  274. return ret;
  275. }
  276. int
  277. ber_pvt_socket_set_nonblock( ber_socket_t sd, int nb )
  278. {
  279. #ifdef HAVE_FCNTL
  280. int flags = fcntl( sd, F_GETFL);
  281. if( nb ) {
  282. flags |= O_NONBLOCK;
  283. } else {
  284. flags &= ~O_NONBLOCK;
  285. }
  286. return fcntl( sd, F_SETFL, flags );
  287. #elif defined( FIONBIO )
  288. ioctl_t status = nb ? 1 : 0;
  289. return ioctl( sd, FIONBIO, &status );
  290. #endif
  291. }
  292. int
  293. ber_int_sb_init( Sockbuf *sb )
  294. {
  295. assert( sb != NULL);
  296. sb->sb_valid=LBER_VALID_SOCKBUF;
  297. sb->sb_options = 0;
  298. sb->sb_debug = ber_int_debug;
  299. sb->sb_fd = AC_SOCKET_INVALID;
  300. sb->sb_iod = NULL;
  301. sb->sb_trans_needs_read = 0;
  302. sb->sb_trans_needs_write = 0;
  303. assert( SOCKBUF_VALID( sb ) );
  304. return 0;
  305. }
  306. int
  307. ber_int_sb_close( Sockbuf *sb )
  308. {
  309. Sockbuf_IO_Desc *p;
  310. assert( sb != NULL);
  311. p = sb->sb_iod;
  312. while ( p ) {
  313. if ( p->sbiod_io->sbi_close && p->sbiod_io->sbi_close( p ) < 0 ) {
  314. return -1;
  315. }
  316. p = p->sbiod_next;
  317. }
  318. sb->sb_fd = AC_SOCKET_INVALID;
  319. return 0;
  320. }
  321. int
  322. ber_int_sb_destroy( Sockbuf *sb )
  323. {
  324. Sockbuf_IO_Desc *p;
  325. assert( sb != NULL);
  326. assert( SOCKBUF_VALID( sb ) );
  327. while ( sb->sb_iod ) {
  328. p = sb->sb_iod->sbiod_next;
  329. ber_sockbuf_remove_io( sb, sb->sb_iod->sbiod_io,
  330. sb->sb_iod->sbiod_level );
  331. sb->sb_iod = p;
  332. }
  333. return ber_int_sb_init( sb );
  334. }
  335. ber_slen_t
  336. ber_int_sb_read( Sockbuf *sb, void *buf, ber_len_t len )
  337. {
  338. ber_slen_t ret;
  339. assert( buf != NULL );
  340. assert( sb != NULL);
  341. assert( sb->sb_iod != NULL );
  342. assert( SOCKBUF_VALID( sb ) );
  343. for (;;) {
  344. ret = sb->sb_iod->sbiod_io->sbi_read( sb->sb_iod, buf, len );
  345. #ifdef EINTR
  346. if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
  347. #endif
  348. break;
  349. }
  350. return ret;
  351. }
  352. ber_slen_t
  353. ber_int_sb_write( Sockbuf *sb, void *buf, ber_len_t len )
  354. {
  355. ber_slen_t ret;
  356. assert( buf != NULL );
  357. assert( sb != NULL);
  358. assert( sb->sb_iod != NULL );
  359. assert( SOCKBUF_VALID( sb ) );
  360. for (;;) {
  361. ret = sb->sb_iod->sbiod_io->sbi_write( sb->sb_iod, buf, len );
  362. #ifdef EINTR
  363. if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
  364. #endif
  365. break;
  366. }
  367. return ret;
  368. }
  369. /*
  370. * Support for TCP
  371. */
  372. static ber_slen_t
  373. sb_stream_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  374. {
  375. assert( sbiod != NULL);
  376. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  377. #if defined(MACOS)
  378. /*
  379. * MacTCP/OpenTransport
  380. */
  381. return tcpread( sbiod->sbiod_sb->sb_fd, 0, (unsigned char *)buf,
  382. len, NULL );
  383. #elif defined( HAVE_PCNFS ) || \
  384. defined( HAVE_WINSOCK ) || defined ( __BEOS__ )
  385. /*
  386. * PCNFS (under DOS)
  387. */
  388. /*
  389. * Windows Socket API (under DOS/Windows 3.x)
  390. */
  391. /*
  392. * 32-bit Windows Socket API (under Windows NT or Windows 95)
  393. */
  394. return recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
  395. #elif defined( HAVE_NCSA )
  396. /*
  397. * NCSA Telnet TCP/IP stack (under DOS)
  398. */
  399. return nread( sbiod->sbiod_sb->sb_fd, buf, len );
  400. #else
  401. return read( sbiod->sbiod_sb->sb_fd, buf, len );
  402. #endif
  403. }
  404. static ber_slen_t
  405. sb_stream_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  406. {
  407. assert( sbiod != NULL);
  408. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  409. #if defined(MACOS)
  410. /*
  411. * MacTCP/OpenTransport
  412. */
  413. #define MAX_WRITE 65535
  414. return tcpwrite( sbiod->sbiod_sb->sb_fd, (unsigned char *)buf,
  415. (len<MAX_WRITE) ? len : MAX_WRITE );
  416. #elif defined( HAVE_PCNFS) \
  417. || defined( HAVE_WINSOCK) || defined ( __BEOS__ )
  418. /*
  419. * PCNFS (under DOS)
  420. */
  421. /*
  422. * Windows Socket API (under DOS/Windows 3.x)
  423. */
  424. /*
  425. * 32-bit Windows Socket API (under Windows NT or Windows 95)
  426. */
  427. return send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
  428. #elif defined(HAVE_NCSA)
  429. return netwrite( sbiod->sbiod_sb->sb_fd, buf, len );
  430. #elif defined(VMS)
  431. /*
  432. * VMS -- each write must be 64K or smaller
  433. */
  434. #define MAX_WRITE 65535
  435. return write( sbiod->sbiod_sb->sb_fd, buf,
  436. (len<MAX_WRITE) ? len : MAX_WRITE);
  437. #else
  438. return write( sbiod->sbiod_sb->sb_fd, buf, len );
  439. #endif
  440. }
  441. static int
  442. sb_stream_close( Sockbuf_IO_Desc *sbiod )
  443. {
  444. assert( sbiod != NULL );
  445. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  446. if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
  447. tcp_close( sbiod->sbiod_sb->sb_fd );
  448. return 0;
  449. }
  450. /* The argument is a pointer to the socket descriptor */
  451. static int
  452. sb_stream_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
  453. assert( sbiod != NULL );
  454. if ( arg != NULL ) {
  455. sbiod->sbiod_sb->sb_fd = *((int *)arg);
  456. }
  457. return 0;
  458. }
  459. static int
  460. sb_stream_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
  461. /* This is an end IO descriptor */
  462. return 0;
  463. }
  464. Sockbuf_IO ber_sockbuf_io_tcp = {
  465. sb_stream_setup, /* sbi_setup */
  466. NULL, /* sbi_remove */
  467. sb_stream_ctrl, /* sbi_ctrl */
  468. sb_stream_read, /* sbi_read */
  469. sb_stream_write, /* sbi_write */
  470. sb_stream_close /* sbi_close */
  471. };
  472. /*
  473. * Support for readahead (UDP needs it)
  474. */
  475. static int
  476. sb_rdahead_setup( Sockbuf_IO_Desc *sbiod, void *arg )
  477. {
  478. Sockbuf_Buf *p;
  479. assert( sbiod != NULL );
  480. p = LBER_MALLOC( sizeof( *p ) );
  481. if ( p == NULL ) return -1;
  482. ber_pvt_sb_buf_init( p );
  483. if ( arg == NULL ) {
  484. ber_pvt_sb_grow_buffer( p, LBER_DEFAULT_READAHEAD );
  485. } else {
  486. ber_pvt_sb_grow_buffer( p, *((int *)arg) );
  487. }
  488. sbiod->sbiod_pvt = p;
  489. return 0;
  490. }
  491. static int
  492. sb_rdahead_remove( Sockbuf_IO_Desc *sbiod )
  493. {
  494. Sockbuf_Buf *p;
  495. assert( sbiod != NULL );
  496. p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
  497. if ( p->buf_ptr != p->buf_end ) return -1;
  498. ber_pvt_sb_buf_destroy( (Sockbuf_Buf *)(sbiod->sbiod_pvt) );
  499. LBER_FREE( sbiod->sbiod_pvt );
  500. sbiod->sbiod_pvt = NULL;
  501. return 0;
  502. }
  503. static ber_slen_t
  504. sb_rdahead_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  505. {
  506. Sockbuf_Buf *p;
  507. ber_slen_t bufptr = 0, ret, max;
  508. assert( sbiod != NULL );
  509. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  510. assert( sbiod->sbiod_next != NULL );
  511. p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
  512. assert( p->buf_size > 0 );
  513. /* Are there anything left in the buffer? */
  514. ret = ber_pvt_sb_copy_out( p, buf, len );
  515. bufptr += ret;
  516. len -= ret;
  517. if ( len == 0 ) return bufptr;
  518. max = p->buf_size - p->buf_end;
  519. ret = 0;
  520. while ( max > 0 ) {
  521. ret = LBER_SBIOD_READ_NEXT( sbiod, p->buf_base + p->buf_end,
  522. max );
  523. #ifdef EINTR
  524. if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
  525. #endif
  526. break;
  527. }
  528. if ( ret < 0 ) {
  529. return ( bufptr ? bufptr : ret );
  530. }
  531. p->buf_end += ret;
  532. bufptr += ber_pvt_sb_copy_out( p, (char *) buf + bufptr, len );
  533. return bufptr;
  534. }
  535. static ber_slen_t
  536. sb_rdahead_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  537. {
  538. assert( sbiod != NULL );
  539. assert( sbiod->sbiod_next != NULL );
  540. return LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
  541. }
  542. static int
  543. sb_rdahead_close( Sockbuf_IO_Desc *sbiod )
  544. {
  545. assert( sbiod != NULL );
  546. /* Just erase the buffer */
  547. ber_pvt_sb_buf_destroy((Sockbuf_Buf *)sbiod->sbiod_pvt);
  548. return 0;
  549. }
  550. static int
  551. sb_rdahead_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
  552. {
  553. Sockbuf_Buf *p;
  554. p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
  555. if ( opt == LBER_SB_OPT_DATA_READY ) {
  556. if ( p->buf_ptr != p->buf_end ) {
  557. return 1;
  558. }
  559. } else if ( opt == LBER_SB_OPT_SET_READAHEAD ) {
  560. if ( p->buf_size >= *((ber_len_t *)arg) ) {
  561. return 0;
  562. }
  563. return ( ber_pvt_sb_grow_buffer( p, *((int *)arg) ) ?
  564. -1 : 1 );
  565. }
  566. return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
  567. }
  568. Sockbuf_IO ber_sockbuf_io_readahead = {
  569. sb_rdahead_setup, /* sbi_setup */
  570. sb_rdahead_remove, /* sbi_remove */
  571. sb_rdahead_ctrl, /* sbi_ctrl */
  572. sb_rdahead_read, /* sbi_read */
  573. sb_rdahead_write, /* sbi_write */
  574. sb_rdahead_close /* sbi_close */
  575. };
  576. /*
  577. * Support for simple file IO
  578. */
  579. static ber_slen_t
  580. sb_fd_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  581. {
  582. assert( sbiod != NULL);
  583. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  584. #ifdef LDAP_PF_LOCAL_SENDMSG
  585. if ( sbiod->sbiod_sb->sb_ungetlen ) {
  586. ber_len_t blen = sbiod->sbiod_sb->sb_ungetlen;
  587. if ( blen > len )
  588. blen = len;
  589. AC_MEMCPY( buf, sbiod->sbiod_sb->sb_ungetbuf, blen );
  590. buf = (char *) buf + blen;
  591. len -= blen;
  592. sbiod->sbiod_sb->sb_ungetlen -= blen;
  593. if ( sbiod->sbiod_sb->sb_ungetlen ) {
  594. AC_MEMCPY( sbiod->sbiod_sb->sb_ungetbuf,
  595. sbiod->sbiod_sb->sb_ungetbuf+blen,
  596. sbiod->sbiod_sb->sb_ungetlen );
  597. }
  598. if ( len == 0 )
  599. return blen;
  600. }
  601. #endif
  602. return read( sbiod->sbiod_sb->sb_fd, buf, len );
  603. }
  604. static ber_slen_t
  605. sb_fd_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  606. {
  607. assert( sbiod != NULL);
  608. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  609. return write( sbiod->sbiod_sb->sb_fd, buf, len );
  610. }
  611. static int
  612. sb_fd_close( Sockbuf_IO_Desc *sbiod )
  613. {
  614. assert( sbiod != NULL );
  615. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  616. if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
  617. close( sbiod->sbiod_sb->sb_fd );
  618. return 0;
  619. }
  620. /* The argument is a pointer to the file descriptor */
  621. static int
  622. sb_fd_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
  623. assert( sbiod != NULL );
  624. if ( arg != NULL )
  625. sbiod->sbiod_sb->sb_fd = *((int *)arg);
  626. return 0;
  627. }
  628. static int
  629. sb_fd_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
  630. /* This is an end IO descriptor */
  631. return 0;
  632. }
  633. Sockbuf_IO ber_sockbuf_io_fd = {
  634. sb_fd_setup, /* sbi_setup */
  635. NULL, /* sbi_remove */
  636. sb_fd_ctrl, /* sbi_ctrl */
  637. sb_fd_read, /* sbi_read */
  638. sb_fd_write, /* sbi_write */
  639. sb_fd_close /* sbi_close */
  640. };
  641. /*
  642. * Debugging layer
  643. */
  644. static int
  645. sb_debug_setup( Sockbuf_IO_Desc *sbiod, void *arg )
  646. {
  647. assert( sbiod != NULL );
  648. if ( arg == NULL ) arg = "sockbuf_";
  649. sbiod->sbiod_pvt = LBER_MALLOC( strlen( arg ) + 1 );
  650. if ( sbiod->sbiod_pvt == NULL ) return -1;
  651. strcpy( (char *)sbiod->sbiod_pvt, (char *)arg );
  652. return 0;
  653. }
  654. static int
  655. sb_debug_remove( Sockbuf_IO_Desc *sbiod )
  656. {
  657. assert( sbiod != NULL );
  658. assert( sbiod->sbiod_pvt != NULL );
  659. LBER_FREE( sbiod->sbiod_pvt );
  660. sbiod->sbiod_pvt = NULL;
  661. return 0;
  662. }
  663. static int
  664. sb_debug_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
  665. {
  666. return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
  667. }
  668. static ber_slen_t
  669. sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  670. {
  671. ber_slen_t ret;
  672. char ebuf[128];
  673. ret = LBER_SBIOD_READ_NEXT( sbiod, buf, len );
  674. if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
  675. int err = sock_errno();
  676. if ( ret < 0 ) {
  677. ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  678. "%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt,
  679. (long)len, AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
  680. } else {
  681. ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  682. "%sread: want=%ld, got=%ld\n", (char *)sbiod->sbiod_pvt,
  683. (long)len, (long)ret );
  684. ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  685. (const char *)buf, ret );
  686. }
  687. sock_errset(err);
  688. }
  689. return ret;
  690. }
  691. static ber_slen_t
  692. sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  693. {
  694. ber_slen_t ret;
  695. char ebuf[128];
  696. ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
  697. if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
  698. int err = sock_errno();
  699. if ( ret < 0 ) {
  700. ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  701. "%swrite: want=%ld error=%s\n",
  702. (char *)sbiod->sbiod_pvt, (long)len,
  703. AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
  704. } else {
  705. ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  706. "%swrite: want=%ld, written=%ld\n",
  707. (char *)sbiod->sbiod_pvt, (long)len, (long)ret );
  708. ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
  709. (const char *)buf, ret );
  710. }
  711. sock_errset(err);
  712. }
  713. return ret;
  714. }
  715. Sockbuf_IO ber_sockbuf_io_debug = {
  716. sb_debug_setup, /* sbi_setup */
  717. sb_debug_remove, /* sbi_remove */
  718. sb_debug_ctrl, /* sbi_ctrl */
  719. sb_debug_read, /* sbi_read */
  720. sb_debug_write, /* sbi_write */
  721. NULL /* sbi_close */
  722. };
  723. #ifdef LDAP_CONNECTIONLESS
  724. /*
  725. * Support for UDP (CLDAP)
  726. *
  727. * All I/O at this level must be atomic. For ease of use, the sb_readahead
  728. * must be used above this module. All data reads and writes are prefixed
  729. * with a sockaddr_storage containing the address of the remote entity. Upper levels
  730. * must read and write this sockaddr_storage before doing the usual ber_printf/scanf
  731. * operations on LDAP messages.
  732. */
  733. static int
  734. sb_dgram_setup( Sockbuf_IO_Desc *sbiod, void *arg )
  735. {
  736. assert( sbiod != NULL);
  737. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  738. if ( arg != NULL ) sbiod->sbiod_sb->sb_fd = *((int *)arg);
  739. return 0;
  740. }
  741. static ber_slen_t
  742. sb_dgram_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  743. {
  744. ber_slen_t rc;
  745. ber_socklen_t addrlen;
  746. struct sockaddr *src;
  747. assert( sbiod != NULL );
  748. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  749. assert( buf != NULL );
  750. addrlen = sizeof( struct sockaddr_storage );
  751. src = buf;
  752. buf = (char *) buf + addrlen;
  753. len -= addrlen;
  754. rc = recvfrom( sbiod->sbiod_sb->sb_fd, buf, len, 0, src, &addrlen );
  755. return rc > 0 ? rc+sizeof(struct sockaddr_storage) : rc;
  756. }
  757. static ber_slen_t
  758. sb_dgram_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
  759. {
  760. ber_slen_t rc;
  761. struct sockaddr *dst;
  762. socklen_t dstsize;
  763. assert( sbiod != NULL );
  764. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  765. assert( buf != NULL );
  766. dst = buf;
  767. buf = (char *) buf + sizeof( struct sockaddr_storage );
  768. len -= sizeof( struct sockaddr_storage );
  769. dstsize = dst->sa_family == AF_INET ? sizeof( struct sockaddr_in )
  770. #ifdef LDAP_PF_INET6
  771. : dst->sa_family == AF_INET6 ? sizeof( struct sockaddr_in6 )
  772. #endif
  773. : sizeof( struct sockaddr_storage );
  774. rc = sendto( sbiod->sbiod_sb->sb_fd, buf, len, 0, dst, dstsize );
  775. if ( rc < 0 ) return -1;
  776. /* fake error if write was not atomic */
  777. if (rc < len) {
  778. # ifdef EMSGSIZE
  779. errno = EMSGSIZE;
  780. # endif
  781. return -1;
  782. }
  783. rc = len + sizeof(struct sockaddr_storage);
  784. return rc;
  785. }
  786. static int
  787. sb_dgram_close( Sockbuf_IO_Desc *sbiod )
  788. {
  789. assert( sbiod != NULL );
  790. assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
  791. if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
  792. tcp_close( sbiod->sbiod_sb->sb_fd );
  793. return 0;
  794. }
  795. static int
  796. sb_dgram_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
  797. {
  798. /* This is an end IO descriptor */
  799. return 0;
  800. }
  801. Sockbuf_IO ber_sockbuf_io_udp =
  802. {
  803. sb_dgram_setup, /* sbi_setup */
  804. NULL, /* sbi_remove */
  805. sb_dgram_ctrl, /* sbi_ctrl */
  806. sb_dgram_read, /* sbi_read */
  807. sb_dgram_write, /* sbi_write */
  808. sb_dgram_close /* sbi_close */
  809. };
  810. #endif /* LDAP_CONNECTIONLESS */