[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-apl] Wrote a FIO∆SockHelper function
From: |
Christian Robert |
Subject: |
[Bug-apl] Wrote a FIO∆SockHelper function |
Date: |
Wed, 22 Apr 2015 20:24:28 -0400 |
User-agent: |
Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
Hi,
I wrote a "FIO∆SockHelper" function that may be included (if you wish)
into
)lib 5 FILE_IO
how does it help ?
FIO∆SockHelper 'AF_INET' 'SOCK_STREAM' 'IPPROTO_TCP'
2 1 6
FIO∆SockHelper 'AF_INET' '132.207.4.11' 587
2 2228159499 587
example session slightly edited with "//" comments:
-----------------------------------------------------------------------------------------
$ aplt
______ _ __ __ __ ___ ____ __
/ ____// | / // / / / / | / __ \ / /
/ / __ / |/ // / / / / /| | / /_/ // /
/ /_/ // /| // /_/ / / ___ | / ____// /___
\____//_/ |_/ \____/ /_/ |_|/_/ /_____/
Welcome to GNU APL version 1.5 / 610
Copyright (C) 2008-2015 Dr. Jürgen Sauermann
Banner by FIGlet: www.figlet.org
This program comes with ABSOLUTELY NO WARRANTY;
for details run: /usr/local/bin/apl --gpl.
This program is free software, and you are welcome to redistribute it
according to the GNU Public License (GPL) version 3 or later.
)load 5 FILE_IO
loading )DUMP file /usr/local/lib/apl/wslib5/FILE_IO.apl...
WAS CLEAR WS
)copy Sock
SAVED 2015-04-22 19:32:28 (GMT-4)
// Allocate an af_inet/stream/tcp socket
s←FIO∆socket FIO∆SockHelper 'AF_INET' 'SOCK_STREAM' 'IPPROTO_TCP'
s
5
// Connect that socket to 132.207.4.11 at port 587
(FIO∆SockHelper 'AF_INET' '132.207.4.11' 587) FIO∆connect s
0
// read from that socket
a←5000 FIO∆recv s // ps: bug, it refused to recv without the left
'5000, the DOC says it is optional
// got 83 bytes including the final \r\n
⍴a
83
// show the bytes in ASCII text
⎕av[1+a]
220 smtp.polymtl.ca ESMTP Sendmail 8.14.3/8.14.3; Wed, 22 Apr 2015 19:35:38
-0400
// Close the socket
FIO∆fclose s
0
-----------------------------------------------------------------------------------------
$ cat workspaces/SockHelper.apl
#!/usr/local/bin/apl --script
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝
⍝ SockHelper 2015-04-22 20:05:46 (GMT-4)
⍝
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
∇z←FIO∆SockHelper what;this;t
→((≡what)<2)/Single
z←FIO∆SockHelper ¨ what
→(∧/1=,⊃⍴¨,¨z)/FinalDisclose
⎕ES 'Error: Problem converting at least one of the parameters'
→0
FinalDisclose:
z←,⊃⊃¨z ⍝ Be sure we return a vector of hopefully integers
→0
Single:
⍝
⍝ Most usefull socket constant
⍝
t←0 2⍴'' 0
t←t⍪"AF_UNSPEC" 0
t←t⍪"AF_LOCAL" 1
t←t⍪"AF_UNIX" 1
t←t⍪"AF_INET" 2
t←t⍪"AF_SNA" 22
t←t⍪"AF_DECnet" 12
t←t⍪"AF_APPLETALK" 5
t←t⍪"AF_ROUTE" 16
t←t⍪"AF_IPX" 4
t←t⍪"AF_ISDN" 34
t←t⍪"AF_INET6" 10
t←t⍪"AF_BLUETOOTH" 31
t←t⍪"AF_MAX" 41
t←t⍪"PF_INET" 2
t←t⍪"PF_LOCAL" 1
t←t⍪"PF_UNIX" 1
t←t⍪"SOCK_STREAM" 1
t←t⍪"SOCK_DGRAM" 2
t←t⍪"SOCK_RAW" 3
t←t⍪"SOCK_RDM" 4
t←t⍪"SOCK_SEQPACKET" 5
t←t⍪"SOCK_DCCP" 6
t←t⍪"SOCK_PACKET" 10
t←t⍪"SOCK_CLOEXEC" 524288
t←t⍪"SOCK_NONBLOCK" 2048
t←t⍪"IPPROTO_IP" 0
t←t⍪"IPPROTO_ICMP" 1
t←t⍪"IPPROTO_IPIP" 4
t←t⍪"IPPROTO_TCP" 6
t←t⍪"IPPROTO_PUP" 12
t←t⍪"IPPROTO_UDP" 17
t←t⍪"IPPROTO_IDP" 22
t←t⍪"IPPROTO_TP" 29
t←t⍪"IPPROTO_DCCP" 33
t←t⍪"IPPROTO_IPV6" 41
t←t⍪"IPPROTO_ROUTING" 43
t←t⍪"IPPROTO_FRAGMENT" 44
t←t⍪"IPPROTO_RSVP" 46
t←t⍪"IPPROTO_GRE" 47
t←t⍪"IPPROTO_ESP" 50
t←t⍪"IPPROTO_AH" 51
t←t⍪"IPPROTO_ICMPV6" 58
t←t⍪"IPPROTO_NONE" 59
t←t⍪"IPPROTO_DSTOPTS" 60
t←t⍪"IPPROTO_MTP" 92
t←t⍪"IPPROTO_ENCAP" 98
t←t⍪"IPPROTO_PIM" 103
t←t⍪"IPPROTO_COMP" 108
t←t⍪"IPPROTO_SCTP" 132
t←t⍪"IPPROTO_UDPLITE" 136
t←t⍪"IPPROTO_RAW" 255
t←t⍪"SOL_SOCKET" 1
t←t⍪"SO_BINDTODEVICE" 25
t←t⍪"SO_REUSEADDR" 2
t←t⍪"SO_BROADCAST" 6
t←t⍪"INADDR_ANY" 0
⍝
⍝ Replace thoses I recognize by their value
⍝ leaving the rest as they were
⍝
→(0=↑0⍴what)/NotSpecial ⍝ can't be an IP
→(3≠+/'.'=,what)/NotSpecial ⍝ can't be an IP
this←,what ⍝ an IP address
(('.'=this)/this)←' ' ⍝ DOT to space
z←(4⍴256)⊥⍎this ⍝ change this IP to integer
→0
NotSpecial:
this←⊂what ⍝ enclose the input for diadic '⍳'
z←(,t[;1]) ⍳ this ⍝ search for recognized constants into 't'
→(z>↑⍴t)/ReturnAsis ⍝ no match
z←t[z;2] ⍝ do the substitution
→0
ReturnAsis: z←what ⍝ return original parameter unchanged
∇
⎕CT←1E¯13
⎕FC←(,⎕UCS 46 44 8902 48 95 175)
⎕IO←1
⎕L←0
⎕LX←' ' ⍝ prototype...
⎕LX←0⍴⎕LX
⎕PP←10
⎕PR←' '
⎕PS←0
⎕PW←500
⎕R←0
⎕RL←16807
⎕TZ←-4
⎕X←0
The 't' part generated by:
$ cat sockconst.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define P(a) printf ("t←t⍪\"%s\" %d\n", #a, a)
int main (void)
{
printf ("t←0 2⍴'' 0\n");
#ifdef AF_UNSPEC
P (AF_UNSPEC);
#endif
#ifdef AF_LOCAL
P (AF_LOCAL);
#endif
#ifdef AF_UNIX
P (AF_UNIX);
#endif
#ifdef AF_INET
P (AF_INET);
#endif
#ifdef AF_SNA
P (AF_SNA);
#endif
#ifdef AF_DECnet
P (AF_DECnet);
#endif
#ifdef AF_APPLETALK
P (AF_APPLETALK);
#endif
#ifdef AF_ROUTE
P (AF_ROUTE);
#endif
#ifdef AF_IPX
P (AF_IPX);
#endif
#ifdef AF_ISDN
P (AF_ISDN);
#endif
#ifdef AF_INET6
P (AF_INET6);
#endif
#ifdef AF_BLUETOOTH
P (AF_BLUETOOTH);
#endif
#ifdef AF_MAX
P (AF_MAX);
#endif
#ifdef PF_INET
P (PF_INET);
#endif
#ifdef PF_LOCAL
P (PF_LOCAL);
#endif
#ifdef PF_UNIX
P (PF_UNIX);
#endif
#ifdef SOCK_STREAM
P (SOCK_STREAM);
#endif
#ifdef SOCK_DGRAM
P (SOCK_DGRAM);
#endif
#ifdef SOCK_RAW
P (SOCK_RAW);
#endif
#ifdef SOCK_RDM
P (SOCK_RDM);
#endif
#ifdef SOCK_SEQPACKET
P (SOCK_SEQPACKET);
#endif
#ifdef SOCK_DCCP
P (SOCK_DCCP);
#endif
#ifdef SOCK_PACKET
P (SOCK_PACKET);
#endif
#ifdef SOCK_CLOEXEC
P (SOCK_CLOEXEC);
#endif
#ifdef SOCK_NONBLOCK
P (SOCK_NONBLOCK);
#endif
#ifdef IPPROTO_IP
P (IPPROTO_IP);
#endif
#ifdef IPPROTO_ICMP
P (IPPROTO_ICMP);
#endif
#ifdef IPPROTO_IPIP
P (IPPROTO_IPIP);
#endif
#ifdef IPPROTO_TCP
P (IPPROTO_TCP);
#endif
#ifdef IPPROTO_PUP
P (IPPROTO_PUP);
#endif
#ifdef IPPROTO_UDP
P (IPPROTO_UDP);
#endif
#ifdef IPPROTO_IDP
P (IPPROTO_IDP);
#endif
#ifdef IPPROTO_TP
P (IPPROTO_TP);
#endif
#ifdef IPPROTO_DCCP
P (IPPROTO_DCCP);
#endif
#ifdef IPPROTO_IPV6
P (IPPROTO_IPV6);
#endif
#ifdef IPPROTO_ROUTING
P (IPPROTO_ROUTING);
#endif
#ifdef IPPROTO_FRAGMENT
P (IPPROTO_FRAGMENT);
#endif
#ifdef IPPROTO_RSVP
P (IPPROTO_RSVP);
#endif
#ifdef IPPROTO_GRE
P (IPPROTO_GRE);
#endif
#ifdef IPPROTO_ESP
P (IPPROTO_ESP);
#endif
#ifdef IPPROTO_AH
P (IPPROTO_AH);
#endif
#ifdef IPPROTO_ICMPV6
P (IPPROTO_ICMPV6);
#endif
#ifdef IPPROTO_NONE
P (IPPROTO_NONE);
#endif
#ifdef IPPROTO_DSTOPTS
P (IPPROTO_DSTOPTS);
#endif
#ifdef IPPROTO_MTP
P (IPPROTO_MTP);
#endif
#ifdef IPPROTO_ENCAP
P (IPPROTO_ENCAP);
#endif
#ifdef IPPROTO_PIM
P (IPPROTO_PIM);
#endif
#ifdef IPPROTO_COMP
P (IPPROTO_COMP);
#endif
#ifdef IPPROTO_SCTP
P (IPPROTO_SCTP);
#endif
#ifdef IPPROTO_UDPLITE
P (IPPROTO_UDPLITE);
#endif
#ifdef IPPROTO_RAW
P (IPPROTO_RAW);
#endif
#ifdef SOL_SOCKET
P (SOL_SOCKET);
#endif
#ifdef SO_BINDTODEVICE
P (SO_BINDTODEVICE);
#endif
#ifdef SO_REUSEADDR
P (SO_REUSEADDR);
#endif
#ifdef SO_BROADCAST
P (SO_BROADCAST);
#endif
#ifdef INADDR_ANY
P (INADDR_ANY);
#endif
return 0;
}
$ gcc -Wall -o sockconst sockconst.c
$ ./sockconst
t←0 2⍴'' 0
t←t⍪"AF_UNSPEC" 0
t←t⍪"AF_LOCAL" 1
t←t⍪"AF_UNIX" 1
t←t⍪"AF_INET" 2
t←t⍪"AF_SNA" 22
t←t⍪"AF_DECnet" 12
t←t⍪"AF_APPLETALK" 5
t←t⍪"AF_ROUTE" 16
t←t⍪"AF_IPX" 4
t←t⍪"AF_ISDN" 34
t←t⍪"AF_INET6" 10
t←t⍪"AF_BLUETOOTH" 31
t←t⍪"AF_MAX" 41
t←t⍪"PF_INET" 2
t←t⍪"PF_LOCAL" 1
t←t⍪"PF_UNIX" 1
t←t⍪"SOCK_STREAM" 1
t←t⍪"SOCK_DGRAM" 2
t←t⍪"SOCK_RAW" 3
t←t⍪"SOCK_RDM" 4
t←t⍪"SOCK_SEQPACKET" 5
t←t⍪"SOCK_DCCP" 6
t←t⍪"SOCK_PACKET" 10
t←t⍪"SOCK_CLOEXEC" 524288
t←t⍪"SOCK_NONBLOCK" 2048
t←t⍪"IPPROTO_IP" 0
t←t⍪"IPPROTO_ICMP" 1
t←t⍪"IPPROTO_IPIP" 4
t←t⍪"IPPROTO_TCP" 6
t←t⍪"IPPROTO_PUP" 12
t←t⍪"IPPROTO_UDP" 17
t←t⍪"IPPROTO_IDP" 22
t←t⍪"IPPROTO_TP" 29
t←t⍪"IPPROTO_DCCP" 33
t←t⍪"IPPROTO_IPV6" 41
t←t⍪"IPPROTO_ROUTING" 43
t←t⍪"IPPROTO_FRAGMENT" 44
t←t⍪"IPPROTO_RSVP" 46
t←t⍪"IPPROTO_GRE" 47
t←t⍪"IPPROTO_ESP" 50
t←t⍪"IPPROTO_AH" 51
t←t⍪"IPPROTO_ICMPV6" 58
t←t⍪"IPPROTO_NONE" 59
t←t⍪"IPPROTO_DSTOPTS" 60
t←t⍪"IPPROTO_MTP" 92
t←t⍪"IPPROTO_ENCAP" 98
t←t⍪"IPPROTO_PIM" 103
t←t⍪"IPPROTO_COMP" 108
t←t⍪"IPPROTO_SCTP" 132
t←t⍪"IPPROTO_UDPLITE" 136
t←t⍪"IPPROTO_RAW" 255
t←t⍪"SOL_SOCKET" 1
t←t⍪"SO_BINDTODEVICE" 25
t←t⍪"SO_REUSEADDR" 2
t←t⍪"SO_BROADCAST" 6
t←t⍪"INADDR_ANY" 0
$
Feel free to use any/all of this code (.c or .apl) as you wish, especially if
it's to be included in whole,
in part or as a starting point idea into GNU APL. Feel free to rename function,
improve the code, etc.
Christian Robert.
- [Bug-apl] Wrote a FIO∆SockHelper function,
Christian Robert <=