graphMaker.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  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 <http://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(parentIP, i)
  36. childLink=resp['result']
  37. if not childLink: continue
  38. childIP=childLink['child']
  39. # Check to see if its one hop away from parent node
  40. if childLink['isOneHop'] != 1:
  41. continue
  42. # If its a new node then we want to follow it
  43. if not childIP[-4:] in G.nodes():
  44. G.add_node(childIP[-4:],ip=childIP)
  45. G.node[childIP[-4:]]['version']=0
  46. nodes.append(childIP)
  47. # If there is not a link between the nodes we should put one there
  48. if (not childIP[-4:] in G[parentIP[-4:]]):
  49. G.add_edge(parentIP[-4:],childIP[-4:])
  50. return G