Skip to content

Commit

Permalink
[libskt] porting windows, unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Mar 10, 2019
1 parent bf48639 commit f1e3ff0
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
50 changes: 50 additions & 0 deletions libskt/Makefile.nmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
###############################################################################
# common
###############################################################################
#ARCH: linux/pi/android/ios/win
LD = link
AR = lib
RM = del

###############################################################################
# target and object
###############################################################################
LIBNAME = libskt
TGT_LIB_A = $(LIBNAME).lib
TGT_LIB_SO = $(LIBNAME).dll
TGT_UNIT_TEST = test_$(LIBNAME).exe

OBJS_LIB = $(LIBNAME).obj
OBJS_UNIT_TEST = test_$(LIBNAME).obj

###############################################################################
# cflags and ldflags
###############################################################################
CFLAGS = /Iinclude /I../libposix4win/ /I.
CFLAGS = $(CFLAGS) /Od /W3 /Zi

LDFLAGS = /LIBPATH:../libposix4win/ /LIBPATH:./
LIBS = ws2_32.lib

###############################################################################
# target
###############################################################################
TGT = $(TGT_LIB_A) $(TGT_LIB_SO) $(TGT_UNIT_TEST)

OBJS = $(OBJS_LIB) $(OBJS_UNIT_TEST)

all: $(TGT)

$(TGT_LIB_A): $(OBJS_LIB)
$(AR) $(OBJS_LIB) $(LIBS) /o:$(TGT_LIB_A)

$(TGT_LIB_SO): $(OBJS_LIB)
$(LD) $(LDFLAGS) /Dll $(OBJS_LIB) $(LIBS) /o:$(TGT_LIB_SO)

$(TGT_UNIT_TEST): $(OBJS_UNIT_TEST)
$(CC) $(TGT_LIB_A) $(OBJS_UNIT_TEST) /o $(TGT_UNIT_TEST) /link $(LDFLAGS)

clean:
$(RM) $(OBJS)
$(RM) $(TGT)
$(RM) $(TGT_LIB_SO)*
70 changes: 60 additions & 10 deletions libskt/libskt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#ifndef __ANDROID__
#if defined (__linux__) || defined (__CYGWIN__)
#include <ifaddrs.h>
#endif
#include <netdb.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#endif


#define LISTEN_MAX_BACKLOG (128)
Expand All @@ -51,14 +49,24 @@ static struct skt_connection *_skt_connect(int type,
int domain = -1;
int protocol = 0;
struct sockaddr_in si;
#if 0
struct sockaddr_un su;
#endif
struct sockaddr *sa;
size_t sa_len = 0;
struct skt_connection *sc = NULL;
#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
WSADATA Ws;
if (WSAStartup(MAKEWORD(2,2), &Ws) != 0) {
printf("Init Windows Socket Failed\n");
return NULL;
}
#endif
if (type < 0 || strlen(host) == 0 || port >= 0xFFFF) {
printf("invalid paraments\n");
return NULL;
}

switch (type) {
case SOCK_STREAM:
case SOCK_DGRAM:
Expand All @@ -69,13 +77,15 @@ static struct skt_connection *_skt_connect(int type,
sa = (struct sockaddr*)&si;
sa_len = sizeof(si);
break;
#if 0
case SOCK_SEQPACKET:
snprintf(su.sun_path, sizeof(su.sun_path), "%s", host);
su.sun_family = PF_UNIX;
domain = PF_UNIX;
sa = (struct sockaddr*)&su;
sa_len = sizeof(su);
break;
#endif
default:
printf("unknown socket type!\n");
return NULL;
Expand All @@ -87,6 +97,7 @@ static struct skt_connection *_skt_connect(int type,
printf("malloc failed!\n");
return NULL;
}

sc->fd = socket(domain, type, protocol);
if (-1 == sc->fd) {
printf("socket: %s\n", strerror(errno));
Expand Down Expand Up @@ -134,8 +145,17 @@ struct skt_connection *skt_unix_connect(const char *host, uint16_t port)

int skt_tcp_bind_listen(const char *host, uint16_t port)
{
SOCKET fd;
struct sockaddr_in si;
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
WSADATA Ws;
if (WSAStartup(MAKEWORD(2,2), &Ws) != 0) {
printf("Init Windows Socket Failed\n");
return -1;
}
#endif
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == fd) {
printf("socket failed: %s\n", strerror(errno));
goto fail;
Expand Down Expand Up @@ -171,7 +191,13 @@ int skt_udp_bind(const char *host, uint16_t port)
{
int fd;
struct sockaddr_in si;

#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
WSADATA Ws;
if (WSAStartup(MAKEWORD(2,2), &Ws) != 0) {
printf("Init Windows Socket Failed\n");
return NULL;
}
#endif
si.sin_family = AF_INET;
si.sin_addr.s_addr = host ? inet_addr(host) : INADDR_ANY;
si.sin_port = htons(port);
Expand All @@ -193,11 +219,16 @@ int skt_udp_bind(const char *host, uint16_t port)
return fd;
}

#if defined (__linux__) || defined (__CYGWIN__)
int skt_unix_bind_listen(const char *host, uint16_t port)
{
int fd;
struct sockaddr_un su;

WSADATA Ws;
if (WSAStartup(MAKEWORD(2,2), &Ws) != 0) {
printf("Init Windows Socket Failed\n");
return NULL;
}
su.sun_family = PF_UNIX;
snprintf(su.sun_path, sizeof(su.sun_path), "%s", host);
fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
Expand All @@ -220,6 +251,7 @@ int skt_unix_bind_listen(const char *host, uint16_t port)
close(fd);
return -1;
}
#endif

int skt_accept(int fd, uint32_t *ip, uint16_t *port)
{
Expand All @@ -241,12 +273,15 @@ void skt_close(int fd)
close(fd);
}

#if defined (__linux__) || defined (__CYGWIN__)
int skt_get_tcp_info(int fd, struct tcp_info *tcpi)
{
socklen_t len = sizeof(*tcpi);
return getsockopt(fd, SOL_TCP, TCP_INFO, tcpi, &len);
}
#endif

#if defined (__linux__) || defined (__CYGWIN__)
int skt_get_local_list(skt_addr_list_t **al, int loopback)
{
#ifdef __ANDROID__
Expand Down Expand Up @@ -309,6 +344,7 @@ int skt_get_local_list(skt_addr_list_t **al, int loopback)
#endif
return 0;
}
#endif

int skt_get_remote_addr_by_fd(int fd, struct skt_addr *addr)
{
Expand Down Expand Up @@ -426,11 +462,11 @@ int skt_gethostbyname(skt_addr_list_t **al, const char *name)

host = gethostbyname(name);
if (NULL == host) {
herror("gethostbyname failed!\n");
printf("gethostbyname failed!\n");
return -1;
}
if (host->h_addrtype != AF_INET && host->h_addrtype != AF_INET6) {
herror("addrtype error!\n");
printf("addrtype error!\n");
return -1;
}

Expand Down Expand Up @@ -461,6 +497,7 @@ int skt_gethostbyname(skt_addr_list_t **al, const char *name)
return 0;
}

#if defined (__linux__) || defined (__CYGWIN__)
int skt_get_local_info(void)
{
int fd;
Expand All @@ -472,7 +509,13 @@ int skt_get_local_info(void)
char ip[32] = {0};
char broadAddr[32] = {0};
char subnetMask[32] = {0};

#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
WSADATA Ws;
if (WSAStartup(MAKEWORD(2,2), &Ws) != 0) {
printf("Init Windows Socket Failed\n");
return NULL;
}
#endif
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
close(fd);
Expand Down Expand Up @@ -553,9 +596,11 @@ int skt_get_local_info(void)
close(fd);
return 0;
}
#endif

int skt_set_noblk(int fd, int enable)
{
#if defined (__linux__) || defined (__CYGWIN__)
int flag;
flag = fcntl(fd, F_GETFL);
if (flag == -1) {
Expand All @@ -571,11 +616,13 @@ int skt_set_noblk(int fd, int enable)
printf("fcntl: %s\n", strerror(errno));
return -1;
}
#endif
return 0;
}

int skt_set_block(int fd)
{
#if defined (__linux__) || defined (__CYGWIN__)
int flag;
flag = fcntl(fd, F_GETFL);
if (flag == -1) {
Expand All @@ -587,12 +634,14 @@ int skt_set_block(int fd)
printf("fcntl: %s\n", strerror(errno));
return -1;
}
#endif
return 0;
}

int skt_set_nonblock(int fd)
{
int flag;
#if defined (__linux__) || defined (__CYGWIN__)
flag = fcntl(fd, F_GETFL);
if (flag == -1) {
printf("fcntl: %s\n", strerror(errno));
Expand All @@ -603,6 +652,7 @@ int skt_set_nonblock(int fd)
printf("fcntl: %s\n", strerror(errno));
return -1;
}
#endif
return 0;
}
int skt_set_reuse(int fd, int enable)
Expand Down
8 changes: 6 additions & 2 deletions libskt/libskt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#ifndef __ANDROID__
#if defined (__linux__) || defined (__CYGWIN__)
#include <unistd.h>
#include <ifaddrs.h>
#endif
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#elif defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
#include "libposix4win.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define MAX_ADDR_STRING (65)
#define INET_ADDRSTRLEN 16

//socket structs

Expand Down
4 changes: 4 additions & 0 deletions libskt/test_libskt.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ int tcp_client(const char *host, uint16_t port)
printf("remote ip = %s, port = %d\n", str_ip, g_sc->remote.port);
skt_set_tcp_keepalive(g_sc->fd, 1);
memset(&tcpi, 0, sizeof(tcpi));
#if defined (__linux__) || defined (__CYGWIN__)
ret = skt_get_tcp_info(g_sc->fd, &tcpi);
if (ret == 0) {
printf("unrecovered=%u "
Expand All @@ -126,6 +127,7 @@ int tcp_client(const char *host, uint16_t port)
tcpi.tcpi_snd_cwnd,
tcpi.tcpi_total_retrans); // Total retransmits for entire connection
}
#endif
struct thread *thread = thread_create(tcp_client_thread, g_sc);
if (!thread) {
printf("thread_create failed!\n");
Expand Down Expand Up @@ -243,13 +245,15 @@ void domain_test()
{
void *p;
char str[MAX_ADDR_STRING];
#if defined (__linux__) || defined (__CYGWIN__)
skt_addr_list_t *tmp;
if (0 == skt_get_local_list(&tmp, 0)) {
for (; tmp; tmp = tmp->next) {
skt_addr_ntop(str, tmp->addr.ip);
printf("ip = %s port = %d\n", str, tmp->addr.port);
}
}
#endif

if (0 == skt_getaddrinfo(&tmp, "www.sina.com", "3478")) {
for (; tmp; tmp = tmp->next) {
Expand Down

0 comments on commit f1e3ff0

Please sign in to comment.