1 /* Jim - A small embeddable Tcl interpreter
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of the Jim Tcl Project.
40 #include "jimautoconf.h"
56 #include "jimiocompat.h"
58 #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_SELECT) && defined(HAVE_NETINET_IN_H) && defined(HAVE_NETDB_H) && defined(HAVE_ARPA_INET_H)
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 #include <arpa/inet.h>
68 #elif defined (__MINGW32__)
69 /* currently mingw32 doesn't support sockets, but has pipe, fdopen */
75 #include <openssl/ssl.h>
76 #include <openssl/err.h>
83 #include "jim-eventloop.h"
84 #include "jim-subcmd.h"
86 #define AIO_CMD_LEN 32 /* e.g. aio.handleXXXXXX */
87 #define AIO_BUF_LEN 256 /* Can keep this small and rely on stdio buffering */
96 #define AIO_KEEPOPEN 1
97 #define AIO_NODELETE 2
107 #if defined(HAVE_SYS_UN_H) && defined(PF_UNIX)
108 #define UNIX_SOCKETS 1
110 #define UNIX_SOCKETS 0
114 /* no fdopen() with ANSIC, so can't support these */
116 #undef HAVE_SOCKETPAIR
119 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
120 /* Avoid type punned pointers */
123 struct sockaddr_in sin
;
125 struct sockaddr_in6 sin6
;
128 struct sockaddr_un sun
;
132 #ifndef HAVE_INET_NTOP
133 const char *inet_ntop(int af
, const void *src
, char *dst
, int size
)
138 snprintf(dst
, size
, "%s", inet_ntoa(((struct sockaddr_in
*)src
)->sin_addr
));
142 #endif /* JIM_BOOTSTRAP */
147 int (*writer
)(struct AioFile
*af
, const char *buf
, int len
);
148 int (*reader
)(struct AioFile
*af
, char *buf
, int len
);
149 const char *(*getline
)(struct AioFile
*af
, char *buf
, int len
);
150 int (*error
)(const struct AioFile
*af
);
151 const char *(*strerror
)(struct AioFile
*af
);
152 int (*verify
)(struct AioFile
*af
);
155 typedef struct AioFile
160 int openFlags
; /* AIO_KEEPOPEN? keep FILE* */
167 const JimAioFopsType
*fops
;
170 static int stdio_writer(struct AioFile
*af
, const char *buf
, int len
)
172 return fwrite(buf
, 1, len
, af
->fp
);
175 static int stdio_reader(struct AioFile
*af
, char *buf
, int len
)
177 return fread(buf
, 1, len
, af
->fp
);
180 static const char *stdio_getline(struct AioFile
*af
, char *buf
, int len
)
182 return fgets(buf
, len
, af
->fp
);
185 static int stdio_error(const AioFile
*af
)
187 if (!ferror(af
->fp
)) {
191 /* EAGAIN and similar are not error conditions. Just treat them like eof */
192 if (feof(af
->fp
) || errno
== EAGAIN
|| errno
== EINTR
) {
196 if (errno
== ECONNRESET
) {
201 if (errno
== ECONNABORTED
) {
208 static const char *stdio_strerror(struct AioFile
*af
)
210 return strerror(errno
);
213 static const JimAioFopsType stdio_fops
= {
222 #if defined(JIM_SSL) && !defined(JIM_BOOTSTRAP)
224 static SSL_CTX
*JimAioSslCtx(Jim_Interp
*interp
);
226 static int ssl_writer(struct AioFile
*af
, const char *buf
, int len
)
228 return SSL_write(af
->ssl
, buf
, len
);
231 static int ssl_reader(struct AioFile
*af
, char *buf
, int len
)
233 return SSL_read(af
->ssl
, buf
, len
);
236 static const char *ssl_getline(struct AioFile
*af
, char *buf
, int len
)
239 for (i
= 0; i
< len
+ 1; i
++) {
240 if (SSL_read(af
->ssl
, &buf
[i
], 1) != 1) {
246 if (buf
[i
] == '\n') {
254 static int ssl_error(const struct AioFile
*af
)
256 if (ERR_peek_error() == 0) {
263 static const char *ssl_strerror(struct AioFile
*af
)
265 int err
= ERR_get_error();
268 return ERR_error_string(err
, NULL
);
271 return stdio_strerror(af
);
275 static int ssl_verify(struct AioFile
*af
)
279 cert
= SSL_get_peer_certificate(af
->ssl
);
285 if (SSL_get_verify_result(af
->ssl
) == X509_V_OK
) {
292 static const JimAioFopsType ssl_fops
= {
300 #endif /* JIM_BOOTSTRAP */
302 static int JimAioSubCmdProc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
303 static AioFile
*JimMakeChannel(Jim_Interp
*interp
, FILE *fh
, int fd
, Jim_Obj
*filename
,
304 const char *hdlfmt
, int family
, const char *mode
, int flags
);
306 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
307 #ifndef HAVE_GETADDRINFO
309 * Poor man's getaddrinfo().
310 * hints->ai_family must be set and must be PF_INET or PF_INET6
311 * Only returns the first matching result.
312 * servname must be numeric.
316 socklen_t ai_addrlen
;
317 struct sockaddr
*ai_addr
; /* simply points to ai_storage */
318 union sockaddr_any ai_storage
;
321 static int getaddrinfo(const char *hostname
, const char *servname
,
322 const struct addrinfo
*hints
, struct addrinfo
**res
)
326 unsigned long port
= strtoul(servname
, &end
, 10);
327 if (port
== 0 || port
> 65536 || *end
) {
332 if ((he
= gethostbyname(hostname
)) != NULL
) {
334 for (i
= 0; he
->h_addr_list
[i
]; i
++) {
335 if (he
->h_addrtype
== hints
->ai_family
) {
336 struct addrinfo
*ai
= malloc(sizeof(*ai
));
337 memset(ai
, 0, sizeof(*ai
));
338 ai
->ai_family
= he
->h_addrtype
;
339 ai
->ai_addr
= &ai
->ai_storage
.sa
;
340 if (ai
->ai_family
== PF_INET
) {
341 ai
->ai_addrlen
= sizeof(ai
->ai_storage
.sin
);
342 ai
->ai_storage
.sin
.sin_family
= he
->h_addrtype
;
343 assert(sizeof(ai
->ai_storage
.sin
.sin_addr
) == he
->h_length
);
344 memcpy(&ai
->ai_storage
.sin
.sin_addr
, he
->h_addr_list
[i
], he
->h_length
);
345 ai
->ai_storage
.sin
.sin_port
= htons(port
);
349 ai
->ai_addrlen
= sizeof(ai
->ai_storage
.sin6
);
350 ai
->ai_storage
.sin6
.sin6_family
= he
->h_addrtype
;
351 assert(sizeof(ai
->ai_storage
.sin6
.sin6_addr
) == he
->h_length
);
352 memcpy(&ai
->ai_storage
.sin6
.sin6_addr
, he
->h_addr_list
[i
], he
->h_length
);
353 ai
->ai_storage
.sin6
.sin6_port
= htons(port
);
364 static void freeaddrinfo(struct addrinfo
*ai
)
370 static int JimParseIPv6Address(Jim_Interp
*interp
, const char *hostport
, union sockaddr_any
*sa
, socklen_t
*salen
)
374 * An IPv6 addr/port looks like:
377 * [fe80::223:6cff:fe95:bdc0%en1]:2000
381 * Note that the "any" address is ::, which is the same as when no address is specified.
389 stport
= strrchr(hostport
, ':');
391 /* No : so, the whole thing is the port */
394 sthost
= Jim_StrDup(hostport
);
400 if (*hostport
== '[') {
401 /* This is a numeric ipv6 address */
402 char *pt
= strchr(++hostport
, ']');
404 sthost
= Jim_StrDupLen(hostport
, pt
- hostport
);
409 sthost
= Jim_StrDupLen(hostport
, stport
- hostport
- 1);
412 memset(&req
, '\0', sizeof(req
));
413 req
.ai_family
= PF_INET6
;
415 if (getaddrinfo(sthost
, stport
, &req
, &ai
)) {
416 Jim_SetResultFormatted(interp
, "Not a valid address: %s", hostport
);
420 memcpy(&sa
->sin6
, ai
->ai_addr
, ai
->ai_addrlen
);
421 *salen
= ai
->ai_addrlen
;
428 Jim_SetResultString(interp
, "ipv6 not supported", -1);
433 static int JimParseIpAddress(Jim_Interp
*interp
, const char *hostport
, union sockaddr_any
*sa
, socklen_t
*salen
)
435 /* An IPv4 addr/port looks like:
440 * If the address is missing, INADDR_ANY is used.
441 * If the port is missing, 0 is used (only useful for server sockets).
449 stport
= strrchr(hostport
, ':');
451 /* No : so, the whole thing is the port */
453 sthost
= Jim_StrDup("0.0.0.0");
456 sthost
= Jim_StrDupLen(hostport
, stport
- hostport
);
460 memset(&req
, '\0', sizeof(req
));
461 req
.ai_family
= PF_INET
;
463 if (getaddrinfo(sthost
, stport
, &req
, &ai
)) {
467 memcpy(&sa
->sin
, ai
->ai_addr
, ai
->ai_addrlen
);
468 *salen
= ai
->ai_addrlen
;
474 Jim_SetResultFormatted(interp
, "Not a valid address: %s", hostport
);
481 static int JimParseDomainAddress(Jim_Interp
*interp
, const char *path
, union sockaddr_any
*sa
, socklen_t
*salen
)
483 sa
->sun
.sun_family
= PF_UNIX
;
484 snprintf(sa
->sun
.sun_path
, sizeof(sa
->sun
.sun_path
), "%s", path
);
485 *salen
= strlen(sa
->sun
.sun_path
) + 1 + sizeof(sa
->sun
.sun_family
);
491 static int JimParseSocketAddress(Jim_Interp
*interp
, int family
, const char *addr
, union sockaddr_any
*sa
, socklen_t
*salen
)
496 return JimParseDomainAddress(interp
, addr
, sa
, salen
);
499 return JimParseIPv6Address(interp
, addr
, sa
, salen
);
501 return JimParseIpAddress(interp
, addr
, sa
, salen
);
507 * Format that address in 'sa' as a string and return it as a zero-refcount object.
510 static Jim_Obj
*JimFormatSocketAddress(Jim_Interp
*interp
, const union sockaddr_any
*sa
, socklen_t salen
)
512 /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
514 const char *addr
= addrbuf
;
517 switch (sa
->sa
.sa_family
) {
520 addr
= sa
->sun
.sun_path
;
521 addrlen
= salen
- 1 - sizeof(sa
->sun
.sun_family
);
530 /* Allow 9 for []:65535\0 */
531 inet_ntop(sa
->sa
.sa_family
, &sa
->sin6
.sin6_addr
, addrbuf
+ 1, sizeof(addrbuf
) - 9);
532 snprintf(addrbuf
+ strlen(addrbuf
), 8, "]:%d", ntohs(sa
->sin6
.sin6_port
));
536 /* Allow 7 for :65535\0 */
537 inet_ntop(sa
->sa
.sa_family
, &sa
->sin
.sin_addr
, addrbuf
, sizeof(addrbuf
) - 7);
538 snprintf(addrbuf
+ strlen(addrbuf
), 7, ":%d", ntohs(sa
->sin
.sin_port
));
542 /* Otherwise just an empty address */
547 return Jim_NewStringObj(interp
, addr
, addrlen
);
550 static int JimSetVariableSocketAddress(Jim_Interp
*interp
, Jim_Obj
*varObjPtr
, const union sockaddr_any
*sa
, socklen_t salen
)
553 Jim_Obj
*objPtr
= JimFormatSocketAddress(interp
, sa
, salen
);
554 Jim_IncrRefCount(objPtr
);
555 ret
= Jim_SetVariable(interp
, varObjPtr
, objPtr
);
556 Jim_DecrRefCount(interp
, objPtr
);
560 static Jim_Obj
*aio_sockname(Jim_Interp
*interp
, AioFile
*af
)
562 union sockaddr_any sa
;
563 socklen_t salen
= sizeof(sa
);
565 if (getsockname(af
->fd
, &sa
.sa
, &salen
) < 0) {
568 return JimFormatSocketAddress(interp
, &sa
, salen
);
570 #endif /* JIM_BOOTSTRAP */
572 static const char *JimAioErrorString(AioFile
*af
)
575 return af
->fops
->strerror(af
);
577 return strerror(errno
);
580 static void JimAioSetError(Jim_Interp
*interp
, Jim_Obj
*name
)
582 AioFile
*af
= Jim_CmdPrivData(interp
);
585 Jim_SetResultFormatted(interp
, "%#s: %s", name
, JimAioErrorString(af
));
588 Jim_SetResultString(interp
, JimAioErrorString(af
), -1);
592 static int JimCheckStreamError(Jim_Interp
*interp
, AioFile
*af
)
594 int ret
= af
->fops
->error(af
);
596 JimAioSetError(interp
, af
->filename
);
601 static void JimAioDelProc(Jim_Interp
*interp
, void *privData
)
603 AioFile
*af
= privData
;
608 if (af
->addr_family
== PF_UNIX
&& (af
->openFlags
& AIO_NODELETE
) == 0) {
609 /* If this is bound, delete the socket file now */
610 Jim_Obj
*filenameObj
= aio_sockname(interp
, af
);
612 if (Jim_Length(filenameObj
)) {
613 remove(Jim_String(filenameObj
));
615 Jim_FreeNewObj(interp
, filenameObj
);
620 Jim_DecrRefCount(interp
, af
->filename
);
622 #ifdef jim_ext_eventloop
623 /* remove all existing EventHandlers */
624 Jim_DeleteFileHandler(interp
, af
->fd
, JIM_EVENT_READABLE
| JIM_EVENT_WRITABLE
| JIM_EVENT_EXCEPTION
);
628 if (af
->ssl
!= NULL
) {
632 if (!(af
->openFlags
& AIO_KEEPOPEN
)) {
639 static int aio_cmd_read(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
641 AioFile
*af
= Jim_CmdPrivData(interp
);
642 char buf
[AIO_BUF_LEN
];
645 jim_wide neededLen
= -1; /* -1 is "read as much as possible" */
647 if (argc
&& Jim_CompareStringImmediate(interp
, argv
[0], "-nonewline")) {
653 if (Jim_GetWide(interp
, argv
[0], &neededLen
) != JIM_OK
)
656 Jim_SetResultString(interp
, "invalid parameter: negative len", -1);
663 objPtr
= Jim_NewStringObj(interp
, NULL
, 0);
664 while (neededLen
!= 0) {
668 if (neededLen
== -1) {
669 readlen
= AIO_BUF_LEN
;
672 readlen
= (neededLen
> AIO_BUF_LEN
? AIO_BUF_LEN
: neededLen
);
674 retval
= af
->fops
->reader(af
, buf
, readlen
);
676 Jim_AppendString(interp
, objPtr
, buf
, retval
);
677 if (neededLen
!= -1) {
681 if (retval
!= readlen
)
684 /* Check for error conditions */
685 if (JimCheckStreamError(interp
, af
)) {
686 Jim_FreeNewObj(interp
, objPtr
);
691 const char *s
= Jim_GetString(objPtr
, &len
);
693 if (len
> 0 && s
[len
- 1] == '\n') {
695 objPtr
->bytes
[objPtr
->length
] = '\0';
698 Jim_SetResult(interp
, objPtr
);
702 AioFile
*Jim_AioFile(Jim_Interp
*interp
, Jim_Obj
*command
)
704 Jim_Cmd
*cmdPtr
= Jim_GetCommand(interp
, command
, JIM_ERRMSG
);
706 /* XXX: There ought to be a supported API for this */
707 if (cmdPtr
&& !cmdPtr
->isproc
&& cmdPtr
->u
.native
.cmdProc
== JimAioSubCmdProc
) {
708 return (AioFile
*) cmdPtr
->u
.native
.privData
;
710 Jim_SetResultFormatted(interp
, "Not a filehandle: \"%#s\"", command
);
714 FILE *Jim_AioFilehandle(Jim_Interp
*interp
, Jim_Obj
*command
)
718 af
= Jim_AioFile(interp
, command
);
726 static int aio_cmd_getfd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
728 AioFile
*af
= Jim_CmdPrivData(interp
);
731 Jim_SetResultInt(interp
, fileno(af
->fp
));
736 static int aio_cmd_copy(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
738 AioFile
*af
= Jim_CmdPrivData(interp
);
740 jim_wide maxlen
= JIM_WIDE_MAX
;
741 AioFile
*outf
= Jim_AioFile(interp
, argv
[0]);
748 if (Jim_GetWide(interp
, argv
[1], &maxlen
) != JIM_OK
) {
753 while (count
< maxlen
) {
754 /* A reasonable compromise between stack size and speed */
755 char buf
[AIO_BUF_LEN
];
756 jim_wide len
= maxlen
- count
;
757 if (len
> sizeof(buf
)) {
761 len
= af
->fops
->reader(af
, buf
, len
);
765 if (outf
->fops
->writer(outf
, buf
, len
) != len
) {
771 if (JimCheckStreamError(interp
, af
) || JimCheckStreamError(interp
, outf
)) {
775 Jim_SetResultInt(interp
, count
);
780 static int aio_cmd_gets(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
782 AioFile
*af
= Jim_CmdPrivData(interp
);
783 char buf
[AIO_BUF_LEN
];
789 objPtr
= Jim_NewStringObj(interp
, NULL
, 0);
791 buf
[AIO_BUF_LEN
- 1] = '_';
793 if (af
->fops
->getline(af
, buf
, AIO_BUF_LEN
) == NULL
)
796 if (buf
[AIO_BUF_LEN
- 1] == '\0' && buf
[AIO_BUF_LEN
- 2] != '\n') {
797 Jim_AppendString(interp
, objPtr
, buf
, AIO_BUF_LEN
- 1);
802 if (len
&& (buf
[len
- 1] == '\n')) {
807 Jim_AppendString(interp
, objPtr
, buf
, len
);
812 if (JimCheckStreamError(interp
, af
)) {
814 Jim_FreeNewObj(interp
, objPtr
);
819 if (Jim_SetVariable(interp
, argv
[0], objPtr
) != JIM_OK
) {
820 Jim_FreeNewObj(interp
, objPtr
);
824 len
= Jim_Length(objPtr
);
826 if (len
== 0 && feof(af
->fp
)) {
827 /* On EOF returns -1 if varName was specified */
830 Jim_SetResultInt(interp
, len
);
833 Jim_SetResult(interp
, objPtr
);
838 static int aio_cmd_puts(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
840 AioFile
*af
= Jim_CmdPrivData(interp
);
846 if (!Jim_CompareStringImmediate(interp
, argv
[0], "-nonewline")) {
855 wdata
= Jim_GetString(strObj
, &wlen
);
856 if (af
->fops
->writer(af
, wdata
, wlen
) == wlen
) {
857 if (argc
== 2 || af
->fops
->writer(af
, "\n", 1) == 1) {
861 JimAioSetError(interp
, af
->filename
);
865 static int aio_cmd_isatty(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
868 AioFile
*af
= Jim_CmdPrivData(interp
);
869 Jim_SetResultInt(interp
, isatty(fileno(af
->fp
)));
871 Jim_SetResultInt(interp
, 0);
877 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
878 static int aio_cmd_recvfrom(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
880 AioFile
*af
= Jim_CmdPrivData(interp
);
882 union sockaddr_any sa
;
884 socklen_t salen
= sizeof(sa
);
887 if (Jim_GetLong(interp
, argv
[0], &len
) != JIM_OK
) {
891 buf
= Jim_Alloc(len
+ 1);
893 rlen
= recvfrom(fileno(af
->fp
), buf
, len
, 0, &sa
.sa
, &salen
);
896 JimAioSetError(interp
, NULL
);
900 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, rlen
));
903 return JimSetVariableSocketAddress(interp
, argv
[1], &sa
, salen
);
910 static int aio_cmd_sendto(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
912 AioFile
*af
= Jim_CmdPrivData(interp
);
916 union sockaddr_any sa
;
917 const char *addr
= Jim_String(argv
[1]);
920 if (JimParseSocketAddress(interp
, af
->addr_family
, addr
, &sa
, &salen
) != JIM_OK
) {
923 wdata
= Jim_GetString(argv
[0], &wlen
);
925 /* Note that we don't validate the socket type. Rely on sendto() failing if appropriate */
926 len
= sendto(fileno(af
->fp
), wdata
, wlen
, 0, &sa
.sa
, salen
);
928 JimAioSetError(interp
, NULL
);
931 Jim_SetResultInt(interp
, len
);
935 static int aio_cmd_accept(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
937 AioFile
*af
= Jim_CmdPrivData(interp
);
939 union sockaddr_any sa
;
940 socklen_t salen
= sizeof(sa
);
942 sock
= accept(af
->fd
, &sa
.sa
, &salen
);
944 JimAioSetError(interp
, NULL
);
949 if (JimSetVariableSocketAddress(interp
, argv
[0], &sa
, salen
) != JIM_OK
) {
954 /* Create the file command */
955 return JimMakeChannel(interp
, NULL
, sock
, Jim_NewStringObj(interp
, "accept", -1),
956 "aio.sockstream%ld", af
->addr_family
, "r+", AIO_NODELETE
) ? JIM_OK
: JIM_ERR
;
959 static int aio_cmd_sockname(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
961 AioFile
*af
= Jim_CmdPrivData(interp
);
962 Jim_Obj
*objPtr
= aio_sockname(interp
, af
);
964 if (objPtr
== NULL
) {
965 JimAioSetError(interp
, NULL
);
968 Jim_SetResult(interp
, objPtr
);
972 static int aio_cmd_peername(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
974 AioFile
*af
= Jim_CmdPrivData(interp
);
975 union sockaddr_any sa
;
976 socklen_t salen
= sizeof(sa
);
978 if (getpeername(af
->fd
, &sa
.sa
, &salen
) < 0) {
979 JimAioSetError(interp
, NULL
);
982 Jim_SetResult(interp
, JimFormatSocketAddress(interp
, &sa
, salen
));
986 static int aio_cmd_listen(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
988 AioFile
*af
= Jim_CmdPrivData(interp
);
991 if (Jim_GetLong(interp
, argv
[0], &backlog
) != JIM_OK
) {
995 if (listen(af
->fd
, backlog
)) {
996 JimAioSetError(interp
, NULL
);
1002 #endif /* JIM_BOOTSTRAP */
1004 static int aio_cmd_flush(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1006 AioFile
*af
= Jim_CmdPrivData(interp
);
1008 if (fflush(af
->fp
) == EOF
) {
1009 JimAioSetError(interp
, af
->filename
);
1015 static int aio_cmd_eof(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1017 AioFile
*af
= Jim_CmdPrivData(interp
);
1019 Jim_SetResultInt(interp
, feof(af
->fp
));
1023 static int aio_cmd_close(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1027 #if defined(HAVE_SOCKETS)
1028 static const char * const options
[] = { "r", "w", "-nodelete", NULL
};
1029 enum { OPT_R
, OPT_W
, OPT_NODELETE
};
1030 AioFile
*af
= Jim_CmdPrivData(interp
);
1032 if (Jim_GetEnum(interp
, argv
[2], options
, &option
, NULL
, JIM_ERRMSG
) != JIM_OK
) {
1037 #if defined(HAVE_SHUTDOWN)
1040 if (shutdown(af
->fd
, option
== OPT_R
? SHUT_RD
: SHUT_WR
) == 0) {
1043 JimAioSetError(interp
, NULL
);
1048 if (af
->addr_family
== PF_UNIX
) {
1049 af
->openFlags
|= AIO_NODELETE
;
1055 Jim_SetResultString(interp
, "not supported", -1);
1060 return Jim_DeleteCommand(interp
, Jim_String(argv
[0]));
1063 static int aio_cmd_seek(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1065 AioFile
*af
= Jim_CmdPrivData(interp
);
1066 int orig
= SEEK_SET
;
1070 if (Jim_CompareStringImmediate(interp
, argv
[1], "start"))
1072 else if (Jim_CompareStringImmediate(interp
, argv
[1], "current"))
1074 else if (Jim_CompareStringImmediate(interp
, argv
[1], "end"))
1080 if (Jim_GetWide(interp
, argv
[0], &offset
) != JIM_OK
) {
1083 if (fseeko(af
->fp
, offset
, orig
) == -1) {
1084 JimAioSetError(interp
, af
->filename
);
1090 static int aio_cmd_tell(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1092 AioFile
*af
= Jim_CmdPrivData(interp
);
1094 Jim_SetResultInt(interp
, ftello(af
->fp
));
1098 static int aio_cmd_filename(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1100 AioFile
*af
= Jim_CmdPrivData(interp
);
1102 Jim_SetResult(interp
, af
->filename
);
1107 static int aio_cmd_ndelay(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1109 AioFile
*af
= Jim_CmdPrivData(interp
);
1111 int fmode
= fcntl(af
->fd
, F_GETFL
);
1116 if (Jim_GetLong(interp
, argv
[0], &nb
) != JIM_OK
) {
1125 (void)fcntl(af
->fd
, F_SETFL
, fmode
);
1127 Jim_SetResultInt(interp
, (fmode
& O_NONBLOCK
) ? 1 : 0);
1132 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
1133 #define SOCKOPT_BOOL 0
1134 #define SOCKOPT_INT 1
1135 #define SOCKOPT_TIMEVAL 2 /* not currently supported */
1137 static const struct sockopt_def
{
1141 int type
; /* SOCKOPT_xxx */
1145 { "broadcast", SOL_SOCKET
, SO_BROADCAST
},
1148 { "debug", SOL_SOCKET
, SO_DEBUG
},
1151 { "keepalive", SOL_SOCKET
, SO_KEEPALIVE
},
1154 { "nosigpipe", SOL_SOCKET
, SO_NOSIGPIPE
},
1157 { "oobinline", SOL_SOCKET
, SO_OOBINLINE
},
1160 { "sndbuf", SOL_SOCKET
, SO_SNDBUF
, SOCKOPT_INT
},
1163 { "rcvbuf", SOL_SOCKET
, SO_RCVBUF
, SOCKOPT_INT
},
1165 #if 0 && defined(SO_SNDTIMEO)
1166 { "sndtimeo", SOL_SOCKET
, SO_SNDTIMEO
, SOCKOPT_TIMEVAL
},
1168 #if 0 && defined(SO_RCVTIMEO)
1169 { "rcvtimeo", SOL_SOCKET
, SO_RCVTIMEO
, SOCKOPT_TIMEVAL
},
1174 { "tcp_nodelay", IPPROTO_TCP
, TCP_NODELAY
},
1179 static int aio_cmd_sockopt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1181 AioFile
*af
= Jim_CmdPrivData(interp
);
1185 Jim_Obj
*dictObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
1186 for (i
= 0; i
< sizeof(sockopts
) / sizeof(*sockopts
); i
++) {
1188 socklen_t len
= sizeof(value
);
1189 if (getsockopt(af
->fd
, sockopts
[i
].level
, sockopts
[i
].opt
, (void *)&value
, &len
) == 0) {
1190 if (sockopts
[i
].type
== SOCKOPT_BOOL
) {
1193 Jim_ListAppendElement(interp
, dictObjPtr
, Jim_NewStringObj(interp
, sockopts
[i
].name
, -1));
1194 Jim_ListAppendElement(interp
, dictObjPtr
, Jim_NewIntObj(interp
, value
));
1197 Jim_SetResult(interp
, dictObjPtr
);
1205 for (i
= 0; i
< sizeof(sockopts
) / sizeof(*sockopts
); i
++) {
1206 if (strcmp(Jim_String(argv
[0]), sockopts
[i
].name
) == 0) {
1208 if (sockopts
[i
].type
== SOCKOPT_BOOL
) {
1209 if (Jim_GetBoolean(interp
, argv
[1], &on
) != JIM_OK
) {
1215 if (Jim_GetLong(interp
, argv
[1], &longval
) != JIM_OK
) {
1220 if (setsockopt(af
->fd
, sockopts
[i
].level
, sockopts
[i
].opt
, (void *)&on
, sizeof(on
)) < 0) {
1221 Jim_SetResultFormatted(interp
, "Failed to set %#s: %s", argv
[0], strerror(errno
));
1228 Jim_SetResultFormatted(interp
, "Unknown sockopt %#s", argv
[0]);
1231 #endif /* JIM_BOOTSTRAP */
1234 static int aio_cmd_sync(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1236 AioFile
*af
= Jim_CmdPrivData(interp
);
1244 static int aio_cmd_buffering(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1246 AioFile
*af
= Jim_CmdPrivData(interp
);
1248 static const char * const options
[] = {
1262 if (Jim_GetEnum(interp
, argv
[0], options
, &option
, NULL
, JIM_ERRMSG
) != JIM_OK
) {
1267 setvbuf(af
->fp
, NULL
, _IONBF
, 0);
1270 setvbuf(af
->fp
, NULL
, _IOLBF
, BUFSIZ
);
1273 setvbuf(af
->fp
, NULL
, _IOFBF
, BUFSIZ
);
1279 #ifdef jim_ext_eventloop
1280 static void JimAioFileEventFinalizer(Jim_Interp
*interp
, void *clientData
)
1282 Jim_Obj
**objPtrPtr
= clientData
;
1284 Jim_DecrRefCount(interp
, *objPtrPtr
);
1288 static int JimAioFileEventHandler(Jim_Interp
*interp
, void *clientData
, int mask
)
1290 Jim_Obj
**objPtrPtr
= clientData
;
1292 return Jim_EvalObjBackground(interp
, *objPtrPtr
);
1295 static int aio_eventinfo(Jim_Interp
*interp
, AioFile
* af
, unsigned mask
, Jim_Obj
**scriptHandlerObj
,
1296 int argc
, Jim_Obj
* const *argv
)
1299 /* Return current script */
1300 if (*scriptHandlerObj
) {
1301 Jim_SetResult(interp
, *scriptHandlerObj
);
1306 if (*scriptHandlerObj
) {
1307 /* Delete old handler */
1308 Jim_DeleteFileHandler(interp
, af
->fd
, mask
);
1311 /* Now possibly add the new script(s) */
1312 if (Jim_Length(argv
[0]) == 0) {
1313 /* Empty script, so done */
1317 /* A new script to add */
1318 Jim_IncrRefCount(argv
[0]);
1319 *scriptHandlerObj
= argv
[0];
1321 Jim_CreateFileHandler(interp
, af
->fd
, mask
,
1322 JimAioFileEventHandler
, scriptHandlerObj
, JimAioFileEventFinalizer
);
1327 static int aio_cmd_readable(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1329 AioFile
*af
= Jim_CmdPrivData(interp
);
1331 return aio_eventinfo(interp
, af
, JIM_EVENT_READABLE
, &af
->rEvent
, argc
, argv
);
1334 static int aio_cmd_writable(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1336 AioFile
*af
= Jim_CmdPrivData(interp
);
1338 return aio_eventinfo(interp
, af
, JIM_EVENT_WRITABLE
, &af
->wEvent
, argc
, argv
);
1341 static int aio_cmd_onexception(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1343 AioFile
*af
= Jim_CmdPrivData(interp
);
1345 return aio_eventinfo(interp
, af
, JIM_EVENT_EXCEPTION
, &af
->eEvent
, argc
, argv
);
1349 #if defined(JIM_SSL) && !defined(JIM_BOOTSTRAP)
1350 static int aio_cmd_ssl(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1352 AioFile
*af
= Jim_CmdPrivData(interp
);
1358 if (!Jim_CompareStringImmediate(interp
, argv
[2], "-server")) {
1363 else if (argc
!= 2) {
1368 Jim_SetResultFormatted(interp
, "%#s: stream is already ssl", argv
[0]);
1372 ssl_ctx
= JimAioSslCtx(interp
);
1373 if (ssl_ctx
== NULL
) {
1377 ssl
= SSL_new(ssl_ctx
);
1382 SSL_set_cipher_list(ssl
, "ALL");
1384 if (SSL_set_fd(ssl
, fileno(af
->fp
)) == 0) {
1389 if (SSL_use_certificate_file(ssl
, Jim_String(argv
[3]), SSL_FILETYPE_PEM
) != 1) {
1393 if (SSL_use_PrivateKey_file(ssl
, Jim_String(argv
[4]), SSL_FILETYPE_PEM
) != 1) {
1397 if (SSL_accept(ssl
) != 1) {
1402 if (SSL_connect(ssl
) != 1) {
1408 af
->fops
= &ssl_fops
;
1410 /* Set the command name as the result */
1411 Jim_SetResult(interp
, argv
[0]);
1419 Jim_SetResultString(interp
, ERR_error_string(ERR_get_error(), NULL
), -1);
1423 static int aio_cmd_verify(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1425 AioFile
*af
= Jim_CmdPrivData(interp
);
1428 if (!af
->fops
->verify
) {
1432 ret
= af
->fops
->verify(af
);
1433 if (ret
!= JIM_OK
) {
1434 if (JimCheckStreamError(interp
, af
) == JIM_OK
) {
1435 Jim_SetResultString(interp
, "failed to verify the connection authenticity", -1);
1440 #endif /* JIM_BOOTSTRAP */
1442 #if defined(HAVE_STRUCT_FLOCK) && !defined(JIM_BOOTSTRAP)
1443 static int aio_cmd_lock(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1445 AioFile
*af
= Jim_CmdPrivData(interp
);
1447 int lockmode
= F_SETLK
;
1450 if (!Jim_CompareStringImmediate(interp
, argv
[0], "-wait")) {
1453 lockmode
= F_SETLKW
;
1458 fl
.l_type
= F_WRLCK
;
1459 fl
.l_whence
= SEEK_SET
;
1461 switch (fcntl(af
->fd
, lockmode
, &fl
))
1464 Jim_SetResultInt(interp
, 1);
1467 if (errno
== EACCES
|| errno
== EAGAIN
)
1468 Jim_SetResultInt(interp
, 0);
1471 Jim_SetResultFormatted(interp
, "lock failed: %s",
1477 Jim_SetResultInt(interp
, 0);
1484 static int aio_cmd_unlock(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1486 AioFile
*af
= Jim_CmdPrivData(interp
);
1490 fl
.l_type
= F_UNLCK
;
1491 fl
.l_whence
= SEEK_SET
;
1493 Jim_SetResultInt(interp
, fcntl(af
->fd
, F_SETLK
, &fl
) == 0);
1496 #endif /* JIM_BOOTSTRAP */
1498 #if defined(HAVE_TERMIOS_H) && !defined(JIM_BOOTSTRAP)
1499 static int aio_cmd_tty(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1501 AioFile
*af
= Jim_CmdPrivData(interp
);
1502 Jim_Obj
*dictObjPtr
;
1506 /* get the current settings as a dictionary */
1507 dictObjPtr
= Jim_GetTtySettings(interp
, af
->fd
);
1508 if (dictObjPtr
== NULL
) {
1509 JimAioSetError(interp
, NULL
);
1512 Jim_SetResult(interp
, dictObjPtr
);
1517 /* Convert name value arguments to a dictionary */
1518 dictObjPtr
= Jim_NewListObj(interp
, argv
, argc
);
1521 /* The settings are already given as a list */
1522 dictObjPtr
= argv
[0];
1524 Jim_IncrRefCount(dictObjPtr
);
1526 if (Jim_ListLength(interp
, dictObjPtr
) % 2) {
1527 /* Must be a valid dictionary */
1528 Jim_DecrRefCount(interp
, dictObjPtr
);
1532 ret
= Jim_SetTtySettings(interp
, af
->fd
, dictObjPtr
);
1534 JimAioSetError(interp
, NULL
);
1537 Jim_DecrRefCount(interp
, dictObjPtr
);
1541 #endif /* JIM_BOOTSTRAP */
1543 static const jim_subcmd_type aio_command_table
[] = {
1545 "?-nonewline? ?len?",
1549 /* Description: Read and return bytes from the stream. To eof if no len. */
1556 /* Description: Copy up to 'size' bytes to the given filehandle, or to eof if no size. */
1563 /* Description: Internal command to return the underlying file descriptor. */
1570 /* Description: Read one line and return it or store it in the var */
1577 /* Description: Write the string, with newline unless -nonewline */
1584 /* Description: Is the file descriptor a tty? */
1586 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
1592 /* Description: Receive up to 'len' bytes on the socket. Sets 'addrvar' with receive address, if set */
1599 /* Description: Send 'str' to the given address (dgram only) */
1606 /* Description: Server socket only: Accept a connection and return stream */
1613 /* Description: Set the listen backlog for server socket */
1620 /* Description: Return a dictionary of sockopts, or set the value of a sockopt */
1627 /* Description: Returns the local address of the socket, if any */
1634 /* Description: Returns the remote address of the socket, if any */
1636 #endif /* JIM_BOOTSTRAP */
1642 /* Description: Flush the stream */
1649 /* Description: Returns 1 if stream is at eof */
1656 JIM_MODFLAG_FULLARGV
,
1657 /* Description: Closes the stream. */
1660 "offset ?start|current|end",
1664 /* Description: Seeks in the stream (default 'current') */
1671 /* Description: Returns the current seek position */
1678 /* Description: Returns the original filename */
1686 /* Description: Set O_NDELAY (if arg). Returns current/new setting. */
1695 /* Description: Flush and fsync() the stream */
1703 /* Description: Sets buffering */
1705 #ifdef jim_ext_eventloop
1707 "?readable-script?",
1711 /* Description: Returns script, or invoke readable-script when readable, {} to remove */
1714 "?writable-script?",
1718 /* Description: Returns script, or invoke writable-script when writable, {} to remove */
1721 "?exception-script?",
1722 aio_cmd_onexception
,
1725 /* Description: Returns script, or invoke exception-script when oob data, {} to remove */
1728 #if !defined(JIM_BOOTSTRAP)
1729 #if defined(JIM_SSL)
1731 "?-server cert priv?",
1735 JIM_MODFLAG_FULLARGV
1736 /* Description: Wraps a stream socket with SSL/TLS and returns a new channel */
1743 /* Description: Verifies the certificate of a SSL/TLS channel */
1746 #if defined(HAVE_STRUCT_FLOCK)
1752 /* Description: Attempt to get a lock, possibly waiting */
1759 /* Description: Relase a lock. */
1762 #if defined(HAVE_TERMIOS_H)
1764 "?baud rate? ?data bits? ?stop bits? ?parity even|odd|none? ?handshake xonxoff|rtscts|none? ?input raw|cooked? ?output raw|cooked? ?echo 0|1? ?vmin n? ?vtime n?",
1768 /* Description: Get or set tty settings - valid only on a tty */
1771 #endif /* JIM_BOOTSTRAP */
1775 static int JimAioSubCmdProc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1777 return Jim_CallSubCmd(interp
, Jim_ParseSubCmd(interp
, aio_command_table
, argc
, argv
), argc
, argv
);
1780 static int JimAioOpenCommand(Jim_Interp
*interp
, int argc
,
1781 Jim_Obj
*const *argv
)
1785 if (argc
!= 2 && argc
!= 3) {
1786 Jim_WrongNumArgs(interp
, 1, argv
, "filename ?mode?");
1790 mode
= (argc
== 3) ? Jim_String(argv
[2]) : "r";
1792 #ifdef jim_ext_tclcompat
1794 const char *filename
= Jim_String(argv
[1]);
1796 /* If the filename starts with '|', use popen instead */
1797 if (*filename
== '|') {
1798 Jim_Obj
*evalObj
[3];
1800 evalObj
[0] = Jim_NewStringObj(interp
, "::popen", -1);
1801 evalObj
[1] = Jim_NewStringObj(interp
, filename
+ 1, -1);
1802 evalObj
[2] = Jim_NewStringObj(interp
, mode
, -1);
1804 return Jim_EvalObjVector(interp
, 3, evalObj
);
1808 return JimMakeChannel(interp
, NULL
, -1, argv
[1], "aio.handle%ld", 0, mode
, 0) ? JIM_OK
: JIM_ERR
;
1811 #if defined(JIM_SSL) && !defined(JIM_BOOTSTRAP)
1812 static void JimAioSslContextDelProc(struct Jim_Interp
*interp
, void *privData
)
1814 SSL_CTX_free((SSL_CTX
*)privData
);
1818 #ifdef USE_TLSv1_2_method
1819 #define TLS_method TLSv1_2_method
1822 static SSL_CTX
*JimAioSslCtx(Jim_Interp
*interp
)
1824 SSL_CTX
*ssl_ctx
= (SSL_CTX
*)Jim_GetAssocData(interp
, "ssl_ctx");
1825 if (ssl_ctx
== NULL
) {
1826 SSL_load_error_strings();
1828 ssl_ctx
= SSL_CTX_new(TLS_method());
1829 if (ssl_ctx
&& SSL_CTX_set_default_verify_paths(ssl_ctx
)) {
1830 SSL_CTX_set_verify(ssl_ctx
, SSL_VERIFY_NONE
, NULL
);
1831 Jim_SetAssocData(interp
, "ssl_ctx", JimAioSslContextDelProc
, ssl_ctx
);
1833 Jim_SetResultString(interp
, ERR_error_string(ERR_get_error(), NULL
), -1);
1838 #endif /* JIM_BOOTSTRAP */
1841 * Creates a channel for fh/fd/filename.
1843 * If fh is not NULL, uses that as the channel (and sets AIO_KEEPOPEN).
1844 * Otherwise, if fd is >= 0, uses that as the channel.
1845 * Otherwise opens 'filename' with mode 'mode'.
1847 * hdlfmt is a sprintf format for the filehandle. Anything with %ld at the end will do.
1848 * mode is used for open or fdopen.
1850 * Creates the command and sets the name as the current result.
1851 * Returns the AioFile pointer on sucess or NULL on failure.
1853 static AioFile
*JimMakeChannel(Jim_Interp
*interp
, FILE *fh
, int fd
, Jim_Obj
*filename
,
1854 const char *hdlfmt
, int family
, const char *mode
, int flags
)
1857 char buf
[AIO_CMD_LEN
];
1859 snprintf(buf
, sizeof(buf
), hdlfmt
, Jim_GetId(interp
));
1861 filename
= Jim_NewStringObj(interp
, buf
, -1);
1864 Jim_IncrRefCount(filename
);
1869 fh
= fdopen(fd
, mode
);
1873 fh
= fopen(Jim_String(filename
), mode
);
1876 JimAioSetError(interp
, filename
);
1882 Jim_DecrRefCount(interp
, filename
);
1887 /* Create the file command */
1888 af
= Jim_Alloc(sizeof(*af
));
1889 memset(af
, 0, sizeof(*af
));
1891 af
->filename
= filename
;
1892 af
->openFlags
= flags
;
1894 af
->fd
= fileno(fh
);
1896 if ((flags
& AIO_KEEPOPEN
) == 0) {
1897 (void)fcntl(af
->fd
, F_SETFD
, FD_CLOEXEC
);
1901 af
->addr_family
= family
;
1902 af
->fops
= &stdio_fops
;
1905 Jim_CreateCommand(interp
, buf
, JimAioSubCmdProc
, af
, JimAioDelProc
);
1907 /* Note that the command must use the global namespace, even if
1908 * the current namespace is something different
1910 Jim_SetResult(interp
, Jim_MakeGlobalNamespaceName(interp
, Jim_NewStringObj(interp
, buf
, -1)));
1915 #if defined(HAVE_PIPE) || (defined(HAVE_SOCKETPAIR) && UNIX_SOCKETS)
1917 * Create a pair of channels. e.g. from pipe() or socketpair()
1919 static int JimMakeChannelPair(Jim_Interp
*interp
, int p
[2], Jim_Obj
*filename
,
1920 const char *hdlfmt
, int family
, const char * const mode
[2])
1922 if (JimMakeChannel(interp
, NULL
, p
[0], filename
, hdlfmt
, family
, mode
[0], 0)) {
1923 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
1924 Jim_ListAppendElement(interp
, objPtr
, Jim_GetResult(interp
));
1925 if (JimMakeChannel(interp
, NULL
, p
[1], filename
, hdlfmt
, family
, mode
[1], 0)) {
1926 Jim_ListAppendElement(interp
, objPtr
, Jim_GetResult(interp
));
1927 Jim_SetResult(interp
, objPtr
);
1932 /* Can only be here if fdopen() failed */
1935 JimAioSetError(interp
, NULL
);
1941 static int JimAioPipeCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1944 static const char * const mode
[2] = { "r", "w" };
1947 Jim_WrongNumArgs(interp
, 1, argv
, "");
1952 JimAioSetError(interp
, NULL
);
1956 return JimMakeChannelPair(interp
, p
, argv
[0], "aio.pipe%ld", 0, mode
);
1960 #if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
1962 static int JimAioSockCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1964 const char *socktypes
[] = {
1968 "unix.dgram.server",
1982 SOCK_UNIX_DGRAM_SERVER
,
1988 SOCK_STREAM_SOCKETPAIR
,
1992 const char *addr
= NULL
;
1993 const char *bind_addr
= NULL
;
1994 const char *connect_addr
= NULL
;
1995 union sockaddr_any sa
;
2000 int family
= PF_INET
;
2001 int type
= SOCK_STREAM
;
2002 Jim_Obj
*argv0
= argv
[0];
2005 if (argc
> 1 && Jim_CompareStringImmediate(interp
, argv
[1], "-ipv6")) {
2007 Jim_SetResultString(interp
, "ipv6 not supported", -1);
2018 Jim_WrongNumArgs(interp
, 1, &argv0
, "?-ipv6? type ?address?");
2022 if (Jim_GetEnum(interp
, argv
[1], socktypes
, &socktype
, "socket type", JIM_ERRMSG
) != JIM_OK
)
2023 return Jim_CheckShowCommands(interp
, argv
[1], socktypes
);
2025 Jim_SetEmptyResult(interp
);
2028 addr
= Jim_String(argv
[2]);
2031 #if defined(HAVE_SOCKETPAIR) && UNIX_SOCKETS
2032 if (socktype
== SOCK_STREAM_SOCKETPAIR
) {
2034 static const char * const mode
[2] = { "r+", "r+" };
2040 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, p
) < 0) {
2041 JimAioSetError(interp
, NULL
);
2044 return JimMakeChannelPair(interp
, p
, argv
[1], "aio.sockpair%ld", PF_UNIX
, mode
);
2048 #if defined(HAVE_PIPE)
2049 if (socktype
== SOCK_STREAM_PIPE
) {
2053 return JimAioPipeCommand(interp
, 1, &argv
[1]);
2057 /* Now all these socket types are very similar */
2059 case SOCK_DGRAM_CLIENT
:
2060 connect_addr
= addr
;
2064 case SOCK_STREAM_CLIENT
:
2068 connect_addr
= addr
;
2071 case SOCK_STREAM_SERVER
:
2080 case SOCK_DGRAM_SERVER
:
2094 connect_addr
= addr
;
2098 case SOCK_UNIX_DGRAM
:
2099 connect_addr
= addr
;
2102 /* A dgram unix domain socket client needs to bind
2103 * to a temporary address to allow the server to
2107 int tmpfd
= Jim_MakeTempFile(interp
, NULL
, 1);
2112 /* This will be valid until a result is next set, which is long enough here */
2113 bind_addr
= Jim_String(Jim_GetResult(interp
));
2117 case SOCK_UNIX_SERVER
:
2126 case SOCK_UNIX_DGRAM_SERVER
:
2137 Jim_SetResultString(interp
, "Unsupported socket type", -1);
2141 /* Now do all the steps necessary for the given socket type */
2142 sock
= socket(family
, type
, 0);
2144 JimAioSetError(interp
, NULL
);
2148 if (JimParseSocketAddress(interp
, family
, bind_addr
, &sa
, &salen
) != JIM_OK
) {
2153 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
, sizeof(on
));
2155 if (bind(sock
, &sa
.sa
, salen
)) {
2156 Jim_SetResultFormatted(interp
, "%s: bind: %s", bind_addr
, strerror(errno
));
2162 if (JimParseSocketAddress(interp
, family
, connect_addr
, &sa
, &salen
) != JIM_OK
) {
2166 if (connect(sock
, &sa
.sa
, salen
)) {
2167 Jim_SetResultFormatted(interp
, "%s: connect: %s", connect_addr
, strerror(errno
));
2173 if (listen(sock
, 5)) {
2174 Jim_SetResultFormatted(interp
, "listen: %s", strerror(errno
));
2180 return JimMakeChannel(interp
, NULL
, sock
, argv
[1], "aio.sock%ld", family
, "r+", 0) ? JIM_OK
: JIM_ERR
;
2182 #endif /* JIM_BOOTSTRAP */
2184 #if defined(JIM_SSL) && !defined(JIM_BOOTSTRAP)
2185 static int JimAioLoadSSLCertsCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2190 Jim_WrongNumArgs(interp
, 1, argv
, "dir");
2194 ssl_ctx
= JimAioSslCtx(interp
);
2198 if (SSL_CTX_load_verify_locations(ssl_ctx
, NULL
, Jim_String(argv
[1])) == 1) {
2201 Jim_SetResultString(interp
, ERR_error_string(ERR_get_error(), NULL
), -1);
2204 #endif /* JIM_BOOTSTRAP */
2206 int Jim_aioInit(Jim_Interp
*interp
)
2208 if (Jim_PackageProvide(interp
, "aio", "1.0", JIM_ERRMSG
))
2211 #if defined(JIM_SSL)
2212 Jim_CreateCommand(interp
, "load_ssl_certs", JimAioLoadSSLCertsCommand
, NULL
, NULL
);
2215 Jim_CreateCommand(interp
, "open", JimAioOpenCommand
, NULL
, NULL
);
2217 Jim_CreateCommand(interp
, "socket", JimAioSockCommand
, NULL
, NULL
);
2220 Jim_CreateCommand(interp
, "pipe", JimAioPipeCommand
, NULL
, NULL
);
2223 /* Create filehandles for stdin, stdout and stderr */
2224 JimMakeChannel(interp
, stdin
, -1, NULL
, "stdin", 0, "r", AIO_KEEPOPEN
);
2225 JimMakeChannel(interp
, stdout
, -1, NULL
, "stdout", 0, "w", AIO_KEEPOPEN
);
2226 JimMakeChannel(interp
, stderr
, -1, NULL
, "stderr", 0, "w", AIO_KEEPOPEN
);