docs: def construct_source_candidate_set(addr, plen, laddr, loname):
      """
      Given all addresses assigned to a specific interface ('laddr' parameter),
      this function returns the "candidate set" associated with 'addr/plen'.
      
      Basically, the function filters all interface addresses to keep only those
      that have the same scope as provided prefix.
      
      This is on this list of addresses that the source selection mechanism 
      will then be performed to select the best source address associated
      with some specific destination that uses this prefix.
      """
      def cset_sort(x,y):
          x_global = 0
          if in6_isgladdr(x):
              x_global = 1
          y_global = 0
          if in6_isgladdr(y):
              y_global = 1
          res = y_global - x_global
          if res != 0 or y_global != 1:
              return res
          # two global addresses: if one is native, it wins.
          if not in6_isaddr6to4(x):
              return -1;
          return -res
  
      cset = []
      if in6_isgladdr(addr) or in6_isuladdr(addr):
          cset = [ x for x in laddr if x[1] == IPV6_ADDR_GLOBAL ]
      elif in6_islladdr(addr):
          cset = [ x for x in laddr if x[1] == IPV6_ADDR_LINKLOCAL ]
      elif in6_issladdr(addr):
          cset = [ x for x in laddr if x[1] == IPV6_ADDR_SITELOCAL ]
      elif in6_ismaddr(addr):
          if in6_ismnladdr(addr):
              cset = [('::1', 16, loname)]
          elif in6_ismgladdr(addr):
              cset = [ x for x in laddr if x[1] == IPV6_ADDR_GLOBAL ]
          elif in6_ismlladdr(addr):
              cset = [ x for x in laddr if x[1] == IPV6_ADDR_LINKLOCAL ]
          elif in6_ismsladdr(addr):
              cset = [ x for x in laddr if x[1] == IPV6_ADDR_SITELOCAL ]
      elif addr == '::' and plen == 0:
          cset = [ x for x in laddr if x[1] == IPV6_ADDR_GLOBAL ]
      cset = [ x[0] for x in cset ]
      cset.sort(key = cmp_to_key(cset_sort)) # Sort with global addresses first
      return cset