Skip to content

Commit

Permalink
[libsock] connect not only ipaddr but domain
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 20, 2021
1 parent 0312b59 commit eab8188
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
3 changes: 3 additions & 0 deletions gear-lib/libsock/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## libsock
This is a simple libsock library.

* wrapper of connect/bind/listen/send/recv/ api of lowlevel socket api
* socket external of server/client highlevel async api
* add new PTCP socket type as pseudo-tcp
57 changes: 50 additions & 7 deletions gear-lib/libsock/libsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ static void sock_post_deinit_win()
}
#endif

static int host_to_sockaddr(const char *host, struct in_addr *addr)
{
struct addrinfo *res = NULL;
struct addrinfo *ap;
struct addrinfo hints;
int ret;
memset(&hints, 0, sizeof(struct addrinfo));
#if USE_IPV6
hints.ai_family = AF_UNSPEC;
#else
hints.ai_family = AF_INET;
#endif
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_CANONNAME;

ret = getaddrinfo(host, NULL, &hints, &res);
if (ret != 0) {
printf("getaddrinfo: %s\n", gai_strerror(ret));
return -1;
}
for (ap = res; ap != NULL; ap = ap->ai_next) {
if (ap->ai_family == hints.ai_family) {
break;
}
}
if (ap == NULL) {
printf("addrinfo is invalid!\n");
freeaddrinfo(res);
return -1;
}

*addr = ((struct sockaddr_in*)(ap->ai_addr))->sin_addr;
freeaddrinfo(res);
return 0;
}

static struct sock_connection *_sock_connect(int type, const char *host, uint16_t port)
{
int domain = -1;
Expand All @@ -88,10 +125,16 @@ static struct sock_connection *_sock_connect(int type, const char *host, uint16_
switch (type) {
case SOCK_STREAM:
case SOCK_DGRAM:
#if USE_IPV6
domain = AF_UNSPEC;
#else
domain = AF_INET;
#endif
si.sin_family = domain;
si.sin_addr.s_addr = inet_addr(host);
//si.sin_addr.s_addr = inet_addr(host);
host_to_sockaddr(host, &si.sin_addr);
si.sin_port = htons(port);

sa = (struct sockaddr*)&si;
sa_len = sizeof(si);
break;
Expand Down Expand Up @@ -137,10 +180,10 @@ static struct sock_connection *_sock_connect(int type, const char *host, uint16_

return sc;
fail:
if (-1 != sc->fd) {
close(sc->fd);
}
if (sc) {
if (-1 != sc->fd) {
close(sc->fd);
}
free(sc);
}
return NULL;
Expand Down Expand Up @@ -815,7 +858,7 @@ int sock_sendto(int fd, const char *ip, uint16_t port,
ssize_t n;
char *p = (char *)buf;
size_t left = len;
size_t step = MTU;
size_t step = len;
struct sockaddr_in sa;
int cnt = 0;

Expand Down Expand Up @@ -855,7 +898,7 @@ int sock_recv(uint64_t fd, void *buf, size_t len)
int n;
char *p = (char *)buf;
size_t left = len;
size_t step = MTU;
size_t step = len;
int cnt = 0;
if (buf == NULL || len == 0) {
printf("%s paraments invalid!\n", __func__);
Expand Down Expand Up @@ -905,7 +948,7 @@ int sock_recvfrom(int fd, uint32_t *ip, uint16_t *port, void *buf, size_t len)
char *p = (char *)buf;
int cnt = 0;
size_t left = len;
size_t step = MTU;
size_t step = len;
struct sockaddr_in si;
socklen_t si_len = sizeof(si);

Expand Down
4 changes: 1 addition & 3 deletions gear-lib/libsock/libsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <libposix.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#if defined (OS_LINUX)
#include <netinet/in.h>
#include <netinet/tcp.h>
Expand Down Expand Up @@ -103,8 +102,7 @@ int sock_sendto(int fd, const char *ip, uint16_t port,
int sock_send_sync_recv(int fd, const void *sbuf, size_t slen,
void *rbuf, size_t rlen, int timeout);
int sock_recv(uint64_t fd, void *buf, size_t len);
int sock_recvfrom(int fd, uint32_t *ip, uint16_t *port,
void *buf, size_t len);
int sock_recvfrom(int fd, uint32_t *ip, uint16_t *port, void *buf, size_t len);

uint32_t sock_addr_pton(const char *ip);
int sock_addr_ntop(char *str, uint32_t ip);
Expand Down
10 changes: 5 additions & 5 deletions gear-lib/libsock/libsock_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,18 @@ static void *sock_client_thread(struct thread *thread, void *arg)
GEAR_API int sock_client_connect(struct sock_client *c)
{
struct gevent *e;
c->conn = sock_tcp_connect(c->host, c->port);
if (!c->conn) {
printf("sock_tcp_connect %s:%d failed!\n", c->host, c->port);
return -1;
}
switch (c->type) {
case SOCK_TYPE_TCP:
c->conn = sock_tcp_connect(c->host, c->port);
c->fd = c->conn->fd;
break;
case SOCK_TYPE_UDP:
c->conn = sock_udp_connect(c->host, c->port);
c->fd = c->conn->fd;
break;
#ifdef ENABLE_PTCP
case SOCK_TYPE_PTCP:
c->conn = sock_ptcp_connect(c->host, c->port);
c->fd = c->conn->fd64;
break;
#endif
Expand Down

0 comments on commit eab8188

Please sign in to comment.