Skip to content

Commit

Permalink
Add support for IPV6 addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
jheinen committed Jan 3, 2023
1 parent 6e2eda3 commit a31ca6d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ foreach(LIBRARY gks_static gks_shared)
if(UNIX)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} dl)
elseif(WIN32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} wsock32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} ws2_32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} msimg32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} gdi32)
endif()
Expand Down
2 changes: 1 addition & 1 deletion js/jsterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ JSTerm = function(ispluto=false) {
return;
}
if (typeof WEB_SOCKET_ADDRESS === 'undefined') {
WEB_SOCKET_ADDRESS = 'ws://127.0.0.1:8081';
WEB_SOCKET_ADDRESS = 'ws://localhost:8081';
}
ws = new WebSocket(WEB_SOCKET_ADDRESS);
ws.onerror = function(e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/gks/makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OBJS = gks.o gksforbnd.o font.o afm.o util.o ft.o dl.o \
malloc.o error.o mf.o wiss.o win.o ps.o \
pdf.o socket.o plugin.o compress.o io.o resample.o

LIBS = -lwsock32 -lmsimg32 -lgdi32
LIBS = -lws2_32 -lmsimg32 -lgdi32

.SUFFIXES: .o .c

Expand Down
70 changes: 41 additions & 29 deletions lib/gks/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <sys/errno.h>
#else
#define __STRSAFE__NO_INLINE
#define _WIN32_WINNT 0x0602
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <strsafe.h>
#endif
Expand All @@ -42,7 +45,7 @@
#define MAXPATHLEN 1024
#endif

#define PORT 8410
#define PORT "8410"

typedef struct
{
Expand Down Expand Up @@ -173,10 +176,9 @@ static int start(void *cmd)

static int connect_socket(int quiet)
{
int s;
char *env;
struct hostent *hp;
struct sockaddr_in sin;
int rc, s;
char *server;
struct addrinfo hints, *res = NULL;
int opt;

#if defined(_WIN32)
Expand All @@ -190,12 +192,34 @@ static int connect_socket(int quiet)
}
#endif

s = socket(PF_INET, /* get a socket descriptor */
SOCK_STREAM, /* stream socket */
IPPROTO_TCP); /* use TCP protocol */
if (s == -1)
server = (char *)gks_getenv("GKS_CONID");
if (!server) server = (char *)gks_getenv("GKSconid");
if (server)
if (!*server) server = NULL;
if (!server) server = "localhost";

memset(&hints, 0x00, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

if ((rc = getaddrinfo(server, PORT, &hints, &res)) != 0)
{
hints.ai_family = AF_INET6;
if ((rc = getaddrinfo(server, PORT, &hints, &res)) != 0)
{
if (!quiet)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc));
}
return -1;
}
}

s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0)
{
if (!quiet) perror("socket");
freeaddrinfo(res);
return -1;
}

Expand All @@ -204,28 +228,15 @@ static int connect_socket(int quiet)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
#endif

env = (char *)gks_getenv("GKS_CONID");
if (env)
if (!*env) env = NULL;
if (!env) env = (char *)gks_getenv("GKSconid");

if ((hp = gethostbyname(env != NULL ? env : "127.0.0.1")) == 0)
{
if (!quiet) perror("gethostbyname");
return -1;
}

memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr_list[0]))->s_addr;
sin.sin_port = htons(PORT);

if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
if (connect(s, res->ai_addr, res->ai_addrlen) < 0)
{
if (!quiet) perror("connect");
freeaddrinfo(res);
return -1;
}

freeaddrinfo(res);

return s;
}

Expand All @@ -237,8 +248,7 @@ static int open_socket(int wstype)
const char *command = NULL, *env;
char *cmd = NULL;
#endif
int retry_count;
int max_retry_count = 20;
size_t retry_count, max_retry_count = 20;
int s;

/* In order to not sleep an excessive amount start with a short sleep time and then ramp
Expand Down Expand Up @@ -298,7 +308,9 @@ static int open_socket(int wstype)
sleep_ms = retry_count <= n_initial_times ? initial_sleep_time_ms[retry_count - 1] : max_sleep_time_ms;
#ifndef _WIN32
{
struct timespec delay = {0, sleep_ms * ms_to_ns};
struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = sleep_ms * ms_to_ns;
while (nanosleep(&delay, &delay) == -1)
;
}
Expand Down
70 changes: 38 additions & 32 deletions lib/gr/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else
#define _WIN32_WINNT 0x0602
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <winsock.h>
#endif

#define PORT 0x1234

#include "gr.h"
#include "stream.h"
#include "gkscore.h"
Expand All @@ -32,9 +34,9 @@ static int s = -1;

static char *buffer = NULL, *static_buffer = NULL;

static char *hostname = NULL;
static char *server = NULL;

static int port = PORT;
static char *port = "4660"; /* 0x1234 */

static int nbytes = 0, size = 0, static_size = 0;

Expand Down Expand Up @@ -63,9 +65,8 @@ static void save(char *string, int nbytes)

static int sendstream(char *string)
{
int pos, i;
struct hostent *hp;
struct sockaddr_in sin;
int rc, pos, i;
struct addrinfo hints, *res = NULL;
char buf[BUFSIZ + 1];
char *env, *display;

Expand All @@ -85,48 +86,51 @@ static int sendstream(char *string)

if (s == -1)
{
s = socket(PF_INET, /* get a socket descriptor */
SOCK_STREAM, /* stream socket */
IPPROTO_TCP); /* use TCP protocol */
if (s != -1)
if (server == NULL)
{
int size = 128 * 128 * 16;
setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(int));

if (hostname == NULL)
env = (char *)getenv("GR_DISPLAY");
if (env != NULL)
{
env = (char *)getenv("GR_DISPLAY");
if (env != NULL)
{
display = gks_strdup(env);
if ((env = strtok(display, ":")) != NULL) hostname = env;
if ((env = strtok(NULL, ":")) != NULL) port = atoi(env);
}
display = gks_strdup(env);
if ((env = strtok(display, ":")) != NULL) server = env;
if ((env = strtok(NULL, ":")) != NULL) port = env;
}
if (hostname == NULL) hostname = "localhost";
}
if (server == NULL) server = "localhost";

if ((hp = gethostbyname(hostname)) != NULL)
memset(&hints, 0x00, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

if ((rc = getaddrinfo(server, port, &hints, &res)) != 0)
{
hints.ai_family = AF_INET6;
rc = getaddrinfo(server, port, &hints, &res);
}

if (rc == 0)
{
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s != -1)
{
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr_list[0]))->s_addr;
sin.sin_port = htons(port);
int size = 128 * 128 * 16;
setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(int));

if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
if (connect(s, res->ai_addr, res->ai_addrlen) == -1)
{
perror("connect");
status = EXIT_FAILURE;
}
}
else
{
perror("gethostbyname");
perror("socket");
status = EXIT_FAILURE;
}
}
else
{
perror("socket");
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc));
status = EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -160,6 +164,8 @@ static int sendstream(char *string)
}
else if (s != -1)
close_socket(s);

if (res != NULL) freeaddrinfo(res);
}

return status;
Expand Down

0 comments on commit a31ca6d

Please sign in to comment.