summaryrefslogtreecommitdiff
path: root/url_handler/handler.cpp
blob: b202d5b0d94afded159f9fad18be3026c2dd0f4d (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <fstream>
#include <filesystem>
#include <unordered_map>
#include <mutex>
#include <thread>
#include <memory>
#include <cstdlib>
#include "handler.h"

using namespace std;


static string shell_escape(const string &str) {
	string res;
	res.reserve(str.size() + 16);
	res += '\'';

	for (char c : str) {
		if (c == '\'') res += "'\"'\"'";
		else res += c;
	}

	res += '\'';
	return res;
}

static string read_file(const string &fname) {
	ifstream f(fname);
	if (!f) return {};

	string contents;
	char buffer[4096];
	while (true) {
		f.read(buffer, sizeof buffer);
		contents.insert(contents.size(), buffer, f.gcount());
		if (!f) break;
	}

	return contents;
}

static string download_url(const string &url) {
	string tempdir_name = "/tmp/tmp.url_handler.XXXXXX";
	if (mkdtemp(tempdir_name.data()) == nullptr) {
		perror("mkdtemp");
		return {};
	}

	string tempfile_name = tempdir_name + "/file";

	string cmd = "curl -sL " + shell_escape(url) + " >" + shell_escape(tempfile_name);
	// fprintf(stderr, "cmd = %s\n", cmd.data());

	system(cmd.data());

	string response = read_file(tempfile_name);
	filesystem::remove_all(tempdir_name);

	fprintf(stderr, "Received response for url '%s'\n", url.data());

	return response;
}

struct State {
	string url;

	thread download_thread;

	// mutex protects 'response' and 'response_present'; the download thread
	// sets 'response_present' to true and simultaneously 'response' to the
	// right value. After 'response_present' can be observed to be true by the
	// main thread, the download thread doesn't touch anything anymore.
	mutex res_mt;
	bool response_present = false;
	string response;

	// owned by host thread
	size_t rescur = 0;
};

static unordered_map<uint32_t, unique_ptr<State>> state_map;

uint16_t handle_icmp(uint32_t source_addr, const array<uint8_t, 16> &payload) {
	auto state_it = state_map.find(source_addr);
	if (state_it == state_map.end()) {
		state_it = state_map.emplace(source_addr, make_unique<State>()).first;
	}
	State &state = *state_it->second;

	switch (payload[0]) {
		case 100:  // url fragment
			// fprintf(stderr, "msg(%u): url fragment\n", source_addr);
			{
				lock_guard<mutex> guard(state.res_mt);
				if (state.response_present) return 0;
			}
			for (size_t i = 1; i < 16; i++) {
				if (payload[i] == '\0') {
					fprintf(stderr, "URL received: <%s>\n", state.url.data());
					state.download_thread = thread([&state]() {
						string response = download_url(state.url);

						{
							lock_guard<mutex> guard(state.res_mt);
							state.response = move(response);
							state.response_present = true;
						}

						// fprintf(stderr, "[dlth] response received: <%s>\n", state.response.data());
					});
					break;
				}
				state.url.push_back(payload[i]);
			}
			return 1;

		case 101: {  // query whether response is ready
			// fprintf(stderr, "msg(%u): ready query\n", source_addr);
			lock_guard<mutex> guard(state.res_mt);
			return state.response_present ? 1 : 0;
		}

		case 102: {  // get response
			// fprintf(stderr, "msg(%u): get_response\n", source_addr);
			{
				lock_guard<mutex> guard(state.res_mt);
				if (!state.response_present) return 0;
			}

			uint16_t retval = 0;
			for (size_t i = 0; i < 2 && state.rescur < state.response.size(); i++) {
				retval |= (uint16_t)state.response[state.rescur] << (8 * i);
				state.rescur++;
			}

			if (state.rescur == state.response.size()) {
				state.download_thread.join();  // should already have exited
				state_map.erase(state_it);
			}
			// fprintf(stderr, "  -> retval %016X\n", (unsigned)retval);
			return retval;
		}
	}

	return 0;
}