DLMS/COSEM 蓝皮书解读三十IPv4 setupclass_id 42—— 电表接入 IPv4 网络必需的地址/子网/网关/DNS四件套系列说明本系列基于 DLMS UA《Blue Book蓝皮书第 16 版 · 第 2 部分》一个接口类一篇。第 29 篇讲了TCP-UDP setupclass_id 41它决定DLMS 应用监听哪个 TCP/UDP 端口、最大并发连接数、空闲超时多久断开。本篇的IPv4 setupclass_id 42就是往下一层走回答这个接口的 IPv4 地址、子网掩码、网关、DNS 怎么配——把电表从端口补到网段。上篇回顾第 29 篇TCP-UDP setup的TCP-UDP_port负责监听 DLMS/COSEM 应用流量但它不管 IP 地址本身。IP 地址、子网掩码、网关、DNS 这些IP 层四件套就由本篇的IPv4 setup负责而且和TCP-UDP setup一样每个网络接口一个实例。0. 为什么需要这个类 —— 电表接入 IP 网络时地址/子网/网关/DNS 缺一不可在 TCP-UDP/IPv4 这个通信 profileIEC 62056-47 Wrapper下一块电表要上网至少需要回答四个问题我的 IP 地址是多少IP_address我在哪个子网、怎么判断目标是同网段还是要走网关subnet_mask出网段的数据包该丢给谁gateway_IP_address域名怎么解析primary_DNS_address/secondary_DNS_addressIPv4 setup就是把这一组IP 层参数建模成一个 COSEM 对象。蓝皮书对它的定位非常明确蓝皮书原文Overview“This IC allows modelling the setup of the IPv4 layer, handling all information related to the IP Address settings associated to a given device and to a lower layer connection on which these settings are used.”而且它按接口建实例不是全局唯一的蓝皮书原文Overview“There shall be an instance of this IC in a device for each different network interface implemented. For example, if a device has two interfaces (using the TCP-UDP/IPv4 profile on both of them), there shall be two instances of the IPv4 setup IC in that device: one for each of these interfaces.”也就是说双网卡比如一个以太网口 一个 PPP 拨号口的电表必须建两个IPv4 setup实例各自挂在自己的下层链路对象上。这一点和第 29 篇TCP-UDP setup的每接口一个实例是一脉相承的。1. 类蓝图1.1 属性表version 0#属性名读写数据类型Short name说明1logical_namestaticoctet-stringx对象实例名OBIS2DL_referencestaticoctet-stringx 0x08指向底层数据链路层 setup 对象3IP_address—double-long-unsignedx 0x10本设备 IPv4 地址4multicast_IP_address—arrayx 0x18组播地址列表5IP_options—arrayx 0x20IP 选项6subnet_mask—double-long-unsignedx 0x28子网掩码7gateway_IP_address—double-long-unsignedx 0x30网关地址8use_DHCP_flagstaticbooleanx 0x38是否用 DHCP9primary_DNS_address—double-long-unsignedx 0x40主 DNS10secondary_DNS_address—double-long-unsignedx 0x48备 DNS注原文属性表里IP_address/multicast_IP_address/IP_options/subnet_mask/gateway_IP_address/primary_DNS_address/secondary_DNS_address未标注(static)即默认是可读写的dynamic 或至少可变。属性名与 Short name 偏移严格取自原文。1.2 方法表Specific methods全部o 可选方法名参数Short name说明add_mc_IP_address (data)IP_Address: double-long-unsignedx 0x60向组播数组加一个地址delete_mc_IP_address (data)IP_Address: double-long-unsignedx 0x68从组播数组删一个地址get_nbof_mc_IP_addresses (data)返回 unsignedx 0x70返回组播数组元素个数蓝皮书原文Specific methods 栏这三类都是ooptional设备可以不实现。需要动态管理组播组的场景才用得上。2. 属性逐条拆解2.1DL_reference指向下层链路蓝皮书原文“References a Data link layer (e.g. Ethernet or PPP) setup object by its logical name. The referenced object contains information about the specific settings of the data link layer supporting the IP layer.”这是一张引用指向第 29 篇的TCP-UDP setup它上面还挂着 Ethernet / PPP 这类链路对象或者直接指向 Ethernet / PPP setup 对象。它是IP 层挂在哪条链路上的绑定关系。2.2IP_address地址的两种存法蓝皮书原文“It can be either (static) or (dynamic). In the latter case, dynamic IP address assignment (for example DHCP) is used. If no IP address is assigned, the value is 0.”关键认知IP_address在对象里是个double-long-unsigned整数不是点分十进制字符串。蓝皮书给了一个必须记住的换算例子蓝皮书原文EXAMPLE“The IPv4 address 192.168.0.1 (in dotted decimal notation) corresponds to C0A80001 (hexa) which gives 3232235521 (double-long-unsigned).”换算方法把四个字节192 168 0 1拼成 32 位无符号整数 0xC0A80001 3 232 235 521。主站读出来是这个数字要在界面上显示成点分十进制得自己拆字节。2.3multicast_IP_address组播监听蓝皮书原文“IP addresses in this array shall fall into the multicast group address range (‘Class D’ addresses, including IP addresses in the range of 224.0.0.0 to 239.255.255.255). When a device receives an IP datagram with one of these IP addresses in the destination IP address field, it shall consider that this datagram is addressed to it.”类型是数组元素也是double-long-unsigned和IP_address同款编码multicast_IP_address :: array double-long-unsigned用途比如电表要监听某个组播组NTP、某广播校时、某主站群发把自己的组播地址加进这个数组即可三个方法就是增删查这个数组。2.4IP_options可选 IP 选项IP_options :: array IP_options_element IP_options_element :: structure { IP_Option_Type: unsigned, IP_Option_Length: unsigned, IP_Option_Data: octet-string }蓝皮书原文NOTE“In all cases, as specified in RFC 791, the IP_Option_Length field includes the total length of all three fields: IP_Option_Type, IP_Option_Length and IP_Option_Data.”允许的IP_Option_Type取值原文列出IP_Option_Type含义0x82SecurityIP_Option_Length 11含安全/隔离/处理限制/TCC 参数0x83Loose Source and Record Route宽松源路由并记录0x89Strict Source and Record Route严格源路由并记录0x07Record Route记录路由0x44Internet Timestamp互联网时间戳工程现实绝大多数电表根本用不到 IP 选项这个属性通常是空数组。但做电力监控专网带安全选项时要填0x82。2.5subnet_mask/gateway_IP_address子网与网关蓝皮书原文subnet_mask“the ‘0’ bits of the subnet_mask indicate the portion of the IP Address which is still used as Device_ID on a sub-networked IP Network.”蓝皮书原文gateway“In order to be able to send non-local datagrams to the gateway, the device shall know the IP address of the gateway device assigned to the given network segment. If no IP address is assigned, the value is 0.”subnet_mask也是double-long-unsigned如255.255.255.00xFFFFFF00 4 294 967 040。gateway_IP_address取 0 表示没有网关/直连网络。2.6use_DHCP_flag静态还是动态蓝皮书原文“TRUE: The device uses DHCP (Dynamic Host Configuration Protocol) to dynamically determine the IP_address, subnet_mask and gateway_IP_address parameters.”“FALSE: The IP_address, subnet_mask and gateway_IP_address parameters shall be set locally.”注意一旦use_DHCP_flag TRUEIP_address/subnet_mask/gateway_IP_address三个属性就由 DHCP 覆盖本地设的值只能当 fallback / 初始值。这是很多现场明明配了地址却不生效的根源。2.7primary_DNS_address/secondary_DNS_address蓝皮书原文主 DNS“If no IP address is assigned, the value is 0.”备 DNS 同理原文secondary_DNS_address写法有小空格笔误语义相同。解析域名比如第 33 篇SMTP setup的server_address填的是名字而不是 IP就靠这俩。都是double-long-unsigned0 表示未配。3. 方法逐条拆解三个方法都是 optional且只和组播数组有关add_mc_IP_address (IP_Address)往multicast_IP_address里加一个double-long-unsigned。蓝皮书原文“Adds one multicast IP address to the multicast_IP_address array. IP_Address:: double-long-unsigned”delete_mc_IP_address (IP_Address)按值删除一个。get_nbof_mc_IP_addresses (data)返回数组长度data :: unsigned。实践判断如果设备出厂就固定监听某个组播组组播地址写死在multicast_IP_address里这三个方法完全可以不实现——它们是运行时动态增删才需要的。4. 实战举例示例 1一个静态地址的 IPv4 setup 实例某台区集中器通过以太网口接入局域网IP 段192.168.1.0/24网关192.168.1.1属性值点分值double-long-unsigned十六进制值十进制IP_address192.168.1.500xC0A801323 232 850 994subnet_mask255.255.255.00xFFFFFF004 294 967 040gateway_IP_address192.168.1.10xC0A801013 232 849 665primary_DNS_address192.168.1.10xC0A801013 232 849 665use_DHCP_flagFALSE——以上配置示例为工程示意、非蓝皮书原文具体 OBIS 与地址需以设备对象列表为准。示例 2换算验证蓝皮书原例192.168.0.1192 0xC0168 0xA80 0x001 0x01拼成0xC0A80001十进制 192×2^24 168×2^16 0×2^8 13 221 225 472 11 010 048 0 13 232 235 521✓ 与蓝皮书一致示例 3DHCP 场景下的 short name 读取主站用 SN 访问第 1 篇讲过x是 base name读IP_address取x 0x10工程示意若 base namex 0x1000则IP_address的 short name 0x1010。DHCP 环境下读到的是当前租约地址不是出厂默认值。示例 4组播地址的 A-XDR 表示组播地址224.0.0.1NTP 常用编码为double-long-unsigned0xE0000001 3 758 096 385。加入数组add_mc_IP_address: 结构method 参数: long-unsigned (tag [18]0x12) 0xE0000001A-XDR 字节为工程示意非蓝皮书原文。示例 5双接口实例的 OBIS 规划接口logical_nameOBIS示例DL_reference 指向以太网0.0.25.1.0.255TCP-UDP setup 实例 APPP 拨号0.0.25.2.0.255PPP setup 实例第 32 篇OBIS 为示例非蓝皮书原文强调每接口一个实例。示例 6IP_options安全选项的字节骨架若启用 Security 选项0x82长度应含三字段共 11structure: unsigned 0x82 (IP_Option_Type) unsigned 0x0B (IP_Option_Length 11) octet-string 11字节安全参数依 RFC 791具体 11 字节内容依 RFC 791非蓝皮书给出工程示意。4.7 与第 29 篇TCP-UDP setup的部署核对清单以下为工程示意、非蓝皮书原文。先建TCP-UDP setup41实例定好 IANA 端口 4059、MSS、连接数、超时再建本IPv4 setup42实例用DL_reference指向上一步的 41决定use_DHCP_flag固定 IP 设 FALSE 并填IP_address/subnet_mask/gateway动态 IP 设 TRUE若用域名访问如 SMTP 服务器务必填primary_DNS_address可经 DHCP 获得需要组播监听预置multicast_IP_address或依赖三个 optional 方法运行时增删多接口以太网 PPP各自建 42 实例OBIS 不冲突。4.8 IP 类属性整数 vs 字符串对照表类属性存法示例值IPv4 setup(42)IP_address/subnet_mask/gateway/DNSdouble-long-unsigned整数192.168.0.1 3232235521SMTP setup(46)server_address/sender_addressoctet-string点分/域名163.187.45.87MAC address setup(43)MAC_addressoctet-string6 字节00 1D 2C 3E 4F 50关键同样是地址42 用整数、46 用字符串、43 用字节串——主站解析时类型处理要分开别混。5. 工程上容易踩的坑地址是数字不是字符串所有 IP 类属性IP_address/subnet_mask/gateway/DNS都是double-long-unsigned。主站/界面务必做点分 ↔ 整数双向转换直接把3232235521当字符串显示就是 bug。DHCP 打开后本地地址被覆盖use_DHCP_flag TRUE时别指望 SETIP_address生效要静态必须用 FALSE。网关为 0 不是网关是 0.0.0.0它语义上是无网关/直连网段。跨网段通信却填 0 会全部丢包。DL_reference指错链路对象它必须指向真正承载这个 IP 接口的链路 setupEthernet / PPP / 第 29 篇 TCP-UDP setup指错会导致报文出口不对。multicast_IP_address必须落在 224.0.0.0–239.255.255.255填非组播地址违反蓝皮书约束行为未定义。组播方法是 optional别假设设备一定实现了add_mc_IP_address动态加组播前先确认方法存在否则用静态预置数组。IPv4 与 IPv6 是两个类本篇只管 IPv4双栈设备还要另建第 35 篇要讲的IPv6 setupclass_id 48。IP_options实际很少用专网带安全选项才填0x82普通场景空数组即可别为了齐全硬填导致某些老协议栈解析异常。6. 小结 下期预告本篇把 IP 层的身份证 路由 域名配齐IP_address/subnet_mask/gateway_IP_address/primary_DNS_address/secondary_DNS_address五个地址类属性全是double-long-unsigneduse_DHCP_flag决定静态还是动态DL_reference把 IP 层绑到具体链路multicast_IP_address配合三个 optional 方法做组播管理IP_options是高级可选项。下一篇第 31 篇MAC address setupclass_id 43—— 再往下到数据链路层看 MAC 地址EUI-48是怎么建模的以及 HDLC、S-FSK、G3-PLC 这些不同介质里MAC 地址分别存在哪个对象里。它和本篇的DL_reference链条正好接上。