OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / udpcli.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module to control udp client 
4
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.
13
14
15 Copyright (C) 2011 Opengate Project Team
16 Written by Yoshiaki Watanabe
17
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.
22
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.
27
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.
31
32 Email: watanaby@is.saga-u.ac.jp
33 **************************************************/
34
35 #include "opengatemmng.h"
36
37 /************************************
38 send mac address to daemon at db update 
39 ************************************/
40 int putMacAddressToServers(char* macAddress){
41   char udpServerAddr[ADDRMAXLN];
42   char udpServerPort[WORDMAXLN];
43   char* udpServer;
44
45   udpServer=GetFirstConfValue("UdpServer");
46   if(isNull(udpServer)){
47     err_msg("ERR at %s#%d: no udp server in conf",__FILE__,__LINE__);
48     return FALSE;
49   }
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__);
53       continue;
54     }
55     PutDataToUdpPort(udpServerAddr, udpServerPort, macAddress);
56     udpServer=GetNextConfValue();
57   }
58   return TRUE;
59 }
60
61 /*****************************************
62 put data to server udp port
63 *****************************************/
64 int putDataToUdpPort(char* udpServerAddr, char* udpServerPort, char* buff){
65
66   int sockfd;
67   struct addrinfo hints, *servinfo, *p;
68   int ret;
69   int numbytes;
70   
71   /* if no buffer return error */
72   if (buff==NULL||*buff=='\0') return FALSE;
73
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 */
78
79   if ((ret = getaddrinfo(udpServerAddr, udpServerPort, &hints, &servinfo))!= 0) {
80     err_msg("ERR at %s#%d: getaddrinfo: %s",__FILE__,__LINE__, 
81             gai_strerror(ret));
82     return FALSE;
83   }
84   
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__,
90               strerror(errno));
91       continue;
92     }
93     break;
94   }
95   if (p == NULL) {
96     err_msg("ERR at %s#%d: failed to bind socket",__FILE__,__LINE__);
97     return FALSE;
98   }
99   
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__,
104             strerror(errno));
105     err_msg("ERR at %s#%d: Check firewall/daemon on [%s] to get udp[%d] packet"
106             "from here", 
107             __FILE__,__LINE__,udpServerAddr,udpServerPort);
108     return FALSE;
109   }
110   
111   /* finalize */
112   freeaddrinfo(servinfo);
113   close(sockfd);
114   
115   return TRUE;
116 }
117
118 /**********************************
119 put out buff to udp addr:port
120 **********************************/
121 int PutDataToUdpPort(char* udpServerAddr, char* udpServerPort,char* buff){
122   int ret;
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);
126   return ret;
127 }
128
129
130 int PutMacAddressToServers(char* macAddress){
131   int ret;
132   if(debug>1) err_msg("DEBUG:=>putMacAddressToServers(%s)",macAddress);
133   ret = putMacAddressToServers(macAddress);
134   if(debug>1) err_msg("DEBUG:(%d)<=putMacAddressToServers( )",ret);
135   return ret;
136 }
137
138