mod_cdr_mongodb: add post-v0.6 patch to driver to support Unix domain sockets

This commit is contained in:
Daniel Swarbrick 2012-10-21 22:59:51 +02:00
parent a92fb019c5
commit 58f1ee7ff0

View File

@ -24,6 +24,7 @@
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
@ -99,6 +100,36 @@ int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
return MONGO_OK;
}
int mongo_env_unix_socket_connect( mongo *conn, const char *sock_path ) {
struct sockaddr_un addr;
int status, len;
conn->connected = 0;
conn->sock = socket( AF_UNIX, SOCK_STREAM, 0 );
if ( conn->sock < 0 ) {
conn->sock = 0;
return MONGO_ERROR;
}
addr.sun_family = AF_UNIX;
strncpy( addr.sun_path, sock_path, sizeof(addr.sun_path) - 1 );
len = sizeof( addr );
status = connect( conn->sock, (struct sockaddr *) &addr, len );
if( status < 0 ){
mongo_env_close_socket( conn->sock );
conn->sock = 0;
conn->err = MONGO_CONN_FAIL;
return MONGO_ERROR;
}
conn->connected = 1;
return MONGO_OK;
}
int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
char port_str[NI_MAXSERV];
int status;
@ -107,6 +138,10 @@ int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
struct addrinfo *ai_list = NULL;
struct addrinfo *ai_ptr = NULL;
if ( port < 0 ) {
return mongo_env_unix_socket_connect( conn, host );
}
conn->sock = 0;
conn->connected = 0;
sprintf(port_str,"%d",port);