📄 en_encap.h
字号:
/****************************************************************************
*****************************************************************************
**
** File Name
** ---------
**
** EN_ENCAP.H
**
*****************************************************************************
*****************************************************************************
**
** Description
** -----------
**
** Ethernet Encapsulation Protocol Interfaces.
**
**
*****************************************************************************
*****************************************************************************
**
** Source Change Indices
** ---------------------
**
** Porting: <none>0----<major> Customization: <none>0----<major>
**
*****************************************************************************
*****************************************************************************
**
** Services List
** -------------
**
** en_cd_AllocMcastAddress()
** en_cd_CheckAndGetServices()
** en_cd_CheckSocketAddresses()
** en_cd_CloseEce()
** en_cd_CreateMcastTable()
** en_cd_DataTransferReceived()
** en_cd_EncapErrorReply()
** en_cd_EncapSessionInput()
** en_cd_GetOutboundAddress()
** en_cd_GetOutboundTcpPort()
** en_cd_GetSocketAddresses()
** en_cd_HostCompare()
** en_cd_IFGetConfig()
** en_cd_IndicateStatusReceived()
** en_cd_InitTargetData()
** en_cd_ListInterfacesReceived()
** en_cd_ListServicesReceived()
** en_cd_ListServicesRequest()
** en_cd_ListIdentityReceived()
** en_cd_ListTargetsReceived()
** en_cd_LookupHost()
** en_cd_MngOutgoingEncapTask()
** en_cd_PktbufAlloc()
** en_cd_PktbufFree()
** en_cd_ProcessEncapMsg()
** en_cd_RegisterReceived()
** en_cd_RegisterRequest()
** en_cd_SaveSendToAddress()
** en_cd_SendEncapCommand()
** en_cd_SetSocketAddresses()
** en_cd_UnRegisterReceived()
**
*****************************************************************************
*****************************************************************************
** **
** ETHERNET/IP EXAMPLE CODE **
** COPYRIGHT (c) 2000-2005 ODVA (Open DeviceNet Vendor Association) **
** & ControlNet International Ltd. **
** **
** All rights reserved, except as specifically licensed in writing. **
** Use of the Ethernet/IP Example Protocol Software is subject to **
** ODVA's and ControlNet International's Terms of Use Agreement. **
** The following work constitutes example program code and is intended **
** merely to illustrate useful programming techniques. The user is **
** responsible for applying the code correctly. The code is provided **
** AS IS without warranty and is in no way guaranteed to be error-free. **
** **
*****************************************************************************
*****************************************************************************
*/
#ifndef EN_ENCAP
#define EN_ENCAP
/*
** Determine if this code is to be included
*/
#ifdef CD_EN_OBJECTS
#include "stdio.h"
#include "fd.h"
#include "fd_comp.h"
#include "cb.h"
#ifdef WIN32
#include "NT_SOCK.h"
#endif
/****************************************************************************
**
** Constants
**
*****************************************************************************
*/
#ifndef __ENDIAN__
/*
** Definitions for byte order,
** according to byte significance from low address to high.
**
** External functions which are capable of dealing with the
** differences.
*/
/*#define __HAVE_SWAP__*/
extern UINT16 swaps(UINT16 s); /* Swap shortword "s" */
extern UINT32 swapl(UINT32 l); /* Swap longword "l" */
#define __ENDIAN__
#endif
/*
** Macros for ENCAP number representation conversion. Multi-byte fields
** in ENCAP are all LITTLE ENDIAN (x86 order). If the byte order of
** the target machine is not specified then LITTLE ENDIAN is assumed.
**/
#ifdef BIG_ENDIAN
#ifndef __HAVE_SWAP__
#define _SWAPL_(a) ((a) << 24) | (((a) << 8) & 0xff0000) | \
(((a) >> 8) & 0xff00) | (((UINT32) (a)) >> 24)
#define _SWAPS_(a) (((a & 0xff) << 8) | ((a) >> 8))
#else
#define _SWAPL_ swapl
#define _SWAPS_ swaps
#endif
/* Macros for handling byte order in ENCAP packets.
** These macros are provided in three forms:
** 1.) In Place Conversion
** Converts a variable and assigns the new value
** back to the variable.
**
** ENCAP_CVT_HL - Convert into Host Long
** ENCAP_CVT_PL - Convert into Protocol Long
** ENCAP_CVT_HS - Convert into Host Short
** ENCAP_CVT_PS - Convert into Protocol Short
**
** 2.) Value Conversion
** Simply convert variable value. This form can
** be used on the right-hand side of an assignment.
**
** ENCAP_TO_HL(x) - To Host Long
** ENCAP_TO_PL(x) - To Protocol Long
** ENCAP_TO_HS(x) - To Host Short
** ENCAP_TO_PS(x) - To Protocol Short
**
** 3.) ENCAP byte order constants
** Intended to be used with constants so that the compiler
** can do the conversions.
**
** ENCAP_VALUE_LONG(x)
** ENCAP_VALUE_SHORT(x)
*/
#define ENCAP_CVT_HL(x) (x) = _SWAPL_((x))
#define ENCAP_CVT_PL(x) (x) = _SWAPL_((x))
#define ENCAP_CVT_HS(x) (x) = _SWAPS_((x))
#define ENCAP_CVT_PS(x) (x) = _SWAPS_((x))
#define ENCAP_TO_HL(x) _SWAPL_((x))
#define ENCAP_TO_PL(x) _SWAPL_((x))
#define ENCAP_TO_HS(x) _SWAPS_((x))
#define ENCAP_TO_PS(x) _SWAPS_((x))
#define ENCAP_VALUE_LONG(x) ((x) << 24) | (((x) << 8) & 0xff0000) | \
(((x) >> 8) & 0xff00) | (((UINT32) (x)) >> 24)
#define ENCAP_VALUE_SHORT(x) ((INT16)(((x) << 8) | ((x) >> 8)))
#else
#define ENCAP_CVT_HL(x) (x)
#define ENCAP_CVT_PL(x) (x)
#define ENCAP_CVT_HS(x) (x)
#define ENCAP_CVT_PS(x) (x)
#define ENCAP_TO_HL(x) (x)
#define ENCAP_TO_PL(x) (x)
#define ENCAP_TO_HS(x) (x)
#define ENCAP_TO_PS(x) (x)
#define ENCAP_VALUE_LONG(x) (x)
#define ENCAP_VALUE_SHORT(x) (x)
#endif
#define CIP_MAX_PACKET (508)
#ifdef WIN32
#define MAXHOSTNAMELEN (100)
#endif
/*
* Current revision for the protocol
*/
#define ENCAP_PROTOCOL_VERSION (1)
/*
* Size of most named (ASCII) ENCAP objects in the protocol
*/
#define ENCAP_OBJ_NAME_SIZE 16
/*
* Values for the COMMAND CODE field of the common header.
*/
#define ENCAP_CMD_NOOP (0) /* No operation */
#define ENCAP_CMD_LISTTARGETS (1) /* List Targets */
#define ENCAP_CMD_LISTSERVICES (4) /* List Target Services Command */
#define ENCAP_CMD_LISTIDENTITY (99) /* List Identity Commmand */
#define ENCAP_CMD_LISTINTERFACES (100) /* List Communication Interfaces */
#define ENCAP_CMD_REGISTERSESSION (101) /* Register Session */
#define ENCAP_CMD_UNREGISTERSESSION (102) /* Unregister Session */
#define ENCAP_CMD_SEND_RRDATA (111) /* Send Request/Reply Data */
#define ENCAP_CMD_SEND_UNITDATA (112) /* Send Unit Data (no reply) */
#define ENCAP_CMD_INDICATESTATUS (114) /* Indicate Status */
#define ENCAP_CMD_CANCEL (115) /* Cancel Outstanding Request */
/*
* Type codes for the various objects in the protocol.
*/
#define ENCAP_OBJTYPE_TARGET 0x0001 /* Target type code */
#define ENCAP_OBJTYPE_INTERFACE 0x0002 /* Interface (communications) type code */
#define ENCAP_OBJTYPE_DRIVER 0x0003 /* Driver type code */
#define ENCAP_OBJTYPE_SERVICE_COMM 0x0100 /* Service (communications) type code */
/* Bit flags for the capabilities provided by a communications service */
#define ENCAP_COMMSERVICE_CIP_DIRECT (0x0020) /* Supports CIP direct/default */
#define ENCAP_COMMSERVICE_IO_CONNECT (0x0100) /* Supports CIP Class 1 I/O connections */
#define ENCAP_COMMSERVICE_VERSION (1) /* Communications service version */
#define ENCAP_COMMSERVICE_NAME "Communications" /* Name for this service */
/* Manufacturer (vendor) type codes */
#define ENCAP_VENDOR_AB (0x1) /* Allen-Bradley */
/* Network type codes */
#define ENCAP_NETWORK_ETHERNET (0x10) /* Allen-Bradley ETHERNET */
#define ENCAP_INTERFACE_HANDLE_DIRECT 0 /* Handle for direct execution */
/* Define values for all the tag types. */
#define CPF_TAG_ADR_NULL 0 /* Null Address Tag */
#define CPF_TAG_ADR_LOCAL 0x81U /* Local Address Tag */
#define CPF_TAG_ADR_OFFLINK 0x82U /* OffLink Address Tag */
#define CPF_TAG_PKT_PCCC 0x91U /* PCCC Packet Tag */
#define CPF_TAG_ADR_CONNECTED 0xa1U /* Connected (CIP) Address Tag */
#define CPF_TAG_ADR_SEQUENCED 0x8002U /* Connected (CIP) Sequenced Tag */
#define CPF_TAG_PKT_UCMM 0xb2U /* Unconnected Messaging Packet Tag */
#define CPF_TAG_PKT_CONNECTED 0xb1U /* Connected Messaging Packet Tag */
#define CPF_TAG_SOCKADDR_OT 0x8000U /* Socket address, originator=>target */
#define CPF_TAG_SOCKADDR_TO 0x8001U /* Socket address, target=>originator */
/* Error codes that may be in the encaph_status field of the fixed header structure */
#define ENCAP_E_SUCCESS (0L)
#define ENCAP_E_UNHANDLED_CMD (1L) /* Command not handled */
#define ENCAP_E_OUTOFMEM (2L) /* Memory not available for command*/
#define ENCAP_E_BADDATA (3L) /* Poorly formed or incomplete data */
#define ENCAP_E_BAD_SESSIONID (100L) /* Invalid session ID */
#define ENCAP_E_BAD_LENGTH (101L) /* Invalid length in header */
#define ENCAP_E_UNSUPPORTED_REV (105L) /* Requested protocol version not supported */
#define ENCAP_E_BAD_TARGET_ID (106L) /* Invalid target ID */
/*
* TCP sequence numbers are 32 bit integers operated
* on with modular arithmetic. These macros can be
* used to compare such integers.
*/
#define IS_LT(a,b) ((INT32)((a)-(b)) < 0)
#define IS_LEQ(a,b) ((INT32)((a)-(b)) <= 0)
#define IS_GT(a,b) ((INT32)((a)-(b)) > 0)
#define IS_GEQ(a,b) ((INT32)((a)-(b)) >= 0)
/* The following set of defines are for interim CIP multicast
* address assignment.
*/
#define CLASS1_UDP_PORT (0x8ae)
#define CIP_MCAST_BASE 0xefc00100 /* 239.192.1.0 */
#define NUM_MCAST_BLOCKS 32 /* Address Block Size */
#define CIP_MCAST_BSHIFT 5 /* Shift for the above */
#define CIP_MCAST_HMASK 0x000003ff /* Host mask, 10 bits */
/* Free a Mulicast Address Allocation Descriptor */
#define FREE_MCAST_ADDRESS(_ma_) \
((_ma_)->lFree = 1)
#define NRVEC 2 /* Number of read iovec entries */
#define NWVEC 8 /* Number of write iovec entries */
#define ECE_IDX(x) ((x).u.idx)
#define ECE_STATE_FREE 1 /* ECE entry is free (available for allocation) */
#define ECE_STATE_2MSL 2 /* ECE entry is waiting for 2 MSL (TCP/IP) */
#define ECE_STATE_LOOKUP 3 /* ECE entry is waiting for name looklup */
#define ECE_STATE_IDLE 3 /* ECE states greater than this value have an fd */
#define ECE_STATE_CONNECTING 4 /* ECE entry is allocated, connection pending */
#define ECE_STATE_CONNECTED 5 /* ECE entry is connected, list services pending */
#define ECE_STATE_REGISTER 6 /* ECE entry waiting for register reply */
#define ECE_STATE_ACTIVE 7 /* ECE entry completed */
#define ECE_2MSL_TIM (2 * 60 * 1000) /* 2MSL wait time (assuming 1 sec slow tick) */
#define ECE_INACT_TIM (5 * 60 * 1000) /* Inactivity wait time (assuming 1 sec slow tick) */
#define ECE_LOOK_TIM (1 * 60 * 1000) /* DNS lookup time (assuming 1 sec slow tick */
/* Type of connection is indicated by type field in the identifier */
#define ECE_TYPE(x) ((x).u.type) /* Type of connection */
#define ECE_TYPE_FREE 1 /* Matches ECE_STATE_FREE */
#define ECE_TYPE_INBOUND 2 /* Inbound connection */
#define ECE_TYPE_OUTBOUND 3 /* Outbound connection */
/* Bits in the flags */
#define ECE_FLAG_NOSESSION 0x01 /* Don't allow sessions to be created */
/* States for read/write to the descriptors */
#define ECE_RS_EMPTY 0 /* No read in progress */
#define ECE_RS_HDR 1 /* Reading in ENCAP header */
#define ECE_RS_DATA 2 /* Reading in remainder of data */
#define ECE_WS_EMPTY 0 /* No write in progress */
#define ECE_WS_PARTIAL 1 /* Partial write to socket */
/* Generic PKTBUF flags. Used by I/O for connected messaging */
#define PKTBUF_F_PENDING 0x01 /* Operation pending on buffer */
#define PKTBUF_F_LOCKED 0x02 /* Buffer locked against updates */
/*
* Validity bit definitions for the ENCAP area in a PKTBUF. The bits indicate which ENCAP
* fields have valid information.
* NOTE...The ENCAP header is always valid.
*/
#define PKTBUF_ENCAPV_FLUSH 0x1 /* Message was truncated */
#define PKTBUF_ENCAPV_DT 0x2 /* Data transfer field is valid */
#define PKTBUF_ENCAPV_OBJ 0x4 /* Object list field is valid */
#define PKTBUF_ENCAPV_ADR 0x8 /* CPF Address tag is valid */
#define PKTBUF_ENCAPV_PKT 0x10 /* CPF packet tag is valid */
#define PKTBUF_ENCAPV_ADD 0x20 /* Packet addendum present */
/****************************************************************************
**
** Typedefs
**
*****************************************************************************
*/
/* Structure for statistics regarding the ENCAP protocol. The command and
* reply counters DO NOT include any of the Data Transfer Commands.
* This class of commands is tallied separately under the individual
* protocol-specific counters, i.e., CIP traffic has its own set of counters.
*/
typedef struct
{
UINT32 lCommandsSent;
UINT32 lCommandsReceived;
UINT32 lRepliesSent;
UINT32 lRepliesSentErrs;
UINT32 lRepliesReceived;
UINT32 lRepliesReceivedErrs;
UINT32 lErrUnhandledCmd;
UINT32 lErrOutOfMem;
UINT32 lErrBadData;
UINT32 lErrBadSessionID;
UINT32 lErrBadLength;
UINT32 lErrNoVirtualDH;
UINT32 lErrBadNodeAddr;
UINT32 lErrDupNodeAddr;
UINT32 lErrUnSupportedRev;
UINT32 lErrBadTargetID;
UINT32 lErrTargetDeleted;
UINT32 lErrCantResolve;
} ENCAP_STATS;
extern ENCAP_STATS sEncapStats;
/*
* Header common to all packets. Defined as a 'C' structure to allow
* easy access to individual fields.
*/
typedef struct encaph
{
UINT16 iEncaph_command; /* Command code */
UINT16 iEncaph_length; /* Total transaction length */
UINT32 lEncaph_session; /* Session identifier */
UINT32 lEncaph_status; /* Status code */
UINT32 alEncaph_context[2]; /* Context information */
UINT32 lEncaph_opt; /* Options flags */
} GNU_PACKED ENCAPH;
#define ENCAP_HDR_SIZE (sizeof(ENCAPH))
/*
* General purpose typedef for the header that precedes a list
* of ENCAP objects. This header consists of a count which specifies
* the number of objects in the list. Each object in the list has
* a standard format consisting of a tag and the size of the object.
*/
typedef struct encap_obj_list
{
UINT16 iO_count; /* Count of data structures to follow */
} GNU_PACKED ENCAP_OBJ_LIST;
#define ENCAP_OBJ_LIST_SIZE 2 /* Gets around alignment */
/*
* Base structure for data structures. Can be used to determine the type of
* data structure which follows.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -