OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / udpcli.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module to control udp client 
4
5  As the address permission check is time consuming procedure, 
6  checked addresses are cached in the memory of daemons.
7
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. 
15
16 Copyright (C) 2011 Opengate Project Team
17 Written by Yoshiaki Watanabe
18
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.
23
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.
28
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.
32
33 Email: watanaby@is.saga-u.ac.jp
34 **************************************************/
35
36 #include "opengatemmng.h"
37
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];
45   char* udpServer;
46
47   udpServer=GetFirstConfValue("UdpServer");
48   if(isNull(udpServer)){
49     err_msg("ERR at %s#%d: no udp server in conf",__FILE__,__LINE__);
50     return FALSE;
51   }
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__);
55       continue;
56     }
57     PutDataToUdpPort(udpServerAddr, udpServerPort, macAddress);
58     udpServer=GetNextConfValue();
59   }
60   return TRUE;
61 }
62
63 /**************************************/
64 /* put data to udp port of a server   */
65 /**************************************/
66 int putDataToUdpPort(char* udpServerAddr, char* udpServerPort, char* buff){
67
68   int sockfd;
69   struct addrinfo hints, *servinfo, *p;
70   int ret;
71   int numbytes;
72   
73   /* if no buffer return error */
74   if (buff==NULL||*buff=='\0') return FALSE;
75
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 */
80
81   if ((ret = getaddrinfo(udpServerAddr, udpServerPort, &hints, &servinfo))!= 0) {
82     err_msg("ERR at %s#%d: getaddrinfo: %s",__FILE__,__LINE__, 
83             gai_strerror(ret));
84     return FALSE;
85   }
86   
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__,
92               strerror(errno));
93       continue;
94     }
95     break;
96   }
97   if (p == NULL) {
98     err_msg("ERR at %s#%d: failed to bind socket",__FILE__,__LINE__);
99     return FALSE;
100   }
101   
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__,
106             strerror(errno));
107     err_msg("ERR at %s#%d: Check firewall/daemon on [%s] to get udp[%d] packet"
108             "from here", 
109             __FILE__,__LINE__,udpServerAddr,udpServerPort);
110     return FALSE;
111   }
112   
113   /* finalize */
114   freeaddrinfo(servinfo);
115   close(sockfd);
116   
117   return TRUE;
118 }
119
120 /**********************************
121 **********************************/
122 int PutDataToUdpPort(char* udpServerAddr, char* udpServerPort,char* buff){
123   int ret;
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);
127   return ret;
128 }
129
130
131 int PutMacAddressToServers(char* macAddress){
132   int ret;
133   if(debug>1) err_msg("DEBUG:=>putMacAddressToServers(%s)",macAddress);
134   ret = putMacAddressToServers(macAddress);
135   if(debug>1) err_msg("DEBUG:(%d)<=putMacAddressToServers( )",ret);
136   return ret;
137 }
138
139