--- embedaddon/mpd/src/web.c 2013/07/22 08:44:29 1.1.1.1 +++ embedaddon/mpd/src/web.c 2016/11/01 09:56:12 1.1.1.2 @@ -42,7 +42,8 @@ static void WebRunBinCmd(FILE *f, const char *query, int priv); static void WebRunCmd(FILE *f, const char *query, int priv); - static void WebShowSummary(FILE *f, int priv); + static void WebShowHTMLSummary(FILE *f, int priv); + static void WebShowJSONSummary(FILE *f, int priv); /* * GLOBAL VARIABLES @@ -217,7 +218,7 @@ static void WebShowCSS(FILE *f) { fprintf(f, "body {font-family: Arial, Helvetica, Sans-Serif; background-color: #EEEEEE; }\n"); - fprintf(f, "table {background-color: #FFFFFF; }\n"); + fprintf(f, "table, pre {background-color: #FFFFFF; }\n"); fprintf(f, "th, td {padding: 0 2pt 0 2pt; }\n"); fprintf(f, "th {background-color: #00B000; }\n"); fprintf(f, "td {background-color: #EEEEEE; }\n"); @@ -225,12 +226,11 @@ WebShowCSS(FILE *f) fprintf(f, "td.y {background-color: #EEEEBB; }\n"); fprintf(f, "td.g {background-color: #BBEEBB; }\n"); fprintf(f, "td.d {background-color: #CCCCCC; }\n"); - fprintf(f, "pre {background-color: #FFFFFF; }\n"); fprintf(f, "a, a:visited, a:link { color: blue; }\n"); } static void -WebShowSummary(FILE *f, int priv) +WebShowHTMLSummary(FILE *f, int priv) { int b,l; Bund B; @@ -238,142 +238,142 @@ WebShowSummary(FILE *f, int priv) Rep R; char buf[64],buf2[64]; - fprintf(f, "

Current status summary

\n"); + fprintf(f, "

Current status summary

\n"); fprintf(f, "\n"); - fprintf(f, "" - "%s", - priv?"":""); + fprintf(f, "\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n%s\n\n\n", + priv?"\n":""); #define FSM_COLOR(s) (((s)==ST_OPENED)?"g":(((s)==ST_INITIAL)?"r":"y")) #define PHYS_COLOR(s) (((s)==PHYS_STATE_UP)?"g":(((s)==PHYS_STATE_DOWN)?"r":"y")) for (b = 0; bbund == NULL && L->rep == NULL) { - fprintf(f, "\n"); - fprintf(f, "\n"); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, L->name); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, FsmStateName(L->lcp.fsm.state)); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, L->lcp.auth.params.authname); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":PHYS_COLOR(L->state), L->name, L->type?L->type->name:""); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":PHYS_COLOR(L->state), L->name, gPhysStateNames[L->state]); if (L->state != PHYS_STATE_DOWN) { PhysGetPeerAddr(L, buf, sizeof(buf)); - fprintf(f, "\n", buf); - fprintf(f, "\n"); + fprintf(f, "\n", buf); + fprintf(f, "\n"); PhysGetCallingNum(L, buf, sizeof(buf)); PhysGetCalledNum(L, buf2, sizeof(buf2)); if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf2, buf); } else { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf, buf2); } } else { - fprintf(f, "\n"); - fprintf(f, "\n"); - fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); } if (priv) { if (!L->tmpl) { switch (L->state) { case PHYS_STATE_DOWN: - fprintf(f, "\n", + fprintf(f, "\n", L->name); break; case PHYS_STATE_UP: - fprintf(f, "\n", + fprintf(f, "\n", L->name); break; default: - fprintf(f, "\n", + fprintf(f, "\n", L->name, L->name); } } else { - fprintf(f, "\n"); + fprintf(f, "\n"); } } - fprintf(f, "\n"); + fprintf(f, "\n"); } } for (b = 0; bn_links?B->n_links:1; int first = 1; - fprintf(f, "\n"); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n", rows, B->tmpl?"d":(B->iface.up?"g":"r"), B->name, B->name); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":(B->iface.up?"g":"r"), B->name, B->iface.ifname); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":(B->iface.up?"g":"r"), B->name, (B->iface.up?"Up":"Down")); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":FSM_COLOR(B->ipcp.fsm.state), B->name,FsmStateName(B->ipcp.fsm.state)); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":FSM_COLOR(B->ipv6cp.fsm.state), B->name,FsmStateName(B->ipv6cp.fsm.state)); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":FSM_COLOR(B->ccp.fsm.state), B->name,FsmStateName(B->ccp.fsm.state)); - fprintf(f, "\n", + fprintf(f, "\n", rows, B->tmpl?"d":FSM_COLOR(B->ecp.fsm.state), B->name,FsmStateName(B->ecp.fsm.state)); if (B->n_links == 0) { - fprintf(f, "\n\n"); + fprintf(f, "\n\n"); } for (l = 0; l < NG_PPP_MAX_LINKS; l++) { if ((L=B->links[l]) != NULL) { if (first) first = 0; else - fprintf(f, "\n"); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, L->name); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, FsmStateName(L->lcp.fsm.state)); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":FSM_COLOR(L->lcp.fsm.state), L->name, L->lcp.auth.params.authname); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":PHYS_COLOR(L->state), L->name, L->type?L->type->name:""); - fprintf(f, "\n", + fprintf(f, "\n", L->tmpl?"d":PHYS_COLOR(L->state), L->name, gPhysStateNames[L->state]); if (L->state != PHYS_STATE_DOWN) { PhysGetPeerAddr(L, buf, sizeof(buf)); - fprintf(f, "\n", buf); + fprintf(f, "\n", buf); if (L->bund != NULL) - fprintf(f, "\n", inet_ntoa(L->bund->ipcp.peer_addr)); + fprintf(f, "\n", inet_ntoa(L->bund->ipcp.peer_addr)); else - fprintf(f, "\n"); + fprintf(f, "\n"); PhysGetCallingNum(L, buf, sizeof(buf)); PhysGetCalledNum(L, buf2, sizeof(buf2)); if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf2, buf); } else { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf, buf2); } } else { - fprintf(f, "\n"); - fprintf(f, "\n"); - fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); } if (priv) { switch (L->state) { case PHYS_STATE_DOWN: - fprintf(f, "\n", + fprintf(f, "\n", L->name); break; case PHYS_STATE_UP: - fprintf(f, "\n", + fprintf(f, "\n", L->name); break; default: - fprintf(f, "\n", + fprintf(f, "\n", L->name, L->name); } } - fprintf(f, "\n"); + fprintf(f, "\n"); } } } @@ -386,57 +386,231 @@ WebShowSummary(FILE *f, int priv) int rows = (R->links[0]?1:0) + (R->links[1]?1:0); if (rows == 0) rows = 1; - fprintf(f, "\n"); - fprintf(f, "\n", rows); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n", rows); + fprintf(f, "\n", rows, R->p_up?"g":"r", R->name, R->name); for (l = 0; l < 2; l++) { if ((L=R->links[l]) != NULL) { if (shown) - fprintf(f, "\n"); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n", PHYS_COLOR(L->state), L->name, L->name); - fprintf(f, "\n"); - fprintf(f, "\n", + fprintf(f, "\n"); + fprintf(f, "\n", PHYS_COLOR(L->state), L->name, L->type?L->type->name:""); - fprintf(f, "\n", + fprintf(f, "\n", PHYS_COLOR(L->state), L->name, gPhysStateNames[L->state]); if (L->state != PHYS_STATE_DOWN) { PhysGetPeerAddr(L, buf, sizeof(buf)); - fprintf(f, "\n", buf); + fprintf(f, "\n", buf); if (L->bund != NULL) - fprintf(f, "\n", inet_ntoa(L->bund->ipcp.peer_addr)); + fprintf(f, "\n", inet_ntoa(L->bund->ipcp.peer_addr)); else - fprintf(f, "\n"); + fprintf(f, "\n"); PhysGetCallingNum(L, buf, sizeof(buf)); PhysGetCalledNum(L, buf2, sizeof(buf2)); if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf2, buf); } else { - fprintf(f, "\n", + fprintf(f, "\n\n\n", buf, buf2); } } else { - fprintf(f, "\n"); - fprintf(f, "\n"); - fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); } - fprintf(f, "\n"); - fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); shown = 1; } } if (!shown) { - fprintf(f, "\n"); - fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\n"); } } } - fprintf(f, "
BundIfaceIPCPIPV6CPCCPECPLinkLCPUserDevicePeerIP
State
BundIfaceIPCPIPV6CPCCPECPLinkLCPUserDevicePeerIP   
State
 %s
 %s%s%s%s%s%s%s%s%s%s%s %s<=%s%s<=%s%s=>%s%s=>%s   [Open][Open][Close][Close][Open] [Close][Open] [Close] 
%s
%s%s%s%s%s%s%s%s%s%s%s%s%s 
 
%s
%s%s%s%s%s%s%s%s%s%s%s%s%s %s<=%s%s<=%s%s=>%s%s=>%s   [Open][Open][Close][Close][Open] [Close][Open] [Close]
Repeater%s
Repeater%s
%s
%s%s %s%s%s%s%s%s%s %s<=%s%s<=%s%s=>%s%s=>%s   
 
 
\n"); + fprintf(f, "\n\n"); } +static void +WebShowJSONSummary(FILE *f, int priv) +{ + int b,l; + Bund B; + Link L; + Rep R; + char buf[64],buf2[64]; + + int first_l = 1; + fprintf(f, "{\"links\":[\n"); + for (b = 0; bbund == NULL && L->rep == NULL) { + if (first_l) { + fprintf(f, "{\n"); + first_l = 0; + } else + fprintf(f, ",\n{\n"); + + fprintf(f, "\"link\": \"%s\",\n", L->name); + fprintf(f, "\"lcp\": \"%s\",\n", FsmStateName(L->lcp.fsm.state)); + fprintf(f, "\"auth\": \"%s\",\n", L->lcp.auth.params.authname); + fprintf(f, "\"type\": \"%s\",\n", L->type?L->type->name:""); + fprintf(f, "\"state\": \"%s\",\n", gPhysStateNames[L->state]); + + if (L->state != PHYS_STATE_DOWN) { + PhysGetPeerAddr(L, buf, sizeof(buf)); + fprintf(f, "\"peer_ip\": \"%s\",\n", buf); + + PhysGetCallingNum(L, buf, sizeof(buf)); + PhysGetCalledNum(L, buf2, sizeof(buf2)); + if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { + fprintf(f, "\"calling_num\": \"%s\",\n", buf); + fprintf(f, "\"called_num\": \"%s\"\n", buf2); + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", buf2); + fprintf(f, "\"called_num\": \"%s\"\n", buf); + } + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", ""); + fprintf(f, "\"called_num\": \"%s\"\n", ""); + } + fprintf(f, "}\n"); + } + } + fprintf(f, "],\n"); + + int first_b = 1; + fprintf(f, "\"bundles\":[\n"); + for (b = 0; bname); + fprintf(f, "\"iface\": \"%s\",\n", B->iface.ifname); + fprintf(f, "\"state\": \"%s\",\n", (B->iface.up?"Up":"Down")); + fprintf(f, "\"ipcp\": \"%s\",\n", FsmStateName(B->ipcp.fsm.state)); + fprintf(f, "\"ipv6cp\": \"%s\",\n", FsmStateName(B->ipv6cp.fsm.state)); + fprintf(f, "\"ccp\": \"%s\",\n", FsmStateName(B->ccp.fsm.state)); + fprintf(f, "\"ecp\": \"%s\",\n", FsmStateName(B->ecp.fsm.state)); + + int first_l = 1; + fprintf(f, "\"links\":[\n"); + for (l = 0; l < NG_PPP_MAX_LINKS; l++) { + if ((L=B->links[l]) != NULL) { + if (first_l) { + fprintf(f, "{\n"); + first_l = 0; + } else + fprintf(f, ",\n{\n"); + + fprintf(f, "\"link\": \"%s\",\n", L->name); + fprintf(f, "\"lcp\": \"%s\",\n", FsmStateName(L->lcp.fsm.state)); + fprintf(f, "\"auth\": \"%s\",\n", L->lcp.auth.params.authname); + fprintf(f, "\"type\": \"%s\",\n", L->type?L->type->name:""); + fprintf(f, "\"state\": \"%s\",\n", gPhysStateNames[L->state]); + + if (L->state != PHYS_STATE_DOWN) { + PhysGetPeerAddr(L, buf, sizeof(buf)); + fprintf(f, "\"peer_ip\": \"%s\",\n", buf); + + if (L->bund != NULL) + fprintf(f, "\"ipcp_ip\": \"%s\",\n", inet_ntoa(L->bund->ipcp.peer_addr)); + else + fprintf(f, "\"ipcp_ip\": \"%s\",\n", ""); + + PhysGetCallingNum(L, buf, sizeof(buf)); + PhysGetCalledNum(L, buf2, sizeof(buf2)); + if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { + fprintf(f, "\"calling_num\": \"%s\",\n", buf); + fprintf(f, "\"called_num\": \"%s\"\n", buf2); + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", buf2); + fprintf(f, "\"called_num\": \"%s\"\n", buf); + } + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", ""); + fprintf(f, "\"called_num\": \"%s\"\n", ""); + } + fprintf(f, "}\n"); + } + } + fprintf(f, "]\n}\n"); + } + } + fprintf(f, "],\n"); + + int first_r = 1; + fprintf(f, "\"repeaters\":[\n"); + for (b = 0; bname); + + int first_l = 1; + fprintf(f, "\"links\":[\n"); + for (l = 0; l < 2; l++) { + if ((L=R->links[l]) != NULL) { + if (first_l) { + fprintf(f, "{\n"); + first_l = 0; + } else + fprintf(f, ",\n{\n"); + + fprintf(f, "\"link\": \"%s\",\n", L->name); + fprintf(f, "\"type\": \"%s\",\n", L->type?L->type->name:""); + fprintf(f, "\"state\": \"%s\",\n", gPhysStateNames[L->state]); + + if (L->state != PHYS_STATE_DOWN) { + PhysGetPeerAddr(L, buf, sizeof(buf)); + fprintf(f, "\"peer_ip\": \"%s\",\n", buf); + + if (L->bund != NULL) + fprintf(f, "\"ipcp_ip\": \"%s\",\n", inet_ntoa(L->bund->ipcp.peer_addr)); + else + fprintf(f, "\"ipcp_ip\": \"%s\",\n", ""); + + PhysGetCallingNum(L, buf, sizeof(buf)); + PhysGetCalledNum(L, buf2, sizeof(buf2)); + if (PhysGetOriginate(L) == LINK_ORIGINATE_REMOTE) { + fprintf(f, "\"calling_num\": \"%s\",\n", buf); + fprintf(f, "\"called_num\": \"%s\"\n", buf2); + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", buf2); + fprintf(f, "\"called_num\": \"%s\"\n", buf); + } + } else { + fprintf(f, "\"calling_num\": \"%s\",\n", ""); + fprintf(f, "\"called_num\": \"%s\"\n", ""); + } + fprintf(f, "}\n"); + } + } + fprintf(f, "]\n"); + + if (b == (gNumReps - 1)) { + fprintf(f, "}\n"); + } else { + fprintf(f, "},\n"); + } + } + } + fprintf(f, "]}\n"); +} + static void WebRunBinCmd(FILE *f, const char *query, int priv) { @@ -516,14 +690,14 @@ WebRunCmd(FILE *f, const char *query, int priv) if (++argc >= MAX_CONSOLE_ARGS) break; - fprintf(f, "

<< Back

\n"); + fprintf(f, "

\nBack\n

\n"); if (argc == 0) { - fprintf(f, "

No command cpecified!

\n"); + fprintf(f, "

No command cpecified!

\n"); goto done; } - fprintf(f, "
\n");
+    fprintf(f, "
\n");
     for (k = 0; k < argc; k++) {
 	int	ac;
 	char	*av[MAX_CONSOLE_ARGS];
@@ -543,10 +717,10 @@ WebRunCmd(FILE *f, const char *query, int priv)
 	DoCommand(&cs->context, ac, av, NULL, 0);
 	Freee(buf1);
     }
-    fprintf(f, "
\n"); + fprintf(f, "
\n"); done: Freee(buf); - fprintf(f, "

<< Back

\n"); + fprintf(f, "

\nBack\n

\n"); RESETREF(cs->context.lnk, NULL); RESETREF(cs->context.bund, NULL); RESETREF(cs->context.rep, NULL); @@ -601,16 +775,22 @@ WebServletRun(struct http_servlet *servlet, if (!strcmp(path,"/mpd.css")) { http_response_set_header(resp, 0, "Content-Type", "text/css"); WebShowCSS(f); - } else if (!strcmp(path,"/bincmd")) { + } else if (!strcmp(path,"/bincmd") || !strcmp(path,"/json")) { http_response_set_header(resp, 0, "Content-Type", "text/plain"); http_response_set_header(resp, 1, "Pragma", "no-cache"); http_response_set_header(resp, 1, "Cache-Control", "no-cache, must-revalidate"); pthread_cleanup_push(WebServletRunCleanup, NULL); GIANT_MUTEX_LOCK(); - WebRunBinCmd(f, query, priv); + + if (!strcmp(path,"/bincmd")) + WebRunBinCmd(f, query, priv); + else if (!strcmp(path,"/json")) + WebShowJSONSummary(f, priv); + GIANT_MUTEX_UNLOCK(); pthread_cleanup_pop(0); + } else if (!strcmp(path,"/") || !strcmp(path,"/cmd")) { http_response_set_header(resp, 0, "Content-Type", "text/html"); http_response_set_header(resp, 1, "Pragma", "no-cache"); @@ -618,24 +798,23 @@ WebServletRun(struct http_servlet *servlet, pthread_cleanup_push(WebServletRunCleanup, NULL); GIANT_MUTEX_LOCK(); - fprintf(f, "\n"); - fprintf(f, "\n"); - fprintf(f, "Multi-link PPP Daemon for FreeBSD (mpd)\n"); - fprintf(f, "\n"); - fprintf(f, "\n\n"); - fprintf(f, "

Multi-link PPP Daemon for FreeBSD

\n"); + + fprintf(f, "\n"); + fprintf(f, "\n"); + fprintf(f, "\nMulti-link PPP Daemon for FreeBSD (mpd)\n"); + fprintf(f, "\n"); + fprintf(f, "\n\n"); + fprintf(f, "

Multi-link PPP Daemon for FreeBSD

\n"); if (!strcmp(path,"/")) - WebShowSummary(f, priv); + WebShowHTMLSummary(f, priv); else if (!strcmp(path,"/cmd")) WebRunCmd(f, query, priv); GIANT_MUTEX_UNLOCK(); pthread_cleanup_pop(0); - fprintf(f, "\n\n"); + fprintf(f, "\n\n"); } else { http_response_send_error(resp, 404, NULL); }