OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / getmac.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module to get mac address
4
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
24
25 #include        "opengatemmng.h"
26
27 /***************************************/
28 /* arp form is reformed to arp form    */ 
29 /* mac addr by arp 00:01:12:0b:..      */
30 /* mac addr by ndp 0:1:12:b:..         */
31 /* these are formated to 00:01:12:0b:..*/
32 /***************************************/
33 int reFormatMacAddr(char* macAddr)
34 {
35   int m1,m2,m3,m4,m5,m6;
36   if(sscanf(macAddr, "%x:%x:%x:%x:%x:%x", &m1,&m2,&m3,&m4,&m5,&m6)!=6) return FALSE;
37   snprintf(macAddr, ADDRMAXLN,"%02x:%02x:%02x:%02x:%02x:%02x", m1,m2,m3,m4,m5,m6);
38   return TRUE;
39 }
40
41 /************************************************************************/
42 /* get MAC address from ndp entry for clientAddr (nnnn:nnnn::nnnn:nnnn) */
43 /*  result is stored in queue                                           */
44 /************************************************************************/
45 int getMacAddrListFromNdp(char* interface){
46   FILE *fpipe;
47   char buf[BUFFMAXLN];
48   char* firstTokenP;
49   char* secondTokenP;
50   char* thirdTokenP;
51   char macAddr[ADDRMAXLN];
52   char* p;
53   char ifStr[WORDMAXLN];
54   char* ndpPath=NULL;
55
56   /* if interface is null, error return */
57   if(isNull(interface)){
58     err_msg("ERR at %s#%d: NIC device name is not set",__FILE__,__LINE__);
59     return -1;
60   }
61
62   /* exec NDP 'ndp -a' , output to pipe */
63   if(isNull(ndpPath=GetConfValue("NdpPath"))){
64     err_msg("ERR at %s#%d:ndp path is not set in conf",__FILE__,__LINE__);
65     return -1;
66   }
67   if((fpipe=Popenl(1, "r", ndpPath,"-a",(char *)0)) == NULL){ 
68     err_msg("ERR at %s#%d: exec ndp -na error",__FILE__,__LINE__);
69     return -1;
70   }
71
72   /*** get ndp response from pipe */
73   /* skip first title line */
74   if(fgets(buf, BUFFMAXLN, fpipe)==NULL){
75     err_msg("ERR at %s#%d: readin error",__FILE__,__LINE__);
76     Pclose(fpipe);
77     return -1;
78   }
79   
80   /* get MAC address from arp response             */
81   /* arp response takes following format           */
82   /* "[IPv6 Addr] [Mac] [InterfaceID] [Expire] [Status] [Flags] [Prbs]" */
83   while(fgets(buf, BUFFMAXLN, fpipe)!=NULL){
84
85     firstTokenP = strtok(buf," "); /* first token */
86     secondTokenP = strtok(NULL," "); /* second token */
87     thirdTokenP = strtok(NULL," "); /* third token */
88
89     /* skip if invalid address */
90     if(strstr(secondTokenP, ":")==NULL)continue;
91
92     /* if other interface, skip */
93     if(strstr(thirdTokenP, interface)==NULL) continue;
94
95     /* remove interface description(%fxp1) from the following form */
96     /* 'fe80::202:b3ff:fe0a:c30e%fxp1' */
97     snprintf(ifStr, WORDMAXLN, "%%%s", interface);
98     if((p=strstr(firstTokenP, ifStr))!=NULL) *p='\0';
99
100     /* Convert to ARP format */
101     strlcpy(macAddr, secondTokenP, ADDRMAXLN);
102     ReFormatMacAddr(macAddr);
103
104     /* Insert to queue */
105     /* initializing queue Initqueue() is needed previously */
106     Enqueue(macAddr, firstTokenP);
107   }
108
109   Pclose(fpipe);
110   
111   return 0;
112 }
113
114 /*******************************************************************/
115 /* get MAC address for clientAddr (nnn.nnn.nnn.nnn) by arp request */
116 /*  result is stored in queue                                      */
117 /*******************************************************************/
118 int getMacAddrListFromArp(char* interface){
119   char buf[BUFFMAXLN];
120   char *startp;
121   char *endp;
122   FILE *fpipe;
123   char* macAddr;
124   char* ipAddr;
125   char options[WORDMAXLN];
126   char* arpPath=NULL;
127
128   /* if null, error return */
129   if(isNull(interface)){
130     err_msg("ERR at %s#%d: NIC device name is not set",__FILE__,__LINE__);
131     return -1;
132   }
133
134   /* set arp options */
135   snprintf(options, WORDMAXLN, "-a -i %s", interface);
136
137   /* exec arp 'arp -a -i fxp1', output to pipe */
138   if(isNull(arpPath=GetConfValue("ArpPath"))){
139     err_msg("ERR at %s#%d: arp path is not set in conf",__FILE__,__LINE__);
140     return -1;
141   }
142   if( (fpipe=Popenl(1, "r", arpPath, options, (char *)0)) == NULL){ 
143     err_msg("ERR at %s#%d: exec arp error",__FILE__,__LINE__);
144     return -1;
145   }
146   
147   /* get arp response from pipe */  
148   while(fgets(buf, BUFFMAXLN, fpipe)!=NULL){
149
150     /* arp response takes following format            */
151     /* "? (133.49.22.1) at 8:0:20:a5:4:62 [ethernet]" */
152     /* get ip address from above string               */
153     if((startp=strstr(buf," ("))==NULL) continue;
154     startp=startp+2;
155     if((endp=strstr(startp, ") "))==NULL) continue;
156     
157     /* save as ip address and cut off string at endp */
158     *endp='\0';
159     ipAddr=startp;
160     
161     /* get MAC address from above string             */
162     if((startp=strstr(endp+1," at "))==NULL) continue;
163     startp=startp+4;
164     if((endp=strstr(startp, " "))==NULL) continue;
165     
166     /* save as mac address and cut off string at endp */
167     *endp='\0';
168     macAddr=startp;
169     
170     /* skip if invalid address */
171     if((strstr(macAddr, ":"))==NULL) continue;
172     
173     /* save to queue */
174     /* initializing queue Initqueue() is needed previously */
175     Enqueue(macAddr, ipAddr);
176   }
177     
178   /* close pipe */
179   Pclose(fpipe);
180     
181   return 0;
182 }
183
184
185 /****************************************/
186 /****************************************/
187 int GetMacAddrListFromArp(char* interface)
188 {
189   int ret;
190   if(debug>1) err_msg("DEBUG:=>getMacAddrListFromArp(%s)", interface);
191   ret=getMacAddrListFromArp(interface);
192   if(debug>1) err_msg("DEBUG:(%d)<=getMacAddrListFromArp( )",ret);
193
194   return ret;
195 }  
196
197 int GetMacAddrListFromNdp(char* interface){
198   int ret;
199   if(debug>1) err_msg("DEBUG:=>getMacAddrListFromNdp(%s)", interface);
200   ret=getMacAddrListFromNdp(interface);
201   if(debug>1) err_msg("DEBUG:(%d)<=getMacAddrListFromNdp( )",ret);
202
203   return ret;
204 }
205
206 int ReFormatMacAddr(char* macAddr){
207   int ret;
208   if(debug>1) err_msg("DEBUG:=>reFormatMacAddr(%s)", macAddr);
209   ret=reFormatMacAddr(macAddr);
210   if(debug>1) err_msg("DEBUG:(%d)<=reFormatMacAddr(%s)", ret, macAddr);
211   return ret;
212 }
213