示例#1
0
     def connect(s):
          """ Establish a connection with the RE atlas. """
          s._setup_socket();
          s._socket.connect((s.host,s.port));
          # Shake hands with server
          msg, closed = netutils.readall(s._socket,11);
          if (closed):
               raise ConnectionError("Could not connect to RE atlas server. ");
          if (msg != "REatlas" + struct.pack('!l',s._protocol_version)):
               if (msg[0] == SRV_MSG):
                    lenrest = netutils.ntohll(msg[1:9]);
                    msg2,closed = netutils.readall(s._socket,lenrest-2);
                    msg = msg[9:] + msg2
                    s.disconnect();
                    raise ConnectionError("Received error message from server: " + msg);
               else:
                    s.disconnect();
                    raise ConnectionError("Bad handshake from server or unsupported server version.");
          try:
               s._socket.sendall(netutils.htonl(428344082)); # Send our handshake to the server
          except socket.error:
               s.disconnect();
               raise ConnectionError("Could not connect to RE atlas server.");

          s._is_connected = True;
          s._request_count = 0;
示例#2
0
     def _download_to_string(s,filename,username):
          s._request_file(filename,username,intent="download");
          (msgtype,msglen) = s._get_next_nonkeepalive_packet_of_type(BIN_FILE);

          buf,closed = netutils.readall(s._socket,msglen);

          if (len(buf) != msglen):
               s.disconnect();
               raise ConnectionError("Server closed connection before file was transferred.");

          return buf;
示例#3
0
     def _get_json_reply(s):
          """ Attempt to receive a JSON-RPC reply string from the server. 
          If the next message is not a JSON-RPC reply, throw an exception
          and close the connection. Skips keepalive messages. """

          (msgtype,msglen) = s._get_next_nonkeepalive_packet_of_type(JSON_MSG);

          (json_msg, closed) = netutils.readall(s._socket,msglen);
          if (closed):
               s.disconnect();
               raise ConnectionError("Connection closed before entire JSON reply was received.");
          
          return json_msg;
示例#4
0
     def _download_to_file(s,fp,filename,username):

          s._request_file(filename,username,intent="download");

          (msgtype,msglen) = s._get_next_nonkeepalive_packet_of_type(BIN_FILE);

          while (msglen > 0):
               buf,closed = netutils.readall(s._socket, min(msglen,16384));
               l = len(buf);
               if (l > 0):
                    fp.write(buf);
               msglen -= l;
               if (closed and msglen > 0):
                    s.disconnect();
                    raise ConnectionError("Server closed connection before file was transferred.");
示例#5
0
 def _get_next_header(s):
      """ Read the header of the next message from the server.
      Assumes that the next data sent from the server is a completely
      new message. """
      (header, closed) = netutils.readall(s._socket,9);
      if (closed):
           s.disconnect();
           if (len(header) > 0):
                raise ConnectionError("Connection closed when reading message.");
           else:
                raise ConnectionError("Trying to read from closed connection.");
      msgtype, msglen = struct.unpack("!BQ", header);
      msgtype = netutils.htonb(msgtype);
      
      return (msgtype, msglen);
示例#6
0
     def _get_next_nonkeepalive_packet_of_type(s,thetype):
          """ Call this instead of get next header to skip
          keepalives and to catch server errors. """

          (msgtype, msglen) = s._get_next_header();
          while (msgtype == KEEPALIVE): # Get any keepalive packets out
               if (msglen != 0):
                    s.disconnect();
                    raise ConnectionError("Received invalid keepalive packet.");
               (msgtype, msglen) = s._get_next_header();
          
          if (msgtype == SRV_MSG):
               msg, closed = netutils.readall(s._socket,msglen);
               s.disconnect();
               raise ConnectionError(msg);

          if (msgtype != thetype): # Catch all 
               s.disconnect();
               msgtype_int = netutils.ntohb(msgtype);
               expected_int = netutils.ntohb(thetype);
               raise ConnectionError("Got unexpected reply message (" + str(msgtype_int) + "), expected " + str(expected_int) + ".");

          return (msgtype,msglen);
示例#7
0
atlas = reatlas_client.REatlas("localhost",65535);

atlas.connect();
atlas.build_functions();
print(atlas.login(username="******",password="******"));
print(atlas._get_available_methods());
print(atlas._select_current_file(filename="test"))

message = "HESTEHEST ! ! ! ! ! ! ! !"

atlas._socket.sendall(reatlas_client.BIN_FILE + netutils.htonll(len(message)));

atlas._socket.sendall(message);


packtype, closed = netutils.readall(atlas._socket,1);
l,closed = netutils.readall(atlas._socket,8);
l = netutils.ntohll(l);

#if (len(packtype) == 1 and len(l) == 8):
if True:
     print("Type: " + str(netutils.ntohb(packtype)) + " (" + packtype + ")");
     print("Length: " + str(l));
     if (l > 0):
          print(atlas._socket.recv(l));



atlas.disconnect();