Beispiel #1
0
int main (int argc, char **argv)
{

    char *fname = NULL;
    char *hostname = NULL;
    char *progname = argv[0];
    int retval = -1;
    int type = -1;
    struct tftp_conn *tc;

    /* Check whether the user wants to put or get a file. */
    while (argc > 0) {

        if (strcmp("-g", argv[0]) == 0) {
            fname = argv[1];
            hostname = argv[2];

            type = TFTP_TYPE_GET;
            break;
        } else if (strcmp("-p", argv[0]) == 0) {
            fname = argv[1];
            hostname = argv[2];

            type = TFTP_TYPE_PUT;
            break;

        }
        argc--;
        argv++;
    }

    /* Print usage message */
    if (!fname || !hostname) {
        fprintf(stderr, "Usage: %s [-g|-p] FILE HOST\n",
                progname);
        return -1;
    }

    /* Connect to the remote server */
    tc = tftp_connect(type, fname, MODE_OCTET, hostname);

    if (!tc) {
        fprintf(stderr, "Failed to connect!\n");
        return -1;
    }

    /* Transfer the file to or from the server */
    retval = tftp_transfer(tc);

    if (retval < 0) {
        fprintf(stderr, "File transfer failed!\n");
    }

    /* We are done. Cleanup our state. */
    tftp_close(tc);

    return retval;
}
Beispiel #2
0
/*
 * Load the BOOT-file from TFTP server
 */
int tftp_boot_load (void)
{
  int rc = 0;

  /* Allocate socket and buffers
   */
  sock   = (sock_type*) malloc (sizeof(sock->udp));
  inbuf  = (struct tftphdr*) malloc (TFTP_HEADSIZE+SEGSIZE);
  outbuf = (struct tftphdr*) malloc (TFTP_HEADSIZE+SEGSIZE);

  if (!sock || !inbuf || !outbuf)
  {
    outsnl (_LANG("No memory for TFTP boot."));
    return (0);
  }

  if (!tftp_boot_remote_file)
  {
    outsnl (_LANG("No remote TFTP boot filename defined."));
    return (0);
  }

  if (tftp_server_name[0] && !tftp_server)
     tftp_server = resolve (tftp_server_name);

  if (!tftp_server)
  {
    outsnl (_LANG("Cannot resolve TFTP-server "));
    return (0);
  }

  if (debug_on)
     outs (_LANG("Doing TFTP boot load..."));

  /* Open connection and request file
   */
  if (!tftp_open (tftp_server, tftp_boot_remote_file))
  {
    tftp_close();
    return (0);
  }

  while (1)
  {
    const char *buf;
    int   size = tftp_get_block (&buf);

    if (size < 0)  /* error in transfer */
    {
      rc = 0;
      break;
    }
    if (size > 0 && (*tftp_writer)(buf,size) < 0)
    {
      rc = -1;     /* writer failed, errno set */
      break;
    }
    if (size < blocksize)    /* got last block */
    {
      rc = 1;
      break;
    }
  }
  tftp_close();
  return (rc);
}