Index: fedd/federation/access.py
===================================================================
--- fedd/federation/access.py	(revision cc8d8e971b7119cdebf6c0a7ae1d1da299531ce6)
+++ fedd/federation/access.py	(revision 6c57fe9deaa34079254d04e9785091c1e9717115)
@@ -18,4 +18,9 @@
 from remote_service import xmlrpc_handler, soap_handler, service_caller
 
+import topdl
+import httplib
+import tempfile
+from urlparse import urlparse
+
 
 # Make log messages disappear if noone configures a fedd logger
@@ -57,4 +62,5 @@
         self.fileserver = config.get("access", "fileserver")
         self.eventserver = config.get("access", "eventserver")
+	self.certdir = config.get("access","certdir")
 
         self.attrs = { }
@@ -649,4 +655,11 @@
             for o in owners:
                 self.auth.set_attribute(o, allocID)
+	    try:
+		f = open("%s/%s.pem" % (self.certdir, aid), "w")
+		print >>f, alloc_cert
+		f.close()
+	    except IOError, e:
+		raise service_error(service_error.internal, 
+			"Can't open %s/%s : %s" % (self.certdir, aid, e))
             resp = self.build_response({ 'fedid': allocID } , ap)
             return resp
@@ -1124,4 +1137,30 @@
 
     def StartSegment(self, req, fid):
+	def get_url(url, cf, tmpdir):
+	    po = urlparse(url)
+	    fn = po.path.rpartition('/')[2]
+	    try:
+		conn = httplib.HTTPSConnection(po.hostname, port=po.port, 
+			cert_file=cf, key_file=cf)
+		conn.putrequest('GET', po.path)
+		conn.endheaders()
+		response = conn.getresponse()
+
+		lf = open("%s/%s" % (tmpdir, fn), "w")
+		buf = response.read(4096)
+		while buf:
+		    lf.write(buf)
+		    buf = response.read(4096)
+		lf.close()
+	    except IOError, e:
+		raise service_error(service_error.internal,
+			"Erro writing tempfile: %s" %e)
+	    except httplib.HTTPException, e:
+		raise service_error(service_error.internal, 
+			"Error retrieving data: %s" % e)
+
+	configs = set(('hosts', 'ssh_pubkey', 'ssh_secretkey'))
+
+
 	try:
 	    req = req['StartSegmentRequestBody']
@@ -1129,7 +1168,36 @@
 	    raise service_error(server_error.req, "Badly formed request")
 	auth_attr = req['allocID']['fedid']
+	attrs = req.get('fedAttr', [])
+	print auth_attr
+	print "%s" % auth_attr
 	if self.auth.check_attribute(fid, auth_attr):
 	    print "OK"
 	else:
 	    print "Fail"
+
+	if req.has_key('segmentdescription') and \
+		req['segmentdescription'].has_key('topdldescription'):
+	    topo = \
+		topdl.Topology(**req['segmentdescription']['topdldescription'])
+	else:
+	    raise service_error(service_error.req, 
+		    "Request missing segmentdescription'")
+
+	certfile = "%s/%s.pem" % (self.certdir, auth_attr)
+        try:
+            tmpdir = tempfile.mkdtemp(prefix="access-")
+        except IOError:
+            raise service_error(service_error.internal, "Cannot create tmp dir")
+
+	sw = set()
+	for e in [c for c in topo.elements if getattr(c, 'software', False)]:
+	    for s in e.software:
+		sw.add(s.location)
+	for s in sw:
+	    get_url(s, certfile, tmpdir)
+
+	for a in attrs:
+	    if a['attribute'] in configs:
+		get_url(a['value'], certfile, tmpdir)
+
 	return { 'allocID': req['allocID'] }
Index: fedd/federation/experiment_control.py
===================================================================
--- fedd/federation/experiment_control.py	(revision cc8d8e971b7119cdebf6c0a7ae1d1da299531ce6)
+++ fedd/federation/experiment_control.py	(revision 6c57fe9deaa34079254d04e9785091c1e9717115)
@@ -18,6 +18,6 @@
 import xml.parsers.expat
 
-from threading import *
-from subprocess import *
+from threading import Lock, Thread, Condition
+from subprocess import call, Popen, PIPE
 
 from urlparse import urlparse
@@ -232,6 +232,5 @@
                 or config.get("globals", "trusted_certs")
 
-	# XXX:
-	self.repodir = '/usr/local/etc/fedd/repo'
+	self.repodir = config.get("experiment_control", "repodir")
 
         self.exp_stem = "fed-stem"
@@ -2152,5 +2151,5 @@
 	    self.caller = caller
 
-	def __call__(self, uri, aid, topo):
+	def __call__(self, uri, aid, topo, attrs=None):
 	    req = {
 		    'allocID': { 'fedid' : aid }, 
@@ -2159,6 +2158,7 @@
 		    },
 		}
-
-	    print "calling %s"  % uri
+	    if attrs:
+		req['fedAttr'] = attrs
+
 	    r = self.caller(uri, req, self.cert_file, self.cert_pwd,
 		    self.trusted_certs)
@@ -2170,5 +2170,5 @@
 
     def new_allocate_resources(self, allocated, master, eid, expid, expcert, 
-            tbparams, topo, tmpdir, alloc_log=None):
+            tbparams, topo, tmpdir, alloc_log=None, attrs=None):
         started = { }           # Testbeds where a sub-experiment started 
                                 # successfully
@@ -2203,5 +2203,5 @@
 			trusted_certs=self.trusted_certs,
 			caller=self.call_StartSegment), 
-                    args=(uri, aid, topo[tb]), name=tb,
+                    args=(uri, aid, topo[tb], attrs), name=tb,
                     pdata=thread_pool, trace_file=self.trace_file)
             threads.append(t)
@@ -2294,4 +2294,18 @@
         to instantiate them and start it all up.
         """
+
+	def add_kit(e, kit):
+	    """
+	    Add a Software object created from the list of (install, location)
+	    tuples passed as kit  to the software attribute of an object e.  We
+	    do this enough to break out the code, but it's kind of a hack to
+	    avoid changing the old tuple rep.
+	    """
+
+	    s = [ topdl.Software(install=i, location=l) for i, l in kit]
+
+	    if isinstance(e.software, list): e.software.extend(s)
+	    else: e.software = s 
+
 
         if not self.auth.check_attribute(fid, 'create'):
@@ -2463,5 +2477,12 @@
 		    reverse=True)
 	    ips = ip_allocator(int(ip_addr("10.0.0.0")), 2 **24)
-	    for s in subs:
+	    ifs = { }
+	    hosts = [ ]
+	    # The config urlpath
+	    configpath = "/%s/config" % expid
+	    # The config file system location
+	    configdir ="%s%s" % ( self.repodir, configpath)
+
+	    for idx, s in enumerate(subs):
 		a = ips.allocate(len(s.interfaces)+2)
 		if a :
@@ -2479,10 +2500,28 @@
 			    topdl.Attribute('ip4_address', 
 				"%s" % ip_addr(base)))
+		    hname = i.element.name[0]
+		    if ifs.has_key(hname):
+			hosts.append("%s\t%s-%s %s-%d" % \
+				(ip_addr(base), hname, s.name, hname,
+				    ifs[hname]))
+		    else:
+			ifs[hname] = 0
+			hosts.append("%s\t%s-%s %s-%d %s" % \
+				(ip_addr(base), hname, s.name, hname,
+				    ifs[hname], hname))
+
+		    ifs[hname] += 1
 		    base += 1
-
+	    # save config files
+	    try:
+		os.makedirs(configdir)
+	    except IOError, e:
+		raise service_error(
+			"Cannot create config directory: %s" % e)
 	    # Find the testbeds to look up
 	    testbeds = set([ a.value for e in top.elements \
 		    for a in e.attribute \
 			if a.attribute == 'testbed'] )
+
 
 	    # Make per testbed topologies.  Copy the main topo and remove
@@ -2510,5 +2549,39 @@
 		topo[tb].make_indices()
 
-
+		for e in topo[tb].elements:
+		    if tb == master:
+			cmd = 'sudo -H /usr/local/federation/bin/make_hosts /proj/%s/exp/%s/tmp/hosts >& /tmp/federate' % (tbparams[tb].get('project', 'project'), eid)
+		    else:
+			cmd = "sudo -H /bin/sh /usr/local/federation/bin/federate.sh >& /tmp/federate"
+		    scmd = e.get_attribute('startup')
+		    if scmd:
+			cmd = "%s \\$USER '%s'" % (cmd, scmd)
+
+		    e.set_attribute('startup', cmd)
+		    if self.fedkit: add_kit(e, self.fedkit)
+
+	    # Copy configuration files into the remote file store
+	    try:
+		f = open("%s/hosts" % configdir, "w")
+		f.write('\n'.join(hosts))
+		f.close()
+	    except IOError, e:
+		raise service_error(service_error.internal, 
+			"Cannot write hosts file: %s" % e)
+	    try:
+		self.copy_file("%s" % gw_pubkey, "%s/%s" % \
+			(configdir, gw_pubkey_base))
+		self.copy_file("%s" % gw_secretkey, "%s/%s" % \
+			(configdir, gw_secretkey_base))
+	    except IOError, e:
+		raise service_error(service_error.internal, 
+			"Cannot copy keyfiles: %s" % e)
+
+	    # Allow the individual testbeds to access the configuration files.
+	    for tb in tbparams.keys():
+		asignee = tbparams[tb]['allocID']['fedid']
+		for f in ("hosts", gw_secretkey_base, gw_pubkey_base):
+		    self.auth.set_attribute(asignee, "%s/%s" % (configpath, f))
+		    print "assigned %s/%s" % (configpath, f)
 
 	    # Now, for each substrate in the main topology, find those that
@@ -2516,21 +2589,24 @@
 	    # into the copies of those substrates on the sub topologies.
 	    for s in top.substrates:
-		tests = { }
+		# tbs will contain an ip address on this subsrate that is in
+		# each testbed.
+		tbs = { }
 		for i in s.interfaces:
 		    e = i.element
 		    tb = e.get_attribute('testbed')
-		    if tb and not tests.has_key(tb):
+		    if tb and not tbs.has_key(tb):
 			for i in e.interface:
 			    if s in i.subs:
-				tests[tb]= \
-					i.get_attribute('ip4_address')
-		if len(tests) < 2:
+				tbs[tb]= i.get_attribute('ip4_address')
+		if len(tbs) < 2:
 		    continue
 
 		# More than one testbed is on this substrate.  Insert
-		# some portals into the subtopologies.
-
-		for st in tests.keys():
-		    for dt in [ t for t in tests.keys() if t != st]:
+		# some portals into the subtopologies.  st == source testbed,
+		# dt == destination testbed.
+		segment_substrate = { }
+		for st in tbs.keys():
+		    segment_substrate[st] = { }
+		    for dt in [ t for t in tbs.keys() if t != st]:
 			myname =  "%stunnel" % dt
 			desthost  =  "%stunnel" % st
@@ -2541,51 +2617,70 @@
 			ddomain = ".%s.%s%s" % (eid, dproject,
 				tbparams[dt].get('domain', ".example.com"))
-			boss = tbparams[master].get('boss', "boss")
-			fs = tbparams[master].get('fs', "fs")
-			event_server = "%s%s" % \
-				(tbparams[st].get('eventserver', "event_server"),
-					tbparams[dt].get('domain', "example.com"))
-			remote_event_server = "%s%s" % \
-				(tbparams[dt].get('eventserver', "event_server"),
-					tbparams[dt].get('domain', "example.com"))
-			seer_control = "%s%s" % \
-				(tbparams[st].get('control', "control"), sdomain)
-			local_key_dir = "/proj/%s/exp/%s/tmp" % ( sproject, eid)
-			remote_conf_dir = "/proj/%s/exp/%s/tmp" % ( dproject, eid)
-			conf_file = "%s%s.gw.conf" % (myname, sdomain)
-			remote_conf_file = "%s%s.gw.conf" % (desthost, ddomain)
-			# translate to lower case so the `hostname` hack for specifying
-			# configuration files works.
-			conf_file = conf_file.lower();
-			remote_conf_file = remote_conf_file.lower();
+			mdomain = "%s.%s%s" % (eid, 
+				tbparams[master].get('project', 'project'),
+				tbparams[master].get('domain', '.example.com'))
+			# XXX: active and type need to be unkludged
 			active = ("%s" % (st == master))
-			portal = topdl.Computer(**{
-				'name': "%stunnel" % dt,
-				'attribute' : [{
-				    'attribute': n,
-				    'value': v,
-				    } for n, v in (\
-					    ('gateway', 'true'),
-					    ('boss', boss),
-					    ('fs', fs),
-					    ('event_server', event_server),
-					    ('remote_event_server', remote_event_server),
-					    ('seer_control', seer_control),
-					    ('local_key_dir', local_key_dir),
-					    ('remote_conf_dir', remote_conf_dir),
-					    ('conf_file', conf_file),
-					    ('remote_conf_file', remote_conf_file),
-					    ('remote_script_dir', "/usr/local/federation/bin"),
-					    ('local_script_dir', "/usr/local/federation/bin"),
-					    )],
-				'interface': [{
-				    'substrate': s.name,
-				    'attribute': [ { 
-					'attribute': 'ip4_addreess', 
-					'value': tests[dt],
-					}, ],
-				    }, ],
-				})
+			if not segment_substrate[st].has_key(dt):
+			    # Put a substrate and a segment for the connected
+			    # testbed in there.
+			    tsubstrate = \
+				    topdl.Substrate(name='%s-%s' % (st, dt))
+			    segment_element = topdl.Segment(
+				    id= tbparams[dt]['allocID'],
+				    type='emulab',
+				    uri = self.tbmap.get(dt, None),
+				    interface=[ 
+					topdl.Interface(
+					    substrate=tsubstrate.name),
+					],
+				    attribute = [
+					topdl.Attribute(attribute=n, value=v)
+					    for n, v in (\
+						('domain', ddomain),
+						('experiment', "%s/%s" % \
+							(dproject, eid)),)
+					],
+				    )
+			    segment_substrate[st][dt] = tsubstrate
+			    topo[st].substrates.append(tsubstrate)
+			    topo[st].elements.append(segment_element)
+			portal = topdl.Computer(
+				name="%stunnel" % dt,
+				attribute=[ 
+				    topdl.Attribute(attribute=n,value=v)
+					for n, v in (\
+					    ('portal', 'true'),
+					    ('masterdomain', mdomain),
+					    ('experiment', "%s/%s" % \
+						    (sproject, eid)),
+					    ('peer', "%s.%s" % \
+						    (desthost, ddomain)),
+					    ('scriptdir', 
+						"/usr/local/federation/bin"),
+					    ('active', "%s" % active),
+					    ('type', 'both'), 
+					    ('startup', 'sudo -H /usr/local/federation/bin/fed-tun.pl -f /proj/%s/exp/%s/tmp/%s%s.gw.conf >& /tmp/bridge.log' % (sproject, eid, myname.lower(), sdomain.lower())))
+				    ],
+				interface=[
+				    topdl.Interface(
+					substrate=s.name,
+					attribute=[ 
+					    topdl.Attribute(
+						attribute='ip4_addreess', 
+						value=tbs[dt]
+					    )
+					]),
+				    topdl.Interface(
+					substrate=\
+					    segment_substrate[st][dt].name
+					),
+				    ],
+				)
+			if self.fedkit: add_kit(portal, self.fedkit)
+			if self.gatewaykit: add_kit(portal, self.gatewaykit)
+
 			topo[st].elements.append(portal)
+
 	    # Connect the gateway nodes into the topologies and clear out
 	    # substrates that are not in the topologies
@@ -2626,4 +2721,5 @@
 		try:
 		    f = open("%s/%s" % (softdir, dest) , "w")
+		    self.log.debug("Writing %s/%s" % (softdir,dest) )
 		    data = u.read(4096)
 		    while data:
@@ -2644,5 +2740,5 @@
 		for tb in tbparams.keys():
 		    self.auth.set_attribute(tbparams[tb]['allocID']['fedid'], 
-			    "%s/%s" % ( path, dest))
+			    "/%s/%s" % ( path, dest))
 
 	    # Convert the software locations in the segments into the local
@@ -2650,5 +2746,6 @@
 	    for soft in [ s for tb in topo.values() \
 		    for e in tb.elements \
-			for s in e.software ]:
+			if getattr(e, 'software', False) \
+			    for s in e.software ]:
 		if softmap.has_key(soft.location):
 		    soft.location = softmap[soft.location]
@@ -2709,8 +2806,28 @@
         alloc_log.addHandler(h)
         
+	# XXX
+	url_base = 'https://users.isi.deterlab.net:23232'
+	attrs = [ 
+		{
+		    'attribute': 'ssh_pubkey', 
+		    'value': '%s/%s/config/%s' % \
+			    (url_base, expid, gw_pubkey_base)
+		},
+		{
+		    'attribute': 'ssh_secretkey', 
+		    'value': '%s/%s/config/%s' % \
+			    (url_base, expid, gw_secretkey_base)
+		},
+		{
+		    'attribute': 'hosts', 
+		    'value': '%s/%s/config/hosts' % \
+			    (url_base, expid)
+		},
+	    ]
+
         # Start a thread to do the resource allocation
         t  = Thread(target=self.new_allocate_resources,
                 args=(allocated, master, eid, expid, expcert, tbparams, 
-                    topo, tmpdir, alloc_log),
+                    topo, tmpdir, alloc_log, attrs),
                 name=eid)
         t.start()
@@ -2774,6 +2891,9 @@
 
     def get_handler(self, path, fid):
-        print "in get_handler %s %s" % (path, fid)
-        return ("/users/faber/test.html", "text/html")
+	print "%s" %  path
+	if self.auth.check_attribute(fid, path):
+	    return ("%s/%s" % (self.repodir, path), "application/binary")
+	else:
+	    return (None, None)
 
     def get_vtopo(self, req, fid):
Index: fedd/federation/server.py
===================================================================
--- fedd/federation/server.py	(revision cc8d8e971b7119cdebf6c0a7ae1d1da299531ce6)
+++ fedd/federation/server.py	(revision 6c57fe9deaa34079254d04e9785091c1e9717115)
@@ -140,10 +140,13 @@
 	    code = 200
 	    fid = fedid(cert=self.request.get_peer_cert())
-	    print "in do_GET: %s %s" % (fid, self.request.get_peer_cert())
 	    fname, type = self.server.get_handler(self.path, fid)
-	    try:
-		f = open(fname, "rb")
-		size = os.path.getsize(fname)
-	    except IOError, e:
+	    if fname:
+		try:
+		    f = open(fname, "rb")
+		    size = os.path.getsize(fname)
+		except IOError, e:
+		    code = 404
+		    size = 0
+	    else:
 		code = 404
 		size = 0
Index: fedd/federation/topdl.py
===================================================================
--- fedd/federation/topdl.py	(revision cc8d8e971b7119cdebf6c0a7ae1d1da299531ce6)
+++ fedd/federation/topdl.py	(revision 6c57fe9deaa34079254d04e9785091c1e9717115)
@@ -1,3 +1,5 @@
 #!/usr/local/bin/python
+
+import re
 
 class base:
@@ -31,5 +33,14 @@
 	return rv
 
-
+    def set_attribute(self, key, value):
+	attrs = getattr(self, 'attribute', None)
+	if attrs is None:
+	    return
+	for a in attrs:
+	    if a.attribute == key: 
+		a.value = value
+		break
+	else:
+	    attrs.append(Attribute(key, value))
 
 class ConsistencyError(RuntimeError): pass
@@ -207,4 +218,26 @@
 	if self.attribute:
 	    rv['attribute'] = [ a.to_dict() for a in self.attribute ]
+	return rv
+
+class ID(base):
+    def __init__(self, fedid=None, uuid=None, uri=None, localname=None,
+	    kerberosUsername=None):
+	self.fedid=fedid
+	self.uuid = uuid
+	self.uri = uri
+	self.localname = localname
+	self.kerberosUsername = kerberosUsername
+
+    def clone(self):
+	return ID(self.fedid, self.uuid, self.uri, self.localname,
+		self.kernberosUsername)
+
+    def to_dict(self):
+	rv = { }
+	if self.fedid: rv['fedid'] = self.fedid
+	if self.uuid: rv['uuid'] = self.uuid
+	if self.uri: rv['uri'] = self.uri
+	if self.localname: rv['localname'] = self.localname
+	if self.kerberosUsername: rv['kerberosUsername'] = self.kerberosUsername
 	return rv
 
@@ -256,4 +289,56 @@
 	return { 'computer': rv }
 
+
+class Testbed(base):
+    def __init__(self, uri, type, interface=[], attribute=[]):
+	self.uri = uri
+	self.type = type
+	self.interface = [ self.init_class(Interface, c) \
+		for c in self.make_list(interface) ]
+	self.attribute = [ self.init_class(Attribute, c) \
+		for c in self.make_list(attribute) ]
+
+    def clone(self):
+	return Testbed(self.uri, self.type,
+		interface=[i.clone() for i in self.interface],
+		attribute=[a.cone() for a in self.attribute])
+
+    def to_dict(self):
+	rv = { }
+	if self.uri: rv['uri'] = self.uri
+	if self.type: rv['type'] = self.type
+	if self.interface:
+	    rv['interface'] = [ i.to_dict() for i in self.interface]
+	if self.attribute:
+	    rv['attribute'] = [ a.to_dict() for a in self.attribute]
+	return { 'testbed': rv }
+
+class Segment(base):
+    def __init__(self, id, type, uri, interface=[], attribute=[]):
+	self.id = self.init_class(ID, id)
+	self.type = type
+	self.uri = uri
+	self.interface = [ self.init_class(Interface, c) \
+		for c in self.make_list(interface) ]
+	self.attribute = [ self.init_class(Attribute, c) \
+		for c in self.make_list(attribute) ]
+
+    def clone(self):
+	return Segment(self.id.clone(), self.type, self.uri, 
+		interface=[i.clone() for i in self.interface], 
+		attribute=[a.clone() for a in attribute])
+
+    def to_dict(self):
+	rv = { }
+	if self.id: rv['id'] = self.id.to_dict()
+	if self.type: rv['type'] = self.type
+	if self.uri: rv['uri'] = self.uri
+	if self.interface:
+	    rv['interface'] = [ i.to_dict() for i in self.interface ]
+	if self.attribute:
+	    rv['attribute'] = [ a.to_dict() for a in self.attribute ]
+	return { 'segment': rv }
+
+
 class Other(base):
     def __init__(self, interface=[], attribute=[]):
@@ -268,8 +353,10 @@
 
     def to_dict(self):
+	rv = {}
 	if self.interface:
 	    rv['interface'] = [ i.to_dict() for i in self.interface ]
 	if self.attribute:
 	    rv['attribute'] = [ a.to_dict() for a in self.attribute ]
+	return {'other': rv }
 
 
@@ -285,4 +372,6 @@
 	classmap = {
 		'computer': Computer,
+		'testbed': Testbed,
+		'segment': Segment,
 		'other': Other,
 	    }
@@ -489,5 +578,30 @@
     return { 'node': nodes, 'lan': lans }
 
-def topology_to_ns2(t):
+def to_tcl_name(n):
+    t = re.subst('-(\d+)', '(\1)', n)
+    return t
+
+def generate_portal_image_filter(image):
+    def rv(e):
+	s =""
+	if isinstance(e, Computer):
+	    gw = e.get_attribute('portal')
+	    if gw:
+		s = "tb-set-node-os $%s %s" % (to_tcl_name(e.name[0]), image)
+	return s
+    return rv
+
+def generate_portal_command_filter(cmd):
+    def rv(e):
+	s =""
+	if isinstance(e, Computer):
+	    gw = e.get_attribute('portal')
+	    if gw:
+		s = "%s $%s" % (cmd, to_tcl_name(e.name[0]))
+	return s
+    return rv
+
+
+def topology_to_ns2(t, filters=[]):
     out = """
 set ns [new Simulator]
@@ -495,9 +609,10 @@
 
 """
+
     for e in t.elements:
 	rpms = ""
 	tarfiles = ""
 	if isinstance(e, Computer):
-	    name = e.name[0]
+	    name = to_tcl_name(e.name[0])
 	    out += "set %s [$ns node]\n" % name
 	    if e.os and len(e.os) == 1:
@@ -517,4 +632,6 @@
 	    if startcmd:
 		out+= 'tb-set-node-startcmd $%s "%s"\n' % (name, startcmd)
+	    for f in filters:
+		out += f(e)
 	    out+= "\n"
     
@@ -525,10 +642,11 @@
 	else:
 	    delay = 0
-	name = s.name or "sub%d" % idx
+	name = to_tcl_name(s.name or "sub%d" % idx)
 
 	if len(s.interfaces) > 2:
 	    # Lan
-	    members = [ i.element.name[0] for i in s.interfaces]
-	    out += 'set %s [$ns make-lan "%s" %f %fms ]\n' % \
+	    members = [ to_tcl_name("$%s") % i.element.name[0] \
+		    for i in s.interfaces]
+	    out += 'set %s [$ns make-lan "%s" %fkb %fms ]\n' % \
 		    (name, " ".join(members), s.capacity.rate, delay)
 	    if loss:
@@ -541,13 +659,13 @@
 		    out += "tb-set-ip-lan $%s $%s %s\n" % (e.name, name, ip)
 		if i.capacity and i.capacity.rate != s.capacity.rate:
-		    out += "tb-set-node-lan-bandwidth $%s $%s %f\n" % \
-			    (e.name[0], name, i.capacity.rate)
+		    out += "tb-set-node-lan-bandwidth $%s $%s %fkb\n" % \
+			    (to_tcl_name(e.name[0]), name, i.capacity.rate)
 		if i.latency and i.latency.time != delay:
 		    out += "tb-set-node-lan-delay $%s $%s %fms\n" % \
-			    (e.name[0], name, i.latency.time)
+			    (to_tcl_name(e.name[0]), name, i.latency.time)
 		iloss = i.get_attribute('loss')
 		if loss and iloss != loss :
 		    out += "tb-set-node-lan-loss $%s $%s %f" % \
-			    (e.name[0], name, float(loss))
+			    (to_tcl_name(e.name[0]), name, float(loss))
 	    out+= "\n"
 	elif len(s.interfaces) == 2:
@@ -555,6 +673,7 @@
 	    t = s.interfaces[1]
 
-	    out += "set %s [$ns duplex-link $%s $%s %f %fms DropTail]\n" %\
-		    (name, f.element.name[0], t.element.name[0],
+	    out += "set %s [$ns duplex-link $%s $%s %fkb %fms DropTail]\n" %\
+		    (name, to_tcl_name(f.element.name[0]), 
+			    to_tcl_name(t.element.name[0]),
 			    s.capacity.rate, delay)
 	    if loss:
@@ -577,7 +696,10 @@
 		    else: loss = loss or 0.0
 
-		    out += "tb-set-link-simplex-params $%s $%s %fms %f %f\n" % \
-			    (name, i.element.name[0], delay, cap, loss)
+		    out += "tb-set-link-simplex-params $%s $%s %fms %fkb %f\n"\
+			    % (name, to_tcl_name(i.element.name[0]),
+				    delay, cap, loss)
 	    out+= "\n"
+	for f in filters:
+	    out+= f(s)
     out+="""
 $ns run
