/* * This function establishes a connection to the specified ip and port and * sends a command to varnishd. If varnishd returns an OK status, the result * is printed and 0 returned. Else, an error message is printed and 1 is * returned */ static int cli_sock(struct vadmin_config_t *vadmin, struct agent_core_t *core) { int fd; unsigned status; char *answer = NULL; char buf[CLI_AUTH_RESPONSE_LEN + 1]; n_arg_sock(core); if (core->config->T_arg == NULL) { logger(vadmin->logger, "No T-arg (Administration port) available. Varnishadm-commands wont work."); return (-1); } vadmin->sock = VSS_open(vadmin->logger, core->config->T_arg, core->config->timeout); if (vadmin->sock < 0) { logger(vadmin->logger, "Connection failed (%s)", core->config->T_arg); return (-1); } (void)VCLI_ReadResult(vadmin->sock, &status, &answer, core->config->timeout); if (status == CLIS_AUTH) { if (core->config->S_arg == NULL) { logger(vadmin->logger, "Authentication required"); assert(close(vadmin->sock) == 0); return(-1); } fd = open(core->config->S_arg, O_RDONLY); if (fd < 0) { logger(vadmin->logger, "Cannot open \"%s\": %s", core->config->S_arg, strerror(errno)); assert(close(vadmin->sock) == 0); return (-1); } VCLI_AuthResponse(fd, answer, buf); assert(close(fd) == 0); free(answer); cli_write(vadmin->sock, "auth "); cli_write(vadmin->sock, buf); cli_write(vadmin->sock, "\n"); (void)VCLI_ReadResult(vadmin->sock, &status, &answer, core->config->timeout); } if (status != CLIS_OK) { logger(vadmin->logger, "Rejected %u\n%s", status, answer); assert(close(vadmin->sock) == 0); return (-1); } free(answer); cli_write(vadmin->sock, "ping\n"); (void)VCLI_ReadResult(vadmin->sock, &status, &answer, core->config->timeout); if (status != CLIS_OK || strstr(answer, "PONG") == NULL) { logger(vadmin->logger, "No pong received from server"); assert(close(vadmin->sock) == 0); return(-1); } free(answer); vadmin->state = 1; return (vadmin->sock); }
/* * This function establishes a connection to the specified ip and port and * sends a command to varnishd. If varnishd returns an OK status, the result * is printed and 0 returned. Else, an error message is printed and 1 is * returned */ static int cli_sock(const char *T_arg, const char *S_arg) { int fd; int sock; unsigned status; char *answer = NULL; char buf[CLI_AUTH_RESPONSE_LEN + 1]; sock = VSS_open(T_arg, timeout); if (sock < 0) { fprintf(stderr, "Connection failed (%s)\n", T_arg); return (-1); } (void)VCLI_ReadResult(sock, &status, &answer, timeout); if (status == CLIS_AUTH) { if (S_arg == NULL) { fprintf(stderr, "Authentication required\n"); AZ(close(sock)); return(-1); } fd = open(S_arg, O_RDONLY); if (fd < 0) { fprintf(stderr, "Cannot open \"%s\": %s\n", S_arg, strerror(errno)); AZ(close(sock)); return (-1); } VCLI_AuthResponse(fd, answer, buf); AZ(close(fd)); free(answer); cli_write(sock, "auth "); cli_write(sock, buf); cli_write(sock, "\n"); (void)VCLI_ReadResult(sock, &status, &answer, timeout); } if (status != CLIS_OK) { fprintf(stderr, "Rejected %u\n%s\n", status, answer); AZ(close(sock)); return (-1); } free(answer); cli_write(sock, "ping\n"); (void)VCLI_ReadResult(sock, &status, &answer, timeout); if (status != CLIS_OK || strstr(answer, "PONG") == NULL) { fprintf(stderr, "No pong received from server\n"); AZ(close(sock)); return(-1); } free(answer); return (sock); }