Helper functions for redis_address
This commit is contained in:
parent
c37187f8ed
commit
58737d9f3b
2
Makefile
2
Makefile
@ -5,7 +5,7 @@
|
||||
|
||||
include ./Makefile.common
|
||||
|
||||
OBJ=net.o hiredis.o sds.o async.o parser.o object.o handle.o format.o context.o
|
||||
OBJ=net.o hiredis.o sds.o async.o parser.o object.o handle.o format.o context.o address.o
|
||||
BINS=hiredis-example hiredis-test
|
||||
|
||||
all: $(DYLIBNAME) $(BINS)
|
||||
|
||||
52
address.c
Normal file
52
address.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "address.h"
|
||||
|
||||
redis_address redis_address_in(const char *ip, int port) {
|
||||
struct sockaddr_in sa;
|
||||
redis_address addr;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = htons(port);
|
||||
assert(inet_pton(AF_INET, ip, &sa.sin_addr) == 1);
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sa_family = sa.sin_family;
|
||||
addr.sa_addrlen = sizeof(sa);
|
||||
addr.sa_addr.in = sa;
|
||||
return addr;
|
||||
}
|
||||
|
||||
redis_address redis_address_in6(const char *ip, int port) {
|
||||
struct sockaddr_in6 sa;
|
||||
redis_address addr;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin6_family = AF_INET6;
|
||||
sa.sin6_port = htons(port);
|
||||
assert(inet_pton(AF_INET6, ip, &sa.sin6_addr) == 1);
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sa_family = sa.sin6_family;
|
||||
addr.sa_addrlen = sizeof(sa);
|
||||
addr.sa_addr.in6 = sa;
|
||||
return addr;
|
||||
}
|
||||
|
||||
redis_address redis_address_un(const char *path) {
|
||||
struct sockaddr_un sa;
|
||||
redis_address addr;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sun_family = AF_LOCAL;
|
||||
strncpy((char*)&sa.sun_path, path, sizeof(sa.sun_path));
|
||||
sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sa_family = sa.sun_family;
|
||||
addr.sa_addrlen = sizeof(sa);
|
||||
addr.sa_addr.un = sa;
|
||||
return addr;
|
||||
}
|
||||
25
address.h
Normal file
25
address.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef HIREDIS_ADDRESS_H
|
||||
#define HIREDIS_ADDRESS_H 1
|
||||
|
||||
/* struct sockaddr_(in|in6|un) */
|
||||
#include <netinet/in.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
typedef struct redis_address_s redis_address;
|
||||
|
||||
struct redis_address_s {
|
||||
int sa_family;
|
||||
socklen_t sa_addrlen;
|
||||
union {
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in in;
|
||||
struct sockaddr_in6 in6;
|
||||
struct sockaddr_un un;
|
||||
} sa_addr;
|
||||
};
|
||||
|
||||
redis_address redis_address_in(const char *ip, int port);
|
||||
redis_address redis_address_in6(const char *ip, int port);
|
||||
redis_address redis_address_un(const char *path);
|
||||
|
||||
#endif
|
||||
17
handle.h
17
handle.h
@ -1,15 +1,12 @@
|
||||
#ifndef _HIREDIS_HANDLE_H
|
||||
#define _HIREDIS_HANDLE_H 1
|
||||
|
||||
/* struct sockaddr_(in|in6|un) */
|
||||
#include <netinet/in.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
/* struct timeval */
|
||||
#include <sys/time.h>
|
||||
|
||||
/* local */
|
||||
#include "parser.h"
|
||||
#include "address.h"
|
||||
|
||||
#define REDIS_OK 0
|
||||
#define REDIS_ESYS -1
|
||||
@ -18,7 +15,6 @@
|
||||
#define REDIS_EEOF -4
|
||||
|
||||
typedef struct redis_handle_s redis_handle;
|
||||
typedef struct redis_address_s redis_address;
|
||||
|
||||
struct redis_handle_s {
|
||||
int fd;
|
||||
@ -28,17 +24,6 @@ struct redis_handle_s {
|
||||
char *rbuf;
|
||||
};
|
||||
|
||||
struct redis_address_s {
|
||||
int sa_family;
|
||||
socklen_t sa_addrlen;
|
||||
union {
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in in;
|
||||
struct sockaddr_in6 in6;
|
||||
struct sockaddr_un un;
|
||||
} sa_addr;
|
||||
};
|
||||
|
||||
int redis_handle_init(redis_handle *h);
|
||||
int redis_handle_close(redis_handle *);
|
||||
int redis_handle_destroy(redis_handle *);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user