machoNetPacket.py (14137B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\script\net\machoNetPacket.py 2 import types 3 import carbon.common.script.net.machobase as machobase 4 import cluster 5 from carbon.common.script.net.machoNetAddress import MachoAddress 6 import log 7 8 class MachoPacket(): 9 __guid__ = 'macho.MachoPacket' 10 __intorstringtype__ = (types.IntType, types.StringType, types.UnicodeType) 11 __bizzarrerouters__ = (cluster.MACHONETMSG_TYPE_SESSIONCHANGENOTIFICATION, 12 cluster.MACHONETMSG_TYPE_SESSIONINITIALSTATENOTIFICATION, 13 cluster.MACHONETMSG_TYPE_PING_REQ, 14 cluster.MACHONETMSG_TYPE_PING_RSP) 15 16 def __init__(self, *args, **keywords): 17 self.userID = None 18 self.compressedPart = 0 19 self.source = MachoAddress() 20 self.destination = MachoAddress() 21 self.contextKey = None 22 self.journeyID = None 23 self.trace_id = None 24 self.parent_span_id = None 25 self.sampled = None 26 self.ingress_id = None 27 self.sample_rate = None 28 self.cut_parent = None 29 self.read_time = None 30 self.oob = {} 31 dtc = 0 32 for each in keywords.iterkeys(): 33 if each != 'donttypecheck': 34 setattr(self, each, keywords[each]) 35 else: 36 dtc = 1 37 38 self.command = self.__machodesc__['command'] 39 if not dtc: 40 for each in self.__machodesc__['params']: 41 if each[-1:] != '?': 42 if not hasattr(self, each): 43 raise TypeError('%s requires %s to be specified' % (self.__class__.__name__, each)) 44 45 if not isinstance(self.source, MachoAddress): 46 raise TypeError('Failed constructing MachoPacket. `source` must be `MachoAddress`') 47 if not isinstance(self.destination, MachoAddress): 48 raise TypeError('Failed constructing MachoPacket. `destination` must be `MachoAddress`') 49 self.pickleSize = 0 50 51 def __getstate__(self): 52 if not isinstance(self.source, MachoAddress): 53 raise TypeError('Failed serializaing MachoPacket. `source` must be `MachoAddress`') 54 if not isinstance(self.destination, MachoAddress): 55 raise TypeError('Failed serializaing MachoPacket. `destination` must be `MachoAddress`') 56 params = self.__machodesc__['params'] 57 body = [None] * len(params) 58 for i in range(len(params)): 59 if params[i].endswith('?'): 60 tmp = params[i][:-1] 61 if hasattr(self, tmp): 62 body[i] = getattr(self, tmp) 63 else: 64 body.pop(-1) 65 break 66 else: 67 body[i] = getattr(self, params[i]) 68 69 oob = None 70 if self.oob or self.compressedPart: 71 oob = self.oob 72 if self.compressedPart: 73 oob['compressedPart'] = self.compressedPart 74 return (self.command, 75 self.source, 76 self.destination, 77 self.userID, 78 tuple(body), 79 oob, 80 self.contextKey, 81 self.journeyID, 82 self.trace_id, 83 self.parent_span_id, 84 self.sampled, 85 self.ingress_id, 86 self.sample_rate, 87 self.cut_parent) 88 89 def __setstate__(self, state): 90 self.command, self.source, self.destination, self.userID, body, self.oob, self.contextKey, self.journeyID, self.trace_id, self.parent_span_id, self.sampled, self.ingress_id, self.sample_rate, self.cut_parent = state 91 if self.oob is None: 92 self.oob = {} 93 if not isinstance(self.source, MachoAddress): 94 raise TypeError('Failed deserializaing MachoPacket. `source` must be `MachoAddress`') 95 if not isinstance(self.destination, MachoAddress): 96 raise TypeError('Failed deserializaing MachoPacket. `destination` must be `MachoAddress`') 97 self.compressedPart = self.oob.get('compressedPart', 0) 98 params = self.__machodesc__['params'] 99 l = len(params) 100 if len(body) < l: 101 l = len(body) 102 for i in range(l): 103 if params[i].endswith('?'): 104 tmp = params[i][:-1] 105 else: 106 tmp = params[i] 107 setattr(self, tmp, body[i]) 108 109 def Response(self, *args, **keywords): 110 if 'response' not in self.__machodesc__: 111 raise AttributeError(self.__class__.__name__, 'Response', 'There is no such thing as a response to a %s' % self.__class__.__name__) 112 theResponse = self.__machodesc__['response'](donttypecheck=1) 113 theResponse.source = self.destination 114 theResponse.destination = self.source 115 theResponse.userID = self.userID 116 theResponse.contextKey = None 117 theResponse.trace_id = self.trace_id 118 theResponse.parent_span_id = None 119 theResponse.ingress_id = self.ingress_id 120 theResponse.sampled = self.sampled 121 theResponse.sample_rate = self.sample_rate 122 theResponse.cut_parent = None 123 responseParams = theResponse.__machodesc__['params'] 124 i = 0 125 for each in responseParams: 126 if each.endswith('?'): 127 if len(args) > i: 128 setattr(theResponse, each[:-1], args[i]) 129 break 130 else: 131 if len(args) <= i: 132 break 133 setattr(theResponse, each, args[i]) 134 i = i + 1 135 136 for each in keywords.iterkeys(): 137 if each not in responseParams: 138 raise TypeError('%s.Response does not take %s as a parameter' % (self.__class__.__name__, each)) 139 setattr(theResponse, each, keywords[each]) 140 141 for each in responseParams: 142 if not each.endswith('?') and not hasattr(theResponse, each): 143 raise TypeError('%s.Response requires %s as a parameter, but it was not specified' % (self.__class__.__name__, each)) 144 145 return theResponse 146 147 def ErrorResponse(self, code, payload): 148 theResponse = ErrorResponse(originalCommand=self.command, code=code, payload=payload) 149 theResponse.source = self.destination 150 theResponse.destination = self.source 151 theResponse.userID = self.userID 152 theResponse.contextKey = None 153 theResponse.trace_id = self.trace_id 154 theResponse.parent_span_id = None 155 theResponse.ingress_id = self.ingress_id 156 theResponse.sampled = self.sampled 157 theResponse.sample_rate = self.sample_rate 158 return theResponse 159 160 def SetReadTime(self, read_time): 161 self.read_time = read_time 162 163 def SetPickle(self, thePickle): 164 self.__dict__['thePickle'] = thePickle 165 self.__dict__['pickleSize'] = len(self.thePickle) 166 167 def GetPickle(self): 168 if not hasattr(self, 'thePickle'): 169 self.__dict__['thePickle'] = machobase.Dumps(self) 170 self.__dict__['pickleSize'] = len(self.thePickle) 171 return self.thePickle 172 173 def GetPickleSize(self, machoNet): 174 if not self.pickleSize: 175 self.GetPickle() 176 return self.pickleSize 177 178 def Changed(self): 179 if hasattr(self, 'thePickle'): 180 delattr(self, 'thePickle') 181 182 def RoutesTo(self, towhat): 183 if self.command in self.__bizzarrerouters__: 184 return 1 185 if self.command == cluster.MACHONETMSG_TYPE_AUTHENTICATION_REQ and machobase.mode == 'proxy': 186 return 0 187 return self.destination.RoutesTo(towhat, self.source) 188 189 def extract_tags(self): 190 tags = {'user': self.userID, 191 'source': self.source, 192 'destination': self.destination, 193 'oob': self.oob, 194 'command': self.__guid__, 195 'pickleBytes': self.pickleSize, 196 'trace_id': self.trace_id} 197 strayload = getattr(self, 'strayload', None) 198 if strayload is not None: 199 tags['strayload'] = strayload 200 params = self.__machodesc__['params'] 201 for p in params: 202 if p.endswith('?'): 203 p = p[:-1] 204 tags[p] = getattr(self, p, None) 205 206 return tags 207 208 def __setattr__(self, attr, value): 209 if hasattr(self, 'thePickle'): 210 if hasattr(self, attr): 211 curr = getattr(self, attr) 212 if type(curr) not in self.__intorstringtype__ or type(value) not in self.__intorstringtype__ or curr != value: 213 self.Changed() 214 else: 215 self.Changed() 216 self.__dict__[attr] = value 217 218 def __repr__(self): 219 try: 220 if self.__guid__ == 'macho.AuthenticationReq': 221 return 'Packet::AuthenticationReq(%s,%s,%s,%s,%s)' % (self.source, 222 self.destination, 223 self.clientinfo, 224 self.userName, 225 getattr(self, 'address', None)) 226 self.GetPickle() 227 if len(self.thePickle) > 1500000: 228 return 'Packet::%s (%s,%s,GENOCIDAL PAYLOAD(%d bytes),%s, %s)' % (self.__class__.__name__, 229 self.source, 230 self.destination, 231 len(self.thePickle), 232 self.oob, 233 self.contextKey) 234 if len(self.thePickle) > 1000000: 235 return 'Packet::%s (%s,%s,MURDEROUS PAYLOAD(%d bytes),%s, %s)' % (self.__class__.__name__, 236 self.source, 237 self.destination, 238 len(self.thePickle), 239 self.oob, 240 self.contextKey) 241 if len(self.thePickle) > 100000: 242 return 'Packet::%s (%s,%s,GARGANTUAN PAYLOAD(%d bytes),%s, %s)' % (self.__class__.__name__, 243 self.source, 244 self.destination, 245 len(self.thePickle), 246 self.oob, 247 self.contextKey) 248 if len(self.thePickle) > 10000: 249 return 'Packet::%s (%s,%s,HUGE PAYLOAD(%d bytes),%s, %s)' % (self.__class__.__name__, 250 self.source, 251 self.destination, 252 len(self.thePickle), 253 self.oob, 254 self.contextKey) 255 if len(self.thePickle) > 1000: 256 return 'Packet::%s (%s,%s,LARGE PAYLOAD(%d bytes),%s, %s)' % (self.__class__.__name__, 257 self.source, 258 self.destination, 259 len(self.thePickle), 260 self.oob, 261 self.contextKey) 262 try: 263 l = len(self.thePickle) 264 params = [] 265 for each in self.__machodesc__['params']: 266 if each[-1:] == '?': 267 tmp = each[:-1] 268 if hasattr(self, tmp): 269 params.append(getattr(self, tmp)) 270 else: 271 params.append(getattr(self, each)) 272 273 if hasattr(self, 'strayload'): 274 return 'Packet::%s (%s,%s,%s bytes,%s,%s, %s)' % (self.__class__.__name__, 275 self.source, 276 self.destination, 277 l, 278 self.strayload, 279 self.oob, 280 self.contextKey) 281 return 'Packet::%s (%s,%s,%s bytes,%s,%s, %s)' % (self.__class__.__name__, 282 self.source, 283 self.destination, 284 l, 285 params, 286 self.oob, 287 self.contextKey) 288 except Exception: 289 log.LogException() 290 return 'Packet::%s (CRAPPY TUPLE)' % self.__class__.__name__ 291 292 except Exception: 293 log.LogException() 294 return 'Packet containing crappy data' 295 296 __str__ = __repr__ 297 298 299 class ErrorResponse(MachoPacket): 300 __guid__ = 'macho.ErrorResponse' 301 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_ERRORRESPONSE, 302 'params': ['originalCommand', 'code', 'payload']} 303 304 305 class IdentificationRsp(MachoPacket): 306 __guid__ = 'macho.IdentificationRsp' 307 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_IDENTIFICATION_RSP, 308 'params': ['accepted', 309 'nodeID', 310 'others', 311 'isProxy', 312 'isApp', 313 'serviceMask']} 314 315 316 class IdentificationReq(MachoPacket): 317 __guid__ = 'macho.IdentificationReq' 318 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_IDENTIFICATION_REQ, 319 'params': ['nodeID', 320 'myaddress', 321 'others', 322 'isProxy', 323 'isApp', 324 'serviceMask'], 325 'response': IdentificationRsp} 326 327 328 class CallRsp(MachoPacket): 329 __guid__ = 'macho.CallRsp' 330 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_CALL_RSP, 331 'params': ['payload']} 332 333 334 class CallReq(MachoPacket): 335 __guid__ = 'macho.CallReq' 336 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_CALL_REQ, 337 'params': ['payload?'], 338 'response': CallRsp} 339 340 341 class TransportClosed(MachoPacket): 342 __guid__ = 'macho.TransportClosed' 343 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_TRANSPORTCLOSED, 344 'params': ['clientID', 'isRemote']} 345 346 347 class Notification(MachoPacket): 348 __guid__ = 'macho.Notification' 349 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_NOTIFICATION, 350 'params': ['payload?']} 351 352 353 class SessionChangeNotification(MachoPacket): 354 __guid__ = 'macho.SessionChangeNotification' 355 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_SESSIONCHANGENOTIFICATION, 356 'params': ['sid', 'change', 'nodesOfInterest?']} 357 358 359 class SessionInitialStateNotification(MachoPacket): 360 __guid__ = 'macho.SessionInitialStateNotification' 361 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_SESSIONINITIALSTATENOTIFICATION, 362 'params': ['sid', 'sessionType', 'initialstate']} 363 364 365 class PingRsp(MachoPacket): 366 __guid__ = 'macho.PingRsp' 367 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_PING_RSP, 368 'params': ['times']} 369 370 371 class PingReq(MachoPacket): 372 __guid__ = 'macho.PingReq' 373 __machodesc__ = {'command': cluster.MACHONETMSG_TYPE_PING_REQ, 374 'response': PingRsp, 375 'params': ['times']}