21#ifndef EEL_NET_MAXSEND
22#define EEL_NET_MAXSEND (EEL_STRING_MAXUSERSTRING_LENGTH_HINT+4096)
26#include "../jnetlib/netinc.h"
27#define JNL_NO_IMPLEMENTATION
28#include "../jnetlib/asyncdns.h"
50 EEL_F
onConnect(
char *hostNameOwned,
int port,
int block);
60 bool m_had_socketlib_init;
67 m_had_socketlib_init=
false;
75 m_cons.Get()[
x].sock = INVALID_SOCKET;
87 if (
s != INVALID_SOCKET)
105 if (!m_had_socketlib_init)
107 m_had_socketlib_init=1;
109 WSAStartup(
MAKEWORD(1, 1), &wsaData);
117 unsigned int ip=inet_addr(hostNameOwned);
118 if (
m_dns && ip == INADDR_NONE && !block)
120 const int r=
m_dns->resolve(hostNameOwned,&ip);
123 if (
r>0) ip = INADDR_NONE;
125#ifndef EEL_NET_NO_SYNC_DNS
126 else if (ip == INADDR_NONE)
128 struct hostent *he = gethostbyname(hostNameOwned);
129 if (he) ip = *(
int *)he->h_addr;
132 if (hostNameOwned || ip != INADDR_NONE)
134 if (ip != INADDR_NONE)
141 s->hostname = hostNameOwned;
142 s->blockmode = !!block;
158 const int port = (
int) handle;
159 if (port < 1 || port > 65535)
161#ifdef EEL_STRING_DEBUGOUT
162 EEL_STRING_DEBUGOUT(
"tcp_listen(%d): invalid port specified, will never succeed",port);
167 if (!m_had_socketlib_init)
169 m_had_socketlib_init=1;
171 WSAStartup(
MAKEWORD(1, 1), &wsaData);
174 SOCKET *sockptr =
m_listens.GetPtr(port);
177 if (!sockptr)
return -1.0;
181 if (
ss != INVALID_SOCKET)
190 struct sockaddr_in sin;
191 memset((
char *) &sin, 0,
sizeof(sin));
196#ifdef EEL_STRING_DEBUGOUT
197 if (!
fn) EEL_STRING_DEBUGOUT(
"tcp_listen(%d): bad string identifier %f for second parameter (interface)",port,*ifStr);
199 if (
fn && *
fn) sin.sin_addr.s_addr=inet_addr(
fn);
202 if (!sin.sin_addr.s_addr || sin.sin_addr.s_addr==INADDR_NONE) sin.sin_addr.s_addr = INADDR_ANY;
203 sin.sin_family = AF_INET;
204 sin.sin_port = htons( (
short) port );
205 SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
206 if (sock != INVALID_SOCKET)
208 SET_SOCK_DEFAULTS(sock);
209 SET_SOCK_BLOCK(sock,0);
210 if (
bind(sock,(
struct sockaddr *)&sin,
sizeof(sin)) || listen(sock,8)==-1)
217#ifdef EEL_STRING_DEBUGOUT
224 if (!sockptr || *sockptr == INVALID_SOCKET)
return -1;
226 struct sockaddr_in saddr;
227 socklen_t
length =
sizeof(
struct sockaddr_in);
228 SOCKET newsock = accept(*sockptr, (
struct sockaddr *) &saddr, &
length);
229 if (newsock == INVALID_SOCKET)
233 SET_SOCK_DEFAULTS(newsock);
250 WDL_FastString *ws=
NULL;
254 const unsigned int a = ntohl(saddr.sin_addr.s_addr);
255 ws->SetFormatted(128,
"%d.%d.%d.%d",(
a>>24)&0xff,(
a>>16)&0xff,(
a>>8)&0xff,
a&0xff);
259#ifdef EEL_STRING_DEBUGOUT
260 EEL_STRING_DEBUGOUT(
"tcp_listen(%d): bad string identifier %f for third parameter (IP-out)",port,*ipOut);
268 closesocket(newsock);
276 SOCKET
s=socket(AF_INET,SOCK_STREAM,0);
277 if (
s == INVALID_SOCKET)
return 0;
278 SET_SOCK_DEFAULTS(
s);
282 struct sockaddr_in sa={0,};
283 sa.sin_family=AF_INET;
284 sa.sin_addr.s_addr = ip;
285 sa.sin_port = htons(cs->
port);
286 if (!connect(
s,(
struct sockaddr *)&sa,16) || (!cs->
blockmode && JNL_ERRNO == JNL_EINPROGRESS))
299 if (cs->
sock != INVALID_SOCKET)
return 0;
303 unsigned int ip=INADDR_NONE;
322 if (idx>=0 && idx<
m_cons.GetSize())
325#ifdef EEL_STRING_DEBUGOUT
326 if (
s->sock == INVALID_SOCKET && !
s->hostname)
327 EEL_STRING_DEBUGOUT(
"tcp_recv: connection identifier %f is not open",
h);
329 if (
__run(
s) ||
s->sock == INVALID_SOCKET)
return s->state ==
STATE_ERR ? -1 : 0;
331 if (maxlen == 0)
return 0;
333 const int rv=(
int)recv(
s->sock,buf,maxlen,0);
334 if (rv < 0 && !s->blockmode && (JNL_ERRNO == JNL_EWOULDBLOCK || JNL_ERRNO == JNL_ENOTCONN))
return 0;
340#ifdef EEL_STRING_DEBUGOUT
341 EEL_STRING_DEBUGOUT(
"tcp_recv: connection identifier %f is out of range",
h);
349 if (idx>=0 && idx<
m_cons.GetSize())
352#ifdef EEL_STRING_DEBUGOUT
353 if (
s->sock == INVALID_SOCKET && !
s->hostname)
354 EEL_STRING_DEBUGOUT(
"tcp_send: connection identifier %f is not open",
h);
356 if (
__run(
s) ||
s->sock == INVALID_SOCKET)
return s->state ==
STATE_ERR ? -1 : 0;
357 const int rv=(
int)send(
s->sock,src,len,0);
358 if (rv < 0 && !s->blockmode && (JNL_ERRNO == JNL_EWOULDBLOCK || JNL_ERRNO == JNL_ENOTCONN))
return 0;
363#ifdef EEL_STRING_DEBUGOUT
364 EEL_STRING_DEBUGOUT(
"tcp_send: connection identifier %f out of range",
h);
373 if (idx>=0 && idx<
m_cons.GetSize())
376 if (
s->blockmode != block)
379 if (
s->sock != INVALID_SOCKET)
381 SET_SOCK_BLOCK(
s->sock,(block?1:0));
385#ifdef EEL_STRING_DEBUGOUT
386 if (!
s->hostname) EEL_STRING_DEBUGOUT(
"tcp_set_block: connection identifier %f is not open",handle);
394#ifdef EEL_STRING_DEBUGOUT
395 EEL_STRING_DEBUGOUT(
"tcp_set_block: connection identifier %f out of range",handle);
404 if (idx>=0 && idx<
m_cons.GetSize())
407 const bool hadhn = !!
s->hostname;
411 if (
s->sock != INVALID_SOCKET)
414 closesocket(
s->sock);
415 s->sock = INVALID_SOCKET;
420#ifdef EEL_STRING_DEBUGOUT
421 EEL_STRING_DEBUGOUT(
"tcp_close: connection identifier %f is not open",handle);
427#ifdef EEL_STRING_DEBUGOUT
428 EEL_STRING_DEBUGOUT(
"tcp_close: connection identifier %f is out of range",handle);
444 if (
fn) dest=strdup(
fn);
447#ifdef EEL_STRING_DEBUGOUT
448 EEL_STRING_DEBUGOUT(
"tcp_connect(): host string identifier %f invalid",parms[0][0]);
452 if (dest)
return ctx->
onConnect(dest, (
int) (parms[1][0]+0.5), np < 3 || parms[2][0] >= 0.5);
477 int ml = np > 2 ? (
int)parms[2][0] : 4096;
484 WDL_FastString *ws=
NULL;
488 if (
ml<=0) ws->Set(
"");
489 else ws->SetRaw(buf,
ml);
507 WDL_FastString *ws=
NULL;
509 l = ws ? ws->GetLength() : (
int) strlen(
fn);
512 int al=(
int)parms[2][0];
552#ifdef EEL_WANT_DOCUMENTATION
553const char *eel_net_function_reference =
554 "tcp_listen\tport[,\"interface\",#ip_out]\tListens on port specified. Returns less than 0 if could not listen, 0 if no new connection available, or greater than 0 (as a TCP connection ID) if a new connection was made. If a connection made and #ip_out specified, it will be set to the remote IP. interface can be empty for all interfaces, otherwise an interface IP as a string.\0"
555 "tcp_listen_end\tport\tEnds listening on port specified.\0"
556 "tcp_connect\t\"address\",port[,block]\tCreate a new TCP connection to address:port. If block is specified and 0, connection will be made nonblocking. Returns TCP connection ID greater than 0 on success.\0"
557 "tcp_send\tconnection,\"str\"[,len]\tSends a string to connection. Returns -1 on error, 0 if connection is non-blocking and would block, otherwise returns length sent. If len is specified and not less than 1, only the first len bytes of the string parameter will be sent.\0"
558 "tcp_recv\tconnection,#str[,maxlen]\tReceives data from a connection to #str. If maxlen is specified, no more than maxlen bytes will be received. If non-blocking, 0 will be returned if would block. Returns less than 0 if error.\0"
559 "tcp_set_block\tconnection,block\tSets whether a connection blocks.\0"
560 "tcp_close\tconnection\tCloses a TCP connection created by tcp_listen() or tcp_connect().\0"
uint8_t a
Definition Spc_Cpu.h:141
static void shutdown(void)
Definition adplugdb.cpp:297
Definition assocarray.h:352
int do_recv(void *opaque, EEL_F h, char *buf, int maxlen)
Definition eel_net.h:319
@ CONNECTION_ID_BASE
Definition eel_net.h:34
int __run(connection_state *cs)
Definition eel_net.h:297
EEL_F onListen(void *opaque, EEL_F handle, int mode, EEL_F *ifStr, EEL_F *ipOut)
Definition eel_net.h:156
~eel_net_state()
Definition eel_net.h:81
eel_net_state(int max_con, JNL_IAsyncDNS *dns)
Definition eel_net.h:64
EEL_F onClose(void *opaque, EEL_F handle)
Definition eel_net.h:401
EEL_F onConnect(char *hostNameOwned, int port, int block)
Definition eel_net.h:101
int __run_connect(connection_state *cs, unsigned int ip)
Definition eel_net.h:274
int do_send(void *opaque, EEL_F h, const char *src, int len)
Definition eel_net.h:346
@ STATE_CONNECTED
Definition eel_net.h:33
@ STATE_ERR
Definition eel_net.h:33
@ STATE_FREE
Definition eel_net.h:33
@ STATE_RESOLVING
Definition eel_net.h:33
JNL_IAsyncDNS * m_dns
Definition eel_net.h:48
WDL_TypedBuf< connection_state > m_cons
Definition eel_net.h:46
EEL_F set_block(void *opaque, EEL_F handle, bool block)
Definition eel_net.h:370
WDL_IntKeyedArray< SOCKET > m_listens
Definition eel_net.h:47
int * l
Definition inflate.c:1579
unsigned ml
Definition inflate.c:944
unsigned bl
Definition inflate.c:935
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
#define NSEEL_addfunc_varparm(name, min_np, pproc, fptr)
Definition eel_import.h:63
#define NSEEL_addfunc_retval(name, np, pproc, fptr)
Definition eel_import.h:51
void *(* NSEEL_PProc_THIS)(void *data, int data_size, struct _compileContext *ctx)
Definition eel_import.h:40
static EEL_F NSEEL_CGEN_CALL _eel_tcp_send(void *opaque, INT_PTR np, EEL_F **parms)
Definition eel_net.h:497
void EEL_tcp_register()
Definition eel_net.h:539
static EEL_F NSEEL_CGEN_CALL _eel_tcp_close(void *opaque, EEL_F *handle)
Definition eel_net.h:464
static EEL_F NSEEL_CGEN_CALL _eel_tcp_connect(void *opaque, INT_PTR np, EEL_F **parms)
Definition eel_net.h:435
#define EEL_NET_MAXSEND
Definition eel_net.h:22
static EEL_F NSEEL_CGEN_CALL _eel_tcp_listen_end(void *opaque, EEL_F *handle)
Definition eel_net.h:531
static EEL_F NSEEL_CGEN_CALL _eel_tcp_recv(void *opaque, INT_PTR np, EEL_F **parms)
Definition eel_net.h:471
static EEL_F NSEEL_CGEN_CALL _eel_tcp_set_block(void *opaque, EEL_F *handle, EEL_F *bl)
Definition eel_net.h:457
static EEL_F NSEEL_CGEN_CALL _eel_tcp_listen(void *opaque, INT_PTR np, EEL_F **parms)
Definition eel_net.h:524
#define EEL_STRING_GET_FOR_WRITE(x, wr)
Definition eel_strings.h:100
#define EEL_STRING_GET_FOR_INDEX(x, wr)
Definition eel_strings.h:96
#define EEL_STRING_MUTEXLOCK_SCOPE
Definition eel_strings.h:90
#define opaque
Definition eelscript.h:281
#define EEL_NET_GET_CONTEXT(opaque)
Definition eelscript.h:196
#define NSEEL_CGEN_CALL
Definition ns-eel.h:44
png_uint_32 length
Definition png.c:2247
png_structrp int mode
Definition png.h:1139
char * hostname
Definition eel_net.h:40
bool blockmode
Definition eel_net.h:44
int state
Definition eel_net.h:42
SOCKET sock
Definition eel_net.h:41
int port
Definition eel_net.h:43
const char const char const char const char char * fn
Definition swell-functions.h:168
intptr_t INT_PTR
Definition swell-types.h:42
#define MAKEWORD(a, b)
Definition swell-types.h:85
memcpy(hh, h, RAND_HEAD_LEN)
int r
Definition crypt.c:458
uch h[RAND_HEAD_LEN]
Definition crypt.c:459
typedef int(UZ_EXP MsgFn)()
ss
Definition zipinfo.c:2292
#define EEL_STRING_MAXUSERSTRING_LENGTH_HINT
Definition ysfx_api_eel.cpp:35