Apache2-AuthenNTLM

 view release on metacpan or  search on metacpan

smb/smbval/rfcnb-io.c  view on Meta::CPAN

  }

#ifdef RFCNB_DEBUG
  fprintf(stderr, "Frags = %i, tot_sent = %i\n", i, tot_sent);
#endif

  /* Set up an alarm if timeouts are set ... */

  if (RFCNB_Timeout > 0) 
    alarm(RFCNB_Timeout);

  if ((len_sent = writev(con -> fd, io_list, i)) < 0) { /* An error */

    con -> rfc_errno = errno;
    if (errno == EINTR)  /* We were interrupted ... */
      RFCNB_errno = RFCNBE_Timeout;
    else
      RFCNB_errno = RFCNBE_BadWrite;
    RFCNB_saved_errno = errno;
    return(RFCNBE_Bad);

  }

  if (len_sent < tot_sent) { /* Less than we wanted */
    if (errno == EINTR)      /* We were interrupted */
      RFCNB_errno = RFCNBE_Timeout;
    else
      RFCNB_errno = RFCNBE_BadWrite;
    RFCNB_saved_errno = errno;
    return(RFCNBE_Bad);
  }

  if (RFCNB_Timeout > 0)
    alarm(0);                /* Reset that sucker */

#ifdef RFCNB_DEBUG

  fprintf(stderr, "Len sent = %i ...\n", len_sent);
  RFCNB_Print_Pkt(stderr, "sent", pkt, len_sent); /* Print what send ... */

#endif

  return(len_sent);

}

/* Read an RFCNB packet off the connection. 

   We read the first 4 bytes, that tells us the length, then read the
   rest. We should implement a timeout, but we don't just yet 

*/


int RFCNB_Get_Pkt(struct RFCNB_Con *con, struct RFCNB_Pkt *pkt, int len)

{ int read_len, pkt_len;
  char hdr[RFCNB_Pkt_Hdr_Len];      /* Local space for the header */
  struct RFCNB_Pkt *pkt_frag;
  int more, this_time, offset, frag_len, this_len;
  BOOL seen_keep_alive = TRUE;

  /* Read that header straight into the buffer */

  if (len < RFCNB_Pkt_Hdr_Len) { /* What a bozo */

#ifdef RFCNB_DEBUG
    fprintf(stderr, "Trying to read less than a packet:");
    perror("");
#endif
    RFCNB_errno = RFCNBE_BadParam;
    return(RFCNBE_Bad);

  }

  /* We discard keep alives here ... */

  if (RFCNB_Timeout > 0) 
    alarm(RFCNB_Timeout);

  while (seen_keep_alive) {

    if ((read_len = read(con -> fd, hdr, sizeof(hdr))) < 0) { /* Problems */
#ifdef RFCNB_DEBUG
      fprintf(stderr, "Reading the packet, we got:");
      perror("");
#endif
      if (errno == EINTR)
	RFCNB_errno = RFCNBE_Timeout;
      else
	RFCNB_errno = RFCNBE_BadRead;
      RFCNB_saved_errno = errno;
      return(RFCNBE_Bad);

    }

    /* Now we check out what we got */

    if (read_len == 0) { /* Connection closed, send back eof?  */

#ifdef RFCNB_DEBUG
      fprintf(stderr, "Connection closed reading\n");
#endif 

      if (errno == EINTR) 
	RFCNB_errno = RFCNBE_Timeout;
      else
	RFCNB_errno = RFCNBE_ConGone;
      RFCNB_saved_errno = errno;
      return(RFCNBE_Bad);

    }

    if (RFCNB_Pkt_Type(hdr) == RFCNB_SESSION_KEEP_ALIVE) {

#ifdef RFCNB_DEBUG
      fprintf(stderr, "RFCNB KEEP ALIVE received\n");
#endif
      
    }
    else {
      seen_keep_alive = FALSE;
    }

  }
 
  /* What if we got less than or equal to a hdr size in bytes? */

  if (read_len < sizeof(hdr)) { /* We got a small packet */

    /* Now we need to copy the hdr portion we got into the supplied packet */

    memcpy(pkt -> data, hdr, read_len);  /*Copy data */

#ifdef RFCNB_DEBUG
    RFCNB_Print_Pkt(stderr, "rcvd", pkt, read_len);
#endif

    return(read_len);

  }

  /* Now, if we got at least a hdr size, alloc space for rest, if we need it */

  pkt_len = RFCNB_Pkt_Len(hdr);

#ifdef RFCNB_DEBUG
  fprintf(stderr, "Reading Pkt: Length = %i\n", pkt_len);
#endif  

  /* Now copy in the hdr */

  memcpy(pkt -> data, hdr, sizeof(hdr));

  /* Get the rest of the packet ... first figure out how big our buf is? */
  /* And make sure that we handle the fragments properly ... Sure should */
  /* use an iovec ...                                                    */

  if (len < pkt_len)            /* Only get as much as we have space for */
    more = len - RFCNB_Pkt_Hdr_Len;
  else
    more = pkt_len;

  this_time = 0;

  /* We read for each fragment ... */

  if (pkt -> len == read_len){     /* If this frag was exact size */
    pkt_frag = pkt -> next;        /* Stick next lot in next frag */
    offset = 0;                    /* then we start at 0 in next  */
  }
  else {
    pkt_frag = pkt;                /* Otherwise use rest of this frag */
    offset = RFCNB_Pkt_Hdr_Len;    /* Otherwise skip the header       */
  }

  frag_len = pkt_frag -> len;

  if (more <= frag_len)     /* If len left to get less than frag space */
    this_len = more;        /* Get the rest ...                        */
  else
    this_len = frag_len - offset;



( run in 0.661 second using v1.01-cache-2.11-cpan-df04353d9ac )