summaryrefslogtreecommitdiff
path: root/com32/libupload/upload_tftp.c
blob: 387113ba0d41e1fae7c5480005376b52def3f433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * TFTP data output backend
 */

#include <string.h>
#include <stdio.h>
#include <syslinux/pxe.h>
#include <syslinux/config.h>
#include <netinet/in.h>
#include <sys/times.h>
#include <fs/pxe/pxe.h>
#include <fs/pxe/url.h>
#include "upload_backend.h"

const char *tftp_string_error_message[]={
"Unknown error",
"File not found",
"Access Denied",
"Disk Full",
"Illegal Operation",
"Unknown Transfert ID",
"File already exists",
"Unknown User",
"Negociation failed",
"Unable to resolve hostname", // not in RFC
"Unable to connect", // not in RFC
"No Error",
};

static int upload_tftp_write(struct upload_backend *be) {
    const union syslinux_derivative_info *sdi =
	syslinux_derivative_info();
    struct url_info url;
    struct inode inode;
    char url_path[255] = {0};
    uint32_t ip;
    int err;

    if (be->argv[1]) {
        ip = pxe_dns(be->argv[1]);
        if (!ip) {
            dprintf("\nUnable to resolve hostname: %s\n", be->argv[1]);
            return -TFTP_ERR_UNABLE_TO_RESOLVE;
        }
    } else {
        ip   = sdi->pxe.ipinfo->serverip;
        if (!ip) {
            dprintf("\nNo server IP address\n");
            return -TFTP_ERR_UNABLE_TO_CONNECT;
        }
    }

    snprintf(url_path, sizeof(url_path), "tftp://%u.%u.%u.%u/%s",
	((uint8_t *)&ip)[0],
	((uint8_t *)&ip)[1],
	((uint8_t *)&ip)[2],
	((uint8_t *)&ip)[3],
	be->argv[0]);

    parse_url(&url, url_path);
    url.ip = ip;

    dprintf("Connecting to %s to send %s\n", url.host, url.path);
    err = tftp_put(&url, 0, &inode, NULL, be->outbuf, be->zbytes);

    if (-err != TFTP_OK)
	printf("upload_tftp_write: TFTP server returned error %d : %s\n", err, tftp_string_error_message[-err]);

    return -err;
}

struct upload_backend upload_tftp = {
    .name       = "tftp",
    .helpmsg    = "filename [tftp_server]",
    .minargs    = 1,
    .write      = upload_tftp_write,
};