forked from RocyLuo/IEC104TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiec104client.py
293 lines (259 loc) · 6.51 KB
/
iec104client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from iec104lib import *
from struct import *
class iec104_tcp_client():
Tx=0
Rx=0
_targetip=''
_port=2404
_socket=''
def __init__(self,targetip):
self._targetip=targetip
def connect(self):
self._socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self._socket.settimeout(0.5)
l_onoff=1
l_linger = 0
self._socket.setsockopt(socket.SOL_SOCKET,socket.SO_LINGER,struct.pack('ii', l_onoff, l_linger))
self._socket.connect((self._targetip,self._port))
def quit(self):
self._socket.close()
def IOA2LE(self,IOA):
first=IOA&255
second=(IOA>>8)&255
third=(IOA>>16)&255
res=(first<<16)+(second<<8)+third
return res
def setRx(self):
self.Rx=self.Rx+1
if self.Rx > 65534:
self.Rx = 1
def getTx(self):
self.Tx = self.Tx + 1
if self.Tx > 65534:
self.Tx = 1
return self.Tx
def buildinfobj45(self,Infobj):
infobj=asdu_infobj_45()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.SCO=Infobj[1]
return infobj
def buildinfobj46(self,Infobj):
infobj=asdu_infobj_46()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.DCO=Infobj[1]
#print Infobj[1]
return infobj
def buildinfobj47(self,Infobj):
infobj=asdu_infobj_47()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.RCO=Infobj[1]
return infobj
def buildinfobj48(self,Infobj):
infobj=asdu_infobj_48()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('h',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
return infobj
def buildinfobj49(self,Infobj):
infobj=asdu_infobj_49()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('h',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
return infobj
def buildinfobj50(self,Infobj):
infobj=asdu_infobj_50()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('f',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
return infobj
def buildinfobj51(self,Infobj):
infobj=asdu_infobj_51()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('I',Infobj[1])
LEvalue=value[0]+value[1]
LEvalue=LEvalue+value[2]
LEvalue=LEvalue+value[3]
infobj.Value=LEvalue
return infobj
def buildinfobj58(self,Infobj):
infobj=asdu_infobj_58()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.SCO=Infobj[1]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj59(self,Infobj):
infobj=asdu_infobj_59()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.DCO=Infobj[1]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj60(self,Infobj):
infobj=asdu_infobj_60()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.RCO=Infobj[1]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj61(self,Infobj):
infobj=asdu_infobj_61()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('h',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj62(self,Infobj):
infobj=asdu_infobj_62()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('h',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj63(self,Infobj):
infobj=asdu_infobj_63()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('f',Infobj[1])
infobj.Value=value
infobj.QOS=Infobj[2]
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj64(self,Infobj):
infobj=asdu_infobj_64()
infobj.IOA=self.IOA2LE(Infobj[0])
value=struct.pack('I',Infobj[1])
LEvalue=value[0]+value[1]
LEvalue=LEvalue+value[2]
LEvalue=LEvalue+value[3]
infobj.Value=LEvalue
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildinfobj101(self,Infobj):
infobj=asdu_infobj_101()
infobj.IOA=self.IOA2LE(Infobj[0])
infobj.Operation=Infobj[1]
return infobj
def buildinfobj103(self,Infobj):
infobj=asdu_infobj_103()
infobj.IOA=self.IOA2LE(Infobj[0])
CPtime=CP56Time()
infobj.CP56Time=CPtime
return infobj
def buildpacket(self,p):
start=p[0]
ApduLen=p[1]
frame_type=p[2]
if(frame_type=='sf'):
sframe=s_frame()
if(start!='START'):
sframe.START=start
if(ApduLen!='auto'):
sframe.ApduLen=ApduLen
sframe.Rx=self.Rx<<1
return sframe
elif(frame_type=='uf'):
uf_type=p[3]
uframe=u_frame()
if(start!='START'):
uframe.START=start
if(ApduLen!='auto'):
uframe.ApduLen=ApduLen
uframe.Type=uf_type
return uframe
elif(frame_type=='short_if'):
iframe=i_frame()
if(start!='START'):
iframe.START=start
if(ApduLen!='auto'):
iframe.ApduLen=ApduLen
iframe.Tx=self.getTx()<<1
iframe.Rx=self.Rx<<1
return iframe
elif(frame_type=='if'):
#build I frame
iframe=i_frame()
if(start!='START'):
iframe.START=start
if(ApduLen!='auto'):
iframe.ApduLen=ApduLen
iframe.Tx=self.getTx()<<1
iframe.Rx=self.Rx<<1
#build asdu head
if_asdu=p[3]
TypeID=if_asdu[0]
SQ=if_asdu[1]
Cause=if_asdu[2]
OA=if_asdu[3]
Addr=if_asdu[4]
asduhead=asdu_head()
asduhead.TypeID=TypeID
asduhead.SQ=SQ
asduhead.Cause=Cause
asduhead.OA=OA
asduhead.Addr=Addr
#build asdu Infobj
Infobj=if_asdu[5]
if(TypeID==45):
asdubody=self.buildinfobj45(Infobj)
elif(TypeID==46):
asdubody=self.buildinfobj46(Infobj)
elif(TypeID==47):
asdubody=self.buildinfobj47(Infobj)
elif(TypeID==48):
asdubody=self.buildinfobj48(Infobj)
elif(TypeID==49):
asdubody=self.buildinfobj49(Infobj)
elif(TypeID==50):
asdubody=self.buildinfobj50(Infobj)
elif(TypeID==51):
asdubody=self.buildinfobj51(Infobj)
elif(TypeID==58):
asdubody=self.buildinfobj58(Infobj)
elif(TypeID==59):
asdubody=self.buildinfobj59(Infobj)
elif(TypeID==60):
asdubody=self.buildinfobj60(Infobj)
elif(TypeID==61):
asdubody=self.buildinfobj61(Infobj)
elif(TypeID==62):
asdubody=self.buildinfobj62(Infobj)
elif(TypeID==63):
asdubody=self.buildinfobj63(Infobj)
elif(TypeID==64):
asdubody=self.buildinfobj64(Infobj)
elif(TypeID==101):
asdubody=self.buildinfobj101(Infobj)
elif(TypeID==103):
asdubody=self.buildinfobj103(Infobj)
else:
print 'ASDU_TypeID is not supported'
#print hexdump(iframe/asduhead/asdubody)
return iframe/asduhead/asdubody
else:
print 'wrong apci type'
def sendOne(self,p):
self.connect()
iec104=self.buildpacket(p)
self._socket.send(str(iec104))
time.sleep(0.015)
try:
output=self._socket.recv(1024)
except Exception,e:
if (str(e).find('[Errno 104]')!=-1):
#self._socket.close()
return 'RST'
else:
#self._socket.close()
return 'Drop'
self._socket.close()
self.setRx()
return 'Accept'