Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
svn
ddnssync
Commits
4612e926
Commit
4612e926
authored
Oct 14, 2007
by
mr-ti
Browse files
No commit message
No commit message
parent
c9cc29eb
Changes
14
Hide whitespace changes
Inline
Side-by-side
ddnssbase/cregex.cpp
View file @
4612e926
...
...
@@ -51,7 +51,7 @@ CRegex::State CRegex::getState(){
bool
CRegex
::
exec
(
int
flags
){
int
match
=
regexec
(
&
regex
,
buffer
->
c_str
()
+
offset
,
nmatch
,
pmatch
,
flags
);
if
(
match
==
REG_NOMATCH
||
pmatch
[
0
].
rm_eo
==
pmatch
[
0
].
rm_so
){
if
(
match
==
REG_NOMATCH
||
/*
pmatch[0].rm_eo ==
*/
pmatch
[
0
].
rm_so
+
offset
>=
buffer
->
length
()
){
state
=
NOMATCH
;
return
false
;
}
else
if
(
match
==
0
){
...
...
@@ -71,20 +71,51 @@ unsigned int CRegex::end(){
return
(
state
==
MATCH
?
pmatch
[
0
].
rm_eo
:
0
);
}
string
CRegex
::
getFirstMatch
(){
string
CRegex
::
getFirstMatch
(
CRegex
*
subRegex
){
string
match
;
if
(
exec
()
&&
pmatch
[
0
].
rm_so
!=-
1
){
match
=
buffer
->
substr
(
pmatch
[
0
].
rm_so
,
pmatch
[
0
].
rm_eo
-
pmatch
[
0
].
rm_so
);
if
(
subRegex
){
subRegex
->
setBuffer
(
&
match
);
match
=
subRegex
->
getFirstMatch
();
}
}
return
match
;
}
list
<
string
>
CRegex
::
getMatchs
(){
list
<
string
>
CRegex
::
getMatchs
(
CRegex
*
subRegex
){
list
<
string
>
lst
;
string
match
;
if
(
state
!=
ERROR
){
while
(
exec
()){
if
(
pmatch
[
0
].
rm_so
!=-
1
){
match
=
buffer
->
substr
(
pmatch
[
0
].
rm_so
+
offset
,
pmatch
[
0
].
rm_eo
-
pmatch
[
0
].
rm_so
);
if
(
subRegex
){
subRegex
->
setBuffer
(
&
match
);
match
=
subRegex
->
getFirstMatch
();
}
lst
.
push_back
(
match
);
}
offset
+=
pmatch
[
0
].
rm_eo
+
1
;
}
offset
=
0
;
}
return
lst
;
}
vector
<
string
>
CRegex
::
getMatchsToVect
(
CRegex
*
subRegex
){
vector
<
string
>
lst
;
string
match
;
if
(
state
!=
ERROR
){
while
(
exec
()){
if
(
pmatch
[
0
].
rm_so
!=-
1
)
lst
.
push_back
(
buffer
->
substr
(
pmatch
[
0
].
rm_so
+
offset
,
pmatch
[
0
].
rm_eo
-
pmatch
[
0
].
rm_so
));
if
(
pmatch
[
0
].
rm_so
!=-
1
){
match
=
buffer
->
substr
(
pmatch
[
0
].
rm_so
+
offset
,
pmatch
[
0
].
rm_eo
-
pmatch
[
0
].
rm_so
);
if
(
subRegex
){
subRegex
->
setBuffer
(
&
match
);
match
=
subRegex
->
getFirstMatch
();
}
lst
.
push_back
(
match
);
}
offset
+=
pmatch
[
0
].
rm_eo
+
1
;
}
offset
=
0
;
...
...
ddnssbase/cregex.h
View file @
4612e926
...
...
@@ -13,6 +13,7 @@
#define CREGEX_H
#include <string>
#include <list>
#include <vector>
#include <regex.h>
#include <unicomctrl/cuniobj.h>
...
...
@@ -34,8 +35,9 @@ public:
unsigned
int
begin
();
unsigned
int
end
();
State
getState
();
list
<
string
>
getMatchs
();
string
getFirstMatch
();
list
<
string
>
getMatchs
(
CRegex
*
subRegex
=
NULL
);
vector
<
string
>
getMatchsToVect
(
CRegex
*
subRegex
=
NULL
);
string
getFirstMatch
(
CRegex
*
subRegex
=
NULL
);
int
getErrorNb
();
string
getErrorMsg
();
virtual
string
className
();
...
...
ddnssbase/ddnsshttpcl.cpp
View file @
4612e926
...
...
@@ -12,6 +12,8 @@
#include "ddnsshttpcl.h"
#include "cregex.h"
string
DdnssHttpCl
::
curPubIp
;
DdnssHttpCl
::
DdnssHttpCl
()
:
CUniObj
()
{
publicIpGetAdress
=
"http://checkip.dyndns.org/"
;
...
...
@@ -30,7 +32,7 @@ DdnssHttpCl::~DdnssHttpCl() {
curl_easy_cleanup
(
handle
);
//Détrut le handle cURL
}
string
DdnssHttpCl
::
getPub
lic
Ip
(){
string
DdnssHttpCl
::
getPubIp
(){
string
res
;
if
(
!
get
(
res
,
publicIpGetAdress
)){
return
""
;
...
...
@@ -40,6 +42,14 @@ string DdnssHttpCl::getPublicIp(){
return
reg
.
getFirstMatch
();
}
string
DdnssHttpCl
::
getCurPubIp
(){
return
curPubIp
;
}
void
DdnssHttpCl
::
setCurPubIp
(
const
string
&
ip
){
curPubIp
=
ip
;
}
void
DdnssHttpCl
::
setHttpUserPwd
(
const
string
&
login
,
const
string
&
passwd
){
string
tmp
=
login
+
":"
+
passwd
;
curl_easy_setopt
(
handle
,
CURLOPT_USERPWD
,
tmp
.
c_str
());
...
...
ddnssbase/ddnsshttpcl.h
View file @
4612e926
...
...
@@ -22,7 +22,9 @@ class DdnssHttpCl : public CUniObj{
public:
DdnssHttpCl
();
~
DdnssHttpCl
();
string
getPublicIp
();
string
getPubIp
();
static
string
getCurPubIp
();
static
void
setCurPubIp
(
const
string
&
ip
);
void
setHttpUserPwd
(
const
string
&
login
,
const
string
&
passwd
);
bool
get
(
string
&
result
,
const
string
&
url
);
bool
post
(
string
&
result
,
const
string
&
url
);
...
...
@@ -30,6 +32,7 @@ public:
private:
string
publicIpGetAdress
;
CURL
*
handle
;
static
string
curPubIp
;
static
size_t
writeInRepBuf
(
void
*
ptr
,
size_t
size
,
size_t
nmemb
,
FILE
*
out
);
};
...
...
ddnssplugins/dyndns/dyndns.cpp
View file @
4612e926
#include "../../ddnssbase/ddnsshttpcl.h"
#include "../../ddnssbase/cregex.h"
#include <unicomctrl/unilib.h>
extern
"C"
{
void
exec
(
const
string
&
request
){
CRegex
regex
(
"('([^']+)')|(([^[:space:]]+))"
);
CRegex
regex
(
"('[^']+')|([^[:space:]]+)"
);
CRegex
subRegex
(
"([^']+)"
);
regex
.
setBuffer
(
&
request
);
list
<
string
>
elts
=
regex
.
getMatchs
();
for
(
list
<
string
>::
iterator
it
=
elts
.
begin
();
it
!=
elts
.
end
();
it
++
){
CUniObj
::
printd
(
""
,
*
it
+
"
\n
"
);
if
(
equ
(
request
,
"HELP"
))
throw
string
(
"dyndns <domaine name> <IP> <login> <password>"
);
vector
<
string
>
elts
=
regex
.
getMatchsToVect
(
&
subRegex
);
if
(
elts
.
size
()
!=
4
){
string
err
=
"Wrong dyndns plugin use !
\n
Use : dyndns <domaine name> <IP> <login> <password>"
;
CUniObj
::
printc
(
"DynDNS plugin"
,
err
+
"
\n
"
);
throw
string
(
err
);
}
CUniObj
::
printd
(
"DynDNS plugin"
,
"Request >>
\"
"
+
request
+
"
\"\n
"
);
DdnssHttpCl
httpcl
;
string
newIp
=
(
elts
.
at
(
1
)
==
"<IP>"
?
httpcl
.
getCurPubIp
()
:
elts
.
at
(
1
));
httpcl
.
setHttpUserPwd
(
elts
.
at
(
2
),
elts
.
at
(
3
));
string
res
;
httpcl
.
post
(
res
,
"https://members.dyndns.org/nic/update?system=dyndns&hostname="
+
elts
.
at
(
0
)
+
"&myip="
+
newIp
);
if
(
res
==
"good "
+
newIp
){
}
else
if
(
res
==
"nochg "
+
newIp
){
string
err
=
"Warning >> Duplicate updates for the same host/ip, you should verify client settings..."
;
CUniObj
::
prints
(
"DynDNS plugin"
,
err
+
"
\n
"
);
throw
string
(
err
);
}
else
{
string
err
=
"FATAL ERROR >> See logs for more details, you must verify client settings..."
;
CUniObj
::
prints
(
"DynDNS plugin"
,
err
+
"
\n
Request result :
\n
"
+
res
+
"
\n
"
);
throw
string
(
err
);
}
}
}
ddnssplugins/zoneedit/zoneedit.cpp
View file @
4612e926
#include "../../ddnssbase/ddnsshttpcl.h"
#include "../../ddnssbase/cregex.h"
#include <unicomctrl/unilib.h>
extern
"C"
{
void
exec
(
const
string
&
request
){
CRegex
regex
(
"('[^']+')|([^[:space:]]+)"
);
CRegex
subRegex
(
"([^']+)"
);
regex
.
setBuffer
(
&
request
);
if
(
equ
(
request
,
"HELP"
))
throw
string
(
"zoneedit <domaine name> <IP> <login> <password>"
);
vector
<
string
>
elts
=
regex
.
getMatchsToVect
(
&
subRegex
);
if
(
elts
.
size
()
!=
4
){
string
err
=
"Wrong zoneedit plugin use !
\n
Use : zoneedit <domaine name> <IP> <login> <password>"
;
CUniObj
::
printc
(
"ZoneEdit plugin"
,
err
+
"
\n
"
);
throw
string
(
err
);
}
CUniObj
::
printd
(
"ZoneEdit plugin"
,
"Request >>
\"
"
+
request
+
"
\"\n
"
);
DdnssHttpCl
httpcl
;
string
newIp
=
(
elts
.
at
(
1
)
==
"<IP>"
?
httpcl
.
getCurPubIp
()
:
elts
.
at
(
1
));
httpcl
.
setHttpUserPwd
(
elts
.
at
(
2
),
elts
.
at
(
3
));
string
res
;
httpcl
.
post
(
res
,
"http://dynamic.zoneedit.com/auth/dynamic.html?zones="
+
elts
.
at
(
0
));
if
(
res
==
"<SUCCESS CODE=
\"
200
\"
TEXT=
\"
Update succeeded.
\"
ZONE=
\"
"
+
elts
.
at
(
0
)
+
"
\"
IP=
\"
"
+
newIp
+
"
\"
>"
){
}
else
if
(
res
==
"<ERROR CODE=
\"
707
\"
TEXT=
\"
Duplicate updates for the same host/ip, adjust client settings
\"
ZONE=
\"
"
+
elts
.
at
(
0
)
+
"
\"
>"
){
string
err
=
"Warning >> Duplicate updates for the same host/ip, you should verify client settings..."
;
CUniObj
::
prints
(
"DynDNS plugin"
,
err
+
"
\n
"
);
throw
string
(
err
);
}
else
{
string
err
=
"FATAL ERROR >> See logs for more details, you must verify client settings..."
;
CUniObj
::
prints
(
"DynDNS plugin"
,
err
+
"
\n
Request result :
\n
"
+
res
+
"
\n
"
);
throw
string
(
err
);
}
}
}
ddnssync/CMakeLists.txt
View file @
4612e926
...
...
@@ -38,3 +38,6 @@ TARGET_LINK_LIBRARIES(ddnssync ${ddnssync_LIBS})
# ### INSTALL ###
INSTALL
(
TARGETS ddnssync DESTINATION bin PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
)
ADD_EXECUTABLE
(
test test.cpp
)
TARGET_LINK_LIBRARIES
(
test ddnssbase
)
ddnssync/ddnsspluginlist.cpp
View file @
4612e926
...
...
@@ -42,6 +42,7 @@ DDnsSPlList::DDnsSPlList(CUniConf &config):CUniObj() {
plugin
->
handle
=
handle
;
plugin
->
exec
=
exec
;
plugList
[
*
it
]
=
plugin
;
printd
(
"DDnsSPlList() >> plugin
\"
"
+*
it
+
"
\"
loaded.
\n
"
);
}
}
}
...
...
@@ -57,7 +58,7 @@ DDnsSPlList::~DDnsSPlList() {
for
(
map
<
string
,
Plugin
*>::
iterator
it
=
plugList
.
begin
();
it
!=
plugList
.
end
();
it
++
){
dlclose
(
it
->
second
->
handle
);
delete
it
->
second
;
printd
(
"~() >> plugin
\"
"
+
it
->
first
+
"
\"
unloaded.
\n
"
);
printd
(
"~
DDnsSPlList
() >> plugin
\"
"
+
it
->
first
+
"
\"
unloaded.
\n
"
);
}
}
...
...
@@ -77,6 +78,18 @@ void DDnsSPlList::exec(const string &request){
}
}
string
DDnsSPlList
::
getHelps
(){
string
uses
;
for
(
map
<
string
,
Plugin
*>::
iterator
it
=
plugList
.
begin
();
it
!=
plugList
.
end
();
it
++
){
try
{
it
->
second
->
exec
(
"help"
);
}
catch
(
string
err
){
uses
+=
" * "
+
err
+
"
\n
"
;
}
}
return
uses
;
}
/**
* @brief The name of the class.
* @return "DDnsSPlList"
...
...
ddnssync/ddnsspluginlist.h
View file @
4612e926
...
...
@@ -36,6 +36,7 @@ public:
DDnsSPlList
(
CUniConf
&
config
);
~
DDnsSPlList
();
void
exec
(
const
string
&
request
);
string
getHelps
();
virtual
string
className
();
static
DDnsSPlList
*
pList
;
private:
...
...
ddnssync/ddnssreqanalyzer.cpp
View file @
4612e926
...
...
@@ -45,7 +45,7 @@ void DDnsSReqAnalyzer::exec(const string& request){
returnMessage
(
"Command list :
\n
"
" * VERSION; Get the version.
\n
"
" * REVISION; Get the revision (SVN).
\n
"
" * BUILD; Get the build date.
\n
"
" * BUILD; Get the build date.
\n
Plugins :
\n
"
+
DDnsSPlList
::
pList
->
getHelps
()
+
" NB:
\n
{choice|other choice} => choice or other choice.
\n
[Option] => this is an option.
\n
"
);
}
else
{
returnError
(
"HELP request error."
);
...
...
@@ -65,6 +65,7 @@ void DDnsSReqAnalyzer::exec(const string& request){
}
else
{
try
{
DDnsSPlList
::
pList
->
exec
(
request
);
returnMessage
(
"Success."
);
}
catch
(
string
err
){
returnError
(
err
);
}
...
...
ddnssync/ddnssslist.cpp
View file @
4612e926
...
...
@@ -10,14 +10,19 @@
//
//
#include "ddnssslist.h"
#include "ddnsspluginlist.h"
#include "../ddnssbase/ddnsshttpcl.h"
#include <queue>
DDnsSSList
::
DDnsSSList
(
CUniConf
&
cf
)
:
CUni
Obj
()
:
CUni
Thread
()
{
StringList
cmdList
=
cf
.
getListValue
(
"dn_list"
);
for
(
StringList
::
iterator
it
=
cmdList
.
begin
();
it
!=
cmdList
.
begin
();
it
++
){
dnList
.
push_back
(
new
DName
(
cf
,
*
it
));
}
time
=
cf
.
getValueToInt
(
"time"
);
doStop
=
false
;
}
...
...
@@ -29,6 +34,31 @@ DDnsSSList::~DDnsSSList()
dnList
.
clear
();
}
int
DDnsSSList
::
run
(){
DdnssHttpCl
httpcl
;
string
ip
;
do
{
ip
=
httpcl
.
getPubIp
();
if
(
ip
!=
httpcl
.
getCurPubIp
()){
DdnssHttpCl
::
setCurPubIp
(
ip
);
for
(
list
<
DName
*>::
iterator
it
=
dnList
.
begin
();
it
!=
dnList
.
begin
();
it
++
){
(
*
it
)
->
doSync
();
}
}
for
(
int
i
=
0
;
i
<
time
;
i
++
){
if
(
doStop
)
return
0
;
}
}
while
(
true
);
return
0
;
}
void
DDnsSSList
::
onExit
(){
doStop
=
true
;
}
/**
* @brief The ame of the class.
* @return "DDnsSSList"
...
...
@@ -43,6 +73,7 @@ DDnsSSList::DName::DName(CUniConf &cf, const string &dname)
{
this
->
dname
=
dname
;
cmdList
=
cf
.
getListValue
(
dname
+
"_req"
);
sync
=
false
;
}
...
...
@@ -50,14 +81,56 @@ DDnsSSList::DName::~DName()
{
}
void
DDnsSSList
::
DName
::
doSync
(){
condSync
.
lock
();
sync
=
true
;
condSync
.
signal
();
condSync
.
unlock
();
}
int
DDnsSSList
::
DName
::
run
(){
queue
<
string
>
q1
,
q2
,
*
orig
=&
q1
,
*
dest
=&
q2
,
*
tmp
;
do
{
if
(
!
condSync
.
lock
())
return
0
;
while
(
!
sync
)
if
(
!
condSync
.
wait
())
//Attente
return
0
;
sync
=
false
;
condSync
.
unlock
();
// Mise en file de la liste des commande
for
(
StringList
::
iterator
it
=
cmdList
.
begin
();
it
!=
cmdList
.
end
();
it
++
){
orig
->
push
(
*
it
);
}
// Traitement de la file
while
(
!
orig
->
empty
()
&&
!
dest
->
empty
()){
try
{
DDnsSPlList
::
pList
->
exec
(
orig
->
front
());
}
catch
(
string
err
){
// Ajoute à la file des commandes à réexécuter
dest
->
push
(
orig
->
front
());
}
orig
->
pop
();
if
(
orig
->
empty
()
&&
!
dest
->
empty
()){
// Inverse les files et attend 3 minutes avant de réexécuter les commande qui ont échoué
tmp
=
orig
;
orig
=
dest
;
dest
=
tmp
;
for
(
unsigned
int
i
=
0
;
i
<
300
;
i
++
){
if
(
!
condSync
.
isEnabled
())
return
0
;
}
}
if
(
!
condSync
.
isEnabled
())
return
0
;
}
}
while
(
true
);
return
0
;
}
void
DDnsSSList
::
DName
::
onExit
(){
condSync
.
enable
(
false
);
}
/**
...
...
ddnssync/ddnssslist.h
View file @
4612e926
...
...
@@ -22,13 +22,14 @@ using namespace std;
/**
@author Emeric VERSCHUUR <contact@mr-ti.com>
*/
class
DDnsSSList
:
public
CUni
Obj
class
DDnsSSList
:
public
CUni
Thread
{
public:
class
DName
:
public
CUniThread
{
public:
DName
(
CUniConf
&
cf
,
const
string
&
dname
);
~
DName
();
void
doSync
();
virtual
string
className
();
protected:
virtual
int
run
();
...
...
@@ -36,12 +37,19 @@ public:
private:
string
dname
;
StringList
cmdList
;
bool
sync
;
CUniCondition
condSync
;
};
DDnsSSList
(
CUniConf
&
cf
);
~
DDnsSSList
();
virtual
string
className
();
protected:
virtual
int
run
();
virtual
void
onExit
();
private:
list
<
DName
*>
dnList
;
int
time
;
bool
doStop
;
};
#endif
ddnssync/main.cpp
View file @
4612e926
...
...
@@ -2,6 +2,7 @@
#include <unicomctrl/cuniconfreg.h>
#include <unicomctrl/cunicomcl.h>
#include <unicomctrl/cunithreadmanager.h>
#include "../ddnssbase/ddnsshttpcl.h"
#include "ddnssreqanalyzer.h"
#include "ddnsspluginlist.h"
#include <string>
...
...
@@ -21,7 +22,7 @@ public:
switch
(
type
){
case
CUniData
::
REQUEST
:
analyzer
.
lock
();
analyzer
.
init
(
this
,
"
URA
"
);
analyzer
.
init
(
this
,
"
DDnsSync
"
);
analyzer
.
exec
(
getMessage
());
analyzer
.
unlock
();
break
;
...
...
@@ -60,6 +61,7 @@ int main(int argc, char **argv) {
"You must correct this error in the configuration file.
\n
"
);
DDnsSPlList
::
pList
=
new
DDnsSPlList
(
config
);
DdnssHttpCl
::
setCurPubIp
(
config
.
getValue
(
"public_ip"
));
string
server
=
config
.
getValue
(
"server"
);
string
user
=
config
.
getValue
(
"user"
);
...
...
@@ -87,6 +89,7 @@ int main(int argc, char **argv) {
thList
.
waitJoin
();
thList
.
killall
();
config
.
setValue
(
"public_ip"
,
DdnssHttpCl
::
getCurPubIp
());
delete
DDnsSPlList
::
pList
;
return
0
;
...
...
ddnssync/test.cpp
0 → 100644
View file @
4612e926
#include "../ddnssbase/cregex.h"
using
namespace
unicomctrl
;
using
namespace
std
;
int
main
(
int
argc
,
char
**
argv
)
{
string
request
(
argv
[
1
]);
CRegex
regex
(
"('[^']+')|([^[:space:]]+)"
);
regex
.
setBuffer
(
&
request
);
list
<
string
>
elts
=
regex
.
getMatchs
();
CUniObj
::
prints
(
""
,
request
+
"
\n
"
);
for
(
list
<
string
>::
iterator
it
=
elts
.
begin
();
it
!=
elts
.
end
();
it
++
){
CUniObj
::
prints
(
""
,
*
it
+
"
\n
"
);
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment