graphMaker.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python2
  2. # You may redistribute this program and/or modify it under the terms of
  3. # the GNU General Public License as published by the Free Software Foundation,
  4. # either version 3 of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. def makeGraph():
  14. import adminTools as admin
  15. import networkx as nx
  16. from publicToIp6 import PublicToIp6_convert
  17. from collections import deque
  18. cjdns=admin.connect()
  19. root=admin.whoami(cjdns)
  20. rootIP=root['IP']
  21. G=nx.Graph()
  22. G.add_node(rootIP[-4:],ip=rootIP)
  23. nodes=deque()
  24. nodes.append(rootIP)
  25. while len(nodes) != 0:
  26. parentIP=nodes.popleft()
  27. resp=cjdns.NodeStore_nodeForAddr(parentIP)
  28. numLinks=0
  29. if 'result' in resp:
  30. link=resp['result']
  31. if 'linkCount' in link:
  32. numLinks=int(resp['result']['linkCount'])
  33. G.node[parentIP[-4:]]['version']=resp['result']['protocolVersion']
  34. for i in range(0,numLinks):
  35. resp = cjdns.NodeStore_getLink(i, parent=parentIP)
  36. childLink=resp['result']
  37. if not childLink: continue
  38. childAddr=admin.parseAddr(childLink['child'])
  39. childIP=PublicToIp6_convert(childAddr['publicKey'])
  40. # Check to see if its one hop away from parent node
  41. if childLink['isOneHop'] != 1:
  42. continue
  43. # If its a new node then we want to follow it
  44. if not childIP[-4:] in G.nodes():
  45. G.add_node(childIP[-4:],ip=childIP)
  46. G.node[childIP[-4:]]['version']=0
  47. nodes.append(childIP)
  48. # If there is not a link between the nodes we should put one there
  49. if (not childIP[-4:] in G[parentIP[-4:]]):
  50. G.add_edge(parentIP[-4:],childIP[-4:])
  51. return G