connection.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: connection.c /main/4 1999/10/14 17:47:12 mgreess $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994 Novell, Inc.
  28. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  29. */
  30. /*
  31. * This file manages server connections.
  32. */
  33. #include <EUSCompat.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <time.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include "connection.h"
  41. #include "rtable2.h"
  42. #include "rtable3.h"
  43. #include "rtable4.h"
  44. #include "cm.h"
  45. #include "debug.h"
  46. #include "agent.h"
  47. #include "convert2-4.h"
  48. #include "convert3-4.h"
  49. #include "rpccalls.h"
  50. #define MAX_COUNT 40
  51. static struct timeval timeout_tv;
  52. static struct timeval retry_tv;
  53. static AUTH *unix_credential = NULL;
  54. static int tcp_count = 0;
  55. static int cl_count = 0;
  56. static _DtCm_Client_Info *client_cache_head = NULL;
  57. static _DtCm_Client_Info *client_cache_tail = NULL;
  58. /*****************************************************************************
  59. * forward declaration of static functions.
  60. *****************************************************************************/
  61. static void create_auth(CLIENT *cl);
  62. static void destroy_auth(CLIENT *cl);
  63. static _DtCm_Client_Info * get_client_info(char *host, int version);
  64. static void destroy_target_list(_DtCm_Target_List *tlist);
  65. static void destroy_client_info(_DtCm_Client_Info *ci);
  66. static void insert_client_info(_DtCm_Client_Info *ci);
  67. static void delete_client_info(_DtCm_Client_Info *oldci);
  68. static void cleanup_some_connection(_DtCm_Client_Info *dontclose);
  69. static void check_registration(_DtCm_Connection *conn);
  70. static _DtCm_Client_Info * get_new_client_handle(_DtCm_Connection *conn);
  71. static CSA_return_code get_client_handle(const char *host, const u_long prognum,
  72. u_long *vers_outp, const u_long vers_low,
  73. const u_long vers_high, char *nettype,
  74. CLIENT **clnt);
  75. static CSA_return_code regstat4_to_dtcmstatus(Registration_Status_4 stat4);
  76. extern CSA_return_code
  77. _DtCm_create_udp_client(
  78. char *host,
  79. int version,
  80. int timeout,
  81. _DtCm_Client_Info **clnt)
  82. {
  83. CSA_return_code stat;
  84. _DtCm_Client_Info *ci;
  85. u_long vers_out;
  86. CLIENT *cl=NULL;
  87. if (host == NULL || clnt == NULL)
  88. return (CSA_E_INVALID_PARAMETER);
  89. /* if client info is found, we have at least the udp handle */
  90. if (((*clnt) = get_client_info(host, version)) != NULL) {
  91. return (CSA_SUCCESS);
  92. }
  93. #if defined(SunOS)
  94. cl = clnt_create_vers(host, TABLEPROG,
  95. &vers_out, TABLEVERS_2, version, "udp");
  96. if (cl==NULL) {
  97. _DtCm_print_errmsg(clnt_spcreateerror(host));
  98. return (_DtCm_clntstat_to_csastat(rpc_createerr.cf_stat));
  99. }
  100. #else
  101. stat = get_client_handle(host, (u_int)TABLEPROG, &vers_out, TABLEVERS_2,
  102. version, "udp", &cl);
  103. if (stat != CSA_SUCCESS)
  104. return (stat);
  105. #endif
  106. /* if version is lower than requested, check the list again */
  107. if (vers_out < version) {
  108. if ((ci = get_client_info(host, vers_out)) != NULL) {
  109. clnt_destroy(cl);
  110. *clnt = ci;
  111. return (CSA_SUCCESS);
  112. }
  113. }
  114. create_auth(cl);
  115. /* Adjust Timeout */
  116. if (timeout==0) timeout = _DtCM_DEFAULT_TIMEOUT;
  117. timeout_tv.tv_sec = timeout;
  118. timeout_tv.tv_usec = 0;
  119. clnt_control(cl, CLSET_TIMEOUT, (char*)&timeout_tv);
  120. /* UDP only!
  121. time rpc waits for server to reply before retransmission =
  122. 'timeout'. since the retry timeout is set to timeout + 10;
  123. this guarantees there won't
  124. be any retransmisssions resulting in duplicate
  125. transactions in the database.
  126. */
  127. retry_tv.tv_sec = timeout + 10;
  128. retry_tv.tv_usec = 0;
  129. clnt_control(cl, CLSET_RETRY_TIMEOUT, (char*)&retry_tv);
  130. if ((ci = (_DtCm_Client_Info *)calloc(1, sizeof(_DtCm_Client_Info))) == NULL) {
  131. destroy_auth(cl);
  132. clnt_destroy(cl);
  133. return (CSA_E_INSUFFICIENT_MEMORY);
  134. }
  135. if ((ci->host = strdup(host)) == NULL) {
  136. destroy_auth(cl);
  137. clnt_destroy(cl);
  138. free(ci);
  139. return (CSA_E_INSUFFICIENT_MEMORY);
  140. }
  141. ci->udpcl = cl;
  142. ci->vers_out = vers_out;
  143. insert_client_info(ci);
  144. *clnt = ci;
  145. return (CSA_SUCCESS);
  146. }
  147. /*
  148. * Creates tcp client handle. Used for calls that potentially return
  149. * large amount of data. If it fails to create a tcp client handle,
  150. * a udp client handle will be returned.
  151. */
  152. extern CSA_return_code
  153. _DtCm_create_tcp_client(
  154. char *host,
  155. int version,
  156. int timeout,
  157. _DtCm_Client_Info **clnt)
  158. {
  159. CSA_return_code stat;
  160. _DtCm_Client_Info *ci;
  161. u_long vers_out;
  162. CLIENT *cl=NULL;
  163. if (host == NULL || clnt == NULL)
  164. return (CSA_E_INVALID_PARAMETER);
  165. /* Get a udp client handle. This serves two purposes: */
  166. /* - to get a udp handle for an old server which talks only udp */
  167. /* - to invoke a server through inetd since only udp is registered.*/
  168. if ((stat = _DtCm_create_udp_client(host, version, timeout, &ci))
  169. != CSA_SUCCESS) {
  170. return (stat);
  171. } else if (ci->tcpcl) {
  172. *clnt = ci;
  173. return (CSA_SUCCESS);
  174. } else {
  175. /* create tcp connection */
  176. #if defined(SunOS)
  177. cl = clnt_create_vers(host, TABLEPROG, &vers_out,
  178. TABLEVERS_2, version, "tcp");
  179. #else
  180. stat = get_client_handle(host, (u_int)TABLEPROG, &vers_out,
  181. TABLEVERS_2, version, "tcp", &cl);
  182. #endif
  183. /* if can't create tcp connection, use udp */
  184. if (cl==NULL) {
  185. _DtCm_print_errmsg(clnt_spcreateerror(host));
  186. *clnt = ci;
  187. return (CSA_SUCCESS);
  188. }
  189. create_auth(cl);
  190. /* Adjust Timeout */
  191. if (timeout==0) timeout = _DtCM_DEFAULT_TIMEOUT;
  192. timeout_tv.tv_sec = timeout;
  193. timeout_tv.tv_usec = 0;
  194. clnt_control(cl, CLSET_TIMEOUT, (char*)&timeout_tv);
  195. /* don't need to set vers_out since it should
  196. * be the same as that of the udp transport
  197. */
  198. ci->tcpcl = cl;
  199. if (++tcp_count > MAX_COUNT)
  200. /* clean up tcp connections */
  201. cleanup_some_connection(ci);
  202. *clnt = ci;
  203. return (CSA_SUCCESS);
  204. }
  205. }
  206. /*
  207. * Used instead of clnt_call by rtableX_clnt.c
  208. *
  209. * Might need locking for the client handle here since
  210. * it might be purged if something's wrong
  211. */
  212. extern enum clnt_stat
  213. _DtCm_clnt_call(
  214. _DtCm_Connection *conn,
  215. u_long proc,
  216. xdrproc_t inproc,
  217. caddr_t in,
  218. xdrproc_t outproc,
  219. caddr_t out,
  220. struct timeval tout)
  221. {
  222. _DtCm_Client_Info *ci;
  223. _DtCm_Transport_Type ttype;
  224. enum clnt_stat status = RPC_FAILED;
  225. int retry = conn->retry;
  226. while (B_TRUE) {
  227. if (conn->ci == NULL)
  228. break;
  229. else {
  230. ci = conn->ci;
  231. ci->last_used = time(0);
  232. }
  233. if (conn->use == udp_transport || ci->tcpcl == NULL)
  234. ttype = udp_transport;
  235. else
  236. ttype = tcp_transport;
  237. status = clnt_call((ttype == tcp_transport ? ci->tcpcl :
  238. ci->udpcl), proc, inproc, in,
  239. outproc, out, tout);
  240. if ((ttype == udp_transport && status == RPC_TIMEDOUT) ||
  241. (status == RPC_CANTRECV)) {
  242. if (retry) {
  243. retry--;
  244. /* don't retry when stat is RPC_TIMEDOUT
  245. * and transpart is tcp since if the server
  246. * is down, stat would be something else
  247. * like RPC_CANTRECV
  248. */
  249. /* get new client handle */
  250. if (get_new_client_handle(conn) == NULL)
  251. break;
  252. } else {
  253. /* purge the client handle */
  254. delete_client_info(conn->ci);
  255. conn->ci = NULL;
  256. break;
  257. }
  258. } else
  259. break;
  260. }
  261. if (status != RPC_SUCCESS && conn->ci != NULL) {
  262. _DtCm_print_errmsg(clnt_sperror((ttype == tcp_transport ? ci->tcpcl :
  263. ci->udpcl), ci->host));
  264. }
  265. conn->stat = status;
  266. return status;
  267. }
  268. extern CSA_return_code
  269. _DtCm_add_registration(
  270. _DtCm_Client_Info *ci,
  271. char *cal,
  272. unsigned long update_type)
  273. {
  274. _DtCm_Target_List *listp, *prev;
  275. _DtCm_Target_List *listitem;
  276. int result;
  277. if (ci == NULL || cal == NULL)
  278. return (CSA_E_INVALID_PARAMETER);
  279. for (listp = prev = ci->tlist; listp != NULL;
  280. prev = listp, listp = listp->next) {
  281. if ((result = strcmp(listp->cal, cal)) == 0) {
  282. /* registered already */
  283. return (CSA_SUCCESS);
  284. } else if (result > 0)
  285. break;
  286. }
  287. /* register the first time, insert in list in ascending order */
  288. if ((listitem = (_DtCm_Target_List *)calloc(1, sizeof(_DtCm_Target_List))) == NULL)
  289. return (CSA_E_INSUFFICIENT_MEMORY);
  290. if ((listitem->cal = strdup(cal)) == NULL) {
  291. free(listitem);
  292. return (CSA_E_INSUFFICIENT_MEMORY);
  293. }
  294. listitem->update_type = update_type;
  295. if (prev == NULL || listp == prev)
  296. ci->tlist = listitem;
  297. else
  298. prev->next = listitem;
  299. listitem->next = listp;
  300. ci->nregistered++;
  301. return (CSA_SUCCESS);
  302. }
  303. extern void
  304. _DtCm_remove_registration(_DtCm_Client_Info *ci, char *cal)
  305. {
  306. _DtCm_Target_List *listp, *prev;
  307. _DtCm_Client_Info *c;
  308. int result;
  309. if (cal == NULL) return;
  310. /* if found, just increment the number of registration */
  311. for (listp = prev = ci->tlist; listp != NULL;
  312. prev = listp, listp = listp->next) {
  313. if ((result = strcmp(listp->cal, cal)) == 0) {
  314. if (listp == prev)
  315. ci->tlist = listp->next;
  316. else
  317. prev->next = listp->next;
  318. /* free target item */
  319. free(listp->cal);
  320. free(listp);
  321. /*
  322. * if no calendar is registered, close tcp connection
  323. */
  324. if (--(ci->nregistered) == 0) {
  325. if (ci->tcpcl) {
  326. destroy_auth(ci->tcpcl);
  327. clnt_destroy(ci->tcpcl);
  328. ci->tcpcl = NULL;
  329. tcp_count--;
  330. }
  331. /* find other tcp connection for the
  332. * same host
  333. */
  334. for (c = client_cache_head; c != NULL;
  335. c = c->next) {
  336. if ((result = strcmp(c->host,
  337. ci->host)) == 0) {
  338. if (c->nregistered == 0 &&
  339. c->tcpcl) {
  340. destroy_auth(c->tcpcl);
  341. clnt_destroy(c->tcpcl);
  342. c->tcpcl = NULL;
  343. tcp_count--;
  344. }
  345. } else if (result > 0)
  346. break;
  347. }
  348. }
  349. return;
  350. } else if (result > 0)
  351. break;
  352. }
  353. /* not found; impossible */
  354. }
  355. extern CSA_return_code
  356. _DtCm_get_server_rpc_version(char *host, int *version)
  357. {
  358. CSA_return_code stat;
  359. _DtCm_Client_Info *ci;
  360. if (host == NULL) {
  361. return (CSA_E_INVALID_PARAMETER);
  362. }
  363. if ((stat = _DtCm_create_tcp_client(host, TABLEVERS,
  364. _DtCM_INITIAL_TIMEOUT, &ci)) == CSA_SUCCESS)
  365. *version = ci->vers_out;
  366. return (stat);
  367. }
  368. extern CSA_return_code
  369. _DtCm_clntstat_to_csastat(enum clnt_stat clntstat)
  370. {
  371. switch (clntstat) {
  372. #if defined(SunOS)
  373. case RPC_N2AXLATEFAILURE:
  374. #endif
  375. case RPC_UNKNOWNHOST:
  376. return (CSA_X_DT_E_INVALID_SERVER_LOCATION);
  377. case RPC_PROGNOTREGISTERED:
  378. return (CSA_X_DT_E_SERVICE_NOT_REGISTERED);
  379. case RPC_TIMEDOUT:
  380. return (CSA_X_DT_E_SERVER_TIMEOUT);
  381. default:
  382. return (CSA_E_SERVICE_UNAVAILABLE);
  383. }
  384. }
  385. /*****************************************************************************
  386. * static functions used within the file
  387. *****************************************************************************/
  388. static void
  389. create_auth(CLIENT *cl)
  390. {
  391. /* Always cache the Unix style credentials. */
  392. if (unix_credential == NULL)
  393. #if defined(SunOS)
  394. unix_credential = authsys_create_default ();
  395. #else
  396. unix_credential = authunix_create_default ();
  397. #endif
  398. cl->cl_auth = unix_credential;
  399. }
  400. static void
  401. destroy_auth(CLIENT *cl)
  402. {
  403. /* It is a no-op for unix-authentication because we always cache it.
  404. * But we have to destroy it when secure RPC is used.
  405. */
  406. }
  407. /*
  408. * Given a host name, find the _DtCm_Client_Info structure which contains
  409. * both udp and tcp handle to the server running in the host.
  410. */
  411. static _DtCm_Client_Info *
  412. get_client_info(char *host, int version)
  413. {
  414. _DtCm_Client_Info *ci;
  415. int result;
  416. if (host==NULL) return(NULL);
  417. for (ci = client_cache_head; ci != NULL; ci = ci->next) {
  418. if ((result = strcmp(ci->host, host)) == 0) {
  419. if (ci->vers_out <= version)
  420. return(ci);
  421. } else if (result > 0)
  422. break;
  423. }
  424. return(NULL);
  425. }
  426. static void
  427. destroy_target_list(_DtCm_Target_List *tlist)
  428. {
  429. _DtCm_Target_List *listp, *listitem;
  430. for (listp = tlist; listp != NULL; ) {
  431. listitem = listp;
  432. listp = listp->next;
  433. if (listitem->cal)
  434. free(listitem->cal);
  435. free(listitem);
  436. }
  437. }
  438. static void
  439. destroy_client_info(_DtCm_Client_Info *ci)
  440. {
  441. if (ci==NULL) return;
  442. if (ci->host != NULL)
  443. free(ci->host);
  444. if (ci->tcpcl) {
  445. destroy_auth(ci->tcpcl);
  446. clnt_destroy(ci->tcpcl);
  447. tcp_count--;
  448. }
  449. if (ci->udpcl) {
  450. destroy_auth(ci->udpcl);
  451. clnt_destroy(ci->udpcl);
  452. }
  453. destroy_target_list(ci->tlist);
  454. free(ci);
  455. cl_count--;
  456. }
  457. /*
  458. * Don't limit the number of cached connections right now.
  459. * Udp client handle does not use up file descriptor only space.
  460. * Tcp client handle is kept open only when there's at least one
  461. * calendar registered with the host and the user probably won't
  462. * be browsing more than 50 calendar at the same time.
  463. */
  464. static void
  465. insert_client_info(_DtCm_Client_Info *ci)
  466. {
  467. _DtCm_Client_Info *citem;
  468. if (++cl_count > MAX_COUNT)
  469. cleanup_some_connection(ci);
  470. /* insert new item alphabetically */
  471. for (citem = client_cache_head; citem != NULL; citem = citem->next) {
  472. /* there shouldn't be an entry with the same host name
  473. * if there's, it would be picked up in get_client_info()
  474. */
  475. if (strcmp(citem->host, ci->host) > 0)
  476. break;
  477. }
  478. if (citem == NULL) {
  479. if (client_cache_head == NULL)
  480. client_cache_head = client_cache_tail = ci;
  481. else {
  482. ci->prev = client_cache_tail;
  483. client_cache_tail->next = ci;
  484. client_cache_tail = ci;
  485. }
  486. } else {
  487. ci->next = citem;
  488. ci->prev = citem->prev;
  489. if (citem == client_cache_head)
  490. client_cache_head = ci;
  491. else
  492. citem->prev->next = ci;
  493. citem->prev = ci;
  494. }
  495. #ifdef CM_DEBUG
  496. fprintf(stderr, "%s: head = %d, tail = %d, newitem = %d\n",
  497. "insert_client_info", client_cache_head,
  498. client_cache_tail, ci);
  499. fprintf(stderr, "tcp_count = %d, cl_count = %d\n", tcp_count, cl_count);
  500. #endif
  501. }
  502. /*
  503. * remove the client info structure from the list
  504. */
  505. static void
  506. delete_client_info(_DtCm_Client_Info *oldci)
  507. {
  508. if (oldci == NULL) return;
  509. if (oldci == client_cache_head) {
  510. client_cache_head = oldci->next;
  511. if (client_cache_head)
  512. client_cache_head->prev = NULL;
  513. } else if (oldci == client_cache_tail) {
  514. client_cache_tail = oldci->prev;
  515. if (client_cache_tail)
  516. client_cache_tail->next = NULL;
  517. } else {
  518. oldci->prev->next = oldci->next;
  519. oldci->next->prev = oldci->prev;
  520. }
  521. if (oldci == client_cache_tail)
  522. client_cache_tail = NULL;
  523. destroy_client_info(oldci);
  524. #ifdef CM_DEBUG
  525. fprintf(stderr, "%s: head = %d, tail = %d, olditem = %d\n",
  526. "delete_client_info", client_cache_head,
  527. client_cache_tail, oldci);
  528. #endif
  529. }
  530. /*
  531. * Number of open tcp connections reaches the maximum.
  532. * This is very unlikely in the normal case since
  533. * a tcp connection is kept open if at least one calendar
  534. * is registered with the host and a user would not be
  535. * browsing a large number of calendars at one time.
  536. * However, when a calendar is deselected in the calendar
  537. * list on the multi-browser window, a lookup call using
  538. * the tcp connection is made after the calendar is
  539. * deregistered. This keeps the tcp connection open
  540. * even if that's the last calendar registered with the
  541. * host. This routine is used to clean up such tcp connections.
  542. * This is a good time to clean up connections that are not
  543. * used for a long time.
  544. */
  545. static void
  546. cleanup_some_connection(_DtCm_Client_Info *dontclose)
  547. {
  548. _DtCm_Client_Info *ci, *oldci;
  549. int total = 0, deleted = 0, done = 0;
  550. for (ci = client_cache_head; ci != NULL; )
  551. {
  552. total++;
  553. if (ci != dontclose && ci->nregistered == 0 &&
  554. (ci->tcpcl || (!done && ci->tcpcl == NULL) ||
  555. (ci->tcpcl==NULL && (time(NULL) - ci->last_used)>DAYSEC)))
  556. {
  557. if (!done) done = 1;
  558. deleted++;
  559. oldci = ci;
  560. ci = ci->next;
  561. delete_client_info(oldci);
  562. } else
  563. ci = ci->next;
  564. }
  565. #ifdef CM_DEBUG
  566. fprintf(stderr, "%s: total = %d, deleted = %d\n",
  567. "cleanup_tcp_connection", total, deleted);
  568. #endif
  569. }
  570. /*
  571. * check registration
  572. * Deergister the first target:
  573. * if it succeeded, the old server is still running, just re-register it;
  574. * else assume that it's a new server so re-register the whole list again.
  575. */
  576. static void
  577. check_registration(_DtCm_Connection *conn)
  578. {
  579. _DtCm_Target_List *listp, *prev;
  580. _DtCm_Transport_Type olduse;
  581. CSA_return_code stat;
  582. if (conn->ci->tlist == NULL)
  583. return;
  584. olduse = conn->use;
  585. conn->use = udp_transport;
  586. conn->retry = B_FALSE;
  587. if ((stat = _DtCm_do_unregistration(conn, conn->ci->tlist->cal,
  588. conn->ci->tlist->update_type)) == CSA_SUCCESS) {
  589. if (_DtCm_do_registration(conn, conn->ci->tlist->cal,
  590. conn->ci->tlist->update_type) != CSA_SUCCESS)
  591. {
  592. conn->ci->nregistered--;
  593. listp = conn->ci->tlist;
  594. conn->ci->tlist = listp->next;
  595. free(listp->cal);
  596. free(listp);
  597. }
  598. } else if (stat == CSA_E_CALLBACK_NOT_REGISTERED || stat == CSA_E_FAILURE) {
  599. for (listp = prev = conn->ci->tlist; listp != NULL; ) {
  600. if (_DtCm_do_registration(conn, listp->cal,
  601. listp->update_type) != CSA_SUCCESS)
  602. {
  603. conn->ci->nregistered--;
  604. if (listp == prev)
  605. conn->ci->tlist = prev = listp->next;
  606. else
  607. prev->next = listp->next;
  608. /* free target item */
  609. free(listp->cal);
  610. free(listp);
  611. listp = (prev ? prev->next : NULL);
  612. } else {
  613. prev = listp;
  614. listp = listp->next;
  615. }
  616. }
  617. }
  618. conn->use = olduse;
  619. }
  620. static _DtCm_Client_Info *
  621. get_new_client_handle(_DtCm_Connection *conn)
  622. {
  623. CLIENT *cl;
  624. int oldver;
  625. if (conn == NULL) return(NULL);
  626. oldver = conn->ci->vers_out;
  627. /* always get a udp client handle first */
  628. #if defined(SunOS)
  629. cl = clnt_create_vers(conn->ci->host, TABLEPROG, &(conn->ci->vers_out),
  630. TABLEVERS_2, oldver, "udp");
  631. if (cl == NULL) {
  632. _DtCm_print_errmsg(clnt_spcreateerror(conn->ci->host));
  633. }
  634. #else
  635. (void) get_client_handle(conn->ci->host, (u_int)TABLEPROG,
  636. &(conn->ci->vers_out), TABLEVERS_2, oldver,
  637. "udp", &cl);
  638. #endif
  639. if (cl == NULL) {
  640. delete_client_info(conn->ci);
  641. conn->ci = NULL;
  642. return(NULL);
  643. } else {
  644. create_auth(cl);
  645. /* adjust timeout */
  646. timeout_tv.tv_sec = _DtCM_INITIAL_TIMEOUT;
  647. timeout_tv.tv_usec = 0;
  648. clnt_control(cl, CLSET_TIMEOUT, (char *)&timeout_tv);
  649. retry_tv.tv_sec = _DtCM_INITIAL_TIMEOUT + 10;
  650. retry_tv.tv_usec = 0;
  651. clnt_control(cl, CLSET_RETRY_TIMEOUT, (char *)&retry_tv);
  652. destroy_auth(conn->ci->udpcl);
  653. clnt_destroy(conn->ci->udpcl);
  654. conn->ci->udpcl = cl;
  655. }
  656. /* check registration */
  657. /* if there's anything wrong, nregistered could be zero */
  658. check_registration(conn);
  659. /* ci might be set to NULL if an rpc call failed */
  660. if (conn->ci == NULL)
  661. return (NULL);
  662. /* now deal with tcp handle */
  663. /* get rid of old handle first */
  664. if (conn->ci->tcpcl) {
  665. destroy_auth(conn->ci->tcpcl);
  666. clnt_destroy(conn->ci->tcpcl);
  667. tcp_count--;
  668. conn->ci->tcpcl = NULL;
  669. }
  670. if (conn->use == udp_transport) {
  671. return(conn->ci);
  672. } else {
  673. /* get a tcp client handle */
  674. oldver = conn->ci->vers_out;
  675. #if defined(SunOS)
  676. cl = clnt_create_vers(conn->ci->host, TABLEPROG,
  677. &(conn->ci->vers_out), TABLEVERS_2, oldver, "tcp");
  678. if (cl == NULL)
  679. _DtCm_print_errmsg(clnt_spcreateerror(conn->ci->host));
  680. #else
  681. (void) get_client_handle(conn->ci->host, (u_int)TABLEPROG,
  682. &(conn->ci->vers_out), TABLEVERS_2, oldver, "tcp",
  683. &cl);
  684. #endif
  685. if (cl == NULL) {
  686. conn->ci->vers_out = oldver;
  687. return(NULL);
  688. } else {
  689. create_auth(cl);
  690. /* adjust timeout */
  691. timeout_tv.tv_sec = _DtCM_INITIAL_TIMEOUT;
  692. timeout_tv.tv_usec = 0;
  693. clnt_control(cl, CLSET_TIMEOUT, (char *)&timeout_tv);
  694. conn->ci->tcpcl = cl;
  695. tcp_count++;
  696. return(conn->ci);
  697. }
  698. }
  699. }
  700. /*
  701. * Get a client handle to a server that supports the highest
  702. * version between the given range.
  703. */
  704. static CSA_return_code
  705. get_client_handle(
  706. const char *host,
  707. const u_long prognum,
  708. u_long *vers_outp,
  709. const u_long vers_low,
  710. const u_long vers_high,
  711. char *nettype,
  712. CLIENT **clnt)
  713. {
  714. CLIENT *cl;
  715. u_int vers;
  716. struct timeval tv;
  717. enum clnt_stat status;
  718. tv.tv_sec = 1;
  719. tv.tv_usec = 0;
  720. *clnt = NULL;
  721. for (vers = vers_high; vers >= vers_low; vers--) {
  722. if ((cl = clnt_create(host, prognum, vers, nettype)) != NULL) {
  723. clnt_control(cl, CLSET_TIMEOUT, (char *)&tv);
  724. status = clnt_call(cl, 0, (xdrproc_t) xdr_void,
  725. (char *)NULL, (xdrproc_t) xdr_void,
  726. (char *)NULL, tv);
  727. if (status == RPC_SUCCESS) {
  728. *vers_outp = vers;
  729. *clnt = cl;
  730. return (CSA_SUCCESS);
  731. } else if (status != RPC_PROGVERSMISMATCH) {
  732. return (_DtCm_clntstat_to_csastat(status));
  733. }
  734. } else {
  735. _DtCm_print_errmsg(clnt_spcreateerror((char *) host));
  736. return (_DtCm_clntstat_to_csastat(rpc_createerr.cf_stat));
  737. }
  738. }
  739. /* cannot find a server that supports a version in the given range */
  740. /* Probably will never get here */
  741. return (CSA_E_SERVICE_UNAVAILABLE);
  742. }
  743. static CSA_return_code
  744. regstat4_to_dtcmstatus(Registration_Status_4 stat4)
  745. {
  746. switch (stat4) {
  747. case registered_4:
  748. return (CSA_SUCCESS);
  749. case deregistered_4:
  750. return (CSA_SUCCESS);
  751. case reg_notable_4:
  752. return (CSA_E_CALENDAR_NOT_EXIST);
  753. case failed_4:
  754. case confused_4:
  755. return (CSA_E_FAILURE);
  756. }
  757. }