openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

network.c (5478B)


      1 /* $Id: //depot/blt/srv/network/network.c#5 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <dlfcn.h>
     33 #include <blt/syscall.h>
     34 #include <blt/namer.h>
     35 #include <blt/tell.h>
     36 #include <blt/os.h>
     37 #include <blt/network/module.h>
     38 #include <blt/network/mbuf.h>
     39 
     40 static int ctl_port, tell_port;
     41 module_t *modlist = NULL;
     42 
     43 void network_cmd (const char *cmd, const char *arg)
     44 {
     45 	char c[] = { 0, 0 }, *name, *num, *prot;
     46 	int i, len, ifnum, state;
     47 	void *handle;
     48 	void (*probe)(void);
     49 	void (*config)(int, const char *, int, const char *);
     50 	module_t *m;
     51 
     52 	if (!strcmp (cmd, "load"))
     53 	{
     54 		printf (" %s", arg);
     55 		name = malloc (len = BLT_MAX_NAME_LENGTH);
     56 		strlcpy (name, "/boot/", len);
     57 		strlcat (name, arg, len);
     58 		strlcat (name, ".so", len);
     59 		handle = dlopen (name, 0);
     60 		if (handle == NULL)
     61 		{
     62 			printf ("(error)", arg);
     63 			return;
     64 		}
     65 		free (name);
     66 		m = malloc (sizeof (module_t));
     67 		m->name = malloc (strlen (arg) + 1);
     68 		strcpy (m->name, arg);
     69 		m->handle = handle;
     70 		m->next = modlist;
     71 		modlist = m;
     72 	}
     73 	else if (!strcmp (cmd, "probe"))
     74 	{
     75 		m = modlist;
     76 		while (m != NULL)
     77 		{
     78 			if (!strcmp (m->name, arg))
     79 			{
     80 				probe = dlsym (m->handle, "probe");
     81 				if (probe != NULL)
     82 				{
     83 					(*probe) ();
     84 					return;
     85 				}
     86 			}
     87 			else
     88 				m = m->next;
     89 		}
     90 	}
     91 	else if (!strcmp (cmd, "config"))
     92 	{
     93 		name = malloc (len = BLT_MAX_NAME_LENGTH);
     94 		for (i = 0; (i < len) && arg[i]; i++)
     95 			if ((arg[i] >= '0') && (arg[i] <= '9'))
     96 			{
     97 				name[i] = 0;
     98 				break;
     99 			}
    100 			else
    101 				name[i] = arg[i];
    102 
    103 		num = malloc (BLT_MAX_NAME_LENGTH);
    104 		*num = 0;
    105 		i++;
    106 		while ((arg[i] != ' ') && arg[i])
    107 		{
    108 			*c = arg[i];
    109 			strlcat (num, c, len);
    110 		}
    111 		ifnum = atoi (num);
    112 
    113 		prot = malloc (BLT_MAX_NAME_LENGTH);
    114 		*prot = 0;
    115 		i++;
    116 		while ((arg[i] != ' ') && arg[i])
    117 		{
    118 			*c = arg[i++];
    119 			strlcat (prot, c, len);
    120 		}
    121 
    122 		*num = 0;
    123 		i++;
    124 		while ((arg[i] != ' ') && arg[i])
    125 		{
    126 			*c = arg[i++];
    127 			strlcat (num, c, len);
    128 		}
    129 		if (!strcmp (num, "up"))
    130 			state = 1;
    131 		else if (!strcmp (num, "down"))
    132 			state = 0;
    133 		else
    134 			;
    135 
    136 		m = modlist;
    137 		while (m != NULL)
    138 		{
    139 			if (!strcmp (m->name, name))
    140 			{
    141 				config = dlsym (m->handle, "config");
    142 				if (config != NULL)
    143 				{
    144 					(*config) (ifnum, prot, state, arg + i + 1);
    145 					return;
    146 				}
    147 			}
    148 			else
    149 				m = m->next;
    150 		}
    151 		free (name);
    152 		free (num);
    153 		free (prot);
    154 	}
    155 }
    156 
    157 void network_tell (const char *msg)
    158 {
    159 	const char *c, *cmd;
    160 	char *full;
    161 	char *d;
    162 	int i, whole, len;
    163 
    164 	c = msg;
    165 	cmd = NULL;
    166 	whole = 0;
    167 	while (*c)
    168 	{
    169 		i = 0;
    170 		while ((c[i] != ' ') && c[i])
    171 			i++;
    172 		d = malloc (i + 1);
    173 		strlcpy (d, c, i + 1);
    174 		if (cmd == NULL)
    175 		{
    176 			cmd = d;
    177 			if (!strcmp (cmd, "load"))
    178 				printf ("network: loading drivers.  [");
    179 			else if (!strcmp (cmd, "config"))
    180 			{
    181 				whole = 1;
    182 				full = malloc (len = strlen (msg + 1));
    183 				*full = 0;
    184 			}
    185 		}
    186 		else if (!whole)
    187 			network_cmd (cmd, d);
    188 		else
    189 		{
    190 			strlcat (full, d, len);
    191 			strlcat (full, " ", len);
    192 		}
    193 		if (i != strlen (c))
    194 			c += i + 1;
    195 		else
    196 			break;
    197 	}
    198 	if (!strcmp (cmd, "load"))
    199 		printf (" ]\n");
    200 	else if (!strcmp (cmd, "config"))
    201 	{
    202 		network_cmd (cmd, full);
    203 		free (full);
    204 	}
    205 }
    206 
    207 int network_main (volatile int *ready)
    208 {
    209 	char *buffer;
    210 	int len;
    211 	msg_hdr_t mh;
    212 
    213 	ctl_port = port_create (0, "network_ctl_port");
    214 	tell_port = port_create (0, "network_tell_port");
    215 	port_slave (ctl_port, tell_port);
    216 	namer_register (ctl_port, "network");
    217 	namer_register (tell_port, "network:tell");
    218 
    219 	printf ("network: ready.\n");
    220 	buffer = malloc (len = 256);
    221 	mbinit ();
    222 	*ready = 1;
    223 
    224 	for (;;)
    225 	{
    226 		mh.src = 0;
    227 		mh.dst = ctl_port;
    228 		mh.data = buffer;
    229 		mh.size = len;
    230 		old_port_recv (&mh);
    231 
    232 		if (mh.dst == tell_port)
    233 		{
    234 			network_tell (buffer);
    235 			mh.dst = mh.src;
    236 			mh.src = tell_port;
    237 			mh.data = &tell_port;
    238 			mh.size = 1;
    239 			old_port_send (&mh);
    240 		}
    241 		else if (mh.dst == ctl_port)
    242 		{
    243 		}
    244 	}
    245 		
    246 	return 0;
    247 }
    248 
    249 int main (int argc, char **argv)
    250 {
    251 	volatile int ready = 0;
    252 
    253 	thr_create (network_main, (int *) &ready, "network");
    254 	while (!ready) ;
    255 	return 0;
    256 }
    257