-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw.c
315 lines (280 loc) · 8.01 KB
/
hw.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*************************
* _ _
*| | | |
*| |__ __ _ __| |
*| '_ \ / _` |/ _` |
*| |_) | (_| | (_| |
*|_.__/ \__,_|\__,_|
* _____ __
* | _ |/ _|
* ___| |/' | |_ ___ ___
* / __| /| | _/ _ \/ _ \
*| (__\ |_/ / || __/ __/
* \___|\___/|_| \___|\___|
*
************************/
/*SINGLE LINE MAKE FOR QEMU ARM GUEST:
/home/sgc/buildroot-2022.11/output/host/usr/bin/arm-linux-gcc --sysroot=/home/sgc/buildroot-2022.11/output/staging hw.c -o hw_oneliner -lcurl -lc
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h> /*someimtes required if unistd.h alone doesn't work*/
#include <curl/curl.h>
#include <string.h>
#include <errno.h> /* NOTE: using for debugging, consider removing before deploy*/
/*NOTE: debug & dev comments are denoted 'NOTE', clean them up before deploying*/
/* GLOBAL CONSTANTS */
#define OK 0
#define INIT_ERR 0
#define REQ_ERR 0
//#define URL "http://192.168.1.1:8000"
// NOTE: may need to use ' --resolve example.com:443:127.0.0.1' method if trouble parsing 'localhost' later
/* GLOBAL VARIABLES */
struct flags
{
int g;
int o;
int p;
int d;
int h;
int v;
int u;
};
/* GLOBAL FUNCTIONS */
/*CURL modes, mutually exclusive, select one*/
void curl_get(char *url, CURL *curl, CURLcode res) {
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
/*GET is default mode, no special options necessary*/
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "[CURL] Could not complete: %s\n", curl_easy_strerror(res));
}
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
}
void curl_post(char *url, char *message, CURL *curl, CURLcode res) {
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
/*POST mode options*/
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(message));
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "[CURL] Could not complete: %s\n", curl_easy_strerror(res));
}
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
}
void curl_put(char *url, char *message, CURL *curl, CURLcode res) {
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
/*PUT mode options*/
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "[CURL] Could not complete: %s\n", curl_easy_strerror(res));
}
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
}
void curl_delete(char *url, char *message, CURL *curl, CURLcode res) {
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
/*DELETE mode options*/
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "[CURL] Could not complete: %s\n", curl_easy_strerror(res));
}
if(res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl);
}
}
/* Other GLOBAL functions, non-curl */
void hw_help() {
printf("Usage: hw [OPTION] -u, --url <URL>\n");
printf(" -g, --get Retrieve contents at <URL>\n");
printf(" -o, --post POST message to <URL>\n");
printf(" -p, --put PUT contents to <URL>\n");
printf(" -d, --delete Delete string from \n");
printf(" -v, --version Print current version number\n");
printf(" -h, --Print help document \n");
}
void hw_usage() {
printf("Usage: hw [OPTION] -u, --url <URL>\n");
}
void hw_version() {
printf("VERSION: 06.01.1991 \n");
}
/************************************/
int main(int argc, char **argv) {
int long_index=0;
/* ARGUMENT FLAGS */
struct flags flags;
int index;
/*Default flags values*/
flags.g = 0;
flags.o = 0;
flags.p = 0;
flags.d = 0;
flags.u = 0;
flags.h = 0;
flags.v = 0;
/*CURL inputs*/
int c;
// char *message;
char message[100]={0};
char *url = NULL;
CURL *curl;
CURLcode res=0;
curl = curl_easy_init();
//DEFINE ARGUMENTS
static struct option long_options[] = {
{"get", no_argument, 0, 'g'},
{"post", no_argument, 0, 'o'},
{"put", no_argument, 0, 'p'},
{"delete", no_argument, 0, 'd'},
{"url", required_argument, 0, 'u'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0 , 0, 0 }
};
// COLLECT ARGUMENTS
//c = getopt_long(argc, argv, "gopdu:hv", long_options, &long_index);
while ((c = getopt_long(argc, argv, "gopdu:hv", long_options, &long_index)) != -1){
switch (c) {
case 'g':
flags.g = 1;
printf("option -g --get set. Setting flags.g = 1 \n");
break;
case 'o':
flags.o = 1;
printf("option -o --put set. Setting flags.o = 1 \n");
break;
case 'p':
flags.p = 1;
printf("option -p --post set. Setting flags.p = 1 \n");
break;
case 'd':
flags.d = 1;
printf("option -d --delete set. Setting flags.d = 1 \n");
break;
case 'u':
flags.u = 1;
url = optarg;
printf("option u with value '%s'\n", url);
break;
case 'h':
printf("option h chosen\n");
hw_help();
break;
case 'v':
printf("option v chosen\n");
hw_version();
break;
case '?':
printf("Unknown option: %c\n", optopt);
default:
hw_usage();
printf("?? getopt.h returned error code 0%o ??\n", c);
exit(EXIT_FAILURE);
}
}
// NON-OPTION ARGUMENT STRING PARSING
for (index = optind; index < argc; index++)
{
printf("\nnon-option argument string: %s\n", argv[index]);
strncat(message, argv[index], 10);
}
printf("\nAssembled String: %s\n", message);
// VALIDATE & PROCESS ARGUMENTS - CURL COMMANDS FIRST THEN HELP, USAGE, VERSION
if ((flags.g + flags.o + flags.p + flags.d)==0){
printf("\nNO MODES SELECTED\n\n");
hw_usage;
exit(1);
}
if (flags.g == 1){
if ((flags.g + flags.o + flags.p + flags.d) > 1){
printf("\nWARNING:Too many options chosen!\n");
hw_usage;
exit(1);
}
if (flags.u != 1){
printf("\nWARNING: No URL specified!\n");
}
else {
printf("\n...running curl_get\n");
printf("\nURL: %s\n", url);
curl_get(url, curl, res);
printf("\nCURL response code: %d\n", res);
return 0; /*exit program, succeeded*/
}
}
if (flags.o == 1){
if ((flags.g + flags.o + flags.p + flags.d) > 1){
printf("\nWARNING:Too many options chosen!\n");
hw_usage;
exit(1);
} else {
printf("\n...running curl_post()\n");
curl_post(url, message, curl, res);
printf("\nCURL response code: %d\n", res);
return 0;
}
}
if (flags.p == 1){
if ((flags.g + flags.o + flags.p + flags.d) > 1){
printf("\nWARNING:Too many options chosen!\n");
hw_usage;
exit(1);
} else {
printf("\n...running curl_put()\n");
curl_put(url, message, curl, res);
printf("\nCURL response code: %d\n", res);
return 0;
}
}
if (flags.d == 1){
if ((flags.g + flags.o + flags.p + flags.d) > 1){
printf("\nWARNING:Too many options chosen!\n");
hw_usage;
exit(1);
} else {
printf("\n...running curl_delete()\n");
curl_delete(url, message, curl, res);
printf("\nCURL response code: %d\n", res);
return 0;
}
}
if (flags.h == 1){
hw_help();
}
if (flags.v == 1){
hw_version();
}
// else {
// printf("WARNING: No modes selected!\n");
//}
return 0;
}