diff --git a/mcmc.c b/mcmc.c index 78181e6..fcf8759 100644 --- a/mcmc.c +++ b/mcmc.c @@ -809,7 +809,7 @@ int mcmc_check_nonblock_connect(void *c, int *err) { // connect_bind_tcp() // ^ fill an internal struct from the stack and call into this central // connect? -int mcmc_connect(void *c, char *host, char *port, int options) { +int mcmc_connect(void *c, char* source, char *host, char *port, int options) { mcmc_ctx_t *ctx = (mcmc_ctx_t *)c; int s; @@ -854,6 +854,31 @@ int mcmc_connect(void *c, char *host, char *port, int options) { } } + if(source != NULL ){ + struct addrinfo source_hints; + struct addrinfo *sai = NULL; + + memset(&source_hints, 0, sizeof(source_hints)); + source_hints.ai_family = next->ai_family; + source_hints.ai_socktype = SOCK_STREAM; + source_hints.ai_flags = AI_NUMERICHOST; + + if( getaddrinfo(source, NULL, &source_hints, &sai) != 0){ + res = MCMC_ERR; + close(sock); + goto end; + } + + if( bind(sock, sai->ai_addr, sai->ai_addrlen) < 0 ){ + freeaddrinfo(sai); + res = MCMC_ERR; + close(sock); + goto end; + } + freeaddrinfo(sai); + } + + if (options & MCMC_OPTION_NONBLOCK) { int flags = fcntl(sock, F_GETFL); if (flags < 0) { diff --git a/mcmc.h b/mcmc.h index 49cacec..2e8e7c4 100644 --- a/mcmc.h +++ b/mcmc.h @@ -123,7 +123,7 @@ int mcmc_fd(void *c); size_t mcmc_size(int options); size_t mcmc_min_buffer_size(int options); int mcmc_parse_buf(const char *buf, size_t read, mcmc_resp_t *r); -int mcmc_connect(void *c, char *host, char *port, int options); +int mcmc_connect(void *c, char *source, char *host, char *port, int options); int mcmc_check_nonblock_connect(void *c, int *err); int mcmc_send_request(void *c, const char *request, int len, int count); int mcmc_request_writev(void *c, const struct iovec *iov, int iovcnt, ssize_t *sent, int count);