// // Készítette: Toldi Balázs Ádám // Dátum: 2019. 10. 14.. // #include "webio.h" int webio_create(Config config, struct Node_data myData, WebIO *webIo) { char *port = map_getValue(config, "interface-port"); if (port == NULL) port = DEFAULT_INTERFACE_PORT; char *folder = map_getValue(config, "interface-folder"); if (folder == NULL) folder = DEFAULT_WWW_FOLDER; char *local_mode_str = map_getValue(config, "interface-local"); bool local_mode = false; if (local_mode_str != NULL && strcmp(local_mode_str, "true") == 0) local_mode = true; struct addrinfo *result = NULL; SOCKET listening; result = tcp_createIPv4Socket(&listening, atoi(port), !local_mode); if (result == NULL) { return 1; } int res = tcp_bindnlisten(listening, result, SOMAXCONN); if (res != 0) { return 2; } WebIO wio; wio.sockaddr = tcp_getAddr_in(listening); wio.socket = listening; wio.nodeData = myData; strcpy(wio.folder, folder); *webIo = wio; return 0; } int webio_handleRequest(WebIO wio, const PeerList *list) { SOCKET client = accept(wio.socket, NULL, NULL); char buf[8192]; memset(buf, 0, 8192); int res = recv(client, buf, 8192, 0); if (res <= 0) { logger_log("Error with web interface!"); closesocket(client); return -1; } char req[10]; sscanf(buf, "%s", req); if (strcmp(req, "GET") == 0) { char file[50]; sscanf(buf, "%*s %s", file); res = webio_handleGETrequest(client, wio, file, list); } else if (strcmp(req, "POST") == 0) { int i = strlen(buf) - 1; while (buf[i] != '\n') { i--; } i++; char tmp[8196]; strcpy(tmp, buf + i); Map post = getPacketData(tmp); res = webio_handlePOSTrequest(client, wio, list, post); free(post.pairs); } else res = -1; return res; } char *webio_getMIMEtype(char *filename) { char type[10]; char *ext = webio_getFiletype(filename); strcpy(type, ext); free(ext); if (strcmp(type, "html") == 0) return "text/html"; else if (strcmp(type, "json") == 0) return "application/json"; else if (strcmp(type, "svg") == 0) return "image/svg+xml"; else if (strcmp(type, "js") == 0) return "application/javascript"; else if (strcmp(type, "css") == 0) return "text/css"; else if (strcmp(type, "jpg") == 0) return "image/jpeg"; else if (strcmp(type, "png") == 0) return "image/png"; else if (strcmp(type, "bmp") == 0) return "image/bmp"; else if (strcmp(type, "ico") == 0) return "image/x-icon"; else if (strcmp(type, "xml") == 0) return "text/xml"; else if (strcmp(type, "zip") == 0) return "application/zip"; else return "text/plain"; } char *webio_getFiletype(char *filename) { char *ext = (char *) malloc(sizeof(char) * 10); char *tmp = strtok(filename, "."); while (tmp != NULL) { strncpy(ext, tmp, 10); tmp = strtok(NULL, "."); } return ext; } static int webio_handleGETrequest(SOCKET client, WebIO wio, char *file, const PeerList *list) { char buf[8192]; sscanf(buf, "%*s %s", file); char path[129]; memset(path, 0, sizeof(path)); strcat(path, wio.folder); char response[8192]; int len = 0; if (file[0] == '/') memmove(file, file + 1, strlen(file)); if (strcmp(file, "") == 0) { char index[8192] = ""; webio_getIndex(wio.folder, list, index); webio_sendPage(client, index); } else if (peer_ID_isFound(*list, file) || webio_isPeerFound(wio.folder, file)) { char cnt[8192] = ""; webio_getPeerPage(wio.folder, file, (peer_ID_getPeer(*list, file) != -1), cnt); webio_sendPage(client, cnt); } else if (strcmp(file, "kill") == 0) { webio_sendPage(client, "