1 /**************************************************
2 OpengateM - MAC address authentication system
3 module to control udp client
5 As the address permission check is time consuming procedure,
6 checked addresses are cached in the memory of daemons.
8 When the address registration is updated by the management program,
9 it is is reported to daemons via UDP, where the management program
10 plays as UDP client and the daemon as UDP server.
11 Addresses of UDP servers and trusted UDP clients are in conf file.
12 When Daemons receive the UDP, clear the cache of the reported
13 address and recheck the address permission.
14 If UDP is failed, the recheck is delayed to the cache timeout.
16 Copyright (C) 2011 Opengate Project Team
17 Written by Yoshiaki Watanabe
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License
21 as published by the Free Software Foundation; either version 2
22 of the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 Email: watanaby@is.saga-u.ac.jp
34 **************************************************/
36 #include "opengatemmng.h"
38 /***********************************************************/
39 /* send mac address to plural daemons via UDP at db update */
40 /* UDP server(=daemon) addresses are defined in conf file */
41 /***********************************************************/
42 int putMacAddressToServers(char* macAddress){
43 char udpServerAddr[ADDRMAXLN];
44 char udpServerPort[WORDMAXLN];
47 udpServer=GetFirstConfValue("UdpServer");
48 if(isNull(udpServer)){
49 err_msg("ERR at %s#%d: no udp server in conf",__FILE__,__LINE__);
52 while(!isNull(udpServer)){
53 if(sscanf(udpServer, "%s %s", udpServerAddr, udpServerPort)!=2){
54 err_msg("ERR at %s#%d: abnormal udp servers in conf",__FILE__,__LINE__);
57 PutDataToUdpPort(udpServerAddr, udpServerPort, macAddress);
58 udpServer=GetNextConfValue();
63 /**************************************/
64 /* put data to udp port of a server */
65 /**************************************/
66 int putDataToUdpPort(char* udpServerAddr, char* udpServerPort, char* buff){
69 struct addrinfo hints, *servinfo, *p;
73 /* if no buffer return error */
74 if (buff==NULL||*buff=='\0') return FALSE;
76 /* prepare address hints */
77 memset(&hints, 0, sizeof hints);
78 hints.ai_family = AF_UNSPEC; /* IPv4/IPv6 dual */
79 hints.ai_socktype = SOCK_DGRAM; /* UDP */
81 if ((ret = getaddrinfo(udpServerAddr, udpServerPort, &hints, &servinfo))!= 0) {
82 err_msg("ERR at %s#%d: getaddrinfo: %s",__FILE__,__LINE__,
87 /* loop through addresses */
88 for(p = servinfo; p != NULL; p = p->ai_next) {
89 if ((sockfd = socket(p->ai_family, p->ai_socktype,
90 p->ai_protocol)) == -1) {
91 err_msg("ERR at %s#%d: socket error: %s",__FILE__,__LINE__,
98 err_msg("ERR at %s#%d: failed to bind socket",__FILE__,__LINE__);
102 /* send data to server */
103 if ((numbytes = sendto(sockfd, buff, strlen(buff), 0,
104 p->ai_addr, p->ai_addrlen)) == -1) {
105 err_msg("ERR at %s#%d: sendto error: %s",__FILE__,__LINE__,
107 err_msg("ERR at %s#%d: Check firewall/daemon on [%s] to get udp[%d] packet"
109 __FILE__,__LINE__,udpServerAddr,udpServerPort);
114 freeaddrinfo(servinfo);
120 /**********************************
121 **********************************/
122 int PutDataToUdpPort(char* udpServerAddr, char* udpServerPort,char* buff){
124 if(debug>1) err_msg("DEBUG:=>putDataToUdpPort(%s)",udpServerAddr, udpServerPort,buff);
125 ret = putDataToUdpPort(udpServerAddr, udpServerPort,buff);
126 if(debug>1) err_msg("DEBUG:(%d)<=putDateToUdpPort( )",ret);
131 int PutMacAddressToServers(char* macAddress){
133 if(debug>1) err_msg("DEBUG:=>putMacAddressToServers(%s)",macAddress);
134 ret = putMacAddressToServers(macAddress);
135 if(debug>1) err_msg("DEBUG:(%d)<=putMacAddressToServers( )",ret);