OSDN Git Service

added missing files needed from v.1.5.27
[opengate/opengate.git] / opengate / opengatesrv / udp-client.c
diff --git a/opengate/opengatesrv/udp-client.c b/opengate/opengatesrv/udp-client.c
new file mode 100644 (file)
index 0000000..aa700ff
--- /dev/null
@@ -0,0 +1,127 @@
+/**************************************************
+opengate Mac addr auth program
+
+ module to control udp client 
+
+  As ip address check by database is time consuming procedure,
+  the recently checked addresses are cached and skiped.
+  Implemented with HashTable and Queue.
+  HashTable has recentry checked dataStress and checked time.
+  If ip is included in table and time is new, ignore checking.
+  Queue has dataStresses odrered by checked time.
+  If an old item is found in table, elder items are removed from table.
+  The queue controls the remove sequence.
+
+
+Copyright (C) 2011 Opengate Project Team
+Written by Yoshiaki Watanabe
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+Email: watanaby@is.saga-u.ac.jp
+**************************************************/
+
+#include "opengatesrv.h"
+
+/************************************
+send mac address to daemon at db update 
+************************************/
+int putMacAddressToOpengateMd(char* macAddress){
+  char* udpPort;
+
+  udpPort=GetConfValue("OpengateMdPort");
+  if(!isNull(udpPort)){
+    PutDataToUdpPort("127.0.0.1", udpPort, macAddress);
+  }
+
+  return TRUE;
+}
+
+/*****************************************
+put data to server udp port
+*****************************************/
+int putDataToUdpPort(char* udpServerAddr, char* udpServerPort, char* buff){
+
+  int sockfd;
+  struct addrinfo hints, *servinfo, *p;
+  int ret;
+  int numbytes;
+  
+  /* if no buffer return error */
+  if (buff==NULL||*buff=='\0') return FALSE;
+
+  /* prepare address hints */ 
+  memset(&hints, 0, sizeof hints);
+  hints.ai_family = AF_UNSPEC; /* IPv4/IPv6 dual */
+  hints.ai_socktype = SOCK_DGRAM; /* UDP */
+
+  if ((ret = getaddrinfo(udpServerAddr, udpServerPort, &hints, &servinfo))!= 0) {
+    err_msg("ERR at %s#%d: getaddrinfo: %s",__FILE__,__LINE__, 
+           gai_strerror(ret));
+    return FALSE;
+  }
+  
+  /* loop through addresses */
+  for(p = servinfo; p != NULL; p = p->ai_next) {
+    if ((sockfd = socket(p->ai_family, p->ai_socktype,
+                        p->ai_protocol)) == -1) {
+      err_msg("ERR at %s#%d: socket error: %s",__FILE__,__LINE__,
+             strerror(errno));
+      continue;
+    }
+    break;
+  }
+  if (p == NULL) {
+    err_msg("ERR at %s#%d: failed to bind socket",__FILE__,__LINE__);
+    return FALSE;
+  }
+  
+  /* send data to server */
+  if ((numbytes = sendto(sockfd, buff, strlen(buff), 0,
+                        p->ai_addr, p->ai_addrlen)) == -1) {
+    err_msg("ERR at %s#%d: sendto error: %s",__FILE__,__LINE__,
+           strerror(errno));
+    err_msg("ERR at %s#%d: Check firewall/daemon on [%s] to get udp[%d] packet"
+           "from here", 
+           __FILE__,__LINE__,udpServerAddr,udpServerPort);
+    return FALSE;
+  }
+  
+  /* finalize */
+  freeaddrinfo(servinfo);
+  close(sockfd);
+  
+  return TRUE;
+}
+
+/**********************************
+put out buff to udp addr:port
+**********************************/
+int PutDataToUdpPort(char* udpServerAddr, char* udpServerPort,char* buff){
+  int ret;
+  if(debug>1) err_msg("DEBUG:=>putDataToUdpPort(%s)",udpServerAddr, udpServerPort,buff);
+  ret = putDataToUdpPort(udpServerAddr, udpServerPort,buff);
+  if(debug>1) err_msg("DEBUG:(%d)<=putDateToUdpPort( )",ret);
+  return ret;
+}
+
+
+int PutMacAddressToOpengateMd(char* macAddress){
+  int ret;
+  if(debug>1) err_msg("DEBUG:=>putMacAddressToOpengateMd(%s)",macAddress);
+  ret = putMacAddressToOpengateMd(macAddress);
+  if(debug>1) err_msg("DEBUG:(%d)<=putMacAddressToOpengateMd( )",ret);
+  return ret;
+}