//####ECOSGPLCOPYRIGHTBEGIN#### // ------------------------------------------- // This file is part of eCos, the Embedded Configurable Operating System. // Copyright (C) 2011 Jürgen Lambrecht J.Lambrecht@televic.com // // eCos 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 or (at your option) any later version. // // eCos 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 eCos; if not, write to the Free Software Foundation, Inc., // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. // // As a special exception, if other files instantiate templates or use macros // or inline functions from this file, or you compile this file and link it // with other works to produce a work based on this file, this file does not // by itself cause the resulting work to be covered by the GNU General Public // License. However the source code for this file must still be made available // in accordance with section (3) of the GNU General Public License. // // This exception does not invalidate any other reasons why a work based on // this file might be covered by the GNU General Public License. // // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc. // at http://sources.redhat.com/ecos/ecos-license/ // ------------------------------------------- //####ECOSGPLCOPYRIGHTEND#### /* ---------------------------------------------------------------------------- | Function name : AddMAC | Description : Add static MAC ENTRY - used in telnet and tty shell | Parameters : dst: IP address (e.g. "157.55.85.212") | gateway: MAC address (e.g. "00-aa-00-62-c6-09") index: interface number (e.g. "1" for eth0) | Return value : 0 on success, -1 on error | Notes : From http://sourceware.org/ml/ecos-discuss/2005-09/msg00023.html | Compile directives: | Author : Sébastien Couret, JL + --------------------------------------------------------------------------*/ int AddMAC(const char* dest, const char* gateway, const int index) { struct sockaddr_dl gway; // Adresse passerelle struct sockaddr_in dst; // Réseau/hote cible char ds[IPSTRING]; struct ecos_rtentry *rt=NULL; // Entrée dans la table de routage int s=0; // Descripteur de socket unsigned char cp[MACSTRING]; // Adresse MAC sous forme chaine memset(&gway,0,sizeof(struct sockaddr_dl)); gway.sdl_family=AF_LINK; // AF_UNSPEC ? gway.sdl_len=sizeof(struct sockaddr_dl); gway.sdl_index=index; gway.sdl_type=IFT_ETHER; gway.sdl_alen=ETHER_ADDR_LEN; memcpy(gway.sdl_data,gateway,ETHER_ADDR_LEN); memset(&dst,0,sizeof(struct sockaddr_in)); dst.sin_family=AF_INET; dst.sin_port=0; dst.sin_len=sizeof(struct sockaddr_in); dst.sin_addr.s_addr=inet_addr(dest); rt=(struct ecos_rtentry*)malloc(sizeof(struct ecos_rtentry)); if (!rt) { debug_printf("Erreur d'allocation d'une route :'%s'",strerror(errno)); return(-1); } memset(rt,0,sizeof(struct ecos_rtentry)); rt->rt_flags|=RTF_LLINFO; rt->rt_flags|=RTF_HOST; // Host entry memcpy(&(rt->rt_gateway), &gway, sizeof(struct sockaddr)); memcpy(&(rt->rt_dst), &dst, sizeof(struct sockaddr_in)); rt->rt_flags|=RTF_UP; // Route utilisable rt->rt_flags|=RTF_STATIC; //static arp entry, no refresh needed //RTF_WAS_CLONED; //rt->rt_use=0; rt->rt_dev=NULL; rt->rt_metric=1; // Reseau local strncpy(ds,inet_ntoa(dst.sin_addr),IPSTRING); ether_print(gway.sdl_data,cp,MACSTRING); //gateway debug_printf("Add MAC '%s' for '%s' on eth%d\n",cp,ds,index-1); s=socket(AF_INET,SOCK_DGRAM,0); if (s<0) { SHOW_RESULT(socket, s); free(rt); return(-1); } if (ioctl(s,SIOCADDRT,rt)<0) { #if JFFS2_TFTP LOG_RESULT(ioctl, -1); #else SHOW_RESULT(ioctl, -1); #endif free(rt); close(s); return (-1); } debug_printf("static ARP entry added\n"); free(rt); close(s); return(0); } /* ---------------------------------------------------------------------------- | Function name : AddRoute | Description : Adds a route (use 'route print' on windows to see them, | or 'ifconfig' in eCos | Parameters : interface (e.g. "eth0") | destination (e.g. "10.0.0.0") | gateway (e.g. "10.0.0.1") | netmask (e.g. "255.255.0.0") | Return value : TLV_TRUE is success | Notes : flags=RTF_UP | RTF_GATEWAY; to add other routes, an | extra function argument is needed. | http://sourceware.org/ml/ecos-discuss/2011-03/msg00185.html | Compile directives: | Author : Grant Edwards, JL + --------------------------------------------------------------------------*/ TLV_BOOL AddRoute(const char *interface, const char *destination, const char *gateway, const char *netmask) { struct sockaddr_in addr; struct ecos_rtentry route; TLV_BOOL ret; int s; s = socket(AF_INET, SOCK_DGRAM, 0); if (s) { memset(&route, 0, sizeof route); memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_len = sizeof addr; addr.sin_addr.s_addr = inet_addr(destination); memcpy(&route.rt_dst, &addr, sizeof route.rt_dst); addr.sin_addr.s_addr = inet_addr(gateway); memcpy(&route.rt_gateway, &addr, sizeof route.rt_gateway); addr.sin_addr.s_addr = inet_addr(netmask); memcpy(&route.rt_genmask, &addr, sizeof route.rt_genmask); route.rt_dev = (char*)interface; route.rt_flags = RTF_UP | RTF_GATEWAY; route.rt_metric = 0; if ((ioctl(s, SIOCADDRT, &route))) { debug_printf("Error adding route: %s\n", strerror(errno)); ret = TLV_FALSE; } else { debug_printf("Added route %s\n", destination); ret = TLV_TRUE; } close(s); } else { ret = TLV_FALSE; } return ret; } /* ---------------------------------------------------------------------------- | Function name : DeleteRoute | Description : Deletes a route (use 'route print' on windows to see them, | or 'ifconfig' in eCos | Parameters : interface (e.g. "eth0") | destination (e.g. "10.0.0.0") | gateway (e.g. "10.0.0.1") | netmask (e.g. "255.255.0.0") | Return value : TLV_TRUE is success | Notes : flags=RTF_UP; to delete other routes, an extra function | argument is needed | Compile directives: | Author : JL + --------------------------------------------------------------------------*/ TLV_BOOL DeleteRoute(const char *interface, const char *destination, const char *gateway, const char *netmask) { struct sockaddr_in addr; struct ecos_rtentry route; TLV_BOOL ret; int s; s = socket(AF_INET, SOCK_DGRAM, 0); if (s) { memset(&route, 0, sizeof route); memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_len = sizeof addr; addr.sin_addr.s_addr = inet_addr(destination); memcpy(&route.rt_dst, &addr, sizeof route.rt_dst); addr.sin_addr.s_addr = inet_addr(gateway); memcpy(&route.rt_gateway, &addr, sizeof route.rt_gateway); addr.sin_addr.s_addr = inet_addr(netmask); memcpy(&route.rt_genmask, &addr, sizeof route.rt_genmask); route.rt_dev = (char*)interface; route.rt_flags = RTF_UP; route.rt_metric = 0; if ((ioctl(s, SIOCDELRT, &route))) { debug_printf("Error deleting route: %s\n", strerror(errno)); ret = TLV_FALSE; } else { debug_printf("Deleted route %s\n", destination); ret = TLV_TRUE; } close(s); } else { ret = TLV_FALSE; } return ret; } /* ------------------------------------------------------------------------- + | END + ------------------------------------------------------------------------- */