/**********************************************************************
 * ti library's routine for closing a connection
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/ti_close.c,v $
 * $Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/ti_close.c,v 0.1 92/03/12 15:27:23 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid_ti_close_c[] = "$Header: /afs/net.mit.edu/project/net_dev/brlewis/src/lib/RCS/ti_close.c,v 0.1 92/03/12 15:27:23 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <ti/memory.h>
#include <ti/ti.h>

/**********************************************************************
 * ti_close(TI_H hti)
 *	caller previously opened tp
 *
 * - if this is the last reference to this connection,
 *	frees memory
 *	closes socket
 **********************************************************************/

/* jms 7/13/92 -- windows-izing completed, pending approval */

void
ti_close(hti)
     TI_h hti;
{
  TI_LP lpti;

  lpti = TI_DEREFERENCE(hti);

  /* decrement reference count */
  lpti->links--;
  if (lpti->links) goto CLEANUP;

  /* free memory */
  Delete(ti_banner(lpti));

  /* close connection */
  if (lpti->sock > 0)
    {
      close(lpti->sock);
      lpti->sock = -1;
    }
CLEANUP:
  TI_REMOVE_DEREFERENCE(hti, lpti);
  return;
}
