예제 #1
0
파일: tftp.c 프로젝트: nejsimon/tftp-labb
/*
  Send a read request to the server.
  1. Format message.
  2. Send the request using the connection handle.
  3. Return the number of bytes sent, or negative on error.
 */
int tftp_send_rrq(struct tftp_conn *tc)
{
    /* struct tftp_rrq *rrq; */

    int reqlen = TFTP_RRQ_LEN(tc->fname, tc->mode);

    struct tftp_rrq *rrq;

    if((rrq = malloc(reqlen)) == NULL)
        return -1;

    rrq->opcode = htons(OPCODE_RRQ);

    strcpy (&rrq->req[0], tc->fname);
    strcpy (&rrq->req[strlen(tc->fname) + 1], tc->mode);

    // Save the message in the msgbuffer
    memcpy(tc->msgbuf, rrq, reqlen);

    size_t size = sendto(tc->sock, rrq, reqlen, 0, (struct sockaddr *) &tc->peer_addr, tc->addrlen);

    free(rrq);

    print_message((struct tftp_msg *) tc->msgbuf, 0);

    return size;
}
예제 #2
0
/*
  Send a read request to the server
  1. Format message
  2. Send the request using the connection handle
  3. Return the number of bytes sent, or negative on error
*/
int tftp_send_rrq(struct tftp_conn *tc)
{
  /* ===ADDED/CHANGED=== */
  struct tftp_rrq *rrq; 
  rrq = malloc(TFTP_RRQ_LEN(tc->fname,tc->mode));

  rrq->opcode = htons(OPCODE_RRQ);
  int index = sprintf(rrq->req, "%s", tc->fname);
  index++;
  sprintf((rrq->req + index), "%s", tc->mode);

  int bytes_sent = sendto(tc->sock, rrq, TFTP_RRQ_LEN(tc->fname, tc->mode), 0,
			  (struct sockaddr *) &(tc->peer_addr), tc->addrlen);

  free(rrq);
  return bytes_sent;
  /* ===END OF ADDED/CHANGED=== */
}