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. */
| 664 | * a connected socket. |
| 665 | * CC_QUIET: Don't print errors if connection fails. */ |
| 666 | static int cliConnect(int flags) { |
| 667 | if (context == NULL || flags & CC_FORCE) { |
| 668 | if (context != NULL) { |
| 669 | redisFree(context); |
| 670 | config.dbnum = 0; |
| 671 | config.in_multi = 0; |
| 672 | cliRefreshPrompt(); |
| 673 | } |
| 674 | |
| 675 | /* Do not use hostsocket when we got redirected in cluster mode */ |
| 676 | if (config.hostsocket == NULL || |
| 677 | (config.cluster_mode && config.cluster_reissue_command)) { |
| 678 | context = redisConnect(config.hostip,config.hostport); |
| 679 | } else { |
| 680 | context = redisConnectUnix(config.hostsocket); |
| 681 | } |
| 682 | |
| 683 | if (!context->err && config.tls) { |
| 684 | const char *err = NULL; |
| 685 | if (cliSecureConnection(context, config.sslconfig, &err) == REDIS_ERR && err) { |
| 686 | fprintf(stderr, "Could not negotiate a TLS connection: %s\n", err); |
| 687 | redisFree(context); |
| 688 | context = NULL; |
| 689 | return REDIS_ERR; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (context->err) { |
| 694 | if (!(flags & CC_QUIET)) { |
| 695 | fprintf(stderr,"Could not connect to Redis at "); |
| 696 | if (config.hostsocket == NULL || |
| 697 | (config.cluster_mode && config.cluster_reissue_command)) |
| 698 | { |
| 699 | fprintf(stderr, "%s:%d: %s\n", |
| 700 | config.hostip,config.hostport,context->errstr); |
| 701 | } else { |
| 702 | fprintf(stderr,"%s: %s\n", |
| 703 | config.hostsocket,context->errstr); |
| 704 | } |
| 705 | } |
| 706 | redisFree(context); |
| 707 | context = NULL; |
| 708 | return REDIS_ERR; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | /* Set aggressive KEEP_ALIVE socket option in the Redis context socket |
| 713 | * in order to prevent timeouts caused by the execution of long |
| 714 | * commands. At the same time this improves the detection of real |
| 715 | * errors. */ |
| 716 | anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL); |
| 717 | |
| 718 | /* Do AUTH, select the right DB, switch to RESP3 if needed. */ |
| 719 | if (cliAuth(context, config.user, config.auth) != REDIS_OK) |
| 720 | return REDIS_ERR; |
| 721 | if (cliSelect() != REDIS_OK) |
| 722 | return REDIS_ERR; |
| 723 | if (cliSwitchProto() != REDIS_OK) |
no test coverage detected