def main(): all_nodes = dict() all_edges = [] for process in range(0, 1 if cjdns_use_default else cjdns_processes): print 'Connecting port %d...' % (cjdns_first_port + process),; sys.stdout.flush() try: cjdns = cjdns_connect(process) print adminTools.whoami(cjdns)['IP'] nodes, edges = generate_graph(cjdns) # Merge results all_nodes.update(nodes) for e in edges: if not e in all_edges: all_edges.append(e) except Exception, err: print 'Failed!' print traceback.format_exc()
def get_network_from_cjdns(ip, port, password): nodes = dict() edges = [] cjdns = admin.connect(ip, port, password) me = admin.whoami(cjdns) my_ip = me['IP'] nodes[my_ip] = Node(my_ip) nodes_to_check = deque() nodes_to_check.append(my_ip) while len(nodes_to_check) != 0: current_ip = nodes_to_check.popleft() resp = cjdns.NodeStore_nodeForAddr(current_ip) if not 'result' in resp or not 'linkCount' in resp['result']: continue result = resp['result'] link_count = result['linkCount'] if 'protocolVersion' in result: nodes[current_ip].version = result['protocolVersion'] for i in range(0, link_count): result = cjdns.NodeStore_getLink(current_ip, i)['result'] if not 'child' in result: continue child_ip = result['child'] # Add links with one hop only if result['isOneHop'] != 1: continue # Add node if not child_ip in nodes: nodes[child_ip] = Node(child_ip) nodes_to_check.append(child_ip) # Add edge e = Edge(nodes[current_ip], nodes[child_ip]) if not e.is_in(edges): edges.append(e) return (nodes, edges)
def makeGraph(): import adminTools as admin import networkx as nx from publicToIp6 import PublicToIp6_convert from collections import deque cjdns = admin.connect() root = admin.whoami(cjdns) rootIP = root['IP'] G = nx.Graph() G.add_node(rootIP[-4:], ip=rootIP) nodes = deque() nodes.append(rootIP) while len(nodes) != 0: parentIP = nodes.popleft() resp = cjdns.NodeStore_nodeForAddr(parentIP) numLinks = 0 if 'result' in resp: link = resp['result'] if 'linkCount' in link: numLinks = int(resp['result']['linkCount']) G.node[parentIP[-4:]]['version'] = resp['result'][ 'protocolVersion'] for i in range(0, numLinks): resp = cjdns.NodeStore_getLink(i, parent=parentIP) childLink = resp['result'] if not childLink: continue childAddr = admin.parseAddr(childLink['child']) childIP = PublicToIp6_convert(childAddr['publicKey']) # Check to see if its one hop away from parent node if childLink['isOneHop'] != 1: continue # If its a new node then we want to follow it if not childIP[-4:] in G.nodes(): G.add_node(childIP[-4:], ip=childIP) G.node[childIP[-4:]]['version'] = 0 nodes.append(childIP) # If there is not a link between the nodes we should put one there if (not childIP[-4:] in G[parentIP[-4:]]): G.add_edge(parentIP[-4:], childIP[-4:]) return G
def makeGraph(): import adminTools as admin import networkx as nx from publicToIp6 import PublicToIp6_convert from collections import deque cjdns=admin.connect() root=admin.whoami(cjdns) rootIP=root['IP'] G=nx.Graph() G.add_node(rootIP[-4:],ip=rootIP) nodes=deque() nodes.append(rootIP) while len(nodes) != 0: parentIP=nodes.popleft() resp=cjdns.NodeStore_nodeForAddr(parentIP) numLinks=0 if 'result' in resp: link=resp['result'] if 'linkCount' in link: numLinks=int(resp['result']['linkCount']) G.node[parentIP[-4:]]['version']=resp['result']['protocolVersion'] for i in range(0,numLinks): resp = cjdns.NodeStore_getLink(i, parent=parentIP) childLink=resp['result'] if not childLink: continue childAddr=admin.parseAddr(childLink['child']) childIP=PublicToIp6_convert(childAddr['publicKey']) # Check to see if its one hop away from parent node if childLink['isOneHop'] != 1: continue # If its a new node then we want to follow it if not childIP[-4:] in G.nodes(): G.add_node(childIP[-4:],ip=childIP) G.node[childIP[-4:]]['version']=0 nodes.append(childIP) # If there is not a link between the nodes we should put one there if (not childIP[-4:] in G[parentIP[-4:]]): G.add_edge(parentIP[-4:],childIP[-4:]) return G