1 /**************************************************
2 OpengateM - MAC address authentication system
3 module to control udp client
5 As ip address check by database is time consuming procedure,
6 the recently checked addresses are cached and skiped.
7 Implemented with HashTable and Queue.
8 HashTable has recentry checked dataStress and checked time.
9 If ip is included in table and time is new, ignore checking.
10 Queue has dataStresses odrered by checked time.
11 If an old item is found in table, elder items are removed from table.
12 The queue controls the remove sequence.
15 Copyright (C) 2011 Opengate Project Team
16 Written by Yoshiaki Watanabe
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 as published by the Free Software Foundation; either version 2
21 of the License, or (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 Email: watanaby@is.saga-u.ac.jp
33 **************************************************/
35 #include "opengatemmng.h"
37 /************************************
38 send mac address to daemon at db update
39 ************************************/
40 int putMacAddressToServers(char* macAddress){
41 char udpServerAddr[ADDRMAXLN];
42 char udpServerPort[WORDMAXLN];
45 udpServer=GetFirstConfValue("UdpServer");
46 if(isNull(udpServer)){
47 err_msg("ERR at %s#%d: no udp server in conf",__FILE__,__LINE__);
50 while(!isNull(udpServer)){
51 if(sscanf(udpServer, "%s %s", udpServerAddr, udpServerPort)!=2){
52 err_msg("ERR at %s#%d: abnormal udp servers in conf",__FILE__,__LINE__);
55 PutDataToUdpPort(udpServerAddr, udpServerPort, macAddress);
56 udpServer=GetNextConfValue();
61 /*****************************************
62 put data to server udp port
63 *****************************************/
64 int putDataToUdpPort(char* udpServerAddr, char* udpServerPort, char* buff){
67 struct addrinfo hints, *servinfo, *p;
71 /* if no buffer return error */
72 if (buff==NULL||*buff=='\0') return FALSE;
74 /* prepare address hints */
75 memset(&hints, 0, sizeof hints);
76 hints.ai_family = AF_UNSPEC; /* IPv4/IPv6 dual */
77 hints.ai_socktype = SOCK_DGRAM; /* UDP */
79 if ((ret = getaddrinfo(udpServerAddr, udpServerPort, &hints, &servinfo))!= 0) {
80 err_msg("ERR at %s#%d: getaddrinfo: %s",__FILE__,__LINE__,
85 /* loop through addresses */
86 for(p = servinfo; p != NULL; p = p->ai_next) {
87 if ((sockfd = socket(p->ai_family, p->ai_socktype,
88 p->ai_protocol)) == -1) {
89 err_msg("ERR at %s#%d: socket error: %s",__FILE__,__LINE__,
96 err_msg("ERR at %s#%d: failed to bind socket",__FILE__,__LINE__);
100 /* send data to server */
101 if ((numbytes = sendto(sockfd, buff, strlen(buff), 0,
102 p->ai_addr, p->ai_addrlen)) == -1) {
103 err_msg("ERR at %s#%d: sendto error: %s",__FILE__,__LINE__,
105 err_msg("ERR at %s#%d: Check firewall/daemon on [%s] to get udp[%d] packet"
107 __FILE__,__LINE__,udpServerAddr,udpServerPort);
112 freeaddrinfo(servinfo);
118 /**********************************
119 put out buff to udp addr:port
120 **********************************/
121 int PutDataToUdpPort(char* udpServerAddr, char* udpServerPort,char* buff){
123 if(debug>1) err_msg("DEBUG:=>putDataToUdpPort(%s)",udpServerAddr, udpServerPort,buff);
124 ret = putDataToUdpPort(udpServerAddr, udpServerPort,buff);
125 if(debug>1) err_msg("DEBUG:(%d)<=putDateToUdpPort( )",ret);
130 int PutMacAddressToServers(char* macAddress){
132 if(debug>1) err_msg("DEBUG:=>putMacAddressToServers(%s)",macAddress);
133 ret = putMacAddressToServers(macAddress);
134 if(debug>1) err_msg("DEBUG:(%d)<=putMacAddressToServers( )",ret);