go_ethernet_ip

package module
v0.0.0-...-27dd23c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 13 Imported by: 1

README

go-ethernet-ip

standard-readme compliant

A complete golang implementation of Ethernet/ip protocol.

This repository contains:

  1. A implementation of ethernet/ip protocol.
  2. A lightweight message router.
  3. Session management.
  4. A lightweight api interface makes you don't need to focus binary steam.
  5. Examples of go-ethernet-ip.

Table of Contents

Background

go-ethernet-ip started with the my own project goplc which used for communication with rockwell control/compactLogix PLCs.

I separate common industrial protocol from ethernet/ip.

If your communication protocol is common industrial protocol, you should move to go-cip which base on this repository.

Install

This project uses golang. Go check them out if you don't have them locally installed.

$ go get github.com/loki-os/go-ethernet-ip

Also go modules is supported.

Usage

I use some cip cases for demonstration.

Before you use these cases.You should block your main thread.Because all logic run in go routine.

func block(){
	some_case()

	// you'd better find other way to do this. Sleep is not recommended.
	time.Sleep(time.Second * 10)
}
Find all LAN devices

Before we start to communication with other device, we need to find them via lan. If you have clear ip skip this step.

devices, err := GetLanDevices(time.Second)
New device
device, err := NewDevice("192.168.0.100")
Device connection config

Device Connect() is optional.You can skip this step to use default config.

Connect() should be call before other function, otherwise it will fail.

cfg := DefaultConfig()
cfg.TCPTimeout = time.Second * 5

device.Connect(cfg)
List identity
identity, err := device.ListIdentity()
List interface
interface, err := device.ListInterface()
List services
services, err := device.ListServices()
Send RRData
device.SendRRData(cpf *commonPacketFormat, timeout typedef.Uint)
Send UnitData
device.SendUnitData(cpf *commonPacketFormat, timeout typedef.Uint)
Disconnect
device.Disconnect()

Maintainers

@末日上投.

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

MIT © 末日上投

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EIPCommandMap map[EIPCommand]bool
View Source
var EIPError map[typedef.Udint]string

Functions

func CtxGenerator

func CtxGenerator() typedef.Ulint

func GetLanDevices

func GetLanDevices(scanTimeout time.Duration) (map[string]*Device, error)

func ReadByte

func ReadByte(reader io.Reader, target interface{})

func ReadByteBigEndian

func ReadByteBigEndian(reader io.Reader, target interface{})

func WriteByte

func WriteByte(writer io.Writer, target interface{})

Types

type CommonPacketFormat

type CommonPacketFormat struct {
	ItemCount typedef.Uint
	Items     []CommonPacketFormatItem
}

func (*CommonPacketFormat) Decode

func (c *CommonPacketFormat) Decode(dataReader *bytes.Reader)

func (*CommonPacketFormat) Encode

func (c *CommonPacketFormat) Encode() []byte

func (*CommonPacketFormat) New

type CommonPacketFormatItem

type CommonPacketFormatItem struct {
	TypeID ItemID
	Length typedef.Uint
	Data   []byte
}

func (*CommonPacketFormatItem) Decode

func (i *CommonPacketFormatItem) Decode(dataReader *bytes.Reader)

func (*CommonPacketFormatItem) Encode

func (i *CommonPacketFormatItem) Encode() []byte

func (*CommonPacketFormatItem) New

func (i *CommonPacketFormatItem) New(id ItemID, data []byte)

type Config

type Config struct {
	TCPPort                 uint16
	UDPPort                 uint16
	TCPTimeout              time.Duration
	TCPReconnectionInterval time.Duration
	AutoSession             bool
}

func DefaultConfig

func DefaultConfig() *Config

type Device

type Device struct {
	IP           net.IP
	VendorID     typedef.Uint
	DeviceType   typedef.Uint
	ProductCode  typedef.Uint
	Major        typedef.Usint
	Minor        typedef.Usint
	Status       typedef.Word
	SerialNumber typedef.Udint
	ProductName  string
	// contains filtered or unexported fields
}

func NewDevice

func NewDevice(ip string) (*Device, error)

func (*Device) Connect

func (d *Device) Connect(config *Config) error

func (*Device) Disconnect

func (d *Device) Disconnect()

func (*Device) ListIdentity

func (d *Device) ListIdentity() (*ListIdentity, error)

func (*Device) ListInterface

func (d *Device) ListInterface() (*ListInterface, error)

func (*Device) ListServices

func (d *Device) ListServices() (*ListServices, error)

func (*Device) SendRRData

func (d *Device) SendRRData(cpf *CommonPacketFormat, timeout typedef.Uint) (*SendDataSpecificData, error)

func (*Device) SendUnitData

func (d *Device) SendUnitData(cpf *CommonPacketFormat, timeout typedef.Uint) (*SendDataSpecificData, error)

func (*Device) String

func (d *Device) String() string

type EIPCommand

type EIPCommand typedef.Uint
const (
	EIPCommandNOP               EIPCommand = 0x00
	EIPCommandListServices      EIPCommand = 0x04
	EIPCommandListIdentity      EIPCommand = 0x63
	EIPCommandListInterfaces    EIPCommand = 0x64
	EIPCommandRegisterSession   EIPCommand = 0x65
	EIPCommandUnRegisterSession EIPCommand = 0x66
	EIPCommandSendRRData        EIPCommand = 0x6F
	EIPCommandSendUnitData      EIPCommand = 0x70
)

type EIPTCP

type EIPTCP struct {
	Connected    func()
	Disconnected func(error)
	Reconnecting func()
	// contains filtered or unexported fields
}

func NewTcpWithAddress

func NewTcpWithAddress(addr string, config *Config) (*EIPTCP, error)

func (*EIPTCP) Close

func (e *EIPTCP) Close()

func (*EIPTCP) Connect

func (e *EIPTCP) Connect() error

func (*EIPTCP) ListIdentity

func (e *EIPTCP) ListIdentity() (*ListIdentity, error)

func (*EIPTCP) ListIdentityDecode

func (e *EIPTCP) ListIdentityDecode(encapsulationPacket *EncapsulationPacket) *ListIdentity

func (*EIPTCP) ListInterface

func (e *EIPTCP) ListInterface() (*ListInterface, error)

func (*EIPTCP) ListInterfaceDecode

func (e *EIPTCP) ListInterfaceDecode(encapsulationPacket *EncapsulationPacket) *ListInterface

func (*EIPTCP) ListServices

func (e *EIPTCP) ListServices() (*ListServices, error)

func (*EIPTCP) ListServicesDecode

func (e *EIPTCP) ListServicesDecode(encapsulationPacket *EncapsulationPacket) *ListServices

func (*EIPTCP) RegisterSession

func (e *EIPTCP) RegisterSession() error

func (*EIPTCP) RegisterSessionDecode

func (e *EIPTCP) RegisterSessionDecode(encapsulationPacket *EncapsulationPacket)

func (*EIPTCP) SendRRData

func (e *EIPTCP) SendRRData(cpf *CommonPacketFormat, timeout typedef.Uint) (*SendDataSpecificData, error)

func (*EIPTCP) SendRRDataDecode

func (e *EIPTCP) SendRRDataDecode(encapsulationPacket *EncapsulationPacket) *SendDataSpecificData

func (*EIPTCP) SendUnitData

func (e *EIPTCP) SendUnitData(cpf *CommonPacketFormat, timeout typedef.Uint) (*SendDataSpecificData, error)

func (*EIPTCP) SendUnitDataDecode

func (e *EIPTCP) SendUnitDataDecode(encapsulationPacket *EncapsulationPacket) *SendDataSpecificData

func (*EIPTCP) UnRegisterSession

func (e *EIPTCP) UnRegisterSession()

func (*EIPTCP) UnRegisterSessionDecode

func (e *EIPTCP) UnRegisterSessionDecode(encapsulationPacket *EncapsulationPacket)

type EIPUDP

type EIPUDP struct {
	Devices         map[string]*Device
	InterfaceHandle func(string, *ListInterface)
	ServicesHandle  func(string, *ListServices)
	// contains filtered or unexported fields
}

func NewUDPWithAddress

func NewUDPWithAddress(address string, config *Config) (*EIPUDP, error)

func NewUDPWithAutoScan

func NewUDPWithAutoScan(config *Config) (*EIPUDP, error)

func (*EIPUDP) Close

func (e *EIPUDP) Close()

func (*EIPUDP) Connect

func (e *EIPUDP) Connect() error

func (*EIPUDP) ListIdentity

func (e *EIPUDP) ListIdentity()

func (*EIPUDP) ListIdentityDecode

func (e *EIPUDP) ListIdentityDecode(encapsulationPacket *EncapsulationPacket) *ListIdentity

func (*EIPUDP) ListInterface

func (e *EIPUDP) ListInterface()

func (*EIPUDP) ListInterfaceDecode

func (e *EIPUDP) ListInterfaceDecode(encapsulationPacket *EncapsulationPacket) *ListInterface

func (*EIPUDP) ListServices

func (e *EIPUDP) ListServices()

func (*EIPUDP) ListServicesDecode

func (e *EIPUDP) ListServicesDecode(encapsulationPacket *EncapsulationPacket) *ListServices

type EncapsulationHeader

type EncapsulationHeader struct {
	Command       EIPCommand
	Length        typedef.Uint
	SessionHandle typedef.Udint
	Status        typedef.Udint
	SenderContext typedef.Ulint
	Options       typedef.Udint
}

type EncapsulationPacket

type EncapsulationPacket struct {
	EncapsulationHeader
	CommandSpecificData []byte
}

func NewListIdentity

func NewListIdentity(context typedef.Ulint) *EncapsulationPacket

func NewListInterface

func NewListInterface(context typedef.Ulint) *EncapsulationPacket

func NewListServices

func NewListServices(context typedef.Ulint) *EncapsulationPacket

func NewNOP

func NewNOP(data []byte) *EncapsulationPacket

func NewRegisterSession

func NewRegisterSession(context typedef.Ulint) *EncapsulationPacket

func NewSendRRData

func NewSendRRData(session typedef.Udint, context typedef.Ulint, cpf *CommonPacketFormat, timeout typedef.Uint) *EncapsulationPacket

func NewSendUnitData

func NewSendUnitData(session typedef.Udint, context typedef.Ulint, cpf *CommonPacketFormat, timeout typedef.Uint) *EncapsulationPacket

func NewUnRegisterSession

func NewUnRegisterSession(session typedef.Udint, context typedef.Ulint) *EncapsulationPacket

func (*EncapsulationPacket) Encode

func (e *EncapsulationPacket) Encode() ([]byte, error)

type FragmentedReadResponse

type FragmentedReadResponse struct {
	ReplyService           typedef.Usint
	Reserved               typedef.Usint
	GeneralStatus          typedef.Usint
	SizeOfAdditionalStatus typedef.Usint
	AdditionalStatus       []byte
	TagTypeValue           uint16
	ResponseData           []byte
}

func (*FragmentedReadResponse) Decode

func (m *FragmentedReadResponse) Decode(data []byte, isString bool)

type ItemID

type ItemID typedef.Uint
const (
	ItemIDUCMM                     ItemID = 0x0000
	ItemIDListIdentityResponse     ItemID = 0x000C
	ItemIDConnectionBased          ItemID = 0x00A1
	ItemIDConnectedTransportPacket ItemID = 0x00B1
	ItemIDUnconnectedMessage       ItemID = 0x00B2
	ItemIDListServicesResponse     ItemID = 0x0100
	ItemIDSockaddrInfoO2T          ItemID = 0x8000
	ItemIDSockaddrInfoT2O          ItemID = 0x8001
	ItemIDSequencedAddressItem     ItemID = 0x8002
)

type ListIdentity

type ListIdentity struct {
	ItemCount typedef.Uint
	Items     []ListIdentityItem
}

func (*ListIdentity) Decode

func (l *ListIdentity) Decode(data []byte)

type ListIdentityItem

type ListIdentityItem struct {
	ItemTypeCode                 typedef.Uint
	ItemLength                   typedef.Uint
	EncapsulationProtocolVersion typedef.Uint
	SinFamily                    typedef.Int
	SinPort                      typedef.Uint
	SinAddr                      typedef.Udint
	SinZero                      typedef.Ulint
	VendorID                     typedef.Uint
	DeviceType                   typedef.Uint
	ProductCode                  typedef.Uint
	Major                        typedef.Usint
	Minor                        typedef.Usint
	Status                       typedef.Word
	SerialNumber                 typedef.Udint
	NameLength                   typedef.Usint
	ProductName                  []byte
	State                        typedef.Usint
}

type ListInterface

type ListInterface struct {
	ItemCount typedef.Uint
	Items     []ListInterfaceItem
}

func (*ListInterface) Decode

func (l *ListInterface) Decode(data []byte)

type ListInterfaceItem

type ListInterfaceItem struct {
	ItemTypeCode typedef.Uint
	ItemLength   typedef.Uint
	ItemData     []byte
}

type ListServices

type ListServices struct {
	ItemCount typedef.Uint
	Items     []ListServicesItem
}

func (*ListServices) Decode

func (l *ListServices) Decode(data []byte)

type ListServicesItem

type ListServicesItem struct {
	ItemTypeCode typedef.Uint
	ItemLength   typedef.Uint
	Version      typedef.Uint
	Flags        typedef.Uint
	Name         []byte
}

type MessageRouterRequest

type MessageRouterRequest struct {
	Service         typedef.Usint
	RequestPathSize typedef.Usint
	RequestPath     []byte
	RequestData     []byte
}

func (*MessageRouterRequest) Encode

func (m *MessageRouterRequest) Encode() []byte

func (*MessageRouterRequest) New

func (m *MessageRouterRequest) New(service typedef.Usint, path []byte, data []byte)

type MessageRouterResponse

type MessageRouterResponse struct {
	ReplyService           typedef.Usint
	Reserved               typedef.Usint
	GeneralStatus          typedef.Usint
	SizeOfAdditionalStatus typedef.Usint
	AdditionalStatus       []byte
	ResponseData           []byte
}

func (*MessageRouterResponse) Decode

func (m *MessageRouterResponse) Decode(data []byte)

type SendDataSpecificData

type SendDataSpecificData struct {
	InterfaceHandle typedef.Udint
	TimeOut         typedef.Uint
	Packet          *CommonPacketFormat
}

func (*SendDataSpecificData) Decode

func (r *SendDataSpecificData) Decode(data []byte)

func (*SendDataSpecificData) Encode

func (r *SendDataSpecificData) Encode() []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL