Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: sockif.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)sockif.fth 1.1 04/09/07 | |
43 | purpose: Generic socket interface | |
44 | copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | headerless | |
48 | ||
49 | 0 constant PRREQ_ATTACH \ New socket has been created | |
50 | 1 constant PRREQ_DETACH \ Socket is being closed | |
51 | 2 constant PRREQ_BIND \ Bind local address to socket | |
52 | 3 constant PRREQ_CONNECT \ Accept connection (SOCK_STREAM only) | |
53 | d# 4 constant PRREQ_LISTEN \ Listen for connections (SOCK_STREAM only) | |
54 | d# 5 constant PRREQ_ACCEPT \ Accept connection from peer | |
55 | d# 6 constant PRREQ_SEND \ Send data on connected socket | |
56 | d# 7 constant PRREQ_SENDTO \ Send data | |
57 | d# 8 constant PRREQ_RECV \ Receive data from connected socket | |
58 | d# 9 constant PRREQ_RECVFROM \ Receive data (Non-blocking) | |
59 | ||
60 | fload ${BP}/pkg/netinet/udp-reqs.fth | |
61 | fload ${BP}/pkg/netinet/tcp-reqs.fth | |
62 | ||
63 | headerless | |
64 | ||
65 | : soreq-execute ( ?? sockaddr req# -- ?? ) | |
66 | swap >so-type w@ case | |
67 | SOCK_STREAM of tcp-prreq-execute endof | |
68 | SOCK_DGRAM of udp-prreq-execute endof | |
69 | ( default ) ." Unknown socket type" -1 throw | |
70 | endcase | |
71 | ; | |
72 | ||
73 | \ Create a socket. | |
74 | : socreate ( family type protocol -- sockaddr ) | |
75 | /socket dup alloc-mem tuck swap erase ( family type proto sockaddr ) | |
76 | tuck >so-protocol w! ( family type sockaddr ) | |
77 | tuck >so-type w! ( family sockaddr ) | |
78 | tuck >so-family w! ( sockaddr ) | |
79 | dup dup PRREQ_ATTACH soreq-execute ( sockaddr ) | |
80 | ; | |
81 | ||
82 | \ Close a socket and terminate connection. | |
83 | : soclose ( sockaddr -- ) | |
84 | dup dup PRREQ_DETACH soreq-execute ( sockaddr ) | |
85 | /socket free-mem ( ) | |
86 | ; | |
87 | ||
88 | \ Assign a local protocol address to a socket. | |
89 | : sobind ( sockaddr addr addrlen -- ) | |
90 | 2 pick PRREQ_BIND soreq-execute | |
91 | ; | |
92 | ||
93 | \ Initiate a connection on a socket. | |
94 | : soconnect ( sockaddr srvaddr addrlen -- 0 | error# ) | |
95 | 2 pick dup >r PRREQ_CONNECT soreq-execute dup 0< if | |
96 | dup r@ soerror! | |
97 | then r> drop | |
98 | ; | |
99 | ||
100 | \ Listen for connections on a socket. | |
101 | : solisten ( sockaddr backlog -- 0 | error# ) | |
102 | over dup >r PRREQ_LISTEN soreq-execute dup 0< if | |
103 | dup r@ soerror! | |
104 | then r> drop | |
105 | ; | |
106 | ||
107 | \ Accept a connection on a socket. This implementation does not create | |
108 | \ a new socket (i.e, the listening socket is the connected socket). | |
109 | : soaccept ( sockaddr fromaddr addrlen -- 0 | error# ) | |
110 | 2 pick dup >r PRREQ_ACCEPT soreq-execute dup 0< if | |
111 | dup r@ soerror! | |
112 | then r> drop | |
113 | ; | |
114 | ||
115 | \ Send a message from a socket. Used with connected sockets. | |
116 | : sosend ( sockaddr buf nbytes flags -- #sent | error# ) | |
117 | 3 pick dup >r PRREQ_SEND soreq-execute dup 0< if | |
118 | dup r@ soerror! | |
119 | then r> drop | |
120 | ; | |
121 | ||
122 | \ Send a message from a socket. | |
123 | : sosendto ( sockaddr buf nbytes flags toaddr addrlen -- #sent | error# ) | |
124 | 5 pick dup >r PRREQ_SENDTO soreq-execute dup 0< if | |
125 | dup r@ soerror! | |
126 | then r> drop | |
127 | ; | |
128 | ||
129 | \ Receive a message from a socket. Blocking. | |
130 | : sorecv ( sockaddr buf nbytes flags -- #rcvd | error# ) | |
131 | 3 pick dup >r PRREQ_RECV soreq-execute dup 0< if | |
132 | dup r@ soerror! | |
133 | then r> drop | |
134 | ; | |
135 | ||
136 | \ Receive a message from a socket. Non-blocking. | |
137 | : sorecvfrom ( sockaddr buf nbytes flags fromaddr addrlen -- #rcvd | error# ) | |
138 | 5 pick dup >r PRREQ_RECVFROM soreq-execute dup 0< if | |
139 | dup r@ soerror! | |
140 | then r> drop | |
141 | ; | |
142 | ||
143 | \ Decode error on connection. | |
144 | : .soerror ( sockaddr -- ) | |
145 | soerror@ case | |
146 | EHOSTUNREACH of ." (Host is unreachable)" endof | |
147 | ECONNREFUSED of ." (Connection refused)" endof | |
148 | ETIMEDOUT of ." (Connection timed out)" endof | |
149 | ECONNRESET of ." (Connection reset)" endof | |
150 | EOPNOTSUPP of ." (Socket operation not supported)" endof | |
151 | endcase | |
152 | ; | |
153 | ||
154 | headers |