/*
    Bind an IPX network to an interface.

    Copyright (C) 1994, 1995  Ales Dryak <e-mail: A.Dryak@sh.cvut.cz>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <linux/ipx.h>
#include "ipxutil.h"

void help()
{
	fprintf(stderr,
		"IPX network bind utility v0.9, (c) Ales Dryak, 1995\n"
		"Usage: ipxbind [-v] <network> <driver> dix|802.3|802.2|snap\n");
	exit(1);
}

int main(int argc,char *argv[])
{
	struct ipx_route_def rt;
	int sock;
	int verbose=0;
	
	if (argc<2) help();
	
	if (strcmp(argv[1],"-v")==0)
	{
		argc--;
		argv++;
		verbose=1;
	}
	
	if (argc!=4) help();
	
	if(sscanf(argv[1],"%lx",&rt.ipx_network)==0)
	{
		fprintf(stderr,"Unable to parse network number '%s'.\n",argv[2]);
		exit(1);
	}

	rt.ipx_network=htonl(rt.ipx_network);	
	rt.ipx_router_network=IPX_ROUTE_NO_ROUTER;
	strncpy(rt.ipx_device,argv[2],16);
	rt.ipx_flags=0;
	if(strcmp(argv[3],"dix")==0)
		rt.ipx_flags|=IPX_RT_BLUEBOOK;
	else 
	{
	if(strcmp(argv[3],"802.2")==0)
		rt.ipx_flags|=IPX_RT_8022;
	else 
	{
	if(strcmp(argv[3],"snap")==0)
		rt.ipx_flags|=IPX_RT_SNAP;
	else 
	{
	if(strcmp(argv[3],"802.3"))
	{
		fprintf(stderr,"Unknown ipx link layer '%s'.\n",argv[3]);
		exit(1);
	}
	}
	}
	}
	sock=socket(AF_IPX,SOCK_DGRAM,PF_IPX);
	if(sock==-1)
	{
		perror("socket");
		exit(1);
	}
	
	if(ioctl(sock,SIOCADDRT,(void *)&rt)==-1)
	{
		perror("add route");
		exit(1);
	}
	if (verbose)
	{
		printf("Network ");
		ipx_print_network(rt.ipx_network);
		printf(" added.\n");
	}
	close(sock);
	return 0;
}
