Commit 36775501 authored by Armando Rivera's avatar Armando Rivera

New IP module

parent d9376cc1
/*
READ THIS FILE AND CHANGE THE SOURCE WHEREVER YOU SEE COMMENTS STARTING
WITH THE WORD *TODO*
WHEN YOU ARE FINISHED YOU CAN
FILE : interface.c
HEADER : interface.h
BAS : ip.bas
AUTHOR : *TODO*
DATE:
CONTENT:
This is the interface.c file for the ScriptBasic module ip
----------------------------------------------------------------------------
Remove the two characters // from following line(s) if this module is supposed to
be compiled under specific OS's. If there is a need for some library to successfully
compile the module under Windows specify the names of the libraries on the line
as it is listed for the linker application. This is usually something like
WINDOWS: libname1.lib libname2.lib ... libnameX.lib
LINUX/MACOS: -lm -ldl
If there are no libraries, but still the module is to be compiled under Windows
do remove the // characters so that the program setup.pl will know that the
module is meant for Windows.
//NTLIBS:
UXLIBS: -lresolv
//DWLIBS:
*/
/*
*TODO*
INCLUDE HERE THE SYSTEM HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#ifdef WIN32
/*
*TODO*
INCLUDE HERE THE WIN32 SPECIFIC HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#else
/*
*TODO*
INCLUDE HERE THE UNIX SPECIFIC HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#endif
/*
*TODO*
INCLUDE HERE THE LOCAL HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../basext.h"
#include "iptools.h"
/*
*TODO*
INSERT THE BASIC CODE THAT WILL GET INTO THE FILE ip.BAS
AFTER THE LINE 'TO_BAS:' AND BEFORE THE LINE END OF THE COMMENT
NOTE THAT SUB AND COMMAND DECLARATIONS ARE CREATED AUTOMATICALLY
FROM THE FUNCTION DEFINTIONS WHEN THE MODULE IS CONFIGURED BEFORE
COMPILATION
TO_BAS:
*/
/*
*TODO*
DECLARE HERE THE MODULE OBJECT TYPE. THIS STRUCTURE SHOULD HOLD THE
DATA AVAILABLE FOR EACH INTERPRETER THREAD. USE THIS STRUCTURE TO
STORE GLOBAL VALUES INSTEAD OF USING GLOBAL VARIABLES.
*/
typedef struct _ModuleObject {
char a; /* You may delete this. It is here to make the initial interface.c compilable. */
}ModuleObject,*pModuleObject;
/*
*TODO*
ALTER THE VERSION NEGOTIATION CODE IF YOU NEED
*/
besVERSION_NEGOTIATE
return (int)INTERFACE_VERSION;
besEND
/*
*TODO*
ALTER THE ERROR MESSAGE FUNCTION
*/
besSUB_ERRMSG
switch( iError ){
case 0x00080000: return "ERROR HAS HAPPENED";
}
return "Unknown ip module error.";
besEND
/*
*TODO*
ALTER THE MODULE INITIALIZATION CODE
*/
besSUB_START
pModuleObject p;
besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
if( besMODULEPOINTER == NULL )return 0;
/*
*TODO*
INSERT HERE ANY CODE THAT YOU NEED TO INITIALIZE THE MODULE FOR THE
ACTUAL INTERPRETER THREAD
*/
besEND
/*
*TODO*
ALTER THE MODULE FINISH CODE IF NEEDED
*/
besSUB_FINISH
pModuleObject p;
/*
YOU CERTAINLY NEED THIS POINTER TO FREE ALL RESOURCES THAT YOU ALLOCATED
YOU NEED NOT CALL besFREE TO FREE THE MEMORY ALLOCATED USING besALLOC
CLOSE ANY FILE THAT REMAINED OPEN, RELEASE DATABASE HANDLES AND ALL
OTHER RESOURCES INCLUDING MEMORY *NOT* ALLOCATED CALLING besALLOC
*/
p = (pModuleObject)besMODULEPOINTER;
if( p == NULL )return 0;
return 0;
besEND
/*
*TODO*
WRITE YOUR MODULE INTERFACE FUNCTIONS FOLLOWING THIS SKELETON
NOTE THAT THIS IS A SAMPLE FUNCTION, YOU CAN ALSO DELETE
LINES FROM IT IF NEEDED
*/
/**
=section Public
=H IP::Public
Returns the Public (WAN) IP Address
Requires NO Arguments
*/
besFUNCTION(Public)
pModuleObject p;
char *pubIP,*iface;
p = (pModuleObject)besMODULEPOINTER;
pubIP = publicIP();
besSET_RETURN_STRING(pubIP);
free(pubIP);
besEND
/*
=section Local
=H IP::Local
Returns the Local (LAN) IP Address
Requires name of interface
IP::Local("eth0")
*/
besFUNCTION(Local)
pModuleObject p;
char *locIP,*iface;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("z")
&iface
besARGEND
locIP = localIP(iface);
besSET_RETURN_STRING(locIP);
free(locIP);
besEND
/*
=section MacAddress
=H IP::MacAddress
Returns the interface MAC Address
Requires name of interface
IP::MacAddress("eth0")
*/
besFUNCTION(MacAddress)
pModuleObject p;
char *mAddr,*iface;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("z")
&iface
besARGEND
mAddr = getMac(iface);
besSET_RETURN_STRING(mAddr);
free(mAddr);
besEND
/*
/*
*TODO*
INSERT HERE THE NAME OF THE FUNCTION AND THE FUNCTION INTO THE
TABLE. THIS TABLE IS USED TO FIND THE FUNCTIONS WHEN THE MODULE
INTERFACE FILE IS COMPILED.
*/
SLFST IP_SLFST[] ={
{ "versmodu" , versmodu },
{ "bootmodu" , bootmodu },
{ "finimodu" , finimodu },
{ "emsgmodu" , emsgmodu },
{ "Public" , Public },
{ NULL , NULL }
};
/*
MIT License
Copyright (c) 2019 Armando I. Rivera <airr@outlook.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "iptools.h"
char *publicIP() {
struct __res_state res;
char host_ip[16],ip[16],*hostname="resolver1.mydns.com", *query = "myip.opendns.com";
int result,i,length;
struct hostent *he;
struct in_addr **addr_list, addr;
ns_msg handle;
ns_rr rr;
u_char answer[NS_PACKETSZ];
he=gethostbyname("resolver1.opendns.com");
addr_list = (struct in_addr **) he->h_addr_list;
strcpy(host_ip, inet_ntoa(*addr_list[0]));
res_ninit( &res);
inet_aton(host_ip, &addr);
res.nsaddr_list[0].sin_addr=addr;
res.nsaddr_list[0].sin_family=AF_INET;;
res.nsaddr_list[0].sin_port=htons(NS_DEFAULTPORT);;
res.nscount=1;
length=res_nquery( &res,query,ns_c_in,ns_t_a,answer,sizeof(answer));
ns_initparse(answer,length, &handle);
if(ns_msg_count(handle,ns_s_an)>0) {
if( ns_parserr( &handle,ns_s_an,0, &rr)==0 )
strcpy(ip,inet_ntoa(*(struct in_addr*)ns_rr_rdata(rr)));
}
return strdup(ip);
}
char* getMac(char* iface) {
int fd;
struct ifreq ifr;
unsigned char *mac;
char ret[16];
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
//display mac address
sprintf(ret,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return strdup(ret);
}
char* localIP(char* iface) {
int fd;
struct ifreq ifr;
char host[16];
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, iface, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
/* display result */
sprintf(host, "%s", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
return strdup(host);
}
/*
MIT License
Copyright (c) 2019 Armando I. Rivera <airr@outlook.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h> //printf
#include <string.h> //strncpy
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h> //ifreq
#include <unistd.h> //close
#include <sys/types.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <resolv.h>
char *publicIP();
char* localIP(char* iface);
char* getMac(char* iface);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment