mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
Submodule src/buffer updated: 99adb71b0b...2fb5523d44
@@ -102,12 +102,18 @@ static const char *cmd_lookup(const char *cmd)
|
||||
static const char *cmderr2str(int err)
|
||||
{
|
||||
switch (err) {
|
||||
case RTTY_CMD_ERR_PERMIT: return "operation not permitted";
|
||||
case RTTY_CMD_ERR_NOT_FOUND: return "not found";
|
||||
case RTTY_CMD_ERR_NOMEM: return "no mem";
|
||||
case RTTY_CMD_ERR_SYSERR: return "sys error";
|
||||
case RTTY_CMD_ERR_RESP_TOOBIG: return "stdout+stderr is too big";
|
||||
default: return "";
|
||||
case RTTY_CMD_ERR_PERMIT:
|
||||
return "operation not permitted";
|
||||
case RTTY_CMD_ERR_NOT_FOUND:
|
||||
return "not found";
|
||||
case RTTY_CMD_ERR_NOMEM:
|
||||
return "no mem";
|
||||
case RTTY_CMD_ERR_SYSERR:
|
||||
return "sys error";
|
||||
case RTTY_CMD_ERR_RESP_TOOBIG:
|
||||
return "stdout+stderr is too big";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +151,7 @@ static void cmd_err_reply(struct rtty *rtty, const char *token, int err)
|
||||
char str[256] = "";
|
||||
|
||||
snprintf(str, sizeof(str) - 1, "{\"token\":\"%s\","
|
||||
"\"attrs\":{\"err\":%d,\"msg\":\"%s\"}}", token, err, cmderr2str(err));
|
||||
"\"attrs\":{\"err\":%d,\"msg\":\"%s\"}}", token, err, cmderr2str(err));
|
||||
|
||||
rtty_send_msg(rtty, MSG_TYPE_CMD, str, strlen(str));
|
||||
}
|
||||
@@ -168,7 +174,7 @@ static void cmd_reply(struct task *t, int code)
|
||||
pos = str;
|
||||
|
||||
ret = snprintf(pos, len, "{\"token\":\"%s\","
|
||||
"\"attrs\":{\"code\":%d,\"stdout\":\"", t->token, code);
|
||||
"\"attrs\":{\"code\":%d,\"stdout\":\"", t->token, code);
|
||||
|
||||
len -= ret;
|
||||
pos += ret;
|
||||
@@ -246,7 +252,7 @@ static void run_task(struct task *t)
|
||||
int err;
|
||||
|
||||
if (pipe2(opipe, O_CLOEXEC | O_NONBLOCK) < 0 ||
|
||||
pipe2(epipe, O_CLOEXEC | O_NONBLOCK) < 0) {
|
||||
pipe2(epipe, O_CLOEXEC | O_NONBLOCK) < 0) {
|
||||
log_err("pipe2 failed: %s\n", strerror(errno));
|
||||
err = RTTY_CMD_ERR_SYSERR;
|
||||
goto ERR;
|
||||
|
||||
135
src/list.h
135
src/list.h
@@ -43,70 +43,62 @@
|
||||
#endif
|
||||
|
||||
struct list_head {
|
||||
struct list_head *next;
|
||||
struct list_head *prev;
|
||||
struct list_head *next;
|
||||
struct list_head *prev;
|
||||
};
|
||||
|
||||
#define LIST_HEAD_INIT(name) { &(name), &(name) }
|
||||
#undef LIST_HEAD
|
||||
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
|
||||
|
||||
static inline void
|
||||
INIT_LIST_HEAD(struct list_head *list)
|
||||
static inline void INIT_LIST_HEAD(struct list_head *list)
|
||||
{
|
||||
list->next = list->prev = list;
|
||||
list->next = list->prev = list;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
list_empty(const struct list_head *head)
|
||||
static inline bool list_empty(const struct list_head *head)
|
||||
{
|
||||
return (head->next == head);
|
||||
return (head->next == head);
|
||||
}
|
||||
|
||||
static inline bool
|
||||
list_is_first(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
static inline bool list_is_first(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
{
|
||||
return list->prev == head;
|
||||
return list->prev == head;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
list_is_last(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
static inline bool list_is_last(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
{
|
||||
return list->next == head;
|
||||
return list->next == head;
|
||||
}
|
||||
|
||||
static inline void
|
||||
_list_del(struct list_head *entry)
|
||||
static inline void _list_del(struct list_head *entry)
|
||||
{
|
||||
entry->next->prev = entry->prev;
|
||||
entry->prev->next = entry->next;
|
||||
entry->next->prev = entry->prev;
|
||||
entry->prev->next = entry->next;
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_del(struct list_head *entry)
|
||||
static inline void list_del(struct list_head *entry)
|
||||
{
|
||||
_list_del(entry);
|
||||
entry->next = entry->prev = NULL;
|
||||
_list_del(entry);
|
||||
entry->next = entry->prev = NULL;
|
||||
}
|
||||
|
||||
static inline void
|
||||
_list_add(struct list_head *_new, struct list_head *prev,
|
||||
struct list_head *next)
|
||||
static inline void _list_add(struct list_head *_new, struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
|
||||
next->prev = _new;
|
||||
_new->next = next;
|
||||
_new->prev = prev;
|
||||
prev->next = _new;
|
||||
next->prev = _new;
|
||||
_new->next = next;
|
||||
_new->prev = prev;
|
||||
prev->next = _new;
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_del_init(struct list_head *entry)
|
||||
static inline void list_del_init(struct list_head *entry)
|
||||
{
|
||||
_list_del(entry);
|
||||
INIT_LIST_HEAD(entry);
|
||||
_list_del(entry);
|
||||
INIT_LIST_HEAD(entry);
|
||||
}
|
||||
|
||||
#define list_entry(ptr, type, field) container_of(ptr, type, field)
|
||||
@@ -135,74 +127,65 @@ list_del_init(struct list_head *entry)
|
||||
#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
|
||||
#define list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)
|
||||
|
||||
static inline void
|
||||
list_add(struct list_head *_new, struct list_head *head)
|
||||
static inline void list_add(struct list_head *_new, struct list_head *head)
|
||||
{
|
||||
_list_add(_new, head, head->next);
|
||||
_list_add(_new, head, head->next);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_add_tail(struct list_head *_new, struct list_head *head)
|
||||
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
|
||||
{
|
||||
_list_add(_new, head->prev, head);
|
||||
_list_add(_new, head->prev, head);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_move(struct list_head *list, struct list_head *head)
|
||||
static inline void list_move(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
_list_del(list);
|
||||
list_add(list, head);
|
||||
_list_del(list);
|
||||
list_add(list, head);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_move_tail(struct list_head *entry, struct list_head *head)
|
||||
static inline void list_move_tail(struct list_head *entry, struct list_head *head)
|
||||
{
|
||||
_list_del(entry);
|
||||
list_add_tail(entry, head);
|
||||
_list_del(entry);
|
||||
list_add_tail(entry, head);
|
||||
}
|
||||
|
||||
static inline void
|
||||
_list_splice(const struct list_head *list, struct list_head *prev,
|
||||
struct list_head *next)
|
||||
static inline void _list_splice(const struct list_head *list, struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
struct list_head *first;
|
||||
struct list_head *last;
|
||||
struct list_head *first;
|
||||
struct list_head *last;
|
||||
|
||||
if (list_empty(list))
|
||||
return;
|
||||
if (list_empty(list))
|
||||
return;
|
||||
|
||||
first = list->next;
|
||||
last = list->prev;
|
||||
first->prev = prev;
|
||||
prev->next = first;
|
||||
last->next = next;
|
||||
next->prev = last;
|
||||
first = list->next;
|
||||
last = list->prev;
|
||||
first->prev = prev;
|
||||
prev->next = first;
|
||||
last->next = next;
|
||||
next->prev = last;
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_splice(const struct list_head *list, struct list_head *head)
|
||||
static inline void list_splice(const struct list_head *list, struct list_head *head)
|
||||
{
|
||||
_list_splice(list, head, head->next);
|
||||
_list_splice(list, head, head->next);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_splice_tail(struct list_head *list, struct list_head *head)
|
||||
static inline void list_splice_tail(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
_list_splice(list, head->prev, head);
|
||||
_list_splice(list, head->prev, head);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_splice_init(struct list_head *list, struct list_head *head)
|
||||
static inline void list_splice_init(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
_list_splice(list, head, head->next);
|
||||
INIT_LIST_HEAD(list);
|
||||
_list_splice(list, head, head->next);
|
||||
INIT_LIST_HEAD(list);
|
||||
}
|
||||
|
||||
static inline void
|
||||
list_splice_tail_init(struct list_head *list, struct list_head *head)
|
||||
static inline void list_splice_tail_init(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
_list_splice(list, head->prev, head);
|
||||
INIT_LIST_HEAD(list);
|
||||
_list_splice(list, head->prev, head);
|
||||
INIT_LIST_HEAD(list);
|
||||
}
|
||||
|
||||
#endif /* _LINUX_LIST_H_ */
|
||||
|
||||
126
src/main.c
126
src/main.c
@@ -60,21 +60,21 @@ static struct option long_options[] = {
|
||||
static void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [option]\n"
|
||||
" -I, --id=string Set an ID for the device(Maximum 63 bytes, valid\n"
|
||||
" character:letter, number, underline and short line)\n"
|
||||
" -h, --host=string Server's host or ipaddr(Default is localhost)\n"
|
||||
" -p, --port=number Server port(Default is 5912)\n"
|
||||
" -d, --description=string Adding a description to the device(Maximum 126 bytes)\n"
|
||||
" -a Auto reconnect to the server\n"
|
||||
" -s SSL on\n"
|
||||
" -D Run in the background\n"
|
||||
" -t, --token=string Authorization token\n"
|
||||
" -f username Skip a second login authentication. See man login(1) about the details\n"
|
||||
" -R Receive file\n"
|
||||
" -S file Send file\n"
|
||||
" -v, --verbose verbose\n"
|
||||
" -V, --version Show version\n"
|
||||
" --help Show usage\n",
|
||||
" -I, --id=string Set an ID for the device(Maximum 63 bytes, valid\n"
|
||||
" character:letter, number, underline and short line)\n"
|
||||
" -h, --host=string Server's host or ipaddr(Default is localhost)\n"
|
||||
" -p, --port=number Server port(Default is 5912)\n"
|
||||
" -d, --description=string Adding a description to the device(Maximum 126 bytes)\n"
|
||||
" -a Auto reconnect to the server\n"
|
||||
" -s SSL on\n"
|
||||
" -D Run in the background\n"
|
||||
" -t, --token=string Authorization token\n"
|
||||
" -f username Skip a second login authentication. See man login(1) about the details\n"
|
||||
" -R Receive file\n"
|
||||
" -S file Send file\n"
|
||||
" -v, --verbose verbose\n"
|
||||
" -V, --version Show version\n"
|
||||
" --help Show usage\n",
|
||||
prog);
|
||||
exit(1);
|
||||
}
|
||||
@@ -100,55 +100,55 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
case 'I':
|
||||
rtty.devid = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
rtty.host = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
rtty.port = atoi(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
if (strlen(optarg) > 126) {
|
||||
log_err("Description too long\n");
|
||||
usage(argv[0]);
|
||||
}
|
||||
rtty.description = optarg;
|
||||
break;
|
||||
case 'a':
|
||||
rtty.reconnect = true;
|
||||
break;
|
||||
case 's':
|
||||
rtty.ssl_on = true;
|
||||
break;
|
||||
case 'D':
|
||||
background = true;
|
||||
break;
|
||||
case 't':
|
||||
rtty.token = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
rtty.username = optarg;
|
||||
break;
|
||||
case 'R':
|
||||
download_file();
|
||||
return 0;
|
||||
case 'S':
|
||||
upload_file(optarg);
|
||||
return 0;
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
case 'V':
|
||||
log_info("rtty version %s\n", RTTY_VERSION_STRING);
|
||||
exit(0);
|
||||
case LONG_OPT_HELP:
|
||||
case 'I':
|
||||
rtty.devid = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
rtty.host = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
rtty.port = atoi(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
if (strlen(optarg) > 126) {
|
||||
log_err("Description too long\n");
|
||||
usage(argv[0]);
|
||||
break;
|
||||
default: /* '?' */
|
||||
usage(argv[0]);
|
||||
break;
|
||||
}
|
||||
rtty.description = optarg;
|
||||
break;
|
||||
case 'a':
|
||||
rtty.reconnect = true;
|
||||
break;
|
||||
case 's':
|
||||
rtty.ssl_on = true;
|
||||
break;
|
||||
case 'D':
|
||||
background = true;
|
||||
break;
|
||||
case 't':
|
||||
rtty.token = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
rtty.username = optarg;
|
||||
break;
|
||||
case 'R':
|
||||
download_file();
|
||||
return 0;
|
||||
case 'S':
|
||||
upload_file(optarg);
|
||||
return 0;
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
case 'V':
|
||||
log_info("rtty version %s\n", RTTY_VERSION_STRING);
|
||||
exit(0);
|
||||
case LONG_OPT_HELP:
|
||||
usage(argv[0]);
|
||||
break;
|
||||
default: /* '?' */
|
||||
usage(argv[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -469,7 +469,7 @@ int rtty_start(struct rtty *rtty)
|
||||
ev_timer_start(rtty->loop, &rtty->tmr);
|
||||
|
||||
if (tcp_connect(rtty->loop, rtty->host, rtty->port, on_net_connected, rtty) < 0
|
||||
&& !rtty->reconnect)
|
||||
&& !rtty->reconnect)
|
||||
return -1;
|
||||
|
||||
start_file_service(&rtty->file_context);
|
||||
|
||||
@@ -94,14 +94,14 @@ int rtty_ssl_init(struct rtty_ssl_ctx **ctx, int sock, const char *host)
|
||||
mbedtls_ctr_drbg_seed(&c->drbg, mbedtls_entropy_func, &c->etpy, NULL, 0);
|
||||
|
||||
mbedtls_ssl_config_defaults(&c->cfg, MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
|
||||
|
||||
mbedtls_ssl_conf_authmode(&c->cfg, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
||||
mbedtls_ssl_conf_ca_chain(&c->cfg, &c->x509, NULL);
|
||||
mbedtls_ssl_conf_rng(&c->cfg, mbedtls_ctr_drbg_random, &c->drbg);
|
||||
|
||||
mbedtls_ssl_set_bio(&c->ssl, &c->net, mbedtls_net_send,
|
||||
mbedtls_net_recv, NULL);
|
||||
mbedtls_net_recv, NULL);
|
||||
mbedtls_ssl_set_hostname(&c->ssl, host);
|
||||
|
||||
mbedtls_ssl_setup(&c->ssl, &c->cfg);
|
||||
|
||||
Reference in New Issue
Block a user