Project creation; initial screen send capability
This commit is contained in:
commit
0243fc3e13
8 files changed
+333
No files matched your search
@@ -0,0 +1,3 @@
|
||||
*.exe
|
||||
example/example
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Matthew R. Wilson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,24 @@
|
||||
Go 3270 Server Library
|
||||
======================
|
||||
|
||||
This library allows you to write Go servers for tn3270 clients by building 3270 data streams from fields and processing the client's response to receive the attention keys and field values entered by users.
|
||||
|
||||
**The library is incomplete, likely buggy, and under heavy development: the interface is UNSTABLE until this notice is removed from this readme and version 1.0 is released.**
|
||||
|
||||
Everything I know about 3270 data streams I learned from [Tommy Sprinkle's tutorial][sprinkle]. The tn3270 telnet negotiation is gleaned from [RFC 1576: TN3270 Current Practices][rfc1576], [RFC 1041: Telnet 3270 Regime Option][rfc1041], and [RFC 854: Telnet Protocol Specification][rfc854].
|
||||
|
||||
[sprinkle]: http://www.tommysprinkle.com/mvs/P3270/
|
||||
[rfc1576]: https://tools.ietf.org/html/rfc1576
|
||||
[rfc1041]: https://tools.ietf.org/html/rfc1041
|
||||
[rfc854]: https://tools.ietf.org/html/rfc854
|
||||
|
||||
Known Problems
|
||||
--------------
|
||||
|
||||
- The telnet data is not checked for the special telnet byte value, 0xFF, which requires escaping while sending and unescaping while receiving data. If your 3270 data streams contain an FF character, things will probably break.
|
||||
- The telnet negotiation does not check for any errors or for any responses from the client. We just assume it goes well and we're actually talking to a tn3270 client.
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
This library is licensed under the MIT license; see the file LICENSE for details.
|
||||
@@ -0,0 +1,64 @@
|
||||
// This file is part of https://github.com/racingmars/go3270/
|
||||
// Copyright 2020 by Matthew R. Wilson, licensed under the MIT license. See
|
||||
// LICENSE in the project root for license information.
|
||||
|
||||
package go3270
|
||||
|
||||
// Each index in this array is the ASCII value, and the value at the index is
|
||||
// the corresponding EBCDIC (codepage 37) value.
|
||||
var ebcdic = []byte{
|
||||
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31, 64, 79, 127, 123, 91, 108,
|
||||
80, 125, 77, 93, 92, 78, 107, 96, 75, 97, 240, 241, 242, 243, 244, 245,
|
||||
246, 247, 248, 249, 122, 94, 76, 126, 110, 111, 124, 193, 194, 195, 196,
|
||||
197, 198, 199, 200, 201, 209, 210, 211, 212, 213, 214, 215, 216, 217, 226,
|
||||
227, 228, 229, 230, 231, 232, 233, 74, 224, 90, 95, 109, 121, 129, 130,
|
||||
131, 132, 133, 134, 135, 136, 137, 145, 146, 147, 148, 149, 150, 151, 152,
|
||||
153, 162, 163, 164, 165, 166, 167, 168, 169, 192, 106, 208, 161, 7, 32,
|
||||
33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27, 48, 49, 26, 51,
|
||||
52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62, 225, 65, 66, 67, 68, 69, 70, 71,
|
||||
72, 73, 81, 82, 83, 84, 85, 86, 87, 88, 89, 98, 99, 100, 101, 102, 103,
|
||||
104, 105, 112, 113, 114, 115, 116, 117, 118, 119, 120, 128, 138, 139, 140,
|
||||
141, 142, 143, 144, 154, 155, 156, 157, 158, 159, 160, 170, 171, 172, 173,
|
||||
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
|
||||
189, 190, 191, 202, 203, 204, 205, 206, 207, 218, 219, 220, 221, 222, 223,
|
||||
234, 235, 236, 237, 238, 239, 250, 251, 252, 253, 254, 255}
|
||||
|
||||
// Each index in this array is the EBCDIC (codepage 37) value, and the value
|
||||
// at the index is the corresponding ASCII value.
|
||||
var ascii = []byte{
|
||||
0, 1, 2, 3, 156, 9, 134, 127, 151, 141, 142, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 157, 133, 8, 135, 24, 25, 146, 143, 28, 29, 30, 31,
|
||||
128, 129, 130, 131, 132, 10, 23, 27, 136, 137, 138, 139, 140, 5, 6, 7,
|
||||
144, 145, 22, 147, 148, 149, 150, 4, 152, 153, 154, 155, 20, 21, 158, 26,
|
||||
32, 160, 161, 162, 163, 164, 165, 166, 167, 168, 91, 46, 60, 40, 43, 33,
|
||||
38, 169, 170, 171, 172, 173, 174, 175, 176, 177, 93, 36, 42, 41, 59, 94,
|
||||
45, 47, 178, 179, 180, 181, 182, 183, 184, 185, 124, 44, 37, 95, 62, 63,
|
||||
186, 187, 188, 189, 190, 191, 192, 193, 194, 96, 58, 35, 64, 39, 61, 34,
|
||||
195, 97, 98, 99, 100, 101, 102, 103, 104, 105, 196, 197, 198, 199, 200,
|
||||
201, 202, 106, 107, 108, 109, 110, 111, 112, 113, 114, 203, 204, 205, 206,
|
||||
207, 208, 209, 126, 115, 116, 117, 118, 119, 120, 121, 122, 210, 211, 212,
|
||||
213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
|
||||
228, 229, 230, 231, 123, 65, 66, 67, 68, 69, 70, 71, 72, 73, 232, 233,
|
||||
234, 235, 236, 237, 125, 74, 75, 76, 77, 78, 79, 80, 81, 82, 238, 239,
|
||||
240, 241, 242, 243, 92, 159, 83, 84, 85, 86, 87, 88, 89, 90, 244, 245,
|
||||
246, 247, 248, 249, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 250, 251, 252,
|
||||
253, 254, 255}
|
||||
|
||||
// a2e converts the input byte array, a, from ASCII to EBCDIC.
|
||||
func a2e(a []byte) []byte {
|
||||
result := make([]byte, len(a))
|
||||
for i := 0; i < len(a); i++ {
|
||||
result[i] = ebcdic[a[i]]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// e2a converts the input byte array, e, from EBCDIC to ASCII
|
||||
func e2a(e []byte) []byte {
|
||||
result := make([]byte, len(e))
|
||||
for i := 0; i < len(e); i++ {
|
||||
result[i] = ascii[e[i]]
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// This file is part of https://github.com/racingmars/go3270/
|
||||
// Copyright 2020 by Matthew R. Wilson, licensed under the MIT license. See
|
||||
// LICENSE in the project root for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/racingmars/go3270"
|
||||
)
|
||||
|
||||
var loginScreen = go3270.Screen{
|
||||
{Row: 0, Col: 0, Intense: true, Content: "Testing . . ."},
|
||||
{Row: 1, Col: 0, Content: "Name . . ."},
|
||||
{Row: 1, Col: 14, Name: "name", Write: true},
|
||||
}
|
||||
|
||||
func main() {
|
||||
ln, err := net.Listen("tcp", ":3270")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go handle(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func handle(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
go3270.NegotiateTelnet(conn)
|
||||
err := go3270.WriteScreen(loginScreen, 10, 15, conn)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for {
|
||||
rbuf := make([]byte, 255)
|
||||
n, err := conn.Read(rbuf)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
fmt.Printf("%x", rbuf[i])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
// This file is part of https://github.com/racingmars/go3270/
|
||||
// Copyright 2020 by Matthew R. Wilson, licensed under the MIT license. See
|
||||
// LICENSE in the project root for license information.
|
||||
|
||||
package go3270
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Field is a field on the 3270 screen.
|
||||
type Field struct {
|
||||
// Row is the row, 0-based, that the field attribute character should
|
||||
// begin at. This library currently only supports 24 rows, so Row must
|
||||
// be 0-23.
|
||||
Row int
|
||||
|
||||
// Col is the column, 0-based, that the field attribute character should
|
||||
// begin at. This library currently only supposed 80 columns, so Column
|
||||
// must be 0-79.
|
||||
Col int
|
||||
|
||||
// Text is the content of the field to display.
|
||||
Content string
|
||||
|
||||
// Write allows the user to edit the value of the field.
|
||||
Write bool
|
||||
|
||||
// Intense indicates this field should be displayed with high intensity.
|
||||
Intense bool
|
||||
|
||||
// Name is the name of this field, which is used to get the user-entered
|
||||
// data. All writeable fields on a screen must have a unique name.
|
||||
Name string
|
||||
}
|
||||
|
||||
// Screen is an array of Fields which compose a complete 3270 screen.
|
||||
// No checking is performed for lack of overlapping fields, unique field
|
||||
// names,
|
||||
type Screen []Field
|
||||
|
||||
// WriteScreen writes the 3270 datastream for the screen to a writer. Fields
|
||||
// that aren't valid (e.g. outside of the 24x80 screen) are silently ignored.
|
||||
// After writing the fields, the curser is set to crow, ccol, which are
|
||||
// 0-based positions: row 0-23 and col 0-79. Errors from io.Writer.Write()
|
||||
// are returned if encountered.
|
||||
func WriteScreen(screen Screen, crow, ccol int, w io.Writer) error {
|
||||
var b bytes.Buffer
|
||||
|
||||
b.WriteByte(0xf5) // Erase/Write to terminal
|
||||
b.WriteByte(0xc3) // WCC = Reset, Unlock Keyboard, Reset MDT
|
||||
|
||||
// Build the commands for each field on the screen
|
||||
for _, fld := range screen {
|
||||
if fld.Row < 0 || fld.Row > 23 || fld.Col < 0 || fld.Col > 79 {
|
||||
// Invalid field position
|
||||
continue
|
||||
}
|
||||
|
||||
b.Write(sba(fld.Row, fld.Col))
|
||||
b.Write(sf(fld.Write, fld.Intense))
|
||||
if fld.Content != "" {
|
||||
b.Write(a2e([]byte(fld.Content)))
|
||||
}
|
||||
}
|
||||
|
||||
// Set cursor position. Correct out-of-bounds values to 0.
|
||||
if crow < 0 || crow > 23 {
|
||||
crow = 0
|
||||
}
|
||||
if ccol < 0 || ccol > 79 {
|
||||
ccol = 0
|
||||
}
|
||||
//b.Write(ic(crow, ccol))
|
||||
|
||||
b.Write([]byte{0xff, 0xef}) // Telnet IAC EOR
|
||||
|
||||
// Now write the datastream to the writer, returning any potential error.
|
||||
fmt.Printf("%x\n", b.Bytes())
|
||||
_, err := w.Write(b.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
// sba is the "set buffer address" 3270 command.
|
||||
func sba(row, col int) []byte {
|
||||
result := make([]byte, 1, 3)
|
||||
result[0] = 0x11 // SBA
|
||||
result = append(result, getpos(row, col)...)
|
||||
return result
|
||||
}
|
||||
|
||||
// sf is the "start field" 3270 command
|
||||
func sf(write, intense bool) []byte {
|
||||
result := make([]byte, 2)
|
||||
result[0] = 0x1d // SF
|
||||
if !write {
|
||||
result[1] |= 1 << 5 // set "bit 2"
|
||||
}
|
||||
if intense {
|
||||
result[1] |= 1 << 3 // set "bit 4"
|
||||
}
|
||||
// Fill in top 2 bits with appropriate values
|
||||
result[1] = codes[result[1]]
|
||||
return result
|
||||
}
|
||||
|
||||
// ic is the "insert cursor" 3270 command. This function will include the
|
||||
// appropriate SBA command.
|
||||
func ic(row, col int) []byte {
|
||||
result := make([]byte, 0, 3)
|
||||
result = append(result, sba(row, col)...)
|
||||
result = append(result, 0x13) // IC
|
||||
return result
|
||||
}
|
||||
|
||||
// getpos translates row and col to buffer address control characters.
|
||||
func getpos(row, col int) []byte {
|
||||
result := make([]byte, 2)
|
||||
address := row*80 + col
|
||||
hi := (address & 0xfc0) >> 6
|
||||
lo := address & 0x3f
|
||||
result[0] = codes[hi]
|
||||
result[1] = codes[lo]
|
||||
return result
|
||||
}
|
||||
|
||||
// codes are the 3270 control character I/O codes, pre-computed as provided
|
||||
// at http://www.tommysprinkle.com/mvs/P3270/iocodes.htm
|
||||
var codes = []byte{0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
|
||||
0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0xd1, 0xd2, 0xd3, 0xd4,
|
||||
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60,
|
||||
0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x6a, 0x6b, 0x6c,
|
||||
0x6d, 0x6e, 0x6f, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
|
||||
0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f}
|
||||
@@ -0,0 +1,27 @@
|
||||
// This file is part of https://github.com/racingmars/go3270/
|
||||
// Copyright 2020 by Matthew R. Wilson, licensed under the MIT license. See
|
||||
// LICENSE in the project root for license information.
|
||||
|
||||
package go3270
|
||||
|
||||
import "io"
|
||||
|
||||
// NegotiateTelnet will naively (e.g. not checking client responses) negotiate
|
||||
// the options necessary for tn3270 on a new telnet connection, conn.
|
||||
func NegotiateTelnet(conn io.ReadWriter) error {
|
||||
rbuf := make([]byte, 255)
|
||||
|
||||
conn.Write([]byte{0xff, 0xfd, 0x18}) // DO TermType
|
||||
conn.Read(rbuf)
|
||||
conn.Write([]byte{0xff, 0xfa, 0x18, 0x01, 0xff, 0xf0}) // TermType suboptions
|
||||
conn.Read(rbuf)
|
||||
conn.Write([]byte{0xff, 0xfd, 0x19}) // DO EOR
|
||||
conn.Read(rbuf)
|
||||
conn.Write([]byte{0xff, 0xfd, 0x00}) // DO Binary
|
||||
conn.Read(rbuf)
|
||||
|
||||
conn.Write([]byte{0xff, 0xfb, 0x19, 0xff, 0xfb, 0x00}) // WILL binary, eor
|
||||
conn.Read(rbuf)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user