Merge pull request #65 from widgetii/master

Improve xmdp output
pull/66/head
Igor Zalatov 2021-11-04 17:25:07 +03:00 committed by GitHub
commit a9bc4e8e3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 25 deletions

View File

@ -4,8 +4,10 @@
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include "cjson/cJSON.h" #include "cjson/cJSON.h"
#include "netip.h" #include "netip.h"
@ -43,8 +45,9 @@ typedef union netip_pkt {
#define NETIP_HSIZE sizeof(netip_preabmle_t) #define NETIP_HSIZE sizeof(netip_preabmle_t)
#define NETIP_MAX_JSON sizeof(resp) - NETIP_HSIZE - 1 #define NETIP_MAX_JSON sizeof(resp) - NETIP_HSIZE - 1
bool netip_connect(const char *addr, uint16_t port) { enum ConnectStatus netip_connect(const char *addr, uint16_t port) {
bool res = true; bool res = CONNECT_OK;
cJSON *json = NULL;
int s = socket(AF_INET, SOCK_STREAM, 0); int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) if (s == -1)
@ -55,8 +58,22 @@ bool netip_connect(const char *addr, uint16_t port) {
srv.sin_family = AF_INET; srv.sin_family = AF_INET;
srv.sin_port = htons(port); srv.sin_port = htons(port);
if (connect(s, (struct sockaddr *)&srv, sizeof(srv)) < 0) const int flags = fcntl(s, F_GETFL, 0);
return false; fcntl(s, F_SETFL, flags | O_NONBLOCK);
(void)connect(s, (struct sockaddr *)&srv, sizeof(srv));
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(s, &fdset);
struct timeval tv = {
.tv_sec = 2, /* 2 second timeout */
};
if (select(s + 1, NULL, &fdset, NULL, &tv) != 1) {
res = CONNECT_ERR;
goto quit;
}
fcntl(s, F_SETFL, flags);
netip_pkt_t msg; netip_pkt_t msg;
memset(&msg.header, 0, sizeof(msg.header)); memset(&msg.header, 0, sizeof(msg.header));
@ -69,29 +86,30 @@ bool netip_connect(const char *addr, uint16_t port) {
msg.header.len_data = sizeof(default_login); msg.header.len_data = sizeof(default_login);
if (send(s, &msg, sizeof(default_login) + NETIP_HSIZE, 0) < 0) { if (send(s, &msg, sizeof(default_login) + NETIP_HSIZE, 0) < 0) {
puts("Send failed"); return CONNECT_ERR;
return false;
} }
if (recv(s, &msg, sizeof(msg), 0) <= NETIP_HSIZE) { if (recv(s, &msg, sizeof(msg), 0) <= NETIP_HSIZE) {
puts("recv failed"); puts("recv failed");
} }
cJSON *json = cJSON_Parse(msg.header.data); json = cJSON_Parse(msg.header.data);
if (!json) { if (!json) {
const char *error_ptr = cJSON_GetErrorPtr(); const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) { if (error_ptr != NULL) {
fprintf(stderr, "Error before: %s\n", error_ptr); fprintf(stderr, "Error before: %s\n", error_ptr);
} }
res = false; res = CONNECT_ERR;
goto skip_loop; goto quit;
} }
const int retval = get_json_intval(json, "Ret", 0); const int retval = get_json_intval(json, "Ret", 0);
if (retval != RESULT_OK) if (retval != RESULT_OK)
return false; return CONNECT_PWDREQ;
skip_loop: quit:
if (json)
cJSON_Delete(json); cJSON_Delete(json);
close(s);
return res; return res;
} }

View File

@ -1,6 +1,12 @@
#ifndef NETIP_H #ifndef NETIP_H
#define NETIP_H #define NETIP_H
bool netip_connect(const char* addr, uint16_t port); enum ConnectStatus {
CONNECT_OK,
CONNECT_ERR,
CONNECT_PWDREQ,
};
enum ConnectStatus netip_connect(const char *addr, uint16_t port);
#endif /* NETIP_H */ #endif /* NETIP_H */

View File

@ -26,6 +26,18 @@ const char brpkt[] =
"\x00\x00\x00\x00"; "\x00\x00\x00\x00";
static const char *Reset = "\x1b[0m"; static const char *Reset = "\x1b[0m";
static const char *FgRed = "\x1b[31m"; static const char *FgRed = "\x1b[31m";
static const char *FgBrightRed = "\033[31;1m";
static const char *color(enum ConnectStatus status) {
switch (status) {
case CONNECT_OK:
return FgRed;
case CONNECT_ERR:
return FgBrightRed;
default:
return "";
}
}
// get sockaddr, IPv4 or IPv6: // get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa) { void *get_in_addr(struct sockaddr *sa) {
@ -175,7 +187,7 @@ int main() {
ipaddr_from32bit(abuf, sizeof abuf, host_ip); ipaddr_from32bit(abuf, sizeof abuf, host_ip);
host_ip = abuf; host_ip = abuf;
} }
bool netip_ok = netip_connect(host_ip, netip_port); enum ConnectStatus netip_conn = netip_connect(host_ip, netip_port);
char verstr[128] = {0}; char verstr[128] = {0};
if (strlen(version)) { if (strlen(version)) {
@ -191,23 +203,21 @@ int main() {
version++; version++;
} }
if (strlen(builddt)) { if (strlen(builddt) == 19 && builddt[10] == ' ') {
const char *end; const char *end = builddt + 10;
if ((end = strchr(builddt, ' '))) {
strcat(verstr + strlen(verstr), " ("); strcat(verstr + strlen(verstr), " (");
snprintf(verstr + strlen(verstr), snprintf(verstr + strlen(verstr),
MIN(sizeof(verstr) - strlen(verstr), end - builddt + 1), MIN(sizeof(verstr) - strlen(verstr), end - builddt + 1), "%s",
"%s", builddt); builddt);
strcat(verstr + strlen(verstr), ")"); strcat(verstr + strlen(verstr), ")");
} }
} }
}
printf("%s%s\t%s\t%s %s, %s", netip_ok ? FgRed : "", host_ip, mac, printf("%s%s\t%s\t%s %s, %s", color(netip_conn), host_ip, mac,
chan_num > 1 ? "DVR" : "IPC", sn, hostname); chan_num > 1 ? "DVR" : "IPC", sn, hostname);
if (strlen(verstr)) if (strlen(verstr))
printf("\t%s", verstr); printf("\t%s", verstr);
printf("%s\n", netip_ok ? Reset : ""); printf("%s\n", *color(netip_conn) ? Reset : "");
skip_loop: skip_loop: