summaryrefslogtreecommitdiff
path: root/com32/libupload/upload_tftp.c
blob: 1e0b0702f35567a4f70ed84a2f4bc9d63a602f1a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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 <fs/pxe/tftp.h>
#include "upload_backend.h"

const char *tftp_string_error_message[]={
    "Unknown error",
    "File not found",
    "Access Denied",
    "Disk Full",
    "Illegal Operation",
    "Unknown Transfer ID",
    "File already exists",
    "Unknown User",
    "Negotiation failed",

    /* These are not in any RFC, defined internally */
    "Unable to resolve hostname",
    "Unable to connect",
    "No Error",
    "Network unavailable",
};

static bool have_real_network(void);

static int upload_tftp_write(struct upload_backend *be) {
    struct url_info url;
    struct inode inode;
    char url_path[255];
    int err;

    if (!have_real_network()) {
	dprintf("\nNot running from the network\n");
	return -TFTP_ERR_NO_NETWORK;
    }

    if (!strncmp(be->argv[0], "tftp://", 7))
	strlcpy(url_path, be->argv[0], sizeof(url_path));
    else
	snprintf(url_path, sizeof(url_path), "tftp://%s/%s",
		 be->argv[1] ? be->argv[1] : "", be->argv[0]);

    parse_url(&url, url_path);
    err = -url_set_ip(&url);
    if (err != TFTP_ERR_OK)
	return err;

    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_ERR_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,
};

/*
 * Dummy functions to prevent link failure for non-network cores
 */
static int _dummy_tftp_put(struct url_info *url, int flags,
			   struct inode *inode, const char **redir,
			   char *data, int data_length)
{
    (void)url;
    (void)flags;
    (void)inode;
    (void)redir;
    (void)data;
    (void)data_length;

    return -TFTP_ERR_NO_NETWORK;
}

__weak int __attribute__((alias("_dummy_tftp_put")))
tftp_put(struct url_info *url, int flags, struct inode *inode,
	 const char **redir, char *data, int data_length);

static int _dummy_tftp_put(struct url_info *url, int flags,
			   struct inode *inode, const char **redir,
			   char *data, int data_length);

static bool have_real_network(void)
{
    return tftp_put != _dummy_tftp_put;
}

__weak int url_set_ip(struct url_info *ui)
{
    (void)ui;

    return -TFTP_ERR_NO_NETWORK;
}

__weak void parse_url(struct url_info *ui, char *url)
{
    (void)ui;
    (void)url;
}