Connect to the server. It is possible to pass certain flags to the function: * CC_FORCE: The connection is performed even if there is already * a connected socket. * CC_QUIET: Don't print errors if connection fails. */
| 857 | * a connected socket. |
| 858 | * CC_QUIET: Don't print errors if connection fails. */ |
| 859 | static int cliConnect(int flags) { |
| 860 | if (context == NULL || flags & CC_FORCE) { |
| 861 | if (context != NULL) { |
| 862 | redisFree(context); |
| 863 | config.dbnum = 0; |
| 864 | config.in_multi = 0; |
| 865 | cliRefreshPrompt(); |
| 866 | } |
| 867 | |
| 868 | /* Do not use hostsocket when we got redirected in cluster mode */ |
| 869 | if (config.hostsocket == NULL || |
| 870 | (config.cluster_mode && config.cluster_reissue_command)) { |
| 871 | context = redisConnect(config.hostip,config.hostport); |
| 872 | } else { |
| 873 | context = redisConnectUnix(config.hostsocket); |
| 874 | } |
| 875 | |
| 876 | if (!context->err && config.tls) { |
| 877 | const char *err = NULL; |
| 878 | if (cliSecureConnection(context, config.sslconfig, &err) == REDIS_ERR && err) { |
| 879 | fprintf(stderr, "Could not negotiate a TLS connection: %s\n", err); |
| 880 | redisFree(context); |
| 881 | context = NULL; |
| 882 | return REDIS_ERR; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | if (context->err) { |
| 887 | if (!(flags & CC_QUIET)) { |
| 888 | fprintf(stderr,"Could not connect to Redis at "); |
| 889 | if (config.hostsocket == NULL || |
| 890 | (config.cluster_mode && config.cluster_reissue_command)) |
| 891 | { |
| 892 | fprintf(stderr, "%s:%d: %s\n", |
| 893 | config.hostip,config.hostport,context->errstr); |
| 894 | } else { |
| 895 | fprintf(stderr,"%s: %s\n", |
| 896 | config.hostsocket,context->errstr); |
| 897 | } |
| 898 | } |
| 899 | redisFree(context); |
| 900 | context = NULL; |
| 901 | return REDIS_ERR; |
| 902 | } |
| 903 | |
| 904 | |
| 905 | /* Set aggressive KEEP_ALIVE socket option in the Redis context socket |
| 906 | * in order to prevent timeouts caused by the execution of long |
| 907 | * commands. At the same time this improves the detection of real |
| 908 | * errors. */ |
| 909 | anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL); |
| 910 | |
| 911 | /* Do AUTH, select the right DB, switch to RESP3 if needed. */ |
| 912 | if (cliAuth(context, config.user, config.auth) != REDIS_OK) |
| 913 | return REDIS_ERR; |
| 914 | if (cliSelect() != REDIS_OK) |
| 915 | return REDIS_ERR; |
| 916 | if (cliSwitchProto() != REDIS_OK) |
no test coverage detected