[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit

Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.am b/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.am
new file mode 100755
index 0000000..fddf27a
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.am
@@ -0,0 +1,12 @@
+lib_LIBRARIES = libcwmp.a
+libcwmp_a_SOURCES = buffer.c  cfg.c  cwmp.c  http.c ssl.c  ini.c  log.c  md5.c  memory.c   session.c  util.c event.c queue.c model.c  callback.c  periodic.c
+
+
+ 
+AM_CPPFLAGS = -DUSE_CWMP_MEMORY_POOL -I./  -I$(top_srcdir)/libcwmp/src/inc  -I$(top_srcdir)/libpool/include  -I$(top_srcdir)/libxmlet/include   -I$(top_srcdir)/libcwmp/include  -I$(top_srcdir)/../../include 
+
+
+
+
+
+
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.in b/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.in
new file mode 100755
index 0000000..c083582
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/Makefile.in
@@ -0,0 +1,544 @@
+# Makefile.in generated by automake 1.11.1 from Makefile.am.
+# @configure_input@
+
+# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+# 2003, 2004, 2005, 2006, 2007, 2008, 2009  Free Software Foundation,
+# Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+
+VPATH = @srcdir@
+pkgdatadir = $(datadir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkglibexecdir = $(libexecdir)/@PACKAGE@
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = $(program_transform_name)
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+build_triplet = @build@
+host_triplet = @host@
+subdir = libcwmp/src
+DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
+ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
+am__aclocal_m4_deps = $(top_srcdir)/configure.in
+am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
+	$(ACLOCAL_M4)
+mkinstalldirs = $(SHELL) $(top_srcdir)/build/mkinstalldirs
+CONFIG_HEADER = $(top_builddir)/config.h
+CONFIG_CLEAN_FILES =
+CONFIG_CLEAN_VPATH_FILES =
+am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
+am__vpath_adj = case $$p in \
+    $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
+    *) f=$$p;; \
+  esac;
+am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`;
+am__install_max = 40
+am__nobase_strip_setup = \
+  srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'`
+am__nobase_strip = \
+  for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||"
+am__nobase_list = $(am__nobase_strip_setup); \
+  for p in $$list; do echo "$$p $$p"; done | \
+  sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \
+  $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \
+    if (++n[$$2] == $(am__install_max)) \
+      { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \
+    END { for (dir in files) print dir, files[dir] }'
+am__base_list = \
+  sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \
+  sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g'
+am__installdirs = "$(DESTDIR)$(libdir)"
+LIBRARIES = $(lib_LIBRARIES)
+ARFLAGS = cru
+libcwmp_a_AR = $(AR) $(ARFLAGS)
+libcwmp_a_LIBADD =
+am_libcwmp_a_OBJECTS = buffer.$(OBJEXT) cfg.$(OBJEXT) cwmp.$(OBJEXT) \
+	http.$(OBJEXT) ssl.$(OBJEXT) ini.$(OBJEXT) log.$(OBJEXT) \
+	md5.$(OBJEXT) memory.$(OBJEXT) session.$(OBJEXT) \
+	util.$(OBJEXT) event.$(OBJEXT) queue.$(OBJEXT) model.$(OBJEXT) \
+	callback.$(OBJEXT) periodic.$(OBJEXT)
+libcwmp_a_OBJECTS = $(am_libcwmp_a_OBJECTS)
+DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
+depcomp = $(SHELL) $(top_srcdir)/build/depcomp
+am__depfiles_maybe = depfiles
+am__mv = mv -f
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+	$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+LTCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
+	--mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
+	$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+CCLD = $(CC)
+LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
+	--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
+	$(LDFLAGS) -o $@
+SOURCES = $(libcwmp_a_SOURCES)
+DIST_SOURCES = $(libcwmp_a_SOURCES)
+ETAGS = etags
+CTAGS = ctags
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+ACLOCAL = @ACLOCAL@
+ALLOCA = @ALLOCA@
+AMTAR = @AMTAR@
+AR = @AR@
+AUTOCONF = @AUTOCONF@
+AUTOHEADER = @AUTOHEADER@
+AUTOMAKE = @AUTOMAKE@
+AWK = @AWK@
+BUILD_CPU = @BUILD_CPU@
+BUILD_OS = @BUILD_OS@
+BUILD_VENDOR = @BUILD_VENDOR@
+CC = @CC@
+CCDEPMODE = @CCDEPMODE@
+CFLAGS = @CFLAGS@
+CPP = @CPP@
+CPPFLAGS = @CPPFLAGS@
+CXX = @CXX@
+CXXCPP = @CXXCPP@
+CXXDEPMODE = @CXXDEPMODE@
+CXXFLAGS = @CXXFLAGS@
+CYGPATH_W = @CYGPATH_W@
+DEFS = @DEFS@
+DEPDIR = @DEPDIR@
+DSYMUTIL = @DSYMUTIL@
+ECHO = @ECHO@
+ECHO_C = @ECHO_C@
+ECHO_N = @ECHO_N@
+ECHO_T = @ECHO_T@
+EGREP = @EGREP@
+EXEEXT = @EXEEXT@
+F77 = @F77@
+FFLAGS = @FFLAGS@
+GREP = @GREP@
+HOST_CPU = @HOST_CPU@
+HOST_OS = @HOST_OS@
+HOST_VENDOR = @HOST_VENDOR@
+INSTALL = @INSTALL@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+LDFLAGS = @LDFLAGS@
+LIBOBJS = @LIBOBJS@
+LIBS = @LIBS@
+LIBTOOL = @LIBTOOL@
+LN_S = @LN_S@
+LTLIBOBJS = @LTLIBOBJS@
+MAKEINFO = @MAKEINFO@
+MKDIR_P = @MKDIR_P@
+NMEDIT = @NMEDIT@
+OBJEXT = @OBJEXT@
+OPENSSL_DIR = @OPENSSL_DIR@
+PACKAGE = @PACKAGE@
+PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
+PACKAGE_NAME = @PACKAGE_NAME@
+PACKAGE_STRING = @PACKAGE_STRING@
+PACKAGE_TARNAME = @PACKAGE_TARNAME@
+PACKAGE_URL = @PACKAGE_URL@
+PACKAGE_VERSION = @PACKAGE_VERSION@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+RANLIB = @RANLIB@
+SED = @SED@
+SET_MAKE = @SET_MAKE@
+SHELL = @SHELL@
+STRIP = @STRIP@
+VERSION = @VERSION@
+abs_builddir = @abs_builddir@
+abs_srcdir = @abs_srcdir@
+abs_top_builddir = @abs_top_builddir@
+abs_top_srcdir = @abs_top_srcdir@
+ac_ct_CC = @ac_ct_CC@
+ac_ct_CXX = @ac_ct_CXX@
+ac_ct_F77 = @ac_ct_F77@
+am__include = @am__include@
+am__leading_dot = @am__leading_dot@
+am__quote = @am__quote@
+am__tar = @am__tar@
+am__untar = @am__untar@
+bindir = @bindir@
+build = @build@
+build_alias = @build_alias@
+build_cpu = @build_cpu@
+build_os = @build_os@
+build_vendor = @build_vendor@
+builddir = @builddir@
+datadir = @datadir@
+datarootdir = @datarootdir@
+docdir = @docdir@
+dvidir = @dvidir@
+exec_prefix = @exec_prefix@
+host = @host@
+host_alias = @host_alias@
+host_cpu = @host_cpu@
+host_os = @host_os@
+host_vendor = @host_vendor@
+htmldir = @htmldir@
+includedir = @includedir@
+infodir = @infodir@
+install_sh = @install_sh@
+libdir = @libdir@
+libexecdir = @libexecdir@
+localedir = @localedir@
+localstatedir = @localstatedir@
+mandir = @mandir@
+mkdir_p = @mkdir_p@
+oldincludedir = @oldincludedir@
+pdfdir = @pdfdir@
+prefix = @prefix@
+program_transform_name = @program_transform_name@
+psdir = @psdir@
+sbindir = @sbindir@
+sharedstatedir = @sharedstatedir@
+srcdir = @srcdir@
+sysconfdir = @sysconfdir@
+target_alias = @target_alias@
+top_build_prefix = @top_build_prefix@
+top_builddir = @top_builddir@
+top_srcdir = @top_srcdir@
+lib_LIBRARIES = libcwmp.a
+libcwmp_a_SOURCES = buffer.c  cfg.c  cwmp.c  http.c ssl.c  ini.c  log.c  md5.c  memory.c   session.c  util.c event.c queue.c model.c  callback.c periodic.c
+AM_CPPFLAGS = -DUSE_CWMP_MEMORY_POOL -I./  -I$(top_srcdir)/libcwmp/src/inc  -I$(top_srcdir)/libpool/include  -I$(top_srcdir)/libxmlet/include   -I$(top_srcdir)/libcwmp/include  -I$(top_srcdir)/../../include
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .lo .o .obj
+$(srcdir)/Makefile.in:  $(srcdir)/Makefile.am  $(am__configure_deps)
+	@for dep in $?; do \
+	  case '$(am__configure_deps)' in \
+	    *$$dep*) \
+	      ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
+	        && { if test -f $@; then exit 0; else break; fi; }; \
+	      exit 1;; \
+	  esac; \
+	done; \
+	echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign libcwmp/src/Makefile'; \
+	$(am__cd) $(top_srcdir) && \
+	  $(AUTOMAKE) --foreign libcwmp/src/Makefile
+.PRECIOUS: Makefile
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+	@case '$?' in \
+	  *config.status*) \
+	    cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
+	  *) \
+	    echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \
+	    cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \
+	esac;
+
+$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
+	cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+
+$(top_srcdir)/configure:  $(am__configure_deps)
+	cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(ACLOCAL_M4):  $(am__aclocal_m4_deps)
+	cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(am__aclocal_m4_deps):
+install-libLIBRARIES: $(lib_LIBRARIES)
+	@$(NORMAL_INSTALL)
+	test -z "$(libdir)" || $(MKDIR_P) "$(DESTDIR)$(libdir)"
+	@list='$(lib_LIBRARIES)'; test -n "$(libdir)" || list=; \
+	list2=; for p in $$list; do \
+	  if test -f $$p; then \
+	    list2="$$list2 $$p"; \
+	  else :; fi; \
+	done; \
+	test -z "$$list2" || { \
+	  echo " $(INSTALL_DATA) $$list2 '$(DESTDIR)$(libdir)'"; \
+	  $(INSTALL_DATA) $$list2 "$(DESTDIR)$(libdir)" || exit $$?; }
+	@$(POST_INSTALL)
+	@list='$(lib_LIBRARIES)'; test -n "$(libdir)" || list=; \
+	for p in $$list; do \
+	  if test -f $$p; then \
+	    $(am__strip_dir) \
+	    echo " ( cd '$(DESTDIR)$(libdir)' && $(RANLIB) $$f )"; \
+	    ( cd "$(DESTDIR)$(libdir)" && $(RANLIB) $$f ) || exit $$?; \
+	  else :; fi; \
+	done
+
+uninstall-libLIBRARIES:
+	@$(NORMAL_UNINSTALL)
+	@list='$(lib_LIBRARIES)'; test -n "$(libdir)" || list=; \
+	files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \
+	test -n "$$files" || exit 0; \
+	echo " ( cd '$(DESTDIR)$(libdir)' && rm -f "$$files" )"; \
+	cd "$(DESTDIR)$(libdir)" && rm -f $$files
+
+clean-libLIBRARIES:
+	-test -z "$(lib_LIBRARIES)" || rm -f $(lib_LIBRARIES)
+libcwmp.a: $(libcwmp_a_OBJECTS) $(libcwmp_a_DEPENDENCIES) 
+	-rm -f libcwmp.a
+	$(libcwmp_a_AR) libcwmp.a $(libcwmp_a_OBJECTS) $(libcwmp_a_LIBADD)
+	$(RANLIB) libcwmp.a
+
+mostlyclean-compile:
+	-rm -f *.$(OBJEXT)
+
+distclean-compile:
+	-rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/buffer.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/callback.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cfg.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cwmp.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/event.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/http.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ini.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/md5.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/memory.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/model.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/queue.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/session.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssl.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/util.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/periodic.Po@am__quote@
+
+
+.c.o:
+@am__fastdepCC_TRUE@	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@	$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(COMPILE) -c $<
+
+.c.obj:
+@am__fastdepCC_TRUE@	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
+@am__fastdepCC_TRUE@	$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(COMPILE) -c `$(CYGPATH_W) '$<'`
+
+.c.lo:
+@am__fastdepCC_TRUE@	$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@	$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(LTCOMPILE) -c -o $@ $<
+
+mostlyclean-libtool:
+	-rm -f *.lo
+
+clean-libtool:
+	-rm -rf .libs _libs
+
+ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
+	list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+	      END { if (nonempty) { for (i in files) print i; }; }'`; \
+	mkid -fID $$unique
+tags: TAGS
+
+TAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) \
+		$(TAGS_FILES) $(LISP)
+	set x; \
+	here=`pwd`; \
+	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+	      END { if (nonempty) { for (i in files) print i; }; }'`; \
+	shift; \
+	if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \
+	  test -n "$$unique" || unique=$$empty_fix; \
+	  if test $$# -gt 0; then \
+	    $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+	      "$$@" $$unique; \
+	  else \
+	    $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+	      $$unique; \
+	  fi; \
+	fi
+ctags: CTAGS
+CTAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) \
+		$(TAGS_FILES) $(LISP)
+	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+	      END { if (nonempty) { for (i in files) print i; }; }'`; \
+	test -z "$(CTAGS_ARGS)$$unique" \
+	  || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
+	     $$unique
+
+GTAGS:
+	here=`$(am__cd) $(top_builddir) && pwd` \
+	  && $(am__cd) $(top_srcdir) \
+	  && gtags -i $(GTAGS_ARGS) "$$here"
+
+distclean-tags:
+	-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
+
+distdir: $(DISTFILES)
+	@srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+	topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+	list='$(DISTFILES)'; \
+	  dist_files=`for file in $$list; do echo $$file; done | \
+	  sed -e "s|^$$srcdirstrip/||;t" \
+	      -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
+	case $$dist_files in \
+	  */*) $(MKDIR_P) `echo "$$dist_files" | \
+			   sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
+			   sort -u` ;; \
+	esac; \
+	for file in $$dist_files; do \
+	  if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+	  if test -d $$d/$$file; then \
+	    dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
+	    if test -d "$(distdir)/$$file"; then \
+	      find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+	    fi; \
+	    if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+	      cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
+	      find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+	    fi; \
+	    cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
+	  else \
+	    test -f "$(distdir)/$$file" \
+	    || cp -p $$d/$$file "$(distdir)/$$file" \
+	    || exit 1; \
+	  fi; \
+	done
+check-am: all-am
+check: check-am
+all-am: Makefile $(LIBRARIES)
+installdirs:
+	for dir in "$(DESTDIR)$(libdir)"; do \
+	  test -z "$$dir" || $(MKDIR_P) "$$dir"; \
+	done
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+	@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+	$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+	  install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+	  `test -z '$(STRIP)' || \
+	    echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
+mostlyclean-generic:
+
+clean-generic:
+
+distclean-generic:
+	-test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+	-test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
+
+maintainer-clean-generic:
+	@echo "This command is intended for maintainers to use"
+	@echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-generic clean-libLIBRARIES clean-libtool \
+	mostlyclean-am
+
+distclean: distclean-am
+	-rm -rf ./$(DEPDIR)
+	-rm -f Makefile
+distclean-am: clean-am distclean-compile distclean-generic \
+	distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+html: html-am
+
+html-am:
+
+info: info-am
+
+info-am:
+
+install-data-am:
+
+install-dvi: install-dvi-am
+
+install-dvi-am:
+
+install-exec-am: install-libLIBRARIES
+
+install-html: install-html-am
+
+install-html-am:
+
+install-info: install-info-am
+
+install-info-am:
+
+install-man:
+
+install-pdf: install-pdf-am
+
+install-pdf-am:
+
+install-ps: install-ps-am
+
+install-ps-am:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+	-rm -rf ./$(DEPDIR)
+	-rm -f Makefile
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic \
+	mostlyclean-libtool
+
+pdf: pdf-am
+
+pdf-am:
+
+ps: ps-am
+
+ps-am:
+
+uninstall-am: uninstall-libLIBRARIES
+
+.MAKE: install-am install-strip
+
+.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \
+	clean-libLIBRARIES clean-libtool ctags distclean \
+	distclean-compile distclean-generic distclean-libtool \
+	distclean-tags distdir dvi dvi-am html html-am info info-am \
+	install install-am install-data install-data-am install-dvi \
+	install-dvi-am install-exec install-exec-am install-html \
+	install-html-am install-info install-info-am \
+	install-libLIBRARIES install-man install-pdf install-pdf-am \
+	install-ps install-ps-am install-strip installcheck \
+	installcheck-am installdirs maintainer-clean \
+	maintainer-clean-generic mostlyclean mostlyclean-compile \
+	mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \
+	tags uninstall uninstall-am uninstall-libLIBRARIES
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/buffer.c b/ap/app/cwmp/netcwmp/libcwmp/src/buffer.c
new file mode 100755
index 0000000..dda0810
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/buffer.c
@@ -0,0 +1,397 @@
+/************************************************************************

+ * Id: buffer.c                                                         *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014 netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *

+ ***********************************************************************/

+#include "cwmp/buffer.h"

+#include "cwmp/util.h"

+#include "cwmp/memory.h"

+#include "cwmp/log.h"

+//#include "cwmp_private.h"

+

+void  cwmp_buffer_clear(void * data)

+{

+    cwmp_buffer_t * b;

+    //FUNCTION_TRACE();

+    if (!data)

+        return;

+    b = (cwmp_buffer_t*)data;

+    b->writed = 0;

+    b->readed = 0;

+    b->offset = b->string;

+    b->size = DEFAULT_BUFFER_SIZE;

+    b->next = 0;

+    b->string[0] = 0;

+}

+

+

+int cwmp_buffer_create(cwmp_buffer_t **news, pool_t * pool)

+{

+    (*news) =  PMALLOC(sizeof(cwmp_buffer_t));

+

+    if ((*news) == NULL)

+    {

+        return CWMP_ENOMEM;

+    }

+    (*news)->size = DEFAULT_BUFFER_SIZE;

+    (*news)->writed = 0;

+    (*news)->readed = 0;

+    (*news)->offset = (*news)->string;

+    (*news)->next = 0;

+    (*news)->string[0] = 0;

+

+    pool_cleanup_add(pool, cwmp_buffer_clear, *news);

+

+    return CWMP_OK;

+}

+

+int cwmp_buffer_init(cwmp_buffer_t * b)

+{

+    b->writed = 0;

+    b->readed = 0;

+    b->size = DEFAULT_BUFFER_SIZE;

+    b->offset = b->string;

+    b->next = 0;

+    b->string[0] = 0;

+    return CWMP_OK;

+}

+

+void  cwmp_buffer_free(cwmp_buffer_t * b, pool_t * pool)

+{

+    if (b == NULL)

+        return;

+

+    PFREE(b);

+

+}

+

+

+char * cwmp_buffer_current(cwmp_buffer_t * b)

+{

+    return b->string + b->writed;

+}

+

+char * cwmp_buffer_string(cwmp_buffer_t * b)

+{

+    return b->string;

+}

+

+size_t cwmp_buffer_length(cwmp_buffer_t * b)

+{

+    return b == NULL ? 0: (b->writed - b->readed) ;

+}

+

+size_t cwmp_buffer_size(cwmp_buffer_t * b)

+{

+    return b == NULL ? 0: b->size;

+}

+

+int cwmp_buffer_remain(cwmp_buffer_t * b)

+{

+    return b == NULL ? 0: (b->size - b->writed);

+}

+

+size_t	cwmp_chunk_length(cwmp_chunk_t * c)

+{

+    return c == NULL? 0 : c->bytes;

+}

+

+

+

+

+void  cwmp_buffer_write_position(cwmp_buffer_t * b, size_t pos, const void * val, cwmp_uint32_t len)

+{

+    memcpy(b->string + pos, val, len);

+    if (pos + len > b->writed)

+    {

+        /* b->writed = pos + len;  */

+    }

+}

+

+void  cwmp_buffer_write_bytes(cwmp_buffer_t * b, const void * val, size_t len)

+{

+    if (len > (b->size - b->writed))

+        return;

+    memcpy(b->offset, val, len);

+    b->offset += len;

+    b->writed += len;

+}

+

+void  cwmp_buffer_write_uint64(cwmp_buffer_t * b, cwmp_uint64_t val)

+{

+    cwmp_buffer_write_bytes(b, &val, 8);

+}

+

+void  cwmp_buffer_write_uint32(cwmp_buffer_t * b, cwmp_uint32_t val)

+{

+    cwmp_buffer_write_bytes(b, &val, 4);

+}

+

+void  cwmp_buffer_write_uint16(cwmp_buffer_t * b, cwmp_uint16_t val)

+{

+    cwmp_buffer_write_bytes(b, &val, 2);

+}

+

+void cwmp_buffer_write_uint8(cwmp_buffer_t * b, cwmp_byte_t val)

+{

+    cwmp_buffer_write_bytes(b, &val, 1);

+}

+

+void  cwmp_buffer_write_format_string(cwmp_buffer_t * b, const char * fmt, ...)

+{

+    va_list ap = {0};

+    int len;

+    va_start(ap, fmt);

+#ifdef WIN32

+    len = _vsnprintf(cwmp_buffer_current(b), b->size - b->writed, fmt, ap);

+#else

+    len =vsnprintf(cwmp_buffer_current(b), b->size - b->writed, fmt, ap);

+#endif

+    b->offset += len;

+    b->writed += len;

+    va_end(ap);

+}

+

+

+

+void  cwmp_buffer_write_string(cwmp_buffer_t * b, const char * str, size_t len)

+{

+    cwmp_buffer_write_bytes(b, str, len);

+}

+

+void  cwmp_buffer_clone(cwmp_buffer_t * b, const cwmp_buffer_t * cb)

+{

+    size_t len = cb->writed;

+    cwmp_buffer_write_bytes(b, cb->string, len);

+}

+

+int cwmp_buffer_read_bytes(void * val,  cwmp_buffer_t * b, const cwmp_uint32_t len)

+{

+    if (len > cwmp_buffer_length(b))

+    {

+        return CWMP_ERROR;

+    }

+    else

+    {

+        memcpy(val, b->string + b->readed, len);

+        b->readed += len;

+        return CWMP_OK;

+    }

+}

+

+int cwmp_buffer_read_uint8(cwmp_byte_t * val, cwmp_buffer_t * b)

+{

+    return cwmp_buffer_read_bytes((void *)val,b,1);

+}

+

+

+int cwmp_buffer_read_uint16(cwmp_uint16_t * val, cwmp_buffer_t * b)

+{

+    cwmp_uint16_t v;

+    if (cwmp_buffer_read_bytes((void *)&v,b,2) != CWMP_OK)

+    {

+        return CWMP_ERROR;

+    }

+    else

+    {

+        (*val) = v;

+        return CWMP_OK;

+    }

+}

+

+int cwmp_buffer_read_uint32(cwmp_uint32_t * val, cwmp_buffer_t * b)

+{

+    cwmp_uint32_t v;

+    if (cwmp_buffer_read_bytes((void *)&v,b,4) != CWMP_OK)

+    {

+        return CWMP_ERROR;

+    }

+    else

+    {

+        (*val) = v;

+        return CWMP_OK;

+    }

+}

+

+int cwmp_chunk_create(cwmp_chunk_t ** news, pool_t * pool)

+{

+    (*news) = PMALLOC(sizeof(cwmp_chunk_t));

+    if (!(*news))

+    {

+        cwmp_log_error("create array buffer out of memory\n");

+        return CWMP_ENOMEM;

+    }

+    (*news)->buffer = NULL;

+    (*news)->current = NULL;

+    (*news)->bytes = (*news)->count = (*news)->used = 0;

+

+    /*

+       if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)

+       {

+       cwmp_log_error("create buffer out of memory\n");

+       PFREE(*news);

+       return CWMP_ENOMEM;

+       }

+       (*news)->buffer = (*news)->current = buffer;

+       */

+

+    pool_cleanup_add(pool, (pool_cleanup_handler)cwmp_chunk_clear, (*news));

+    return CWMP_OK;

+}

+

+void  cwmp_chunk_print(cwmp_chunk_t * cb)

+{

+    cwmp_buffer_t * b = cb->buffer;

+    FUNCTION_TRACE();

+    while (b)

+    {

+        //fwrite(cwmp_buffer_string(b), 1, cwmp_buffer_length(b), out);

+        cwmp_log_info("%s", cwmp_buffer_string(b));

+        b = b->next;

+    }

+

+}

+

+

+int cwmp_chunk_write_string(cwmp_chunk_t * cb, const char * str, size_t length, pool_t * pool)

+{

+    cwmp_buffer_t *buffer;

+    size_t len, leftbytes, writebytes, writed;

+    //FUNCTION_TRACE();

+    writed = 0;

+    leftbytes = length;

+

+    while (leftbytes>0)

+    {

+        len = cwmp_buffer_remain(cb->current);

+

+        if (len <= 0)

+        {

+            if (cb->current && cb->current->next)

+            {

+                cb->current = cb->current->next;

+                cwmp_buffer_clear(cb->current);

+            }

+            else

+            {

+                if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)

+                {

+                    cwmp_log_error("create buffer out of memory\n");

+                    return CWMP_ERROR;

+                }

+                if (cb->current != NULL)

+                {

+                    cb->current->next = buffer;

+                }

+                else

+                {

+                    cb->buffer = buffer;

+                }

+                cb->current = buffer;

+            }

+

+

+            len=cwmp_buffer_remain(cb->current);

+

+        }

+

+

+        writebytes = leftbytes > len ? len :leftbytes;

+

+        cwmp_buffer_write_string(cb->current, str + writed, writebytes);

+        writed += writebytes;

+

+        cb->bytes += writebytes;

+        leftbytes -= writebytes;

+        /*if (leftbytes > 0)

+          {

+          if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)

+          {

+          cwmp_log_error("create buffer out of memory\n");

+          return CWMP_ERROR;

+          }

+          cb->current->next = buffer;

+          cb->current = buffer;

+          }

+          else

+          {

+          break;

+          }

+          */

+    }

+

+    return writed;

+}

+

+

+int cwmp_chunk_copy(char * dest, const cwmp_chunk_t * cb, size_t max_length)

+{

+    size_t bufleft, destwrited, writebytes;

+    size_t b,d;

+    char * ptr = dest;

+    cwmp_buffer_t * buffer = cb->buffer;

+    bufleft = cb->bytes;

+    destwrited = 0;

+    while ((destwrited <= max_length) && (bufleft > 0) && (buffer!=NULL))

+    {

+        b = bufleft > cwmp_buffer_length(buffer) ? cwmp_buffer_length(buffer) : bufleft;

+        d = max_length - destwrited;

+        writebytes =  d > b	? b : d;

+        strncpy(ptr, cwmp_buffer_string(buffer), writebytes);

+        ptr += writebytes;

+        destwrited += writebytes;

+        bufleft -= writebytes;

+        buffer = buffer->next;

+    }

+

+    dest[destwrited] = 0;

+    return destwrited;

+}

+

+void cwmp_chunk_init(cwmp_chunk_t * cb)

+{

+    FUNCTION_TRACE();

+

+    cb->buffer = NULL;

+    cb->bytes = 0;

+    cb->count = 0;

+    cb->used = 0;

+    cb->current = cb->buffer;

+}

+

+void cwmp_chunk_clear(cwmp_chunk_t * cb)

+{

+    //FUNCTION_TRACE();

+

+    cb->buffer = NULL;

+    cb->bytes = 0;

+    cb->count = 0;

+    cb->used = 0;

+    cb->current = cb->buffer;

+}

+

+

+int cwmp_chunk_release(cwmp_chunk_t * cb, int remain, pool_t * pool)

+{

+    int i;

+    cwmp_buffer_t * next;

+    cwmp_buffer_t * buffer = cb->buffer;

+    for (i=0; i<remain && buffer; i++)

+    {

+        buffer = buffer->next;

+    }

+

+

+    while (buffer)

+    {

+        next = buffer->next;

+        cwmp_buffer_free(buffer, pool);

+        buffer = next;

+    }

+    return 0;

+}

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/callback.c b/ap/app/cwmp/netcwmp/libcwmp/src/callback.c
new file mode 100755
index 0000000..2352b50
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/callback.c
@@ -0,0 +1,13 @@
+//#include "inc/cwmp_private.h"
+#include <cwmp/cwmp.h>
+#include "cwmp/queue.h"
+
+ 
+
+int callback_register_task(cwmp_t * cwmp, callback_func_t callback, void *data1, void *data2)
+{
+    
+    queue_add(cwmp->queue, callback, TASK_CALLBACK_TAG, QUEUE_PRIORITY_HIGH, data1, data2);
+    return CWMP_OK;
+}
+
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/cfg.c b/ap/app/cwmp/netcwmp/libcwmp/src/cfg.c
new file mode 100755
index 0000000..5925b23
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/cfg.c
@@ -0,0 +1,140 @@
+/************************************************************************

+ * Id: cfg.c                                                            *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *

+ ***********************************************************************/

+

+#include <cwmp/cwmp.h>

+#include <cwmp/pool.h>

+#include <cwmp/log.h>

+#include <cwmp/cfg.h>

+#include <ini.h>

+

+

+typedef struct conf_t conf_t;

+

+struct conf_t {

+        char * filename;

+        FILE * fd;

+};

+

+

+static conf_t	* cwmp_conf_handle = NULL;

+

+int cwmp_conf_open(const char * filename)

+{

+    FUNCTION_TRACE();

+    cwmp_conf_handle = malloc(sizeof(conf_t)+1);

+    if (!cwmp_conf_handle)

+    {

+        cwmp_log_error("conf malloc faild.\n");

+        return CWMP_ERROR;

+    }

+    cwmp_conf_handle->filename = TRstrdup(filename);

+    if(NULL == cwmp_conf_handle->filename)

+    {

+        cwmp_log_error("cwmp_conf_handle->filename is null, filename:%s", filename);

+		return CWMP_ERROR;

+    }

+	

+    return CWMP_OK;

+}

+

+void cwmp_conf_split(char * name, char **s , char **k)

+{

+    *s = strchr(name, ':');

+    if(*s == NULL)

+    {

+        k = &name;

+        *s = "cwmp";

+    }

+    else

+    {

+        *s[0] = 0;

+        *k = *s+1;

+        *s = name;

+    }

+}

+

+int cwmp_conf_get(const char * key, char *value)

+{

+    char * s, *k;

+    char name[INI_BUFFERSIZE+1] = {0};

+    //char value[INI_BUFFERSIZE] = {0};

+    FUNCTION_TRACE();

+    if(key == NULL)

+    {

+        return -1;

+    }

+    TRstrncpy(name, key, INI_BUFFERSIZE);

+    cwmp_conf_split(name, &s, &k);

+

+    ini_gets(s,k,NULL,value,INI_BUFFERSIZE, cwmp_conf_handle->filename);

+    return 0;

+}

+

+int cwmp_conf_set(const char * key, const char * value)

+{

+    char * s, *k;

+    char name[INI_BUFFERSIZE+1] = {0};

+    FUNCTION_TRACE();

+    if(key == NULL)

+    {

+        return CWMP_ERROR;

+    }

+    TRstrncpy(name, key, INI_BUFFERSIZE);

+    cwmp_conf_split(name, &s, &k);

+

+    return ini_puts(s, k, value, cwmp_conf_handle->filename);

+}

+

+char * cwmp_conf_pool_get(pool_t * pool, const char * key)

+{

+    char * s, *k;

+    char name[INI_BUFFERSIZE+1] = {0};

+    char value[INI_BUFFERSIZE+1] = {0};

+    //FUNCTION_TRACE();

+    if(key == NULL)

+    {

+        return NULL;

+    }

+    TRstrncpy(name, key, INI_BUFFERSIZE);

+

+    cwmp_conf_split(name, &s, &k);

+

+    ini_gets(s,k,NULL,value,INI_BUFFERSIZE, cwmp_conf_handle->filename);

+

+    return pool_pstrdup(pool, value);

+}

+

+int cwmp_conf_get_int(const char * key)

+{

+    char * s, *k;

+    char name[INI_BUFFERSIZE+1] = {0};

+

+    FUNCTION_TRACE();

+    if(key == NULL)

+    {

+        cwmp_log_error("key is NULL");

+		return 0;

+    }

+    TRstrncpy(name, key, INI_BUFFERSIZE);

+    cwmp_conf_split(name, &s, &k);

+

+    return (int)ini_getl(s,k,0,cwmp_conf_handle->filename);

+}

+

+

+

+

+

+

+

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/common.h b/ap/app/cwmp/netcwmp/libcwmp/src/common.h
new file mode 100755
index 0000000..b0153d5
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/common.h
@@ -0,0 +1,46 @@
+#ifndef _COMMON_H_INCLUDED

+#define _COMMON_H_INCLUDED

+

+#ifdef WITH_SSL

+#include <openssl/crypto.h>

+#include <openssl/x509.h>

+#include <openssl/pem.h>

+#include <openssl/ssl.h>

+#include <openssl/err.h>

+#endif

+

+#define FTP_DEFAULT_PORT	21

+#define FTP_DEFAULT_PROXY_PORT	21

+

+#define HTTP_DEFAULT_PORT	80

+#define HTTP_DEFAULT_PROXY_PORT	3128

+

+

+/* Structure used for error message lists */

+struct fetcherr {

+	const int	 num;

+	const int	 cat;

+	const char	*string;

+};

+

+/* Connection */

+typedef struct fetchconn conn_t;

+struct fetchconn {

+	int		 sd;		/* socket descriptor */

+	char		*buf;		/* buffer */

+	size_t		 bufsize;	/* buffer size */

+	size_t		 buflen;	/* length of buffer contents */

+	int		 err;		/* last protocol reply code */

+#ifdef WITH_SSL

+	SSL		*ssl;		/* SSL handle */

+	SSL_CTX		*ssl_ctx;	/* SSL context */

+	X509		*ssl_cert;	/* server certificate */

+	SSL_METHOD	*ssl_meth;	/* SSL method */

+#endif

+	int		 ref;		/* reference count */

+};

+

+/* for _fetch_writev */

+struct iovec;

+

+#endif

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/core b/ap/app/cwmp/netcwmp/libcwmp/src/core
new file mode 100755
index 0000000..8fa3005
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/core
Binary files differ
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/cwmp.c b/ap/app/cwmp/netcwmp/libcwmp/src/cwmp.c
new file mode 100755
index 0000000..e680cad
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/cwmp.c
Binary files differ
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/event.c b/ap/app/cwmp/netcwmp/libcwmp/src/event.c
new file mode 100755
index 0000000..a629298
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/event.c
@@ -0,0 +1,628 @@
+#include <cwmp/types.h>
+#include <cwmp/cwmp.h>
+#include <cwmp/event.h>
+#include <cwmp/log.h>
+
+
+typedef struct  memorystruct {
+    char *data;
+    size_t size;
+}memorystruct_t;
+
+
+static inform_event_t inform_event_table[] =
+{
+    {INFORM_BOOTSTRAP,                 			0,	CWMP_INFORM_EVENT_CODE_0 , "inform:event_bootstrap",      				    "inform:command_bootstrap"},
+    {INFORM_BOOT,                      			0,	CWMP_INFORM_EVENT_CODE_1 , "inform:event_boot",                    	"inform:command_boot"},
+    {INFORM_PERIODIC,                  			0,	CWMP_INFORM_EVENT_CODE_2 , "inform:event_periodic",                	"inform:command_periodic"},
+    {INFORM_SCHEDULED,                 			0,	CWMP_INFORM_EVENT_CODE_3 , "inform:event_scheduled",               	"inform:command_scheduled"},
+    {INFORM_VALUECHANGE,               			0,	CWMP_INFORM_EVENT_CODE_4 , "inform:event_valuechange",             	"inform:command_valuechange"},
+    {INFORM_KICKED,                    			0,	CWMP_INFORM_EVENT_CODE_5 , "inform:event_kicked",                  	"inform:command_kicked"},
+    {INFORM_CONNECTIONREQUEST,         			0,	CWMP_INFORM_EVENT_CODE_6 , "inform:event_connreq",       				    "inform:command_connreq"},
+    {INFORM_TRANSFERCOMPLETE,          			0,	CWMP_INFORM_EVENT_CODE_7 , "inform:event_transcomplt",        			  "inform:command_transcomplt"},
+    {INFORM_DIAGNOSTICSCOMPLETE,       			0,	CWMP_INFORM_EVENT_CODE_8 , "inform:event_diagcomplt",            		"inform:command_diagcomplt"},
+    {INFORM_REQUESTDOWNLOAD,           			0,	CWMP_INFORM_EVENT_CODE_9 , "inform:event_reqdl",         				    "inform:command_reqdl"},
+    {INFORM_AUTONOMOUSTRANSFERCOMPLETE,			0,	CWMP_INFORM_EVENT_CODE_10, "inform:event_autotranscomplt",         	"inform:command_autotranscomplt"},
+    {INFORM_MREBOOT,                   			0,	CWMP_INFORM_EVENT_CODE_11, "inform:event_mreboot",                 	"inform:command_mreboot"},
+    {INFORM_MSCHEDULEINFORM,           			0,	CWMP_INFORM_EVENT_CODE_12, "inform:event_mscheinform",         		  "inform:command_mscheinform"},
+    {INFORM_MDOWNLOAD,                 			0,	CWMP_INFORM_EVENT_CODE_13, "inform:event_mdownload",               	"inform:command_mdownload"},
+    {INFORM_MUPLOAD,                   			0,	CWMP_INFORM_EVENT_CODE_14, "inform:event_mupload",                 	"inform:command_mupload"},
+    {INFORM_ACCOUNTCHANGE,						0,	CWMP_INFORM_EVENT_CODE_15, "inform:event_accountchange",           	"inform:command_accountchange"},
+    {INFORM_MVENDORSPECRPC,         			0,	CWMP_INFORM_EVENT_CODE_16, "inform:event_mvendorspecrpc",       		  "inform:command_mvendorspecrpc"},
+    {INFORM_XOUIEVENT,                 			0,	CWMP_INFORM_EVENT_CODE_17, "inform:event_xouievent",                	"inform:command_xouievent"},
+    {INFORM_MAX,             					0,	"", "",                	                    ""}
+};
+
+
+
+event_list_t * cwmp_event_list_create(pool_t * pool, int size)
+{
+    //create event list
+    event_list_t * el;
+    el = PMALLOC(sizeof(event_list_t));
+    if (!el)
+    {
+        cwmp_log_error("el pmalloc return null");
+        return NULL;
+    }
+    el->events = PMALLOC(sizeof(event_code_t *) * size);
+    if(!el->events)
+    {
+        cwmp_log_error("el->event pmalloc return null");
+        return NULL;
+    }
+	
+    el->count = 0;
+    el->size = size;
+    return el;
+}
+
+event_code_t * cwmp_event_code_create(pool_t * pool )
+{
+    event_code_t * ev;
+    ev = PMALLOC(sizeof(event_code_t));
+    //ev->event = 0;
+    //ev->code = NULL;
+    //memset(ev->command_key, 0 , COMMAND_KEY_LEN+1);
+    //ev->have_key = 0;
+    //ev->policy = 0;
+    //ev->ref = 0;
+    //ev->fault_code = 0;
+    //ev->start = 0;
+    //ev->end = 0;
+    return ev;
+}
+
+int cwmp_event_list_init(pool_t * pool, event_list_t * el)
+{
+    //create event list
+    int i, size;
+    size = el->size;
+    for(i=0; i<size; i++)
+    {
+    	event_code_t * ec = cwmp_event_code_create(pool);
+    	el->events[i] = ec;	
+    }
+    el->count = 0;
+    return CWMP_OK;
+}
+
+
+int cwmp_event_global_init(cwmp_t * cwmp)
+{
+    FILE    *fp = NULL;
+	int i = 0;
+
+	FUNCTION_TRACE(); 
+    
+    if(!cwmp)
+    {
+        cwmp_log_error("param cwmp is NULL\n");
+        return CWMP_ERROR;
+    }
+    memset(&cwmp->event_global, 0, sizeof(event_global_t));
+    
+    //ÅжÏÅäÖÃÎļþÊÇ·ñ´æÔÚ
+    if(access(cwmp->event_filename, F_OK) == -1)
+    {
+        cwmp_log_error("event_file (%s) no exist!\n", cwmp->event_filename);
+        return CWMP_ERROR;
+    }
+
+    fp = fopen(cwmp->event_filename, "rb");
+    if(!fp)
+    {
+        cwmp_log_error("could not open event file %s\n", cwmp->event_filename);
+        return CWMP_ERROR;
+    }
+
+    if(fread(&cwmp->event_global, sizeof(event_global_t), 1, fp) != 1)
+    {
+        cwmp_log_error("read event global file fail\n");
+    }
+	
+	cwmp_log_info("event_flag:%d", cwmp->event_global.event_flag);
+#if 0
+	for(i=0; i<COMMAND_KEY_LEN+1; i++)
+	{
+        cwmp_log_info("event_key:%d", cwmp->event_global.event_key[i]);
+	}
+#endif
+	cwmp_log_info("fault_code:%d", cwmp->event_global.fault_code);
+	cwmp_log_info("start:%d", cwmp->event_global.start);
+	cwmp_log_info("end:%d", cwmp->event_global.end);
+
+    fclose(fp);
+    
+    return CWMP_OK;
+}
+
+//°ÑʼþÐÅϢдÈëÎļþÖÐ
+int cwmp_event_file_save(cwmp_t * cwmp)
+{
+    FILE    *fp = NULL;
+    int     ret = CWMP_OK;
+	int i = 0;
+	
+    FUNCTION_TRACE(); 
+    if(!cwmp)
+    {
+        return CWMP_ERROR;
+    }
+    
+    if((fp = fopen(cwmp->event_filename, "wb+")) == NULL)
+    {
+        cwmp_log_error("can't open file: %s.\n", cwmp->event_filename);
+        return CWMP_ERROR;
+    }
+
+    if(fwrite(&cwmp->event_global, sizeof(event_global_t), 1, fp) != 1)
+    {
+        cwmp_log_error("can't write event global to %s\n", cwmp->event_filename);
+     
+        ret = CWMP_ERROR;
+    }
+
+
+	cwmp_log_info("event_flag:%d", cwmp->event_global.event_flag);
+
+	for(i=0; i<COMMAND_KEY_LEN+1; i++)
+	{
+        cwmp_log_info("event_key:%d", cwmp->event_global.event_key[i]);
+	}
+
+	cwmp_log_info("fault_code:%d", cwmp->event_global.fault_code);
+	cwmp_log_info("start:%d", cwmp->event_global.start);
+	cwmp_log_info("end:%d", cwmp->event_global.end);
+	
+
+    fclose(fp);
+    return ret;
+}
+
+
+
+//É豸Æô¶¯ÒÔºó£¬Ò»Ð©Ê¼þÐèÒªÉϱ¨
+int cwmp_event_init(cwmp_t *cwmp)
+{
+    int     bootstrap_flag = 0;
+    int     upgrade_flag = 0;
+    char    val[64] = {0};
+
+	FUNCTION_TRACE(); 
+    
+    if(!cwmp)
+    {
+        cwmp_log_error("param cwmp is NULL\n");
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_init(&cwmp->event_mutex, NULL);
+
+    cwmp->el = cwmp_event_list_create(cwmp->pool, INFORM_MAX);
+	if(!cwmp->el)
+	{
+	    cwmp_log_error("cwmp->el is null");
+		return CWMP_ERROR;
+	}
+
+    cwmp_event_list_init(cwmp->pool, cwmp->el);
+
+    cwmp_event_global_init(cwmp);
+
+
+    if(cwmp->event_global.event_flag == EVENT_REBOOT_NONE_FLAG || cwmp->event_global.event_flag & EVENT_REBOOT_BOOTSTRAP_FLAG)
+    {
+        
+		cwmp_log_info("1.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
+	    cwmp->event_global.event_flag = EVENT_REBOOT_BOOTSTRAP_FLAG;    
+	    cwmp_event_set_value(cwmp, INFORM_BOOTSTRAP, 1, NULL, 0, 0, 0);
+
+		cwmp->boot_flag = 1;
+
+    }    
+    else    //reboot
+    {
+        cwmp_log_info("2.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
+
+	    cwmp_event_set_value(cwmp, INFORM_BOOT, 1, NULL, 0, 0, 0);
+        if(cwmp->event_global.event_flag & EVENT_REBOOT_ACS_FLAG)
+        {
+	         cwmp_event_set_value(cwmp, INFORM_MREBOOT, 1, NULL, 0, 0, 0);
+    	}
+    }
+
+    //upgrade firmware
+    if(cwmp->event_global.event_flag & EVENT_REBOOT_TRANSFERCOMPLETE_FLAG)
+    {
+        cwmp_log_info("4.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
+        cwmp_event_set_value(cwmp, INFORM_MDOWNLOAD, 1, NULL, 0, 0, 0);
+        cwmp_event_set_value(cwmp, INFORM_TRANSFERCOMPLETE, 1, cwmp->event_global.event_key, cwmp->event_global.fault_code, cwmp->event_global.start, cwmp->event_global.end);
+    }
+
+    return CWMP_OK;
+}
+
+/*
+//ÉèÖÃij¸öʼþµÄcommandkey
+int cwmp_event_set_commandkey(cwmp_t *cwmp, const int event, const char *cmd_key, time_t start)
+{
+    cwmp_log_debug( "cwmp_event_set_commandkey begin, event=%d\n", event);
+    if(event < 0 || event >= INFORM_MAX)
+    {
+        cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+
+    int max = cwmp->el->size;
+    int i;
+    for(i=0; i<max; i++)
+    {
+	event_code_t * ec = cwmp->el->events[i];
+	if(ec->event == event)
+	{
+		strcpy(ec->command_key, cmd_key);
+		ec->start = start;		
+		break;
+	}
+    }
+
+
+    pthread_mutex_unlock(&cwmp->event_mutex);
+    
+    cwmp_log_debug( "cwmp_event_set_commandkey end\n");
+    return CWMP_OK;
+}
+*/
+
+int cwmp_event_set_value(cwmp_t *cwmp,  int event,   int value, const char * cmd_key, int fault_code, time_t start, time_t end)
+{
+    cwmp_log_debug( "cwmp_event_set_value begin, event=%d, value=%d\n", event, value);
+    if(event < 0 || event >= INFORM_MAX)
+    {
+        cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
+        return CWMP_ERROR;
+    }
+    int count = cwmp->el->count;
+	
+    int max = cwmp->el->size;
+
+    if(count >= cwmp->el->size)
+    {
+		return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+
+ 
+    int i;
+    int k=count;
+	
+    for(i=0; i<count; i++)
+    {
+    	event_code_t * ec = cwmp->el->events[i];
+    	if(ec->event == event)
+    	{
+    		k = i;		
+    		break;
+    	}
+    }
+
+	event_code_t * ec = cwmp->el->events[k];
+	memset(ec, 0, sizeof(event_code_t));
+	ec->event = event;
+	ec->ref = value;
+	ec->code = inform_event_table[event].code;	
+	if(cmd_key != NULL)
+	{
+		TRstrncpy(ec->command_key, cmd_key,COMMAND_KEY_LEN);
+		cwmp_log_info("command key:%s", ec->command_key);
+	}
+
+	ec->fault_code = fault_code;
+	ec->start = start;
+	ec->end = end;
+	cwmp->el->count ++;
+
+    pthread_mutex_unlock(&cwmp->event_mutex);
+    
+    return CWMP_OK;
+
+}
+
+/*
+//ÉèÖÃij¸öʼþµÄÖµ
+int cwmp_event_set_value1(cwmp_t *cwmp,  int event,   int value)
+{
+    cwmp_log_debug( "cwmp_event_set_value begin, event=%d, value=%d\n", event, value);
+    if(event < 0 || event >= INFORM_MAX)
+    {
+        cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+    if(cwmp->el->count < cwmp->el->size-1)
+    {
+        int count = cwmp->el->count;
+	event_code_t * ec = cwmp->el->events[count];
+	ec->event = event;
+        ec->ref = value;
+	ec->code = inform_event_table[event].code;	
+	cwmp->el->count ++;
+    }
+    
+    pthread_mutex_unlock(&cwmp->event_mutex);
+    
+    cwmp_log_debug( "cwmp_event_set_value end\n");
+    return CWMP_OK;
+}
+
+int cwmp_event_inc_set_value(cwmp_t *cwmp, int event,  int step)
+{
+    cwmp_log_debug( "cwmp_event_inc_set_value begin, event=%d, step=%d\n", event, step);
+    if(event < 0 || event >= INFORM_MAX)
+    {
+        cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+   // cwmp->event_info[index].ref += step;
+    int max = cwmp->el->size;
+    int i;
+    for(i=0; i<max-1; i++)
+    {
+	event_code_t * ec = cwmp->el->events[i];
+	if(ec->event == event)
+	{
+		ec->ref += step;
+		break;
+	}
+    }
+    if(i == max)
+    {
+    	event_code_t * newec = cwmp->el->events[cwmp->el->count];
+	newec->event = event;
+	newec->ref += 1;
+	newec->code = inform_event_table[event].code;	 
+    }
+		
+	
+    pthread_mutex_unlock(&cwmp->event_mutex);
+    
+    cwmp_log_debug( "cwmp_event_inc_set_value end\n");
+    return CWMP_OK;
+}
+
+int cwmp_event_dec_set_value(cwmp_t *cwmp, const int event, const unsigned int step)
+{
+    cwmp_log_debug( "cwmp_event_dec_set_value begin, event=%d, step=%d\n", event, step);
+    if(event < 0 || event >= INFORM_MAX)
+    {
+        cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+    int max = cwmp->el->size;
+    int i;
+    for(i=0; i<max-1; i++)
+    {
+	event_code_t * ec = cwmp->el->events[i];
+	if(ec->event == event)
+	{
+		ec->ref -= step;
+		if(ec->ref < 0)
+		{
+			ec->ref = 0;
+		}
+		break;
+	}
+    }
+    pthread_mutex_unlock(&cwmp->event_mutex);
+    
+    cwmp_log_debug( "cwmp_event_dec_set_value end\n");
+    return CWMP_OK;
+}
+
+*/
+
+int cwmp_event_get_index(cwmp_t *cwmp,const char *name)
+{
+    int index = -1;
+    int i;
+	int size = 0;
+
+    if(!name)
+    {
+        goto finish;
+    }
+	
+    size = cwmp->el->size;
+	
+    for(i=0; i<INFORM_MAX; i++)
+    {
+        if(TRstrcmp(cwmp->el->events[i]->code, name) == 0)
+        {
+            index = i;
+            break;
+        }
+    }
+
+finish:
+    return index;
+}
+
+#if 0
+size_t cwmp_write_callback(void *ptr, size_t size, size_t nmemb, void *data)
+{
+    size_t          realsize = size * nmemb;
+    memorystruct_t  *mem = (memorystruct_t*)data;
+
+    if(!mem || !ptr)
+    {
+        cwmp_log_error( "ptr or data is NULL\n");
+        return 0;
+    }
+
+    mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
+    if (mem->data)
+    {
+        memcpy(&(mem->data[mem->size]), ptr, realsize);
+        mem->size += realsize;
+        mem->data[mem->size] = 0;
+    }
+    
+    return realsize;
+}
+#endif
+
+
+//Çå³ýµôÏà¹ØµÄÐÅÏ¢
+int cwmp_event_clear_active(cwmp_t *cwmp)
+{
+    int     i;
+    int     j;
+    int     notify_flag = 0;
+
+    assert(cwmp != NULL);
+
+	FUNCTION_TRACE();
+  
+    pthread_mutex_lock(&cwmp->event_mutex);
+ 
+    event_code_t ** pec = cwmp->el->events;
+    int evsize = cwmp->el->count;
+	
+    for(i=0; i<evsize; i++)
+    {
+        if(pec[i]->ref <= 0)
+        {
+            continue;
+        }
+
+    	switch(pec[i]->event)
+    	{
+    		case INFORM_BOOTSTRAP:
+    			{
+    				cwmp->event_global.event_flag |=  EVENT_REBOOT_ACS_FLAG;
+    		        cwmp_event_file_save(cwmp);
+
+    		    }
+    		    break;
+    
+    		case INFORM_MREBOOT:
+    			cwmp->event_global.event_flag |= EVENT_REBOOT_ACS_FLAG;
+                cwmp_event_file_save(cwmp);
+    		    break;
+    
+    		case INFORM_VALUECHANGE:
+    			notify_flag = 1;
+    		    break;
+    
+    		case INFORM_TRANSFERCOMPLETE:
+    			{
+        			cwmp->event_global.event_flag |= EVENT_REBOOT_TRANSFERCOMPLETE_FLAG;
+                    memset(cwmp->event_global.event_key, 0, COMMAND_KEY_LEN+1);
+        			strncpy(cwmp->event_global.event_key, pec[i]->command_key, COMMAND_KEY_LEN);
+        			cwmp->event_global.fault_code = pec[i]->fault_code;
+        			cwmp->event_global.start = pec[i]->start;
+        			cwmp->event_global.end = pec[i]->end;			
+                    cwmp_event_file_save(cwmp);
+    			}
+    			break;
+    			
+    	}
+    
+    	pec[i]->ref = 0; //clear
+    }
+
+    cwmp->el->count = 0;
+    pthread_mutex_unlock(&cwmp->event_mutex);
+
+    //Çå³ýValue Change
+    /*
+    if(notify_flag == 1)
+    {
+        hash_index_t    *hi = NULL;
+        val_change_t    *tmp = NULL;
+        char            *key = NULL;
+        
+        for (hi = hash_first(cwmp->ht_val_change); hi; hi = hash_next(hi))
+        {
+            hash_this(hi, (const void**)(&key), NULL, (void**)&tmp);
+            if(!key || !tmp)
+            {
+                continue;
+            }
+
+            //´Óvalue change hashÈ¥³ý
+            hash_set(cwmp->ht_val_change, (void*)tmp->name, strlen(tmp->name), NULL);
+
+            //ÊÍ·ÅÄÚ´æ
+            if(tmp->value)
+            {
+                free_check(tmp->value);
+            }
+            free_check(tmp);
+        }
+    }
+    */
+
+    return CWMP_OK;
+}
+
+
+//È¡µÃactive eventÒÔ¼°count
+/*
+static int get_active_event_list(cwmp_t *cwmp, event_list_t **pevent_list, int *pevt_count)
+{
+    int             i;
+    event_list_t    *evlist = NULL;
+    int             count = 0;
+    int             ret = CWMP_OK;
+    
+    if(!cwmp || !pevent_list || !pevt_count)
+    {
+        cwmp_log_error( "param is NULL\n");
+        return CWMP_ERROR;
+    }
+
+    pthread_mutex_lock(&cwmp->event_mutex);
+    int max = cwmp->event_list->count;
+    for(i=0; max; i++)
+    {
+        if(cwmp->event_info[i].ref > 0)
+        {            
+            evlist = realloc(evlist, sizeof(event_t)*(count+1));
+            if(!evlist)
+            {
+                ret = CWMP_ERROR;
+                cwmp_log_error( "realloc fail\n");
+                break;
+            }
+            memcpy(&evlist[count], &cwmp->event_info[i], sizeof(event_list_t));
+            count++;
+        }
+    }
+    pthread_mutex_unlock(&cwmp->event_mutex);
+
+    if(ret == CWMP_OK)
+    {
+        *pevent_list = evlist;
+        *pevt_count = count;
+    }
+    else
+    {
+        free(evlist);
+    }
+
+    return ret;
+}
+
+*/
+
+
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/http.c b/ap/app/cwmp/netcwmp/libcwmp/src/http.c
new file mode 100755
index 0000000..e4aed5e
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/http.c
@@ -0,0 +1,3119 @@
+/************************************************************************

+ * Id: http.c                                                           *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *

+ ***********************************************************************/

+ 

+#include "cwmp/http.h"

+#include "cwmp/log.h"

+#include "cwmp_private.h"

+#include <cwmp/md5.h>

+

+#include <errno.h>

+

+#include <netdb.h>

+#include <sys/socket.h>

+

+struct http_sockaddr_t

+{

+

+    /** IPv6 sockaddr structure */

+    struct sockaddr_in6 sin6;

+

+};

+

+

+

+int http_read_body_chunked(http_socket_t * sock);

+

+

+/*********************************************************************/

+

+const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 

+static char* base64_encode(const char* data, int data_len); 

+static char *base64_decode(const char* data, int data_len); 

+static char find_pos(char ch);

+

+

+/* */ 

+static char *base64_encode(const char* data, int data_len) 

+{ 

+    //int data_len = strlen(data); 

+    int prepare = 0; 

+    int ret_len; 

+    int temp = 0; 

+    char *ret = NULL; 

+    char *f = NULL; 

+    int tmp = 0; 

+    char changed[4]; 

+    int i = 0; 

+    ret_len = data_len / 3; 

+    temp = data_len % 3; 

+    if (temp > 0) 

+    { 

+        ret_len += 1; 

+    } 

+    ret_len = ret_len*4 + 1; 

+    ret = (char *)malloc(ret_len); 

+

+    if ( ret == NULL) 

+    { 

+        printf("No enough memory.\n"); 

+        return NULL; 

+    } 

+    memset(ret, 0, ret_len); 

+    f = ret; 

+    while (tmp < data_len) 

+    { 

+        temp = 0; 

+        prepare = 0; 

+        memset(changed, '\0', 4); 

+        while (temp < 3) 

+        { 

+            //printf("tmp = %d\n", tmp); 

+            if (tmp >= data_len) 

+            { 

+                break; 

+            } 

+            prepare = ((prepare << 8) | (data[tmp] & 0xFF)); 

+            tmp++; 

+            temp++; 

+        } 

+        prepare = (prepare<<((3-temp)*8)); 

+        //printf("before for : temp = %d, prepare = %d\n", temp, prepare); 

+        for (i = 0; i < 4 ;i++ ) 

+        { 

+            if (temp < i) 

+            { 

+                changed[i] = 0x40; 

+            } 

+            else 

+            { 

+                changed[i] = (prepare>>((3-i)*6)) & 0x3F; 

+            } 

+            *f = base[changed[i]]; 

+            //printf("%.2X", changed[i]); 

+            f++; 

+        } 

+    } 

+    *f = '\0'; 

+

+    return ret; 

+

+} 

+/* */ 

+static char find_pos(char ch)   

+{ 

+    char *ptr = (char*)strrchr(base, ch);//the last position (the only) in base[] 

+    return (ptr - base); 

+} 

+/* */ 

+static char *base64_decode(const char *data, int data_len) 

+{ 

+    int ret_len = (data_len / 4) * 3; 

+    int equal_count = 0; 

+    char *ret = NULL; 

+    char *f = NULL; 

+    int tmp = 0; 

+    int temp = 0; 

+    char need[3]; 

+    int prepare = 0; 

+    int i = 0; 

+    if (*(data + data_len - 1) == '=') 

+    { 

+        equal_count += 1; 

+    } 

+    if (*(data + data_len - 2) == '=') 

+    { 

+        equal_count += 1; 

+    } 

+    if (*(data + data_len - 3) == '=') 

+    {//seems impossible 

+        equal_count += 1; 

+    } 

+    switch (equal_count) 

+    { 

+    case 0: 

+        ret_len += 4;//3 + 1 [1 for NULL] 

+        break; 

+    case 1: 

+        ret_len += 4;//Ceil((6*3)/8)+1 

+        break; 

+    case 2: 

+        ret_len += 3;//Ceil((6*2)/8)+1 

+        break; 

+    case 3: 

+        ret_len += 2;//Ceil((6*1)/8)+1 

+        break; 

+    } 

+    ret = (char *)malloc(ret_len); 

+    if (ret == NULL) 

+    { 

+        printf("No enough memory.\n"); 

+        return NULL; 

+    } 

+    memset(ret, 0, ret_len); 

+    f = ret; 

+    while (tmp < (data_len - equal_count)) 

+    { 

+        temp = 0; 

+        prepare = 0; 

+        memset(need, 0, sizeof(need)); 

+        while (temp < 4) 

+        { 

+            if (tmp >= (data_len - equal_count)) 

+            { 

+                break; 

+            } 

+            prepare = (prepare << 6) | (find_pos(data[tmp])); 

+            temp++; 

+            tmp++; 

+        } 

+        prepare = prepare << ((4-temp) * 6); 

+        for (i=0; i<3 ;i++ ) 

+        { 

+            if (i == temp) 

+            { 

+                break; 

+            } 

+            *f = (char)((prepare>>((2-i)*8)) & 0xFF); 

+            f++; 

+        } 

+    } 

+    *f = '\0'; 

+    return ret; 

+}

+

+

+/*********************************************************************/

+

+

+

+char * http_get_variable(http_parser_t * parser, const char *name)

+{

+    int i;

+    for (i=0; i<parser->count && i<MAX_HEADERS; i++)

+    {

+        if (TRstrcasecmp(parser->header[i]->name, name) == 0)

+        {

+            return parser->header[i]->value;

+        }

+    }

+	

+    return NULL;

+}

+

+void http_set_variable(http_parser_t *parser, const char *name, const char *value, pool_t * pool)

+{

+    key_value_t *var;

+

+    //FUNCTION_TRACE();

+

+    if (name == NULL || value == NULL)

+        return;

+

+

+    var = (key_value_t *)pool_pcalloc(pool, sizeof(key_value_t));

+    if (var == NULL)

+    {

+        return;

+    }

+

+    var->name = pool_pstrdup_lower(pool, name);

+    var->value = pool_pstrdup(pool, value);

+    if (parser->count >= MAX_HEADERS)

+    {

+        return;

+    }

+    parser->header[parser->count++] = var;

+}

+

+

+int http_connect(http_socket_t * sock, const char * url)

+{

+    return 0;

+}

+

+int http_dest_create(http_dest_t ** dest, const char * url, pool_t * pool)

+{

+    http_dest_t * d = (http_dest_t*)pool_pcalloc(pool, sizeof(http_dest_t));

+    if(NULL == d)

+    {

+        cwmp_log_error("d pool_pcalloc return null");

+		return CWMP_ERROR;

+    }

+	

+//    cwmp_uint32_t length = TRstrlen(url);

+    cwmp_log_info("url:%s", url);

+    http_parse_url(d, url);

+    d->url = pool_pstrdup(pool, url);

+    if(NULL == d->url)

+    {

+        cwmp_log_error("d->url pool_pstrdup is null, url:%s", url);

+		return CWMP_ERROR;

+    }

+	

+//	d->url = (char *)pool_pcalloc(pool, length+1);

+//	strncpy(d->url, url, length);

+    cwmp_log_debug("dest create url is %s", d->url);

+    *dest = d;

+    return CWMP_OK;

+}

+

+

+

+void http_sockaddr_set(http_sockaddr_t * addr, int family, int port, const char * hostname)

+{

+    FUNCTION_TRACE();

+

+    if (hostname)

+    {

+        cwmp_log_info("host name is:%s", hostname);

+        struct sockaddr_in sa;

+        char *ptr,**pptr;

+        char str[256];

+        struct hostent *host = gethostbyname2(hostname, family);

+        

+        if(!host){

+        	cwmp_log_error("Get IP address error, host name:%s!", hostname);

+			return;

+        }

+        

+        switch(host->h_addrtype) 

+        {	

+        	case AF_INET:	

+ 

+        		pptr=host->h_addr_list;   

+        		inet_ntop(host->h_addrtype, *pptr, str, sizeof(str));

+    		    cwmp_log_info("ip is:%s", str);

+        		((struct sockaddr_in *)&(addr->sin6))->sin_addr.s_addr = inet_addr(str);

+                ((struct sockaddr_in*)&(addr->sin6))->sin_family       = AF_INET;

+                ((struct sockaddr_in*)&(addr->sin6))->sin_port         = htons((unsigned short)port);

+                

+        		break;

+

+        	case AF_INET6:	

+                pptr=host->h_addr_list;   

+                inet_ntop(host->h_addrtype, *pptr, str, sizeof(str));  

+                cwmp_log_info("ip6 is:%s", str);

+                {

+                    int ret = inet_pton(AF_INET6, str, (void*)&addr->sin6.sin6_addr);

+					if(ret <= 0)//cov

+					{

+						cwmp_log_error("http_sockaddr_set inet_pton6 error!\n");

+					}

+                }

+                cwmp_log_info("ip6 is:%s", str);

+

+                addr->sin6.sin6_port   = htons((unsigned short)port);

+                addr->sin6.sin6_family = AF_INET6;               

+

+                break;                        

+        

+        	default:  

+        		cwmp_log_error("unknown address type\n");  

+        		break;

+        }

+

+    }

+    else

+    {

+       cwmp_log_error("null hostname\n");  

+       if(family == AF_INET6)

+       {

+           addr->sin6.sin6_port = htons((unsigned short)(port&0xFFFF));

+           addr->sin6.sin6_family = family; 

+       }

+       else if(family == AF_INET)

+       {

+           ((struct sockaddr_in *)&(addr->sin6))->sin_port = htons((unsigned short)(port&0xFFFF));

+           ((struct sockaddr_in *)&(addr->sin6))->sin_family = family;        

+       }

+       else

+       {

+           cwmp_log_error("unknown family type:%d", family);

+       }

+    }

+}

+

+

+int http_socket_calloc(http_socket_t **news, pool_t * pool)

+{

+    (*news) = (http_socket_t *)pool_pcalloc(pool, sizeof(http_socket_t));

+

+    if ((*news) == NULL)

+    {

+        cwmp_log_error("socket create pool pcalloc null.\n");

+        return CWMP_ERROR;

+    }

+

+    (*news)->addr = (http_sockaddr_t*)pool_pcalloc(pool, sizeof(http_sockaddr_t));

+    if ((*news)->addr == NULL)

+    {

+        (*news) = NULL;

+        cwmp_log_error("http_sockaddr_t  pool pcalloc  null.\n");

+        return CWMP_ERROR;

+    }

+    (*news)->sockdes = -1;

+    (*news)->timeout = -1;

+    (*news)->pool = pool;

+

+

+    pool_cleanup_add(pool, (pool_cleanup_handler)http_socket_close, (*news));

+    return CWMP_OK;

+}

+

+

+int http_socket_create(http_socket_t **news, int family, int type, int protocol, pool_t * pool)

+{

+    int stat;

+    stat = http_socket_calloc(news, pool);

+    if (stat == CWMP_ERROR)

+    {

+        return CWMP_ERROR;

+    }

+

+   

+    (*news)->sockdes = socket(family, type, protocol);

+

+#if HAVE_IPV6

+    if ((*news)->sockdes == -1)

+    {

+        cwmp_log_error(" have ipv6");

+        family = AF_INET;

+        (*news)->sockdes = socket(family, type, protocol);

+    }

+#endif

+

+    if ((*news)->sockdes == -1)

+    {

+        cwmp_log_error("sockdes is -1.\n");

+        return - errno;

+    }

+

+    (*news)->type = type;

+    (*news)->protocol = protocol;

+    http_sockaddr_set((*news)->addr,family, 0, NULL);

+    (*news)->timeout = -1;

+

+    return CWMP_OK;

+}

+

+int http_socket_server (http_socket_t **news, int port, int backlog, int timeout, pool_t * pool)

+{

+    int i;

+    http_socket_t * sock;

+    int rc;

+

+    int family = get_family_type();

+

+    if(family == -1)

+    {

+        cwmp_log_error("family error:%d.", family);

+        return CWMP_ERROR;

+    }

+

+    rc = http_socket_create(&sock, family, SOCK_STREAM, 0, pool);

+        

+    if (rc != CWMP_OK)

+    {

+        cwmp_log_error("http_socket_create faild. %s", strerror(errno));

+        return CWMP_ERROR;

+    }

+    

+    i = 1;

+    if (setsockopt (sock->sockdes, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof(i)) == -1)

+    {

+        cwmp_log_error ("http_socket_server: setsockopt SO_REUSEADDR: %sock", strerror (errno));

+    }

+

+    http_sockaddr_set(sock->addr, family, port, NULL);

+

+    if(family == AF_INET)

+    {

+        rc = bind (sock->sockdes, (struct sockaddr *)sock->addr, sizeof (struct sockaddr_in));

+    }

+    else if(family == AF_INET6)

+    {

+        rc = bind (sock->sockdes, (struct sockaddr *)sock->addr, sizeof (struct sockaddr_in6));

+    }

+    

+    if (rc == -1)

+    {

+        cwmp_log_error ("http_socket_server: bind fail: %s", strerror (errno));

+        http_socket_close (sock);

+        

+        return CWMP_ERROR;

+    }

+

+

+    if (listen (sock->sockdes, (unsigned)backlog) == -1)

+    {

+        http_socket_close (sock);

+        cwmp_log_error ("http_socket_server: listen fail: %s", strerror (errno));

+        return CWMP_ERROR;

+    }

+

+    *news = sock;

+

+    return CWMP_OK;

+

+

+}

+

+

+int http_socket_connect(http_socket_t * sock, int family, const char * host, int port)

+{ 

+    int rc = 0;

+    http_sockaddr_set(sock->addr, family, port, host);

+

+    if(family == AF_INET)

+    {

+        rc = connect(sock->sockdes, (const struct sockaddr *)&sock->addr->sin6,

+                sizeof(struct sockaddr_in));

+    }

+    else if(family == AF_INET6)

+    {

+        rc = connect(sock->sockdes, (const struct sockaddr *)&sock->addr->sin6,

+            sizeof(struct sockaddr_in6));

+    }

+    else

+    {

+        cwmp_log_error("undefine pdp type. %d", family);

+        return CWMP_ERROR;

+    }

+    

+    if (rc == -1)

+    {

+        cwmp_log_error("connect error no:%d(%s)\n", errno, strerror(errno));

+		return CWMP_ERROR;

+    }    

+

+    return CWMP_OK; 

+}

+

+int http_socket_accept(http_socket_t *sock, http_socket_t ** news)

+{

+    struct sockaddr addr4 = {0}; //cov

+    struct sockaddr_in6 addr6 = {0}; //cov

+

+    size_t len;

+    pool_t * pool;

+    int rc, s;

+    cwmp_log_debug("TRACE: socket_tcp_accept\n");

+

+    int family = get_family_type();

+

+    if(family == AF_INET)

+    {

+        len = sizeof(addr4);

+        s = accept (sock->sockdes, &addr4, &len);

+    }

+    else if(family == AF_INET6)

+    {

+        len = sizeof(addr6);

+        s = accept (sock->sockdes, &addr6, &len);

+    }

+    else

+    {

+        cwmp_log_error("undefine family type. %d", family);

+        return CWMP_ERROR;

+    }

+

+    if (s == -1)

+    {

+        cwmp_log_error("accept return -1");

+        return CWMP_ERROR;

+    }

+

+    pool = pool_create(POOL_DEFAULT_SIZE);

+    if(NULL == pool)

+    {

+        cwmp_log_error("pool create return null, size: POOL_DEFAULT_SIZE");

+		close(s);

+		return CWMP_ERROR;

+    }

+	

+    rc = http_socket_calloc(news, pool);

+    if (rc != CWMP_OK)

+    {

+        cwmp_log_error("http_socket_calloc return ERROR;");

+		close(s);

+        return CWMP_ERROR;

+    }

+    (*news)->sockdes = s;

+

+    if(family == AF_INET)

+    {

+        memcpy(&(*news)->addr->sin6, &addr4, sizeof(struct sockaddr_in));

+    }

+    else if(family == AF_INET6)

+    {

+        memcpy(&(*news)->addr->sin6, &addr6, sizeof(struct sockaddr_in6));

+    }

+

+    return CWMP_OK;

+

+}

+

+

+void http_socket_close(http_socket_t * sock)

+{

+    FUNCTION_TRACE();

+    if (sock)

+    {

+        if (sock->sockdes != -1)

+        {

+#ifdef WIN32

+            closesocket(sock->sockdes);

+#else

+            close(sock->sockdes);

+#endif

+            sock->sockdes = -1;

+        }

+

+    }

+

+}

+

+void http_socket_destroy(http_socket_t * sock)

+{

+    pool_t * pool;

+    pool = sock->pool;

+

+    pool_destroy(pool);

+

+}

+

+int http_socket_get_fd(http_socket_t * sock)

+{

+    if (sock)

+        return sock->sockdes;

+    else

+        return -1;

+}

+

+pool_t * http_socket_get_pool(http_socket_t * sock)

+{

+    if(sock && sock->pool)

+    {

+        return sock->pool;

+    }

+    else

+    {

+        return NULL;

+    }

+}

+

+

+int http_socket_read (http_socket_t * sock, char *buf, int bufsize)

+{

+    int res = 0;

+    

+    if(sock->use_ssl)

+    {

+        

+#ifdef USE_CWMP_OPENSSL        

+        do

+        {

+            res = SSL_read(sock->ssl, buf, bufsize);

+        }

+        while (res == -1 && errno == EINTR);

+#endif   

+        return res;

+    }

+    else

+    {        

+        do

+        {

+            res = recv (sock->sockdes, buf, bufsize, 0);

+        }

+        while (res == -1 && errno == EINTR);

+        return res;

+        

+    }

+}

+

+int http_socket_write (http_socket_t * sock, const char *buf, int bufsize)

+{

+    int res = 0;

+    if(sock->use_ssl)

+    {

+        cwmp_log_debug("http socket ssl write buffer: %s, length: %d", buf, bufsize);

+#ifdef USE_CWMP_OPENSSL

+        do

+        {

+            

+            res = SSL_write (sock->ssl, buf, bufsize);

+        }

+        while (res == -1 && errno == EINTR);

+#endif        

+        return res;

+    }

+    else

+    {

+        cwmp_log_debug("http socket write buffer fd:%d, length:%d,  [\n%s\n]", sock->sockdes, bufsize, buf);

+        do

+        {           

+            res = send (sock->sockdes, buf, bufsize, 0);

+        }while (res == -1 && errno == EINTR);

+

+		

+        return res;

+        

+    }    

+}

+

+void http_socket_set_sendtimeout(http_socket_t * sock, int timeout)

+{

+    struct timeval to;

+    to.tv_sec = timeout;

+    to.tv_usec = 0;

+    sock->timeout = timeout;

+    if(0 != setsockopt(sock->sockdes, SOL_SOCKET, SO_SNDTIMEO,

+               (char *) &to,

+               sizeof(to)))

+    {

+        cwmp_log_error("setsockopt send timeout fail!");

+    }

+}

+

+void http_socket_set_recvtimeout(http_socket_t * sock, int timeout)

+{

+    struct timeval to;

+    to.tv_sec = timeout;

+    to.tv_usec = 0;

+    sock->timeout = timeout;

+    if(0 != setsockopt(sock->sockdes, SOL_SOCKET, SO_RCVTIMEO,

+               (char *) &to,

+               sizeof(to)))

+    {

+        cwmp_log_error("setsockopt recv timeout fail!");

+	}

+}

+

+int http_socket_set_writefunction(http_socket_t * sock, http_write_callback_pt callback, void * calldata)

+{

+    if(!sock)

+    {

+        return CWMP_ERROR;

+    }

+    sock->write_callback = callback;

+    sock->write_calldata = calldata;

+    return CWMP_OK;

+}

+

+

+int http_request_create(http_request_t ** request , pool_t * pool)

+{

+    http_request_t * req;

+    req = (http_request_t*)pool_pcalloc(pool, sizeof(http_request_t));

+    if(NULL == req)

+    {

+        cwmp_log_error("req pool pcalloc return fail");

+		return CWMP_ERROR;

+    }

+	

+    req->parser = (http_parser_t*)pool_pcalloc(pool, sizeof(http_parser_t));

+    if(NULL == req->parser)

+    {

+        cwmp_log_error("req pool pcalloc return fail");

+		return CWMP_ERROR;

+    }	

+

+    *request = req;

+

+    return CWMP_OK;

+}

+

+int http_response_create(http_response_t ** response, pool_t * pool)

+{

+    http_response_t * res;

+    res = (http_response_t*)pool_pcalloc(pool, sizeof(http_response_t));

+	if(NULL == res)

+	{

+	    cwmp_log_error("res pool_pcalloc return NULL");

+		*response = res;

+		return CWMP_ERROR;

+	}

+

+	

+    res->parser = (http_parser_t*)pool_pcalloc(pool, sizeof(http_parser_t));

+	if(NULL == res->parser)

+	{

+	    cwmp_log_error("res->parser pool_pcalloc return NULL");

+		*response = res;

+		return CWMP_ERROR;

+	}

+

+    *response = res;

+

+    return CWMP_OK;

+}

+

+

+int http_parse_cookie(const char * cookie, char * dest_cookie)

+{

+    char data[MIN_BUFFER_LEN+1] = {0};

+    char * s ;

+    char buffer[128];

+    char * end;

+

+    FUNCTION_TRACE();

+

+    if (!cookie)

+        return CWMP_ERROR;

+

+    for (s =  (char*)cookie; isspace(*s); s++);

+

+

+    TRstrncpy(dest_cookie, s, MIN_BUFFER_LEN);

+

+    return CWMP_OK;

+

+}

+

+

+void http_parse_key_value(char ** from, char *to, int len, int shift)

+{

+    int n;

+    char fmt[20];

+    char *p = *from + shift;

+

+    *from = p;

+

+    if (*p == '"')//notice that '"' is not two " ,but ' and " and ',Jeff Sun - Jul.24.2005

+    {

+        TRsnprintf(fmt, sizeof(fmt), "%%%d[^\"]%%n", len - 1);

+        p++;

+    }

+    else

+    {

+        TRsnprintf(fmt, sizeof(fmt), "%%%d[^ \t,]%%n", len - 1);

+    }

+

+    if (sscanf(p, fmt, to, &n))

+    {

+        if(n > strlen(p))

+        {

+            cwmp_log_info("n is out of string len");

+			return;

+        }

+        p += n;		

+        *from = p;

+    }

+}

+

+

+

+int http_parse_url(http_dest_t * dest, const char * url)

+{

+    char *d;

+    const char *p, *q;

+    const char * uri;

+    int i;

+

+    /* allocate struct url */

+    //char urlbuf[1024] = {0};

+    //strncpy(urlbuf, url, strlen(url));

+    FUNCTION_TRACE();

+

+	cwmp_log_info("url:%s", url);

+	

+    uri = url;

+    /* scheme name */

+    if ((p = strstr(url, ":/")))

+    {

+        TRsnprintf(dest->scheme, URL_SCHEME_LEN+1,

+                   "%.*s", (int)(p - uri), uri);

+        uri = ++p;

+        /*

+         * Only one slash: no host, leave slash as part of document

+         * Two slashes: host follows, strip slashes

+         */

+        if (uri[1] == '/')

+            uri = (p += 2);

+    }

+    else

+    {

+        p = uri;

+    }

+    if (!*uri || *uri == '/' || *uri == '.')

+        goto nohost;

+

+    p = strpbrk(uri, "/@");

+    if (p && *p == '@')

+    {

+        /* username */

+        for (q = uri, i = 0; (*q != ':') && (*q != '@'); q++)

+            if (i < URL_USER_LEN)

+            {

+                dest->user[i++] = *q;

+            }

+

+        /* password */

+        if (*q == ':')

+            for (q++, i = 0; (*q != ':') && (*q != '@'); q++)

+                if (i < URL_PWD_LEN)

+                {

+                    dest->password[i++] = *q;

+                }

+

+        p++;

+    }

+    else

+    {

+        p = uri;

+    }

+

+    /* hostname */

+//#ifdef INET6

+    if (*p == '[' && (q = strchr(p + 1, ']')) != NULL &&

+            (*++q == '\0' || *q == '/' || *q == ':'))

+    {

+        if ((i = q - p - 2) > MAX_HOST_NAME_LEN)

+        {

+            i = MAX_HOST_NAME_LEN;

+        }

+        memset(dest->host, 0, sizeof(dest->host));

+        memcpy(dest->host, ++p, i);

+        p = q;

+    }

+    else

+//#endif

+   {

+        memset(dest->host, 0, MAX_HOST_NAME_LEN+1);

+        for (i = 0; *p && (*p != '/') && (*p != ':'); p++)

+        if (i < MAX_HOST_NAME_LEN)

+        {

+            dest->host[i++] = *p;

+        }

+   }

+

+

+    /* port */

+    if(strncmp(url, "https:", 6) == 0)

+    {

+        dest->port = 443;    

+    }

+    else

+    {

+        dest->port = 80;

+    }

+    if (*p == ':')

+    {

+        dest->port = 0;

+        for (q = ++p; *q && (*q != '/'); q++)

+            if (isdigit(*q))

+                dest->port = dest->port * 10 + (*q - '0');

+            else

+            {

+                /* invalid port */

+                goto outoff;

+            }

+        p = q;

+    }

+

+nohost:

+    /* document */

+    if (!*p)

+        p = "/";

+

+    if (TRstrcasecmp(dest->scheme, "http") == 0 ||

+            TRstrcasecmp(dest->scheme, "https") == 0)

+    {

+        const char hexnums[] = "0123456789abcdef";

+        d = dest->uri;

+        while (*p != '\0')

+        {

+            if (!isspace(*p))

+            {

+                *d++ = *p++;

+            }

+            else

+            {

+                *d++ = '%';

+                *d++ = hexnums[((unsigned int)*p) >> 4];

+                *d++ = hexnums[((unsigned int)*p) & 0xf];

+                p++;

+            }

+        }

+        *d = '\0';

+    }

+    else

+    {

+        //strncpy(d, p, MAX_URI_LEN);

+    }

+

+    cwmp_log_debug(

+        "scheme:   [%s]\n"

+        "user:     [%s]\n"

+        "password: [%s]\n"

+        "host:     [%s]\n"

+        "port:     [%d]\n"

+        "uri: [%s]\n",

+        dest->scheme, dest->user, dest->password,

+        dest->host, dest->port, dest->uri);	

+

+    return CWMP_OK;

+

+outoff:

+    cwmp_log_error("parse url error.\n");

+    return CWMP_ERROR;

+}

+

+

+int http_get_host_type_by_url(char *url)

+{

+    http_dest_t d= {0};

+    

+    http_parse_url(&d, url);

+    

+    cwmp_log_info("url:%s, host:%s", url, d.host);

+    

+    struct hostent *host = NULL;

+    host  = gethostbyname2(d.host, AF_INET6);

+    cwmp_log_info("AF_INET6:%p", host);

+    if(host)

+    {

+         return AF_INET6;

+    }

+

+    host  = gethostbyname2(d.host, AF_INET);

+    cwmp_log_info("AF_INET4:%p", host);

+    if(host)

+    {

+         return AF_INET;

+    }

+

+    return -1;

+}

+

+

+int http_get_host_by_url(char *url, int family)

+{

+    http_dest_t d= {0};

+    

+    http_parse_url(&d, url);

+    

+    cwmp_log_info("url:%s, host:%s", url, d.host);

+    

+    struct hostent *host = NULL;

+    host  = gethostbyname2(d.host, family);

+    if(host)

+    {

+         return CWMP_OK;

+    }

+

+    return CWMP_ERROR;

+}

+

+

+static int http_split_headers(char *data, unsigned long len, char **line)

+{

+    int lines = 0;

+    unsigned long i;

+

+    //FUNCTION_TRACE();

+

+    line[lines] = data;

+    for (i = 0; i < len && lines < MAX_HEADERS; i++)

+    {

+        if (data[i] == '\r')

+        {

+            data[i] = '\0';

+        }

+

+		if (data[i] == '\n')

+        {

+            lines++;

+            data[i] = '\0';

+            if (lines >= MAX_HEADERS)

+            {

+                return MAX_HEADERS;

+            }

+			

+            if (i + 1 < len)

+            {

+                if (data[i + 1] == '\n' || data[i + 1] == '\r')

+                {

+                    break;

+                }	

+                line[lines] = &data[i + 1];

+            }

+

+        }

+    }

+

+    i++;

+    while (i < len && data[i] == '\n') i++;

+

+    return lines;

+}

+

+

+

+

+

+static void http_parse_headers(http_parser_t * parser, char **line, int lines, pool_t * pool)

+{

+    int i,l;

+    int whitespace, where, slen;

+    char *name = NULL;

+    char *value = NULL;

+

+    //FUNCTION_TRACE();

+

+    /* parse the name: value lines. */

+    for (l = 1; l < lines; l++)

+    {

+        where = 0;

+        whitespace = 0;

+        name = line[l];

+        value = NULL;

+        slen = strlen(line[l]);

+        for (i = 0; i < slen; i++)

+        {

+            if (line[l][i] == ':')

+            {

+                whitespace = 1;

+                line[l][i] = '\0';

+            }

+            else

+            {

+                if (whitespace)

+                {

+                    whitespace = 0;

+                    while (i < slen && line[l][i] == ' ')

+                        i++;

+

+                    if (i < slen)

+                        value = &line[l][i];

+

+                    break;

+                }

+            }

+        }

+

+        if (name != NULL && value != NULL)

+        {            

+            http_set_variable(parser, name, value, pool);

+            name = NULL;

+            value = NULL;

+        }

+    }

+}

+

+int http_read_line(http_socket_t * sock, char * buffer, int max)

+{

+    char c;

+

+    int i=0;

+    while (i < max)

+    {

+

+        if ( http_socket_read(sock, &c, 1) <= 0 )

+        {

+            cwmp_log_error("recv, CANNOT READ 1 char");

+            return CWMP_ERROR;

+        };

+

+        buffer[i++]=c;

+

+        if (c=='\r')  // GOT CR

+        {

+            if ( http_socket_read(sock, &c, 1) < 0 )

+            {

+                cwmp_log_error("http_socket_read fail");

+				return CWMP_ERROR;

+            };

+

+            buffer[i++]=c;

+            break ;

+        }

+    }

+    if (i >= max)

+    {

+        cwmp_log_error("i(%d) > max(%d)", i, max);

+		cwmp_log_info("alread read info:%s", buffer);

+        return CWMP_ERROR;

+    }

+

+    buffer[i] = 0;

+    return i;

+}

+

+int http_read_header(http_socket_t * sock, cwmp_chunk_t * header, pool_t * pool)

+{

+    char buffer[1024];

+    int rc, bytes;

+

+    FUNCTION_TRACE();

+    bytes = 0;

+    for (;;)

+    {

+        rc = http_read_line(sock, buffer, 1023);

+        if (rc < 0)

+        {

+            cwmp_log_error("http_read_line rc:%d", rc);

+            return rc;

+        }

+		buffer[rc] = 0;       

+	//cwmp_log_debug("%s", buffer);

+        cwmp_chunk_write_string(header, buffer, rc, pool);

+        bytes += rc;

+        if (buffer[0] == '\r' && buffer[1] == '\n')

+        {

+            break;

+        }

+    }

+

+    return bytes;

+

+}

+

+

+int http_read_body(http_socket_t * sock, int max)//, cwmp_chunk_t * body, pool_t * pool)

+{

+    int bytes = 0;

+    int len;

+    char buffer[512];

+	int read_len = 0;

+

+	FUNCTION_TRACE();

+

+	cwmp_log_info("max len:%d", max);

+	cwmp_log_info("-------------------------");

+

+    while (bytes < max)

+    {

+        read_len = (max-bytes) >= 512 ? 512 : (max-bytes);

+		len = http_socket_read(sock, buffer, read_len);

+		

+        if ( len < 0 )

+        {

+            cwmp_log_error("recv, CANNOT READ 512 chars");

+            return CWMP_ERROR;

+        }

+		else if (len == 0)

+        {

+            return bytes;

+        }

+

+        //memcpy(b, buffer, len);

+

+        if(sock->write_callback)

+        {

+            (*sock->write_callback)(buffer, 1, len, sock->write_calldata);

+        }

+		else

+		{

+		    cwmp_log_info("%s", buffer);

+		}

+

+        bytes += len;

+    }

+

+	cwmp_log_info("-------------------------");

+

+    cwmp_log_info("read len:%d", bytes);

+

+    return bytes;

+

+}

+

+int http_read_request(http_socket_t * sock, http_request_t * request, pool_t * pool)

+{

+    int rc;

+    cwmp_chunk_t * header;

+    char *line[MAX_HEADERS]; /* limited to 64 lines, should be more than enough */

+

+    int lines, len;

+    size_t	bytes;

+    char *req_type = NULL;

+    char *uri = NULL;

+    char *version = NULL;

+    int whitespace, wheres, slen;

+    int i;

+    http_parser_t * parser;

+    char data[2048];

+

+

+

+    FUNCTION_TRACE();

+    bytes = 0;

+    parser = request->parser;

+    rc = cwmp_chunk_create(&header, pool);

+	if(CWMP_OK != rc)

+	{

+	    cwmp_log_error("cwmp_chunk_create fail:%d", rc);

+		return CWMP_ERROR;

+	}

+

+    rc = http_read_header(sock, header, pool);

+    if (rc <= 0)

+    {

+        cwmp_log_error("http_read_header return rc:%d", rc);

+        return rc;

+    }

+

+

+

+    len = cwmp_chunk_copy(data, header, 2047);

+    cwmp_log_debug("http read request: %s\n", data);

+    bytes += len;

+    lines = http_split_headers(data, len, line);

+

+

+    wheres = 0;

+    whitespace = 0;

+    slen = strlen(line[0]);

+    req_type = line[0];

+    for (i = 0; i < slen; i++)

+    {

+        if (line[0][i] == ' ')

+        {

+            whitespace = 1;

+            line[0][i] = '\0';

+        }

+        else

+        {

+            /* we're just past the whitespace boundry */

+            if (whitespace)

+            {

+                whitespace = 0;

+                wheres++;

+                switch (wheres)

+                {

+                case 1:

+                    uri = &line[0][i];

+                    break;

+                case 2:

+                    version = &line[0][i];

+                    break;

+                }

+            }

+        }

+    }

+

+    if (TRstrcasecmp("GET", req_type) == 0)

+    {

+        request->method = HTTP_GET;

+    }

+    else if (TRstrcasecmp("POST", req_type) == 0)

+    {

+        request->method = HTTP_POST;

+    }

+    else if (TRstrcasecmp("HEAD", req_type) == 0)

+    {

+        request->method = HTTP_HEAD;

+    }

+    else

+    {

+        request->method = HTTP_UNKNOWN;

+    }

+

+

+    http_parse_headers(parser, line, lines, pool);

+

+    return bytes;

+

+#if 0

+    cwmp_chunk_t header;

+    cwmp_chunk_t body;

+    int rc;

+

+    char *tmp;

+    char *line[MAX_HEADERS]; /* limited to 32 lines, should be more than enough */

+    int i;

+    int lines;

+    char *req_type = NULL;

+    char *uri = NULL;

+    char *version = NULL;

+    int whitespace, where, slen;

+

+

+    rc = http_read_header(sock, &header);

+    if (rc <= 0)

+    {

+        return CWMP_ERROR;

+    }

+

+    lines = http_split_headers(data, len, line);

+

+    where = 0;

+    whitespace = 0;

+    slen = strlen(line[0]);

+    req_type = line[0];

+    for (i = 0; i < slen; i++)

+    {

+        if (line[0][i] == ' ')

+        {

+            whitespace = 1;

+            line[0][i] = '\0';

+        }

+        else

+        {

+            /* we're just past the whitespace boundry */

+            if (whitespace)

+            {

+                whitespace = 0;

+                where++;

+                switch (where)

+                {

+                case 1:

+                    uri = &line[0][i];

+                    break;

+                case 2:

+                    version = &line[0][i];

+                    break;

+                }

+            }

+        }

+    }

+

+    http_parse_headers(request->parser,

+#endif

+

+}

+

+int http_parse_request(http_request_t * request, char *data, unsigned long len)

+{

+    char *line[MAX_HEADERS]; /* limited to 32 lines, should be more than enough */

+    int i;

+    int lines;

+    char *req_type = NULL;

+    char *uri = NULL;

+    char *version = NULL;

+    int whitespace, where, slen;

+

+    if (data == NULL)

+        return 0;

+

+    /* make a local copy of the data, including 0 terminator */

+    //data = (char *)malloc(len+1);

+    //if (data == NULL) return 0;

+    //memcpy(data, http_data, len);

+    //data[len] = 0;

+

+    lines = http_split_headers(data, len, line);

+

+    /* parse the first line special

+    ** the format is:

+    ** REQ_TYPE URI VERSION

+    ** eg:

+    ** GET /index.html HTTP/1.0

+    */

+    where = 0;

+    whitespace = 0;

+    slen = strlen(line[0]);

+    req_type = line[0];

+    for (i = 0; i < slen; i++)

+    {

+        if (line[0][i] == ' ')

+        {

+            whitespace = 1;

+            line[0][i] = '\0';

+        }

+        else

+        {

+            /* we're just past the whitespace boundry */

+            if (whitespace)

+            {

+                whitespace = 0;

+                where++;

+                switch (where)

+                {

+                case 1:

+                    uri = &line[0][i];

+                    break;

+                case 2:

+                    version = &line[0][i];

+                    break;

+                }

+            }

+        }

+    }

+

+#if 0

+    if (strcasecmp("GET", req_type) == 0)

+    {

+        parser->req_type = httpp_req_get;

+    }

+    else if (strcasecmp("POST", req_type) == 0)

+    {

+        parser->req_type = httpp_req_post;

+    }

+    else if (strcasecmp("HEAD", req_type) == 0)

+    {

+        parser->req_type = httpp_req_head;

+    }

+    else if (strcasecmp("SOURCE", req_type) == 0)

+    {

+        parser->req_type = httpp_req_source;

+    }

+    else if (strcasecmp("PLAY", req_type) == 0)

+    {

+        parser->req_type = httpp_req_play;

+    }

+    else if (strcasecmp("STATS", req_type) == 0)

+    {

+        parser->req_type = httpp_req_stats;

+    }

+    else

+    {

+        parser->req_type = httpp_req_unknown;

+    }

+

+    if (uri != NULL && strlen(uri) > 0)

+    {

+        char *query;

+        if ((query = strchr(uri, '?')) != NULL)

+        {

+            http_set_variable(parser, HTTPP_VAR_RAWURI, uri);

+            *query = 0;

+            query++;

+            parse_query(parser, query);

+        }

+

+        parser->uri = strdup(uri);

+    }

+    else

+    {

+        free(data);

+        return 0;

+    }

+

+    if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL))

+    {

+        tmp[0] = '\0';

+        if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0))

+        {

+            http_set_variable(parser, HTTPP_VAR_PROTOCOL, version);

+            http_set_variable(parser, HTTPP_VAR_VERSION, &tmp[1]);

+        }

+        else

+        {

+            free(data);

+            return 0;

+        }

+    }

+    else

+    {

+        free(data);

+        return 0;

+    }

+

+    if (parser->req_type != httpp_req_none && parser->req_type != httpp_req_unknown)

+    {

+        switch (parser->req_type)

+        {

+        case httpp_req_get:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "GET");

+            break;

+        case httpp_req_post:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "POST");

+            break;

+        case httpp_req_head:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "HEAD");

+            break;

+        case httpp_req_source:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");

+            break;

+        case httpp_req_play:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "PLAY");

+            break;

+        case httpp_req_stats:

+            http_set_variable(parser, HTTPP_VAR_REQ_TYPE, "STATS");

+            break;

+        default:

+            break;

+        }

+    }

+    else

+    {

+        free(data);

+        return 0;

+    }

+

+    if (parser->uri != NULL)

+    {

+        http_set_variable(parser, HTTPP_VAR_URI, parser->uri);

+    }

+    else

+    {

+        free(data);

+        return 0;

+    }

+

+    parse_headers(parser, line, lines);

+

+    free(data);

+#endif

+

+    return 1;

+}

+

+int http_read_response(http_socket_t * sock, http_response_t * response, pool_t * pool)

+{

+    char *line[MAX_HEADERS];

+    int lines, slen,i, whitespace=0, where=0,code;

+    char *version=NULL, *resp_code=NULL, *message=NULL;

+

+    cwmp_chunk_t *header;

+    //cwmp_chunk_t body;

+    int rc;

+    size_t len;

+

+    char * data;

+    char * ctxlen;

+    size_t cont_len;

+

+	char * transfer_encoding;

+	char * content_type;

+	int chunk_flag = 0;

+	int type_flag = 0;

+

+	FUNCTION_TRACE();

+

+    rc = cwmp_chunk_create(&header, pool);

+    if (CWMP_OK != rc)

+    {

+        cwmp_log_error("cwmp_chunk_create fail, rc = %d", rc);

+		return CWMP_ERROR;

+    }

+	

+    rc = http_read_header(sock, header, pool);

+    if (rc <= 0)

+    {

+        cwmp_log_error("http read header fail, rc = %d", rc);

+		return CWMP_ERROR;

+    }

+

+    len = cwmp_chunk_length(header);

+

+    data = pool_pcalloc(pool, len + 1);

+    if(NULL == data)

+    {

+        cwmp_log_error("data pool_pcalloc return null");

+		return CWMP_ERROR;

+    }

+	

+    cwmp_chunk_copy(data, header, len);

+

+    data[len] = 0;

+

+    cwmp_log_debug("http read header length: %d, [\n%s\n]", len, data);

+

+    lines = http_split_headers(data, len, line);

+

+    /* In this case, the first line contains:

+     * VERSION RESPONSE_CODE MESSAGE, such as HTTP/1.0 200 OK

+     */

+    slen = strlen(line[0]);

+    version = line[0];

+    for (i=0; i < slen; i++)

+    {

+        if (line[0][i] == ' ')

+        {

+            line[0][i] = 0;

+            whitespace = 1;

+        }

+        else if (whitespace)

+        {

+            whitespace = 0;

+            where++;

+            if (where == 1)

+            {

+                resp_code = &line[0][i];

+            }

+			else

+            {

+                message = &line[0][i];

+                break;

+            }

+        }

+    }

+

+

+    //if (version == NULL || resp_code == NULL || message == NULL)

+    if (version == NULL || resp_code == NULL)

+    {

+        cwmp_log_error("version:%p, resp_code:%p, message:%p", version, resp_code, message);

+		return CWMP_ERROR;

+    }

+

+

+

+    http_set_variable(response->parser, HTTPP_VAR_ERROR_CODE, resp_code, pool);

+    code = TRatoi(resp_code);

+    response->status = code;

+    if (code < 200 || code >= 300)

+    {

+        cwmp_log_info("response code:%s->%d", resp_code, code);

+		http_set_variable(response->parser, HTTPP_VAR_ERROR_MESSAGE, message, pool);

+    }

+

+    //http_set_variable(response->parser, HTTPP_VAR_URI, uri);

+    http_set_variable(response->parser, HTTPP_VAR_REQ_TYPE, "NONE", pool);

+

+    http_parse_headers(response->parser, line, lines, pool);

+

+    ctxlen = http_get_variable(response->parser, "Content-Length");

+

+    cwmp_log_info("ctrlen:%s", ctxlen);

+	

+    cont_len = 0;

+    if (ctxlen)

+    {

+        cont_len = TRatoi(ctxlen);

+    }

+

+#if 0

+    rc = http_read_body(sock, cont_len);//, &body, pool);

+    if (rc < 0 || code != 200)

+    {

+    	cwmp_log_info("Http read response rc is:%d, code is (%d)\n", rc, code); 	   

+    }

+

+#else

+

+    transfer_encoding =  http_get_variable(response->parser, "Transfer-Encoding");

+	if(transfer_encoding)

+	{

+	    if(TRstrcmp(transfer_encoding, "chunked") == 0)

+	    {

+	        cwmp_log_info("chunked message");

+	        chunk_flag = 1;   

+	    }

+	}

+

+

+    content_type =  http_get_variable(response->parser, "Content-Disposition");

+

+	if(content_type)

+	{

+	    cwmp_log_info("Content-Type message:%s", content_type);

+	    type_flag = 1;   

+	}

+

+	if(chunk_flag == 1 && type_flag == 1)

+	{

+        rc = http_read_body_chunked(sock);

+        if (rc < 0 || code != 200)

+        {

+            cwmp_log_info("Http read chunked response rc is:%d, code is (%d)\n", rc, code);        

+        }

+

+		return code;

+	}

+	else

+	{	

+        if(cont_len <= 0 || cont_len > 0x7FFFFFFF)

+        {

+            cwmp_log_error("cont len is invalid:%d", cont_len);

+            return code;

+        }

+	

+        rc = http_read_body(sock, cont_len);//, &body, pool);

+        if (rc < 0 || code != 200)

+        {

+            cwmp_log_info("Http read response rc is:%d, code is (%d)\n", rc, code);        

+        }

+	}

+	

+#endif

+

+	return code; 

+}

+

+//#define http_set_variable(header, name, value)  http_set_var( &header, name, value)

+

+char * http_method(int method)

+{

+    switch (method)

+    {

+    case HTTP_POST:

+        return "POST";

+    case HTTP_PUT:

+        return "PUT";

+    default:

+        return "GET";

+    };

+

+//    return "GET";

+}

+

+

+

+

+/* calculate H(A1) as per spec */

+

+void http_digest_calc_ha1(

+        const char *pszAlg,

+        const char *pszUserName,

+        const char *pszRealm,

+        const char *pszPassword,

+        const char *pszNonce,

+        const char *pszCNonce,

+        char *SessionKey)

+{

+    cwmp_MD5_CTX Md5Ctx;

+    char HA1[HASHLEN];

+

+    cwmp_MD5Init(&Md5Ctx);

+    cwmp_MD5Update(&Md5Ctx, (unsigned char *)pszUserName, strlen(pszUserName));

+    cwmp_MD5Update(&Md5Ctx, (unsigned char *)":", 1);

+    cwmp_MD5Update(&Md5Ctx, (unsigned char *)pszRealm, strlen(pszRealm));

+    cwmp_MD5Update(&Md5Ctx, (unsigned char *)":", 1);

+    cwmp_MD5Update(&Md5Ctx, (unsigned char *)pszPassword, strlen(pszPassword));

+    cwmp_MD5Final((unsigned char *)HA1, &Md5Ctx);

+    if (TRstrcasecmp(pszAlg, "md5-sess") == 0)

+    {

+        cwmp_log_info("md5-sess enter");

+        cwmp_MD5Init(&Md5Ctx);

+        cwmp_MD5Update(&Md5Ctx, (unsigned char *)HA1, HASHLEN);

+        cwmp_MD5Update(&Md5Ctx, (unsigned char *)":", 1);

+        cwmp_MD5Update(&Md5Ctx, (unsigned char *)pszNonce, strlen(pszNonce));

+        cwmp_MD5Update(&Md5Ctx, (unsigned char *)":", 1);

+        cwmp_MD5Update(&Md5Ctx, (unsigned char *)pszCNonce, strlen(pszCNonce));

+        cwmp_MD5Final((unsigned char *)HA1, &Md5Ctx);

+    };

+    convert_to_hex(HA1, SessionKey);

+};

+

+

+/***********************************************************************

+***********************************************************************/

+

+#if 1

+

+int http_check_digest_auth(const char * auth_realm, const char * auth, char * cpe_user, char * cpe_pwd)

+{

+    char data[512] = {0};

+    char * s ;

+//    char buffer[128];

+	char		realm[256] = {0};

+    char		user[256] = {0}; /*CDRouter will test largest size ConnectionRequest Username*/

+    char		uri[256] = {0};//uri[32768]

+    char		cnonce[33] = {0};

+    char		nonce[33] = {0};

+

+    char		qop[16] = {0};

+    char		nc[16] = {0};

+

+    char		response[128] = {0};

+//    char		method[16] = {0};

+//    char		resp[33] = {0};

+

+

+    char ha1[HASHHEXLEN+1];

+    char ha2[HASHHEXLEN+1];

+    char validResponse[HASHHEXLEN+1];

+

+    char * end;

+

+    if (!auth)

+    {

+        cwmp_log_info("auth is %s", auth);

+        return -1;

+    }

+

+    for (s =  (char*)auth; isspace(*s); s++);

+    strncpy(data, s, 511);

+    s = data;

+    if (TRstrncasecmp(s, "digest", 6) != 0)

+    {

+        cwmp_log_info("s is %s, not begin with[digest]", s);

+        return -1;

+    }

+	

+    for (s += 6;  isspace(*s); s++);

+

+    end = s + strlen(s);

+//    memset(buffer, 0, 128);

+    while (s<end && NULL != s)

+    {

+        if (!strncmp(s, "username=", 9))

+        {

+            http_parse_key_value(&s, user, sizeof(user), 9);

+        }

+        else if (! strncmp(s, "nonce=", 6))

+        {

+            http_parse_key_value(&s, nonce, sizeof(nonce), 6);

+        }

+        else if (! strncmp(s, "response=", 9))

+        {

+            http_parse_key_value(&s, response, sizeof(response), 9);

+        }

+        else if (! strncmp(s, "uri=", 4))

+        {

+            http_parse_key_value(&s, uri, sizeof(uri), 4);

+        }

+        else if (! strncmp(s, "qop=", 4))

+        {

+            http_parse_key_value(&s, qop, sizeof(qop), 4);

+        }

+        else if (! strncmp(s, "cnonce=", 7))

+        {

+            http_parse_key_value(&s, cnonce, sizeof(cnonce), 7);

+        }

+        else if (! strncmp(s, "nc=", 3))

+        {

+            http_parse_key_value(&s, nc, sizeof(nc), 3);

+        }

+		else if (! strncmp(s, "realm=", 6))

+		{

+            http_parse_key_value(&s, realm, sizeof(nc), 6);

+		}

+		

+        s++;

+    }

+    cwmp_log_info("user[%s], nonce[%s], response[%s], uri[%s], qop[%s], cnonce[%s], nc[%s]\n",

+                  user, nonce, response, uri, qop, cnonce, nc);

+

+    if (TRstrcmp(cpe_user, user) != 0)

+    {

+        cwmp_log_info("cpe_user [%s] != user:[%s]\n", cpe_user, user);

+		return -1;

+    }

+

+    http_digest_calc_ha1("MD5", cpe_user, realm, cpe_pwd, nonce, cnonce, ha1);

+	cwmp_log_info("ha1:%s", ha1);

+

+    cwmp_MD5(ha2, "GET", ":", uri, NULL);

+    cwmp_MD5(validResponse, ha1, ":", nonce, ":", nc, ":", cnonce, ":", qop, ":", ha2, NULL);

+  

+    if (TRstrcasecmp(validResponse, response) == 0)

+	{

+		cwmp_log_info("auth ok. [%s] [%s]\n", validResponse, response);

+        return 0;

+	}

+    else

+    {

+        cwmp_log_info("auth fail. ha1:[%s], ha2:[%s], validResponse:[%s] response:[%s]\n", ha1, ha2, validResponse, response);

+        return -1;

+    }

+

+    return 0;

+

+}

+

+#else

+

+int http_check_digest_auth(const char * auth_realm, const char * auth, char * cpe_user, char * cpe_pwd)

+{

+    char data[512] = {0};

+    char * s ;

+//    char buffer[128];

+	char		realm[256] = {0};

+    char		user[256] = {0}; /*CDRouter will test largest size ConnectionRequest Username*/

+    char		uri[256] = {0};//uri[32768]

+    char		cnonce[33] = {0};

+    char		nonce[33] = {0};

+

+    char		qop[16] = {0};

+    char		nc[16] = {0};

+	char        pwd[256] = {0};

+

+    char		response[128] = {0};

+//    char		method[16] = {0};

+//    char		resp[33] = {0};

+

+	int auth_type = HTTP_NONE_AUTH;

+

+    char ha1[HASHHEXLEN+1];

+    char ha2[HASHHEXLEN+1];

+    char validResponse[HASHHEXLEN+1];

+	char basic_content[256+1] = {0};

+	char *decoded_basic_content = NULL;

+

+    char * end;

+

+    if (!auth)

+    {

+        cwmp_log_info("auth is %s", auth);

+        return -1;

+    }

+

+    for (s =  (char*)auth; isspace(*s); s++);

+    strncpy(data, s, 511);

+    s = data;

+

+    if (TRstrncasecmp(s, "digest", 6) == 0)

+    {

+        cwmp_log_error("s is:%s, return", s);

+        for (s += 6;  isspace(*s); s++);

+        end = s + strlen(s);

+

+		auth_type = HTTP_DIGEST_AUTH;

+

+		goto auth_digest;

+    }

+	else if(TRstrncasecmp(s, "Basic", 5) == 0)

+	{

+        cwmp_log_error("s is:%s, return", s);

+        for (s += 5;  isspace(*s); s++);

+        end = s + strlen(s);

+

+		auth_type = HTTP_BASIC_AUTH;

+

+		goto auth_basic;

+    }

+	else

+	{

+	    cwmp_log_error("s is:%s, return", s);

+		return -1;

+	}

+

+auth_digest:

+	

+//    memset(buffer, 0, 128);

+    while (s<end && NULL != s)

+    {

+        if (!strncmp(s, "username=", 9))

+        {

+            http_parse_key_value(&s, user, sizeof(user), 9);

+        }

+        else if (! strncmp(s, "nonce=", 6))

+        {

+            http_parse_key_value(&s, nonce, sizeof(nonce), 6);

+        }

+        else if (! strncmp(s, "response=", 9))

+        {

+            http_parse_key_value(&s, response, sizeof(response), 9);

+        }

+        else if (! strncmp(s, "uri=", 4))

+        {

+            http_parse_key_value(&s, uri, sizeof(uri), 4);

+        }

+        else if (! strncmp(s, "qop=", 4))

+        {

+            http_parse_key_value(&s, qop, sizeof(qop), 4);

+        }

+        else if (! strncmp(s, "cnonce=", 7))

+        {

+            http_parse_key_value(&s, cnonce, sizeof(cnonce), 7);

+        }

+        else if (! strncmp(s, "nc=", 3))

+        {

+            http_parse_key_value(&s, nc, sizeof(nc), 3);

+        }

+		else if (! strncmp(s, "realm=", 6))

+		{

+            http_parse_key_value(&s, realm, sizeof(nc), 6);

+		}

+		

+        s++;

+    }

+    cwmp_log_info("user[%s], nonce[%s], response[%s], uri[%s], qop[%s], cnonce[%s], nc[%s]\n",

+                  user, nonce, response, uri, qop, cnonce, nc);

+

+    if (TRstrcmp(cpe_user, user) != 0)

+    {

+        cwmp_log_info("cpe_user [%s] != user:[%s]\n", cpe_user, user);

+		return -1;

+    }

+

+    http_digest_calc_ha1("MD5", cpe_user, realm, cpe_pwd, nonce, cnonce, ha1);

+	cwmp_log_info("ha1:%s", ha1);

+

+    cwmp_MD5(ha2, "GET", ":", uri, NULL);

+    cwmp_MD5(validResponse, ha1, ":", nonce, ":", nc, ":", cnonce, ":", qop, ":", ha2, NULL);

+  

+    if (TRstrcasecmp(validResponse, response) == 0)

+	{

+		cwmp_log_info("auth ok. [%s] [%s]\n", validResponse, response);

+        return 0;

+	}

+    else

+    {

+        cwmp_log_info("auth fail. ha1:[%s], ha2:[%s], validResponse:[%s] response:[%s]\n", ha1, ha2, validResponse, response);

+        return -1;

+    }

+

+    return 0;

+

+

+auth_basic:

+	TRstrncpy(basic_content, s, sizeof(s)-1);

+	decoded_basic_content = base64_decode(basic_content, strlen(basic_content));

+	if(NULL == decoded_basic_content)

+	{

+	    cwmp_log_error("decode basic auth content[%s] fail!", basic_content);

+		return -1;

+	}

+

+	sscanf(decoded_basic_content, "%256s:%256s", user, pwd);

+	

+	cwmp_log_info("src:[%s], decoded:[%s], slipted:user:[%s], passwd:[%s]", basic_content, decoded_basic_content, user, pwd);

+	free(decoded_basic_content);

+	

+    if (TRstrcmp(cpe_user, user) != 0 || TRstrcmp(cpe_pwd, pwd) != 0)

+    {

+        cwmp_log_info("cpe_user:[%s],user:[%s] cpe_pwd:[%s],pwd:[%s]\n", cpe_user, user, cpe_pwd, pwd);

+		return -1;

+    }

+

+	return 0;

+

+}

+#endif

+

+int http_calc_digest_response(const char * user, const char * pwd,

+                const char * realm,

+                const char * nonce,

+                const char * uri,

+                const char * cnonce,

+                const char * nc,

+                const char * qop,

+                char * response)

+{

+    char ha1[HASHHEXLEN+1];

+    char ha2[HASHHEXLEN+1];

+    char valid_response[HASHHEXLEN+1];

+	char valid_response_t[HASHHEXLEN+1];

+    http_digest_calc_ha1("MD5", user, realm, pwd, nonce, cnonce, ha1);

+    cwmp_MD5(ha2, "POST", ":", uri, NULL);

+    cwmp_MD5(valid_response_t, ha1, ":", nonce, ":", nc, ":", cnonce, ":", qop, ":", ha2, NULL);

+    cwmp_MD5(valid_response, ha1, ":", nonce, ":", ha2, NULL);

+

+

+    TRstrncpy(response, valid_response_t, HASHHEXLEN);

+

+	cwmp_log_info("ha1:%s, ha2:%s, response:%s, valid_response_t:%s", ha1, ha2, response, valid_response_t);

+

+    return CWMP_OK;

+}

+

+int http_parse_digest_auth(const char * auth, http_digest_auth_t * digest_auth, int *auth_type)

+{

+    char data[512] = {0};

+    char * s ;

+//    char buffer[128];

+    char * end;

+

+    char		user[256] = {0}; /*CDRouter will test largest size ConnectionRequest Username*/

+    char		uri[256] = {0};//uri[32768]

+    char		nonce[33] = {0};

+    char		cnonce[33] = {0};

+    char        realm[128] = {0};

+

+    char		qop[16] = {0};

+    char		nc[16] = {0};

+

+    char		response[128] = {0};

+

+

+    FUNCTION_TRACE();

+

+    if (!auth)

+    {

+        return CWMP_ERROR;

+    }

+

+    for (s =  (char*)auth; isspace(*s); s++);

+	

+    strncpy(data, s, 511);

+	

+    s = data;

+

+	

+    if (TRstrncasecmp(s, "digest", 6) == 0)

+    {

+        cwmp_log_error("s is:%s, return", s);

+        for (s += 6;  isspace(*s); s++);

+        end = s + strlen(s);

+

+		*auth_type = HTTP_DIGEST_AUTH;

+    }

+	else if(TRstrncasecmp(s, "Basic", 5) == 0)

+	{

+        cwmp_log_error("s is:%s, return", s);

+        for (s += 5;  isspace(*s); s++);

+        end = s + strlen(s);

+

+		*auth_type = HTTP_BASIC_AUTH;

+    }

+	else

+	{

+	    cwmp_log_error("s is:%s, return", s);

+		return -1;

+	}

+	

+

+	

+//    memset(buffer, 0, 128);

+    while (s<end && NULL != s)

+    {

+        if (!strncmp(s, "realm=", 6))

+        {

+            http_parse_key_value(&s, realm, sizeof(realm), 6);

+        }

+        else if (! strncmp(s, "nonce=", 6))

+        {

+		    http_parse_key_value(&s, nonce, sizeof(nonce), 6);

+        }

+        else if (! strncmp(s, "response=", 9))

+        {

+            http_parse_key_value(&s, response, sizeof(response), 9);

+        }

+        else if (! strncmp(s, "uri=", 4))

+        {

+            http_parse_key_value(&s, uri, sizeof(uri), 4);

+        }	

+        else if (! strncmp(s, "qop=", 4))

+        {

+            http_parse_key_value(&s, qop, sizeof(qop), 4);

+        }	

+        else if (! strncmp(s, "cnonce=", 7))

+        {

+            http_parse_key_value(&s, cnonce, sizeof(cnonce), 7);

+        }

+        else if (! strncmp(s, "nc=", 3))

+        {

+            http_parse_key_value(&s, nc, sizeof(nc), 3);

+        }

+        else if (! strncmp(s, "domain=", 7))

+        {

+            http_parse_key_value(&s, uri, sizeof(uri), 7);

+        }

+		

+        s++;

+    }

+	

+    cwmp_log_info("user[%s], realm[%s], nonce[%s], response[%s], uri[%s], qop[%s], cnonce[%s], nc[%s]\n",

+                  user, realm, nonce, response, uri, qop, cnonce, nc);

+

+    TRstrncpy(digest_auth->realm, realm, MIN_DEFAULT_LEN);

+    TRstrncpy(digest_auth->nonce, nonce, MIN_DEFAULT_LEN);

+    TRstrncpy(digest_auth->uri, uri, MIN_DEFAULT_LEN*4);

+    TRstrncpy(digest_auth->cnonce, cnonce, MIN_DEFAULT_LEN);

+    TRstrncpy(digest_auth->qop, "auth", MIN_DEFAULT_LEN);

+    TRstrncpy(digest_auth->nc, nc, MIN_DEFAULT_LEN);

+

+    return CWMP_OK;

+}

+

+

+static int	  acs_g_AuthNonce = 0;

+

+

+static void get_acs_nc_cnonce(char * nc, int nc_len, char *cnonce, int cnonce_len)

+{

+	char nonce[33] = {0};

+	

+	char buffer[256] = {0};

+

+    acs_g_AuthNonce++;

+	

+	TRsnprintf(nc, nc_len,  "%08x", acs_g_AuthNonce);	

+	TRsnprintf(buffer, 256,  "%d", acs_g_AuthNonce);

+	cwmp_MD5(nonce, buffer, NULL);

+	

+	nonce[32] = 0;

+

+	TRstrncpy(cnonce, nonce, cnonce_len);

+

+}

+

+

+int http_write_request(http_socket_t * sock , http_request_t * request, cwmp_chunk_t * chunk, pool_t * pool)

+{

+    char buffer[HTTP_DEFAULT_LEN+1];

+    char * data;

+

+    size_t len1     = 0 ;

+    size_t len2     = 0;

+    size_t totallen = 0;

+

+    const char * header_fmt_4 =

+    	"%s %s HTTP/1.1\r\n"

+    	"Host: %s:%d\r\n"

+    	"User-Agent: %s\r\n"

+    	"Accept: */*\r\n"

+    	"Content-Type: text/xml; charset=utf-8\r\n"

+    	"Content-Length: %d\r\n"

+    	"Connection:Keep-Alive\r\n"

+    	;

+

+    const char * header_fmt_6 =

+    	"%s %s HTTP/1.1\r\n"

+    	"Host: [%s]:%d\r\n"

+    	"User-Agent: %s\r\n"

+    	"Accept: */*\r\n"

+    	"Content-Type: text/xml; charset=utf-8\r\n"

+    	"Content-Length: %d\r\n"

+    	"Connection:Keep-Alive\r\n"

+    	;        

+

+    int family = -1;

+

+    const char * auth_fmt = "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", algorithm=\"MD5\", cnonce=\"%s\", nc=%s, qop=\"%s\"\r\n";

+	const char * auth_basic = "Authorization: Basic %s\r\n";

+	const char * basic_autn_info = "%s:%s";

+	char userinfo[HTTP_DEFAULT_LEN+1] = {0};

+	char *encoded_userinfo = NULL;

+

+    http_dest_t * dest = request->dest;

+

+	FUNCTION_TRACE();

+

+    len2 = cwmp_chunk_length(chunk);

+

+    family = get_family_type();

+

+    if(family == AF_INET6)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_6,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent",

+                        len2); 

+

+    }

+    else if(family == AF_INET)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_4,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent",

+                        len2); 

+

+    }

+    else

+    {

+        cwmp_log_error("unsupport family type:%d", family);

+    }                    

+

+

+    cwmp_log_info("dest->auth.active:%d, dest->auth_type:%d, len2:%d", dest->auth.active, dest->auth_type, len2);

+	cwmp_log_info("dest->user:%s, dest->password:%s,dest->auth.realm:%s, dest->auth.nonce:%s, dest->auth.uri:%s, dest->auth.cnonce:%s, dest->auth.nc:%s, dest->auth.qop:%s, dest->auth.response:%s", dest->user, dest->password,

+                    dest->auth.realm, dest->auth.nonce, dest->auth.uri, dest->auth.cnonce, dest->auth.nc, dest->auth.qop, dest->auth.response);

+	

+    if(/*(dest->auth.active == CWMP_FALSE) && */ (dest->auth_type == HTTP_DIGEST_AUTH) &&(0 != TRstrcmp(dest->auth.realm, "")))

+    {

+        cwmp_log_info("enter digest auth");

+			    

+	    char nc[9] = {0};		

+		char cnonce[33] = {0};

+

+		get_acs_nc_cnonce(nc, 9, cnonce, 33);

+

+		cnonce[32] = '\0';

+

+        http_calc_digest_response(dest->user, dest->password,

+          		  dest->auth.realm, dest->auth.nonce, dest->uri, cnonce, nc, "auth", dest->auth.response);

+          

+          

+        len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, auth_fmt,

+          				  dest->user,

+          				  dest->auth.realm, dest->auth.nonce,

+          				  dest->uri, dest->auth.response,

+          				  cnonce, nc, "auth"

+          				  );

+		

+    }

+	else if(dest->auth_type == HTTP_BASIC_AUTH)

+	{

+	    cwmp_log_info("enter basic auth");

+	    TRsnprintf(userinfo, HTTP_DEFAULT_LEN, basic_autn_info, dest->user, dest->password);

+		encoded_userinfo = base64_encode(userinfo, TRstrlen(userinfo));

+		len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, auth_basic,

+          				  encoded_userinfo);

+		free(encoded_userinfo);

+	}

+

+    if(dest->cookie[0] != '\0')

+    {

+        cwmp_log_info("dest->cookie:%s", dest->cookie);

+        len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, "Cookie: %s\r\n",

+                    dest->cookie);

+    }

+

+    len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, "\r\n");

+

+    len1 = TRstrlen(buffer);

+

+    if(len2 > 0)

+    {

+        data = (char *)pool_palloc(pool, len1 + len2 + 1);

+		if(NULL == data)

+		{

+		    cwmp_log_error("data pool_palloc return null");

+		    return -1;

+		}

+        TRstrncpy(data, buffer, len1);

+        cwmp_chunk_copy(data+len1,chunk,  len2);

+    }

+    else

+    {

+        data = buffer;

+    }

+

+    return http_socket_write(sock, data, (int)len1 + len2);

+}

+

+int http_get(http_socket_t * sock, http_request_t * request, cwmp_chunk_t * data, pool_t * pool)

+{

+    request->method = HTTP_GET;

+

+    return http_write_request(sock, request, data, pool);

+}

+

+int http_post(http_socket_t * sock, http_request_t * request, cwmp_chunk_t * data, pool_t * pool)

+{

+    request->method = HTTP_POST;

+

+    return http_write_request(sock, request, data, pool);

+}

+

+size_t http_send_file_callback(char *data, size_t size, size_t nmemb, void * calldata)

+{

+	FILE * tf = (FILE*) calldata;

+

+	return  fread(data, size, nmemb, tf);

+}

+

+

+size_t http_receive_file_callback(char *data, size_t size, size_t nmemb, void * calldata)

+{

+	FILE * tf = (FILE*) calldata;

+    size_t len = 0;

+	int file_no;

+	cwmp_log_info("write file len:%d", size);

+

+	len = fwrite(data, size, nmemb, tf);

+

+	file_no = fileno(tf);

+

+	if(fsync(file_no) < 0) {

+        // todo: cov m

+    } 

+

+	return len;

+}

+

+

+int http_send_file_request(http_socket_t * sock , http_request_t * request, const char  * fromfile, pool_t * pool)

+{

+    char buffer[HTTP_DEFAULT_LEN+1];

+    char * data;

+

+    size_t len1, len2, totallen;

+

+	int rc = CWMP_ERROR;

+    int family = -1;

+

+    const char * header_fmt_4 =

+        "%s %s HTTP/1.1\r\n"

+        "Authorization: Basic ZnRwdXNlcjpmdHB1c2Vy\r\n"

+        "Host: %s:%d\r\n"

+        //"User-Agent: %s\r\n"

+        "Accept: */*\r\n"

+       // "Content-Type: multipart/form-data\r\n"

+        //"Connection: Keep-Alive\r\n"

+        "Content-Length: %d\r\n"

+        "Expect: 100-continue\r\n"

+        ;

+

+    const char * header_fmt_6 =

+        "%s %s HTTP/1.1\r\n"

+        "Authorization: Basic ZnRwdXNlcjpmdHB1c2Vy\r\n"

+        "Host: [%s]:%d\r\n"

+        //"User-Agent: %s\r\n"

+        "Accept: */*\r\n"

+        // "Content-Type: multipart/form-data\r\n"

+        //"Connection: Keep-Alive\r\n"

+        "Content-Length: %d\r\n"

+        "Expect: 100-continue\r\n"

+        ;

+        

+#if 0		

+    const char * auth_fmt = "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"\r\n";

+    //qop=%s, nc=%s, cnonce=\"%s\"

+#else 

+    const char * auth_fmt = "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", algorithm=\"MD5\", cnonce=\"%s\", nc=%s, qop=\"%s\"\r\n";

+#endif

+

+    http_dest_t * dest = request->dest;

+

+    struct stat buf;

+	if(stat(fromfile, &buf)<0)

+	{

+	    cwmp_log_error("send file not exist:%s", fromfile);

+		len2 = 0;

+	}

+	else

+	{

+		 len2 = buf.st_size;

+	}

+

+    FILE *tf = fopen(fromfile, "rb");

+    if(!tf)

+    {

+        cwmp_log_error("open file fail:%s", fromfile);

+	    return CWMP_ERROR;

+    }

+

+    family = get_family_type();

+

+    if(family == AF_INET6)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_6,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent",

+                        len2);

+

+    }

+    else if(family == AF_INET)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_4,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent",

+                        len2);

+

+    }

+    else

+    {

+		cwmp_log_error("unknown family type:%d", family);

+        fclose(tf);

+	    return CWMP_ERROR;    

+    }

+

+

+

+    len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, "\r\n");

+

+

+    if(/*(dest->auth.active == CWMP_FALSE) && (dest->auth_type == HTTP_DIGEST_AUTH) && */(0 != TRstrcmp(dest->auth.realm, "")))

+    {

+        cwmp_log_info("enter auth");

+			    

+	    char nc[9] = {0};		

+		char cnonce[33] = {0};

+

+		get_acs_nc_cnonce(nc, 9, cnonce, 33);

+

+		cnonce[32] = '\0';

+

+        http_calc_digest_response(dest->user, dest->password,

+          		  dest->auth.realm, dest->auth.nonce, dest->uri, cnonce, nc, "auth", dest->auth.response);

+          

+          

+        len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, auth_fmt,

+          				  dest->user,

+          				  dest->auth.realm, dest->auth.nonce,

+          				  dest->uri, dest->auth.response,

+          				  cnonce, nc, "auth"

+          				  );

+		

+    }	

+

+    cwmp_log_debug("SEND: %d[\n%s\n]", len1, buffer);

+

+    http_socket_write(sock, buffer, (int)len1);

+

+	

+    http_response_t * response;

+    rc = http_response_create(&response, pool);

+	if(rc != CWMP_OK)

+	{

+	    cwmp_log_error("http_response_create return:%d", rc);

+		fclose(tf);

+		return CWMP_ERROR;

+	}

+

+    rc = http_read_response(sock, response, pool);

+    if(rc != HTTP_100)

+    {

+		fclose(tf);

+

+		cwmp_log_debug("rc[%d] != 100", rc);

+	    return CWMP_ERROR;

+    }

+

+

+    totallen = len1;

+

+    while(1)

+    {

+    	len2 = fread(buffer, 1, HTTP_DEFAULT_LEN, tf);

+    	if(len2 <= 0)

+    	{

+    		break;

+    	}

+    	len2 = http_socket_write(sock, buffer, (int)len2);

+    	if(len2 <= 0)

+    	{

+    		break;

+    	}

+    	totallen += len2;

+    }

+

+

+	fclose(tf);

+

+    return totallen;

+}

+

+

+int http_send_file(const char * fromfile, const char *tourl )

+{

+	pool_t * pool;

+	http_dest_t *  dest;

+	http_socket_t * sock;

+	http_request_t * request;

+	http_response_t * response;

+    int family  = -1;

+    int rc = -1;

+	

+	pool = pool_create(POOL_DEFAULT_SIZE);

+	if(NULL == pool)

+	{

+	    cwmp_log_error("pool create return null, size: POOL_DEFAULT_SIZE");

+		return CWMP_ERROR;

+	}

+

+    cwmp_log_info("send from:[%s] to url:[%s]", fromfile, tourl);

+	

+	http_dest_create(&dest, tourl, pool);

+    

+    family = get_family_type();

+

+    if(family == -1)

+    {

+        cwmp_log_error("family error:%d.", family);

+        goto out;

+    }

+

+    rc = http_socket_create(&sock, family, SOCK_STREAM, 0, pool);

+    if (rc != CWMP_OK)

+    {

+        cwmp_log_error("http send file: create socket error.");

+        goto out;

+    }

+    

+    rc = http_socket_connect(sock, family, dest->host, dest->port);

+    if(rc != CWMP_OK)

+    {

+         cwmp_log_error("connect to host faild. Host is %s:%d.", dest->host, dest->port);

+         goto out;

+    }

+    

+

+    http_socket_set_recvtimeout(sock, 30);

+	http_request_create(&request, pool);

+    

+	request->dest   = dest;

+    request->method = HTTP_PUT;

+

+    rc = http_send_file_request_chunked(sock, request, fromfile, pool);

+    if(rc <= 0)

+    {

+    	cwmp_log_error("http_send_file_request_chunked rc : %d.", rc);

+    	pool_destroy(pool);

+		return CWMP_ERROR;

+    }

+

+    rc = http_response_create(&response, pool);

+    if(rc != CWMP_OK)

+    {

+    	cwmp_log_error("http_response_create rc : %d.", rc);

+    	pool_destroy(pool);

+		return CWMP_ERROR;

+    }

+

+	rc = http_read_response(sock, response, pool);

+

+    cwmp_log_debug("http_read_response rc is:%d", rc);

+	

+out:

+

+	pool_destroy(pool);

+

+	if(rc != HTTP_200)

+		return CWMP_ERROR;

+	else

+		return CWMP_OK;

+}

+

+int http_receive_file(const char *fromurl, const char * tofile)

+{

+	pool_t * pool;

+	http_dest_t *  dest;

+	http_socket_t * sock;

+	http_request_t * request;

+	http_response_t * response;

+    int family  = -1;

+    int rc = -1;

+

+	FILE * tf = NULL;

+	

+	pool = pool_create(POOL_DEFAULT_SIZE);

+	if(NULL == pool)

+	{

+	    cwmp_log_error("pool create return null, size: POOL_DEFAULT_SIZE");

+		return CWMP_ERROR;

+	}

+

+	

+	http_dest_create(&dest, fromurl, pool);

+

+    family = get_family_type();

+

+    if(family == -1)

+    {

+        cwmp_log_error("family error:%d.", family);

+        goto out;

+    }

+

+    rc = http_socket_create(&sock, family, SOCK_STREAM, 0, pool);

+    if (rc != CWMP_OK)

+    {

+        cwmp_log_error("http receive file: create socket error.");

+        goto out;

+    }

+    

+    rc = http_socket_connect(sock, family, dest->host, dest->port);

+    if(rc != CWMP_OK)

+    {

+        cwmp_log_error("connect to host faild. Host is %s:%d.", dest->host, dest->port);

+        goto out;

+    }

+

+

+	tf = fopen(tofile, "wb+");

+	if(!tf)

+	{

+		cwmp_log_error("create file faild. %s\n", tofile);

+		goto out;

+	}

+

+	http_socket_set_writefunction(sock, http_receive_file_callback, tf);

+    http_socket_set_recvtimeout(sock, 30);

+

+	rc = http_request_create(&request, pool);

+    if(rc != CWMP_OK)

+    {

+        cwmp_log_error("http_request_create fail.");

+        goto out;

+    }

+

+	

+	request->dest = dest;

+	rc = http_get(sock, request, NULL, pool);

+    if(rc <= 0)

+    {

+        cwmp_log_error("http get host faild. Host is %s:%d.", dest->host, dest->port);

+        goto out;

+    }

+

+

+    rc = http_response_create(&response, pool);

+    if(rc != CWMP_OK)

+    {

+        cwmp_log_error("http_response_create fail.");

+        goto out;

+    }	

+

+	rc = http_read_response(sock, response, pool);

+    cwmp_log_info("read response: %d", rc);

+

+	

+out:

+	if(tf)

+	{

+		fclose(tf);

+	}

+	pool_destroy(pool);

+

+	return rc;

+		

+

+	

+}

+

+

+

+

+/****************************************************************************/

+

+

+int htoi(unsigned char *s) 

+{ 

+    int i; 

+    int n = 0; 

+    if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) //???????0x??0X

+    { 

+        i = 2; 

+    } 

+    else 

+    { 

+        i = 0; 

+    } 

+    for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i) 

+    {   

+        if (tolower(s[i]) > '9') 

+        { 

+            n = 16 * n + (10 + tolower(s[i]) - 'a'); 

+        } 

+        else 

+        { 

+            n = 16 * n + (tolower(s[i]) - '0'); 

+        } 

+    } 

+    return n; 

+} 

+

+

+

+int http_read_line_chunk(http_socket_t * sock, char * buffer, int max)

+{

+    char c;

+

+    int i=0;

+    while (i < max)

+    {

+        if ( http_socket_read(sock, &c, 1) <= 0 )

+        {

+            cwmp_log_error("recv, CANNOT READ 1 char");

+            return CWMP_ERROR;

+        };

+

+        buffer[i++]=c;

+

+		cwmp_log_info("get c:%c", c);

+

+        if (c=='\r')  // GOT CR

+        {

+            // ??n

+            if ( http_socket_read(sock, &c, 1) < 0 )

+            {

+                cwmp_log_error("http_socket_read fail");

+				return CWMP_ERROR;

+            }

+			

+			if(c != '\n')

+            {

+                cwmp_log_info("the char after enter is:%d, not n ", c);

+				return CWMP_ERROR;

+            }else

+            {

+                cwmp_log_info("the char after enter is:%d", c);

+                buffer[i++]=c;

+                break;

+			}

+        }

+    }

+    if (i >= max)

+    {

+        cwmp_log_error("i(%d) > max(%d)", i, max);

+        return CWMP_ERROR;

+    }

+

+    buffer[i] = 0;

+    return i;

+

+}

+

+

+

+

+int http_read_body_chunked(http_socket_t * sock)

+{

+    int bytes = 0;

+    int rc;

+    char buf_len[256];

+	int len = 0;

+	char tail[3] = {0};

+	char c;

+

+	FUNCTION_TRACE();

+

+    while (1)

+    {

+        rc = http_read_line_chunk(sock, buf_len, 256);

+        if (rc < 0)

+        {

+            cwmp_log_error("http_read_line_chunk return:%d", rc);

+            return rc;

+        }

+		buf_len[rc] = 0;

+		len = htoi(buf_len);

+

+		cwmp_log_info("%s:%d", buf_len, len);

+

+		if(len == 0)

+		{

+            cwmp_log_info("read content end!");

+			break;

+		}

+

+		rc = http_read_body(sock, len);

+        if (rc <= 0)

+        {

+            cwmp_log_error("read body content fail!");

+            return rc;

+        }

+

+		cwmp_log_info("read len:%d", rc);

+

+        if (http_socket_read(sock, tail, 2) <= 0 )

+        {

+            cwmp_log_error("recv, CANNOT READ 2 char");

+            return CWMP_ERROR;

+        };		

+

+        if(tail[0] != '\r' || tail[1] != '\n')

+        {

+            cwmp_log_error("read len is wrong! %d%d", tail[0], tail[1]);

+		}

+

+		bytes += len;

+    }

+

+    cwmp_log_info("read len:%d", bytes);

+

+    return bytes;

+}

+

+

+

+

+#define MAX_LEN   (10)

+

+

+int http_send_file_request_chunked(http_socket_t * sock , http_request_t * request, const char  * fromfile, pool_t * pool)

+{

+    char buffer[HTTP_DEFAULT_LEN+3];

+    char * data;

+

+    size_t len1, len2, totallen, read_len, file_len;

+	int hex_len;

+

+	int rc = CWMP_ERROR;

+

+	char chunked_len[MAX_LEN + 1];

+    int family = -1;

+

+    const char * header_fmt_6 =

+        "%s %s HTTP/1.1\r\n"

+        "Authorization: Basic ZnRwdXNlcjpmdHB1c2Vy\r\n"

+        "Host: [%s]:%d\r\n"

+        "User-Agent: %s\r\n"

+        "Accept: */*\r\n"

+       // "Content-Type: multipart/form-data\r\n"

+        //"Connection: Keep-Alive\r\n"

+        "Content-Type: application/octet-stream\r\n"

+        "Transfer-Encoding: chunked\r\n"

+        "Expect: 100-continue\r\n"

+        ;

+

+    const char * header_fmt_4 =

+        "%s %s HTTP/1.1\r\n"

+        "Authorization: Basic ZnRwdXNlcjpmdHB1c2Vy\r\n"

+        "Host: %s:%d\r\n"

+        "User-Agent: %s\r\n"

+        "Accept: */*\r\n"

+       // "Content-Type: multipart/form-data\r\n"

+        //"Connection: Keep-Alive\r\n"

+        "Content-Type: application/octet-stream\r\n"

+        "Transfer-Encoding: chunked\r\n"

+        "Expect: 100-continue\r\n"

+        ;        

+#if 0		

+		const char * auth_fmt = "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"\r\n";

+		//qop=%s, nc=%s, cnonce=\"%s\"

+#else 

+		const char * auth_fmt = "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", algorithm=\"MD5\", cnonce=\"%s\", nc=%s, qop=\"%s\"\r\n";

+#endif

+

+

+    http_dest_t * dest = request->dest;

+    family = get_family_type();

+

+    if(family == AF_INET6)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_6,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent");

+

+

+    }

+    else if(family == AF_INET)

+    {

+        len1 = TRsnprintf(buffer, HTTP_DEFAULT_LEN, header_fmt_4,

+                        http_method(request->method),

+                        dest->uri,

+                        dest->host,

+                        dest->port,

+                        "CPE Netcwmp Agent");

+    }

+    else

+    {

+        cwmp_log_error("unsupport family type:%d", family);

+        return CWMP_ERROR;

+    }  

+

+    len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, "\r\n");

+

+    if(/*(dest->auth.active == CWMP_FALSE) && */ (dest->auth_type == HTTP_DIGEST_AUTH) && (0 != TRstrcmp(dest->auth.realm, "")))

+    {

+        cwmp_log_info("enter auth");

+			    

+	    char nc[9] = {0};		

+		char cnonce[33] = {0};

+

+		get_acs_nc_cnonce(nc, 9, cnonce, 33);

+

+		cnonce[32] = '\0';

+

+        http_calc_digest_response(dest->user, dest->password,

+          		  dest->auth.realm, dest->auth.nonce, dest->uri, cnonce, nc, "auth", dest->auth.response);

+          

+          

+        len1 += TRsnprintf(buffer + len1, HTTP_DEFAULT_LEN - len1, auth_fmt,

+          				  dest->user,

+          				  dest->auth.realm, dest->auth.nonce,

+          				  dest->uri, dest->auth.response,

+          				  cnonce, nc, "auth"

+          				  );

+		

+    }	

+

+    cwmp_log_debug("SEND: %d[\n%s\n]", len1, buffer);

+

+    http_socket_write(sock, buffer, (int)len1);

+

+	

+    http_response_t * response;

+    rc = http_response_create(&response, pool);

+	if(CWMP_OK != rc)

+	{

+	    cwmp_log_error("http_response_create return :%d", rc);

+		return CWMP_ERROR;

+	}

+

+    rc = http_read_response(sock, response, pool);

+    if(rc != HTTP_100)

+    {

+		cwmp_log_debug("rc[%d] != 100", rc);

+	    return CWMP_ERROR;

+    }

+

+    totallen = len1;

+

+	cwmp_log_info("begin to send file:%s", fromfile);

+

+    struct stat buf;

+	if(stat(fromfile, &buf)<0)

+	{

+	    cwmp_log_error("send file not exist:%s", fromfile);

+		file_len = 0;

+		return CWMP_ERROR;

+	}

+	else

+	{

+	    file_len = buf.st_size;

+	}

+

+    FILE *tf = fopen(fromfile, "rb");

+    if(!tf)

+    {

+        cwmp_log_error("open file fail:%s", fromfile);

+	    return CWMP_ERROR;

+    }

+

+    while(file_len > 0)

+    {

+        memset(chunked_len, 0, sizeof(chunked_len));

+		memset(buffer, 0, sizeof(buffer));

+		

+    	read_len = fread(buffer, 1, 1024, tf);

+    	if(read_len <= 0)

+    	{

+    		break;

+    	}

+

+		cwmp_log_info("read len:%d", read_len);

+

+        TRsnprintf(chunked_len, MAX_LEN, "%0x\r\n", read_len);

+

+        hex_len = TRstrlen(chunked_len);

+

+        len2 = http_socket_write(sock, chunked_len, hex_len);

+    	if(len2 <= 0)

+    	{

+    	    cwmp_log_error("write len:%d", len2);

+//			if(tf != NULL)

+			{

+				fclose(tf);

+			}

+

+    		return CWMP_ERROR;

+    	}	

+

+		totallen += len2;

+

+        buffer[read_len] = '\r';

+		buffer[read_len+1] = '\n';

+		

+        len2 = http_socket_write(sock, buffer, (int)read_len + 2);

+    	if(len2 <= 0)

+    	{

+    	    cwmp_log_error("write len:%d", len2);

+//			if(tf != NULL)

+			{

+				fclose(tf);

+			}

+

+    		return CWMP_ERROR;

+    	}		

+

+    	totallen += len2;

+

+		file_len -= read_len;

+    }

+

+    len2 = http_socket_write(sock, "0\r\n", 3);

+    if(len2 <= 0)

+    {

+        cwmp_log_error("write len:%d", len2);

+//		if(tf != NULL)

+		{

+			fclose(tf);

+		}

+

+		return CWMP_ERROR;

+    }

+    totallen += 3;	

+

+//    if(tf != NULL)

+    {

+	    fclose(tf);

+    }

+

+    return totallen;

+}

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Entries b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Entries
new file mode 100755
index 0000000..a6a5db9
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Entries
@@ -0,0 +1,3 @@
+/ixmlmembuf.h/1.1.1.1/Thu Apr 24 03:47:28 2008//
+/ixmlparser.h/1.1.1.1/Thu Apr 24 03:47:28 2008//
+D
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Repository b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Repository
new file mode 100755
index 0000000..3e6f55b
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Repository
@@ -0,0 +1 @@
+russiaDSL/Source/apps/cwmpd/libixml/src/inc
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Root b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Root
new file mode 100755
index 0000000..39298f7
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Root
@@ -0,0 +1 @@
+:pserver:mike_zhou@cvs.sdc.sercomm.com:12121/home/cvsadmin/sbu-i
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Template b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Template
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/CVS/Template
diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/cwmp_private.h b/ap/app/cwmp/netcwmp/libcwmp/src/inc/cwmp_private.h
new file mode 100755
index 0000000..190873f
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/cwmp_private.h
@@ -0,0 +1,171 @@
+/************************************************************************

+ *                                                                      *

+ * Netcwmp Project                                                      *

+ *                                                                      *

+ * A software client for enabling TR-069 in embedded devices (CPE).     *

+ *                                                                      *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                         *

+ *                                                                      *

+ * Copyright 2013-2014           Mr.x() <netcwmp@gmail.com>          *

+ *                                                                      *

+ ***********************************************************************/

+

+#ifndef __CWMP_PRIVATE_H__

+#define __CWMP_PRIVATE_H__

+

+#include <cwmp/cwmp.h>

+#include <cwmp/buffer.h>

+#include <cwmp/pool.h>

+#include <cwmp/http.h>

+

+

+

+

+

+

+

+struct  device_id_st

+{

+    char *manufactorer;

+    char *name;

+    char *oui;

+    char *model_name;

+    char *product_class;

+    char *serial_number;

+} ;

+

+

+

+

+

+

+

+struct parameter_value_node_st

+{

+    const char *name;

+    const char *type;

+    char *  value;

+    int     value_length;

+};

+

+

+struct parameter_st

+{

+    const char * name;

+    const char * value;

+    unsigned int value_length;

+

+    int	type;

+    int	fault_code;

+

+    parameter_node_attr_st	attr;	

+

+};

+

+

+

+struct parameter_list_st

+{

+    int count;

+    int size;

+    parameter_t ** parameters;

+};

+

+

+

+struct header_st

+{

+    char *  id;

+    int     hold_requests;

+    int     no_more_requests;

+};

+

+

+

+

+struct key_value_t

+{

+    char *name;

+    char *value;

+};

+

+

+

+struct http_parser_t

+{

+    http_header_t *     header[MAX_HEADERS];

+    int                 count;

+

+};

+

+

+struct http_digest_auth_t

+{

+	int		active; //digest auth

+	char 	realm[MIN_DEFAULT_LEN+1];

+	char 	nonce[MIN_DEFAULT_LEN+1];

+	char 	cnonce[MIN_DEFAULT_LEN+1];

+	char    response[MIN_DEFAULT_LEN+1];

+	char    qop[MIN_DEFAULT_LEN+1];

+	char    nc[MIN_DEFAULT_LEN+1];

+    char    uri[MIN_DEFAULT_LEN*4+1];

+};

+

+struct http_dest_t

+{

+    char	scheme[URL_SCHEME_LEN+1];

+    char	host[MAX_HOST_NAME_LEN+1];

+    char    uri[MAX_URI_LEN + 1];

+

+    int     port;

+    char*   url;

+

+

+    char    user[URL_USER_LEN+1];

+    char    password[URL_PWD_LEN+1];

+

+    const char *    proxy_name;

+    const char *    proxy_auth;

+    const char *	user_agent;

+    int             proxy_port;

+

+    int             auth_type;

+    char    cookie[MIN_BUFFER_LEN+1];

+    http_digest_auth_t auth;

+};

+

+

+struct http_socket_t

+{

+    int sockdes;

+    int type;

+    int protocol;

+    http_sockaddr_t * addr;

+    cwmp_int64_t           timeout;

+

+    int option;

+    http_write_callback_pt write_callback;

+    void *  write_calldata;

+

+    int error_code;

+    

+    int use_ssl;

+    

+#ifdef USE_CWMP_OPENSSL    

+    SSL * ssl;

+#endif    

+    

+    

+    pool_t * pool;

+    

+    

+};

+

+int callback_register_task(cwmp_t * cwmp, callback_func_t callback, void *data1, void *data2);

+

+

+

+

+

+#endif

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/inc/ini.h b/ap/app/cwmp/netcwmp/libcwmp/src/inc/ini.h
new file mode 100755
index 0000000..8a7a1e7
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/inc/ini.h
@@ -0,0 +1,62 @@
+/************************************************************************

+ *                                                                      *

+ * Netcwmp Project                                                      *

+ *                                                                      *

+ * A software client for enabling TR-069 in embedded devices (CPE).     *

+ *                                                                      *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                         *

+ *                                                                      *

+ * Copyright 2013-2014           Mr.x() <netcwmp@gmail.com>          *

+ *                                                                      *

+ ***********************************************************************/

+

+

+#ifndef __CWMPINI_H__

+#define __CWMPINI_H__

+

+#include <stdio.h>

+

+#define ini_openread(filename,file)   ((*(file) = fopen((filename),"rt")) != NULL)

+#define ini_openwrite(filename,file)  ((*(file) = fopen((filename),"wt")) != NULL)

+#define ini_close(file)               fclose(*(file))

+#define ini_sysc(file)                fsync(fileno(*(file)))

+#define ini_read(buffer,size,file)    fgets((buffer),(size),*(file))

+#define ini_write(buffer,file)        fputs((buffer),*(file))

+#define ini_rename(source,dest)       rename((source),(dest))

+#define ini_remove(filename)          remove(filename)

+#define ini_rewind(file)              rewind(*(file))

+

+

+#if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY

+#include <tchar.h>

+#elif !defined __T

+typedef char TCHAR;

+#endif

+

+

+typedef TCHAR tchar_t;

+

+

+

+

+#if defined __cplusplus

+extern "C"

+{

+#endif

+

+    long ini_getl(const tchar_t *section, const tchar_t *key, long default_value, const tchar_t *filename);

+    int ini_gets(const tchar_t *section, const tchar_t *key, const tchar_t *default_value,

+             tchar_t *buffer, int buffer_size, const tchar_t *filename);

+    int ini_putl(const tchar_t *section, const tchar_t *key, long value, const tchar_t *filename);

+    int ini_puts(const tchar_t *section, const tchar_t *key, const tchar_t *value, const tchar_t *filename);

+    int  ini_get_section(int idx, tchar_t * buffer, int buffer_size, const tchar_t *filename);

+    int  ini_get_key(const tchar_t *section, int idx, tchar_t *buffer, int buffer_size, const tchar_t *filename);

+

+#if defined __cplusplus

+}

+#endif

+

+

+#endif /* __CWMPINI_H__ */

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/ini.c b/ap/app/cwmp/netcwmp/libcwmp/src/ini.c
new file mode 100755
index 0000000..1bb32e4
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/ini.c
@@ -0,0 +1,644 @@
+

+#if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined MININI_ANSI

+# if !defined UNICODE   /* for Windows */

+#   define UNICODE

+# endif

+# if !defined _UNICODE  /* for C library */

+#   define _UNICODE

+# endif

+#endif

+

+#include "ini.h"

+#include <cwmp/log.h>

+#include "errno.h"

+#if defined NDEBUG

+#define assert(e)

+#else

+//#include <assert.h>

+#define assert(e)

+

+#endif

+

+#if !defined __T

+#include <string.h>

+#include <stdlib.h>

+/* definition of tchar_t already in minIni.h */

+#define __T(s)    s

+#define _tcscmp   strcmp

+#define _tcsicmp  stricmp

+#define _tcsnicmp strncmp

+

+#define _tcscat   strcat

+

+#define _tcscpy   strcpy

+#define _tcsncpy  strncpy

+

+#define _tcschr   strchr

+#define _tcsrchr  strrchr

+

+#define _tcslen   strlen

+#define _tcstol   strtol

+

+#define _tremove  remove

+#define _trename  rename

+

+#define _tfopen   fopen

+

+#define _tfgets   fgets

+#define _tfputs   fputs

+

+#endif

+

+

+#if !defined INI_LINETERM

+#define INI_LINETERM    __T("\n")

+#endif

+#if !defined INI_FILETYPE

+#define INI_FILETYPE    FILE*

+#endif

+

+

+#if defined __linux || defined __linux__

+#define __LINUX__

+#endif

+#if defined FREEBSD && !defined __FreeBSD__

+#define __FreeBSD__

+#endif

+#if !defined strnicmp

+#if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__

+#define strnicmp  strncasecmp

+#endif

+#endif

+

+

+#if !defined sizearray

+#define sizearray(a)    (sizeof(a) / sizeof((a)[0]))

+#endif

+

+static void ini_string_reverse(tchar_t *str);

+

+

+#define INI_BUFFERSIZE  128

+

+static tchar_t *ini_skip_trailing(const tchar_t *str, const tchar_t *base)

+{

+    assert(str != NULL);

+    assert(base != NULL);

+    while (str > base && *(str-1) <= ' ')

+        str--;

+    return (tchar_t *)str;

+}

+

+static tchar_t *ini_skip_leading(const tchar_t *str)

+{

+    assert(str != NULL);

+    while (*str != '\0' && *str <= ' ')

+        str++;

+    return (tchar_t *)str;

+}

+

+

+static tchar_t *ini_save_strncpy(tchar_t *dest, const tchar_t *source, size_t maxlen)

+{

+    if(NULL == dest || NULL == source || maxlen <= 0)

+    {

+        return dest;

+    }

+	

+    assert(maxlen>0);

+    _tcsncpy(dest,source,maxlen);

+    dest[maxlen-1]='\0';

+    return dest;

+}

+

+static tchar_t *ini_strip_trailing(tchar_t *str)

+{

+    tchar_t *ptr = ini_skip_trailing(_tcschr(str, '\0'), str);

+    assert(ptr != NULL);

+    *ptr='\0';

+    return str;

+}

+

+static int ini_get_keystring(INI_FILETYPE *fp, const tchar_t *section, const tchar_t *key,

+                        int section_index, int idxKey, tchar_t *buffer, int buffer_size)

+{

+    tchar_t *sp, *ep;

+    int len, idx;

+    tchar_t local_buffer[INI_BUFFERSIZE];

+

+    assert(fp != NULL);

+    len = (section != NULL) ? _tcslen(section) : 0;

+    if (len > 0 || section_index >= 0)

+    {

+        idx = -1;

+        do

+        {

+            if (!ini_read(local_buffer, INI_BUFFERSIZE, fp))

+            {

+                return 0;

+            }

+

+//			cwmp_log_info("idx:%d, section_index:%d, local:%s", idx, section_index, local_buffer);

+			

+            sp = ini_skip_leading(local_buffer);

+            ep = _tcschr(sp, ']');

+        }while (*sp != '[' || ep == NULL || (((int)(ep-sp-1) != len || (section != NULL && _tcsnicmp(sp+1,section,len) != 0)) && ++idx != section_index));

+		

+        if (section_index >= 0)

+        {

+            if (idx == section_index)

+            {

+                assert(ep != NULL);

+                assert(*ep == ']');

+                *ep = '\0';

+                ini_save_strncpy(buffer, sp + 1, buffer_size);

+				cwmp_log_info("section_index:%d, buffer:%s", section_index, buffer);

+                return 1;

+            } /* if */

+            return 0; /* no more section found */

+        } /* if */

+    } /* if */

+

+    assert(key != NULL || idxKey >= 0);

+    len = (key != NULL) ? (int)_tcslen(key) : 0;

+    idx = -1;

+    do

+    {

+        if (!ini_read(local_buffer,INI_BUFFERSIZE,fp) || *(sp = ini_skip_leading(local_buffer)) == '[')

+            return 0;

+        sp = ini_skip_leading(local_buffer);

+        ep = _tcschr(sp, '='); /* Parse out the equal sign */

+        if (ep == NULL)

+            ep = _tcschr(sp, ':');

+    }while (*sp == ';' || *sp == '#' || ep == NULL || (((int)(ini_skip_trailing(ep,sp)-sp) != len || _tcsnicmp(sp,key,len) != 0) && ++idx != idxKey));

+		

+    if (idxKey >= 0)

+    {

+        if (idx == idxKey)

+        {

+            assert(ep != NULL);

+            assert(*ep == '=' || *ep == ':');

+            *ep = '\0';

+            ini_strip_trailing(sp);

+            ini_save_strncpy(buffer, sp, buffer_size);

+			cwmp_log_info("buffer:%s", buffer);

+            return 1;

+        } /* if */

+        return 0;   /* no more key found (in this section) */

+    } /* if */

+

+    /* Copy up to buffer_size chars to buffer */

+//    assert(ep != NULL);

+//    assert(*ep == '=' || *ep == ':');

+    sp = ini_skip_leading(ep + 1);

+    ini_strip_trailing(sp);

+    /* Remove double quotes surrounding a value */

+    ep = _tcschr(sp, '\0');

+	

+    if (*sp == '"' /*&& ep != NULL*/ && (ep - 1) != NULL && *(ep - 1) == '"')

+    {

+        sp++;

+        *--ep = '\0';

+    } /* if */

+    ini_save_strncpy(buffer, sp, buffer_size);

+    return 1;

+}

+

+						

+long ini_getl(const tchar_t *section, const tchar_t *key, long default_value, const tchar_t *filename)

+{

+	tchar_t buff[64];

+	int len = ini_gets(section, key, __T(""), buff, sizearray(buff), filename);

+	if(len == 0)

+	{

+		return default_value;

+	}

+	else

+	{

+		errno = 0;

+		long ret = _tcstol(buff,NULL,10);

+		if (errno == ERANGE)// kw ERRNO.NOT_CHECKED

+		{

+			printf("strtol errno %d: %s\n", errno, strerror(errno));

+		}

+		return ret;

+	}

+	//return (len == 0) ? default_value : _tcstol(buff,NULL,10);

+}

+

+int ini_gets(const tchar_t *section, const tchar_t *key, const tchar_t *default_value,

+             tchar_t *buffer, int buffer_size, const tchar_t *filename)

+{

+    INI_FILETYPE fp;

+    int ok = 0;

+

+    if (buffer == NULL || buffer_size <= 0 || key == NULL)

+    {

+        return 0;

+    }

+	

+    if (ini_openread(filename, &fp))

+    {

+        ok = ini_get_keystring(&fp, section, key, -1, -1, buffer, buffer_size);

+        ini_close(&fp);

+    } /* if */

+    if (!ok)

+    {

+        if(default_value == NULL)

+        {

+            buffer[0] = 0;

+        }

+        else

+        {

+            ini_save_strncpy(buffer, default_value, buffer_size);

+        }

+    }

+    return _tcslen(buffer);

+}

+			 

+int  ini_get_key(const tchar_t *section, int idx, tchar_t *buffer, int buffer_size, const tchar_t *filename)

+{

+ INI_FILETYPE fp;

+ int ok = 0;

+

+ if (buffer == NULL || buffer_size <= 0 || idx < 0)

+ {

+	 return 0;

+ }

+ 

+ if (ini_openread(filename, &fp))

+ {

+	 ok = ini_get_keystring(&fp, section, NULL, -1, idx, buffer, buffer_size);

+	 ini_close(&fp);

+ } /* if */

+ if (!ok)

+	 *buffer = '\0';

+ return _tcslen(buffer);

+}

+

+int  ini_get_section(int idx, tchar_t *buffer, int buffer_size, const tchar_t *filename)

+{

+    INI_FILETYPE fp;

+    int ok = 0;

+

+    if (buffer == NULL || buffer_size <= 0 || idx < 0)

+        return 0;

+    if (ini_openread(filename, &fp))

+    {

+        ok = ini_get_keystring(&fp, NULL, NULL, idx, -1, buffer, buffer_size);

+        ini_close(&fp);

+    } /* if */

+    if (!ok)

+        *buffer = '\0';

+    return _tcslen(buffer);

+}

+

+

+

+#if ! defined INI_READONLY

+static void ini_tempname(tchar_t *dest, const tchar_t *source, int maxlength)

+{

+    tchar_t *p;

+

+    ini_save_strncpy(dest, source, maxlength);	

+#if 0	

+    p = _tcsrchr(dest, '\0');

+#else

+    p = dest + _tcslen(dest);

+#endif

+

+    assert(p != NULL);

+    *(p - 1) = '~';

+}

+

+static void ini_write_section(tchar_t *local_buffer, const tchar_t *section, INI_FILETYPE *fp)

+{

+    tchar_t *p;

+

+    if (section != NULL && _tcslen(section) > 0)

+    {

+        local_buffer[0] = '[';

+        ini_save_strncpy(local_buffer + 1, section, INI_BUFFERSIZE - 4);  /* -1 for '[', -1 for ']', -2 for '\r\n' */

+#if 0		

+        p = _tcsrchr(local_buffer, '\0');

+#else

+        p = local_buffer + _tcslen(local_buffer);

+#endif

+	

+        assert(p != NULL);

+        *p++ = ']';

+        _tcsncpy(p, INI_LINETERM, INI_BUFFERSIZE - _tcslen(local_buffer) - 1); /* copy line terminator (typically "\n") */

+        ini_write(local_buffer, fp);

+    } /* if */

+}

+

+static void ini_write_key(tchar_t *local_buffer, const tchar_t *key, const tchar_t *value, INI_FILETYPE *fp)

+{

+    tchar_t *p;

+

+    ini_save_strncpy(local_buffer, key, INI_BUFFERSIZE - 3);  /* -1 for '=', -2 for '\r\n' */

+#if 0	

+		p = _tcsrchr(local_buffer, '\0');

+#else

+		p = local_buffer + _tcslen(local_buffer);

+#endif

+

+    assert(p != NULL);

+    *p++ = '=';

+    ini_save_strncpy(p, value, INI_BUFFERSIZE - (p - local_buffer) - 2); /* -2 for '\r\n' */

+

+#if 0	

+    p = _tcsrchr(local_buffer, '\0');

+#else

+	p = local_buffer + _tcslen(local_buffer);

+#endif

+

+    assert(p != NULL);	

+	

+    _tcsncpy(p, INI_LINETERM, INI_BUFFERSIZE - _tcslen(local_buffer) - 1); /* copy line terminator (typically "\n") */

+	

+    ini_write(local_buffer, fp);

+}

+

+/** ini_puts()

+ * \param section     the name of the section to write the string in

+ * \param key         the name of the entry to write, or NULL to erase all keys in the section

+ * \param value       a pointer to the buffer the string, or NULL to erase the key

+ * \param filename    the name and full path of the .ini file to write to

+ *

+ * \return            1 if successful, otherwise 0

+ */

+int ini_puts(const tchar_t *section, const tchar_t *key, const tchar_t *value, const tchar_t *filename)

+{

+    INI_FILETYPE rfp;

+    INI_FILETYPE wfp;

+    tchar_t *sp, *ep;

+    tchar_t local_buffer[INI_BUFFERSIZE+1] = {0};

+    int len, match, count;

+	tchar_t temp_name[INI_BUFFERSIZE+1] = {0};

+

+	if(NULL == section || NULL == filename || NULL == key )

+	{

+	    return 0;

+	} 	

+

+    assert(filename!=NULL);

+    if (!ini_openread(filename, &rfp))

+    {

+        /* If the .ini file doesn't exist, make a new file */

+//        if (key!=NULL && value!=NULL)

+        {

+            if (!ini_openwrite(filename, &wfp))

+            {

+                return 0;

+            }

+            ini_write_section(local_buffer, section, &wfp);

+            ini_write_key(local_buffer, key, value, &wfp);

+			ini_sysc(&wfp);

+            ini_close(&wfp);

+        } /* if */

+        return 1;

+    } /* if */

+

+    /* If parameters key and value are valid (so this is not an "erase" request)

+     * and the setting already exists and it already has the correct value, do

+     * nothing. This early bail-out avoids rewriting the INI file for no reason.

+     */

+    if (/*key!=NULL && */value!=NULL)

+    {

+        match = ini_get_keystring(&rfp, section, key, -1, -1, local_buffer, sizearray(local_buffer));

+        if (match && _tcscmp(local_buffer,value)==0)

+        {

+            ini_close(&rfp);

+            return 1;

+        } /* if */

+        /* key not found, or different value -> proceed (but rewind the input file first) */

+        ini_rewind(&rfp);

+    } /* if */

+

+    /* Get a temporary file name to copy to. Use the existing name, but with

+     * the last character set to a '~'.

+     */

+    ini_tempname(temp_name, filename, INI_BUFFERSIZE);

+    if (!ini_openwrite(temp_name, &wfp))

+    {

+        ini_close(&rfp);

+        return 0;

+    } /* if */

+

+    /* Move through the file one line at a time until a section is

+     * matched or until EOF. Copy to temp file as it is read.

+     */

+    count = 0;

+    //len = (section != NULL) ? _tcslen(section) : 0;

+    len = _tcslen(section);

+    if (len > 0)

+    {

+        do

+        {

+            if (!ini_read(local_buffer, INI_BUFFERSIZE, &rfp))

+            {

+                /* Failed to find section, so add one to the end */

+//                if (key!=NULL && value!=NULL)

+                {

+                    ini_write(INI_LINETERM, &wfp);  /* force a new line (there may not have been one) behind the last line of the INI file */

+                    ini_write_section(local_buffer, section, &wfp);

+                    ini_write_key(local_buffer, key, value, &wfp);

+                } /* if */

+                /* Clean up and rename */

+                ini_close(&rfp);

+				ini_sysc(&wfp);

+                ini_close(&wfp);

+                if(ini_remove(filename) <0)

+                {

+                }

+                ini_tempname(temp_name, filename, INI_BUFFERSIZE);

+                if(ini_rename(temp_name, filename) < 0)

+				{

+				}

+                return 1;

+            } /* if */

+            /* Copy the line from source to dest, but not if this is the section that

+             * we are looking for and this section must be removed

+             */

+            sp = ini_skip_leading(local_buffer);

+            ep = _tcschr(sp, ']');

+            match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _tcsnicmp(sp + 1,section,len) == 0);

+//            if (!match || key!=NULL)

+            {

+                /* Remove blank lines, but insert a blank line (possibly one that was

+                 * removed on the previous iteration) before a new section. This creates

+                 * "neat" INI files.

+                 */

+                if (_tcslen(sp) > 0)

+                {

+                    if (*sp == '[' && count > 0)

+                        ini_write(INI_LINETERM, &wfp);

+                    ini_write(sp, &wfp);

+                    count++;

+                } /* if */

+            } /* if */

+        }

+        while (!match);

+    } /* if */

+

+    /* Now that the section has been found, find the entry. Stop searching

+     * upon leaving the section's area. Copy the file as it is read

+     * and create an entry if one is not found.

+     */

+    //len = (key!=NULL) ? _tcslen(key) : 0;

+	len =  _tcslen(key);

+    for ( ;; )

+    {

+        if (!ini_read(local_buffer, INI_BUFFERSIZE, &rfp))

+        {

+            /* EOF without an entry so make one */

+//            if (key!=NULL && value!=NULL)

+            {

+                ini_write(INI_LINETERM, &wfp);  /* force a new line (there may not have been one) behind the last line of the INI file */

+                ini_write_key(local_buffer, key, value, &wfp);

+            } /* if */

+            /* Clean up and rename */

+            ini_close(&rfp);

+			ini_sysc(&wfp);

+            ini_close(&wfp);

+            if(ini_remove(filename) < 0)

+            {

+            }

+            ini_tempname(temp_name, filename, INI_BUFFERSIZE);

+            if(ini_rename(temp_name, filename) < 0)

+            {

+            

+            }

+            return 1;

+        } /* if */

+        sp = ini_skip_leading(local_buffer);

+        ep = _tcschr(sp, '='); /* Parse out the equal sign */

+        if (ep == NULL)

+            ep = _tcschr(sp, ':');

+        match = (ep != NULL && (int)(ep-sp) == len && _tcsnicmp(sp,key,len) == 0);

+        if ((/*key!=NULL && */match) || *sp == '[')

+            break;  /* found the key, or found a new section */

+        /* in the section that we re-write, do not copy empty lines */

+        if (/*key!=NULL && */_tcslen(sp) > 0)

+            ini_write(sp, &wfp);

+    } /* for */

+    if (*sp == '[')

+    {

+        /* found start of new section, the key was not in the specified

+         * section, so we add it just before the new section

+         */

+//        if (key!=NULL && value!=NULL)

+        {

+            /* We cannot use "ini_write_key()" here, because we need to preserve the

+             * contents of local_buffer.

+             */

+            ini_write(key, &wfp);

+            ini_write("=", &wfp);

+			if(value!=NULL)

+			{

+                ini_write(value, &wfp);

+			}

+            ini_write(INI_LINETERM INI_LINETERM, &wfp); /* put a blank line between the current and the next section */

+        } /* if */

+        /* write the new section header that we read previously */

+        ini_write(sp, &wfp);

+    }

+    else

+    {

+        /* We found the key; ignore the line just read (with the key and

+         * the current value) and write the key with the new value.

+         */

+//        if (key!=NULL && value!=NULL)

+            ini_write_key(local_buffer, key, value, &wfp);

+    } /* if */

+    /* Copy the rest of the INI file (removing empty lines, except before a section) */

+    while (ini_read(local_buffer, INI_BUFFERSIZE, &rfp))

+    {

+        sp = ini_skip_leading(local_buffer);

+        if (_tcslen(sp) > 0)

+        {

+            if (*sp == '[')

+                ini_write(INI_LINETERM, &wfp);

+            ini_write(sp, &wfp);

+        } /* if */

+    } /* while */

+    /* Clean up and rename */

+    ini_close(&rfp);

+	ini_sysc(&wfp);

+    ini_close(&wfp);

+    if(ini_remove(filename) < 0)

+    {

+    }

+    ini_tempname(temp_name, filename, INI_BUFFERSIZE);

+    if(ini_rename(temp_name, filename) < 0)

+    {   

+    }

+    return 1;

+}

+

+#define ABS(v)  ((v) < 0 ? -(v) : (v))

+

+static void ini_long2str(long value, tchar_t *str)

+{

+    int i = 0;

+    long sign = value;

+    int n;

+

+    /* generate digits in reverse order */

+    do

+    {

+        n = (int)(value % 10);              /* get next lowest digit */

+        str[i++] = (tchar_t)(ABS(n) + '0');   /* handle case of negative digit */

+    }

+    while (value /= 10);                /* delete the lowest digit */

+    if (sign < 0)

+        str[i++] = '-';

+    str[i] = '\0';

+

+    ini_string_reverse(str);

+}

+

+static void ini_string_reverse(tchar_t *str)

+{

+    tchar_t t;

+    int i, j;

+

+    for (i = 0, j = _tcslen(str) - 1; i < j; i++, j--)

+    {

+        t = str[i];

+        str[i] = str[j];

+        str[j] = t;

+    } /* for */

+}

+int ini_putl(const tchar_t *section, const tchar_t *key, long value, const tchar_t *filename)

+{

+    tchar_t str[32];

+    ini_long2str(value, str);

+    return ini_puts(section, key, str, filename);

+}

+#endif /* !INI_READONLY */

+

+

+#if defined PORTABLE_STRNICMP

+int strnicmp(const tchar_t *s1, const tchar_t *s2, size_t n)

+{

+    register unsigned tchar_t c1, c2;

+

+    while (n-- != 0 && (*s1 || *s2))

+    {

+        c1 = *(const unsigned tchar_t *)s1++;

+        if ('a' <= c1 && c1 <= 'z')

+            c1 += ('A' - 'a');

+        c2 = *(const unsigned tchar_t *)s2++;

+        if ('a' <= c2 && c2 <= 'z')

+            c2 += ('A' - 'a');

+        if (c1 != c2)

+            return c1 - c2;

+    } /* while */

+    return 0;

+}

+#endif /* PORTABLE_STRNICMP */

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/log.c b/ap/app/cwmp/netcwmp/libcwmp/src/log.c
new file mode 100755
index 0000000..05809b7
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/log.c
@@ -0,0 +1,263 @@
+/************************************************************************

+ * Id: log.c                                                            *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *

+ ***********************************************************************/

+ 

+ 

+#include <stdlib.h>

+#include <stdio.h>

+#include <stdarg.h>

+#include <string.h>

+#include "cwmp/log.h"

+

+#include <syslog.h>

+

+

+struct cwmp_log_t

+{

+    FILE * file;

+    int level;

+    char * name;

+	int file_no;

+	long max_size;

+};

+

+static cwmp_log_t 	    g_cwmp_log_file;

+static cwmp_log_t 		*	g_ot_log_file_ptr;

+

+

+//¶ÁÈ¡LogÅäÖÃÎļþ

+

+int cwmp_log_init(const char * filename, int level)

+{

+

+    g_cwmp_log_file.file = NULL;

+    g_cwmp_log_file.name = NULL;

+    if (filename)

+    {

+        g_cwmp_log_file.file = fopen(filename,"a+");

+        g_cwmp_log_file.name = strdup(filename);

+    }

+

+    if (g_cwmp_log_file.file == NULL)

+    {

+        g_cwmp_log_file.file = stdout;

+    }

+

+    g_cwmp_log_file.level = level;

+

+	g_cwmp_log_file.file_no = fileno(g_cwmp_log_file.file);

+

+	g_cwmp_log_file.max_size = 512*1024;

+

+    g_ot_log_file_ptr = &g_cwmp_log_file;

+

+    return 0;

+}

+

+void cwmp_log_fini()

+{

+    if (g_cwmp_log_file.name)

+    {

+        free(g_cwmp_log_file.name);

+    }

+

+    if ((g_cwmp_log_file.file != stdout) && (g_cwmp_log_file.file != NULL))

+    {

+        fclose(g_cwmp_log_file.file);

+    }

+

+	g_cwmp_log_file.file_no = -1;

+

+}

+

+void cwmp_log_console(int level, cwmp_log_t * log, const char * fmt, va_list ap)

+{

+    int priority = 0;

+	char buf[1024] = {0}; 

+

+    switch(level)

+    {

+        case CWMP_LOG_STDERR:

+			priority = LOG_DEBUG;

+			break;

+			

+        case CWMP_LOG_EMERG:

+			priority = LOG_EMERG;

+			break;

+			

+        case CWMP_LOG_ALERT:

+			priority = LOG_ALERT;

+			break;

+			

+        case CWMP_LOG_CRIT:

+			priority = LOG_CRIT;

+			break;

+			

+        case CWMP_LOG_ERROR:

+			priority = LOG_ERR;

+			break;

+			

+        case CWMP_LOG_WARN:

+			priority = LOG_WARNING;

+			break;

+			

+        case CWMP_LOG_NOTICE:

+			priority = LOG_NOTICE;

+			break;

+			

+        case CWMP_LOG_INFO:

+			priority = LOG_INFO;

+			break;

+			

+        case CWMP_LOG_TRACE:

+			priority = LOG_INFO;

+			break;

+			

+        case CWMP_LOG_DEBUG:

+			priority = LOG_DEBUG;

+			break;

+

+		default:

+			priority = LOG_DEBUG;

+			break;			

+    }

+

+//    vsyslog(priority, fmt, ap);

+

+    vsnprintf(buf, 1023, fmt, ap);

+    printf("%s",buf);

+}

+

+void cwmp_log_write(int level, cwmp_log_t * log, const char * fmt, va_list ap)

+{

+    cwmp_log_t * logger;

+	long cur_log_size = 0;

+

+    if (log)

+    {

+        if (log->level < level )

+        {

+            return;

+        }

+    }

+    else

+    {

+        if (g_cwmp_log_file.level < level)

+        {

+            return;

+        }

+    }

+

+    logger = g_ot_log_file_ptr;

+

+    vfprintf(logger->file, fmt, ap);

+    fprintf(logger->file, "\n");

+

+    fflush(logger->file);

+

+    fsync(logger->file_no);

+

+    fseek(logger->file, 0, SEEK_END);

+

+    cur_log_size = ftell(logger->file);	

+

+	if(cur_log_size > logger->max_size)

+	{   

+	    char buf[1024] = {0};

+		cur_log_size = 0;

+		fclose(logger->file);

+		snprintf(buf, 1023, "%s_bak", g_cwmp_log_file.name);

+		if(rename(g_cwmp_log_file.name, buf) < 0)

+		{

+		    printf("Rename fail!");

+		}

+

+        g_cwmp_log_file.file = fopen(g_cwmp_log_file.name,"a+");

+

+        if (g_cwmp_log_file.file == NULL)

+        {

+            g_cwmp_log_file.file = stdout;

+        }

+    	g_cwmp_log_file.file_no = fileno(g_cwmp_log_file.file);		

+	}

+		

+}

+

+void cwmp_log_tracer_src(int level, cwmp_log_t * log,const char * fmt, ...)

+{

+    va_list ap = {0};

+

+    va_start(ap, fmt);

+	cwmp_log_console(level, log, fmt, ap);

+    cwmp_log_write(level, log, fmt, ap);

+    va_end(ap);

+

+}

+

+void cwmp_log_debug_src(const char * fmt, ...)

+{

+    va_list ap = {0};

+    if (g_ot_log_file_ptr->level < CWMP_LOG_DEBUG)

+        return;

+

+    va_start(ap, fmt);

+	cwmp_log_console(CWMP_LOG_DEBUG, g_ot_log_file_ptr, fmt, ap);

+    cwmp_log_write(CWMP_LOG_DEBUG, g_ot_log_file_ptr, fmt, ap);

+    va_end(ap);

+}

+

+void cwmp_log_info_src(const char * fmt, ...)

+{

+    va_list ap = {0};

+    if (g_ot_log_file_ptr->level < CWMP_LOG_INFO)

+        return;

+

+    va_start(ap, fmt);

+	cwmp_log_console(CWMP_LOG_INFO, g_ot_log_file_ptr, fmt, ap);

+    cwmp_log_write(CWMP_LOG_INFO, g_ot_log_file_ptr, fmt, ap);

+    va_end(ap);

+}

+

+void cwmp_log_error_src(const char * fmt, ...)

+{

+    va_list ap = {0};

+    if (g_ot_log_file_ptr->level < CWMP_LOG_ERROR)

+        return;

+

+    va_start(ap, fmt);

+	cwmp_log_console(CWMP_LOG_ERROR, g_ot_log_file_ptr, fmt, ap);

+    cwmp_log_write(CWMP_LOG_ERROR, g_ot_log_file_ptr, fmt, ap);

+    va_end(ap);

+}

+

+void cwmp_log_alert_src(const char * fmt, ...)

+{

+    va_list ap = {0};

+    if (g_ot_log_file_ptr->level < CWMP_LOG_ALERT)

+        return;

+

+    va_start(ap, fmt);

+	cwmp_log_console(CWMP_LOG_ALERT, g_ot_log_file_ptr, fmt, ap);

+    cwmp_log_write(CWMP_LOG_ALERT, g_ot_log_file_ptr, fmt, ap);

+    va_end(ap);

+}

+

+void cwmp_log_critical_src(const char * fmt, ...)

+{

+    va_list ap = {0};

+    if (g_ot_log_file_ptr->level < CWMP_LOG_CRIT)

+        return;

+

+    va_start(ap, fmt);

+	cwmp_log_console(CWMP_LOG_CRIT, g_ot_log_file_ptr, fmt, ap);

+    cwmp_log_write(CWMP_LOG_CRIT, g_ot_log_file_ptr, fmt, ap);

+    va_end(ap);

+}

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/md5.c b/ap/app/cwmp/netcwmp/libcwmp/src/md5.c
new file mode 100755
index 0000000..61ce062
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/md5.c
@@ -0,0 +1,438 @@
+/*

+   MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm

+ */

+

+/*

+   Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All

+   rights reserved.

+

+   License to copy and use this software is granted provided that it

+   is identified as the &quot;RSA Data Security, Inc. MD5 Message-Digest

+   Algorithm&quot; in all material mentioning or referencing this software

+   or this function.

+

+   License is also granted to make and use derivative works provided

+   that such works are identified as &quot;derived from the RSA Data

+   Security, Inc. MD5 Message-Digest Algorithm&quot; in all material

+   mentioning or referencing the derived work.

+

+   RSA Data Security, Inc. makes no representations concerning either

+   the merchantability of this software or the suitability of this

+   software for any particular purpose. It is provided &quot;as is&quot;

+   without express or implied warranty of any kind.

+

+   These notices must be retained in any copies of any part of this

+   documentation and/or software.

+

+ */

+

+#include "cwmp/md5.h"

+

+/*

+   Constants for MD5Transform routine.

+ */

+#define S11 7

+#define S12 12

+#define S13 17

+#define S14 22

+#define S21 5

+#define S22 9

+#define S23 14

+#define S24 20

+#define S31 4

+#define S32 11

+#define S33 16

+#define S34 23

+#define S41 6

+#define S42 10

+#define S43 15

+#define S44 21

+

+static void MD5Transform PROTO_LIST( ( UINT4[4],

+                                       unsigned char[64] ) );

+static void Encode PROTO_LIST( ( unsigned char *,

+                                 UINT4 *,

+                                 unsigned int ) );

+

+static void Decode PROTO_LIST( ( UINT4 *,

+                                 unsigned char *,

+                                 unsigned int ) );

+

+static void MD5_memcpy PROTO_LIST( ( POINTER,

+                                     POINTER,

+                                     unsigned int ) );

+static void MD5_memset PROTO_LIST( ( POINTER,

+                                     int,

+                                     unsigned int ) );

+

+static unsigned char PADDING[64] =

+{

+    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

+};

+

+/*

+   F, G, H and I are basic MD5 functions.

+ */

+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))

+#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))

+#define H(x, y, z) ((x) ^ (y) ^ (z))

+#define I(x, y, z) ((y) ^ ((x) | (~z)))

+

+/*

+   ROTATE_LEFT rotates x left n bits.

+ */

+#define ROTATE_LEFT(x, n) (((x) <<(n)) | ((x) >> (32-(n))))

+

+/*

+   FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.

+   Rotation is separate from addition to prevent recomputation.

+ */

+#define FF(a, b, c, d, x, s, ac) { \

+ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \

+ (a) = ROTATE_LEFT ((a), (s)); \

+ (a) += (b); \

+  }

+

+#define GG(a, b, c, d, x, s, ac) { \

+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \

+ (a) = ROTATE_LEFT ((a), (s)); \

+ (a) += (b); \

+  }

+

+#define HH(a, b, c, d, x, s, ac) { \

+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \

+ (a) = ROTATE_LEFT ((a), (s)); \

+ (a) += (b); \

+  }

+

+#define II(a, b, c, d, x, s, ac) { \

+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \

+ (a) = ROTATE_LEFT ((a), (s)); \

+ (a) += (b); \

+  }

+

+/*

+   MD5 initialization. Begins an MD5 operation, writing a new context.

+ */

+void

+cwmp_MD5Init( context )

+cwmp_MD5_CTX *context;          /* context */

+{

+    context->count[0] = context->count[1] = 0;

+

+    /*

+       Load magic initialization constants.

+     */

+    context->state[0] = 0x67452301;

+    context->state[1] = 0xefcdab89;

+    context->state[2] = 0x98badcfe;

+    context->state[3] = 0x10325476;

+}

+

+/*

+   MD5 block update operation. Continues an MD5 message-digest

+   operation, processing another message block, and updating the

+   context.

+ */

+

+void

+cwmp_MD5Update( context,

+           input,

+           inputLen )

+cwmp_MD5_CTX *context;          /* context */

+unsigned char *input;      /* input block */

+unsigned int inputLen;     /* length of input block */

+{

+    unsigned int i,

+    index,

+    partLen;

+

+    /*

+       Compute number of bytes mod 64

+     */

+    index = ( unsigned int )( ( context->count[0] >> 3 ) & 0x3F );

+

+    /*

+       Update number of bits

+     */

+    if ( ( context->count[0] +=

+                ( ( UINT4 ) inputLen << 3 ) ) < ( ( UINT4 ) inputLen << 3 ) )

+        context->count[1]++;

+

+    context->count[1] += ( ( UINT4 ) inputLen >> 29 );

+

+    partLen = 64 - index;

+

+    /*

+       Transform as many times as possible.

+     */

+    if ( inputLen >= partLen )

+    {

+

+        MD5_memcpy

+        ( ( POINTER ) & context->buffer[index], ( POINTER ) input,

+          partLen );

+        MD5Transform( context->state, context->buffer );

+

+        for ( i = partLen; i + 63 < inputLen; i += 64 )

+            MD5Transform( context->state, &input[i] );

+

+        index = 0;

+    }

+    else

+        i = 0;

+

+    /*

+       Buffer remaining input

+     */

+    MD5_memcpy

+    ( ( POINTER ) & context->buffer[index], ( POINTER ) & input[i],

+      inputLen - i );

+

+}

+

+/*

+   MD5 finalization. Ends an MD5 message-digest operation, writing the

+   the message digest and zeroizing the context.

+ */

+

+void

+cwmp_MD5Final( digest,

+          context )

+unsigned char digest[16];  /* message digest */

+cwmp_MD5_CTX *context;          /* context */

+{

+

+    unsigned char bits[8];

+    unsigned int index,

+    padLen;

+

+    /*

+       Save number of bits

+     */

+    Encode( bits, context->count, 8 );

+

+    /*

+       Pad out to 56 mod 64.

+     */

+    index = ( unsigned int )( ( context->count[0] >> 3 ) & 0x3f );

+

+    padLen = ( index < 56 ) ? ( 56 - index ) : ( 120 - index );

+

+    cwmp_MD5Update( context, PADDING, padLen );

+

+    /*

+       Append length (before padding)

+     */

+    cwmp_MD5Update( context, bits, 8 );

+

+    /*

+       Store state in digest

+     */

+    Encode( digest, context->state, 16 );

+

+    /*

+       Zeroize sensitive information.

+     */

+    MD5_memset( ( POINTER ) context, 0, sizeof( *context ) );

+

+}

+

+/*

+   MD5 basic transformation. Transforms state based on block.

+ */

+static void

+MD5Transform( state,

+              block )

+UINT4 state[4];

+unsigned char block[64];

+{

+    UINT4 a = state[0],

+              b = state[1],

+                  c = state[2],

+                      d = state[3],

+                          x[16];

+

+    Decode( x, block, 64 );

+

+    /*

+       Round 1

+     */

+    FF( a, b, c, d, x[0], S11, 0xd76aa478 );    /* 1 */

+    FF( d, a, b, c, x[1], S12, 0xe8c7b756 );    /* 2 */

+    FF( c, d, a, b, x[2], S13, 0x242070db );    /* 3 */

+    FF( b, c, d, a, x[3], S14, 0xc1bdceee );    /* 4 */

+    FF( a, b, c, d, x[4], S11, 0xf57c0faf );    /* 5 */

+    FF( d, a, b, c, x[5], S12, 0x4787c62a );    /* 6 */

+    FF( c, d, a, b, x[6], S13, 0xa8304613 );    /* 7 */

+    FF( b, c, d, a, x[7], S14, 0xfd469501 );    /* 8 */

+    FF( a, b, c, d, x[8], S11, 0x698098d8 );    /* 9 */

+    FF( d, a, b, c, x[9], S12, 0x8b44f7af );    /* 10 */

+    FF( c, d, a, b, x[10], S13, 0xffff5bb1 );   /* 11 */

+    FF( b, c, d, a, x[11], S14, 0x895cd7be );   /* 12 */

+    FF( a, b, c, d, x[12], S11, 0x6b901122 );   /* 13 */

+    FF( d, a, b, c, x[13], S12, 0xfd987193 );   /* 14 */

+    FF( c, d, a, b, x[14], S13, 0xa679438e );   /* 15 */

+    FF( b, c, d, a, x[15], S14, 0x49b40821 );   /* 16 */

+

+    /*

+       Round 2

+     */

+    GG( a, b, c, d, x[1], S21, 0xf61e2562 );    /* 17 */

+    GG( d, a, b, c, x[6], S22, 0xc040b340 );    /* 18 */

+    GG( c, d, a, b, x[11], S23, 0x265e5a51 );   /* 19 */

+    GG( b, c, d, a, x[0], S24, 0xe9b6c7aa );    /* 20 */

+    GG( a, b, c, d, x[5], S21, 0xd62f105d );    /* 21 */

+    GG( d, a, b, c, x[10], S22, 0x2441453 );    /* 22 */

+    GG( c, d, a, b, x[15], S23, 0xd8a1e681 );   /* 23 */

+    GG( b, c, d, a, x[4], S24, 0xe7d3fbc8 );    /* 24 */

+    GG( a, b, c, d, x[9], S21, 0x21e1cde6 );    /* 25 */

+    GG( d, a, b, c, x[14], S22, 0xc33707d6 );   /* 26 */

+    GG( c, d, a, b, x[3], S23, 0xf4d50d87 );    /* 27 */

+    GG( b, c, d, a, x[8], S24, 0x455a14ed );    /* 28 */

+    GG( a, b, c, d, x[13], S21, 0xa9e3e905 );   /* 29 */

+    GG( d, a, b, c, x[2], S22, 0xfcefa3f8 );    /* 30 */

+    GG( c, d, a, b, x[7], S23, 0x676f02d9 );    /* 31 */

+    GG( b, c, d, a, x[12], S24, 0x8d2a4c8a );   /* 32 */

+

+    /*

+       Round 3

+     */

+    HH( a, b, c, d, x[5], S31, 0xfffa3942 );    /* 33 */

+    HH( d, a, b, c, x[8], S32, 0x8771f681 );    /* 34 */

+    HH( c, d, a, b, x[11], S33, 0x6d9d6122 );   /* 35 */

+    HH( b, c, d, a, x[14], S34, 0xfde5380c );   /* 36 */

+    HH( a, b, c, d, x[1], S31, 0xa4beea44 );    /* 37 */

+    HH( d, a, b, c, x[4], S32, 0x4bdecfa9 );    /* 38 */

+    HH( c, d, a, b, x[7], S33, 0xf6bb4b60 );    /* 39 */

+    HH( b, c, d, a, x[10], S34, 0xbebfbc70 );   /* 40 */

+    HH( a, b, c, d, x[13], S31, 0x289b7ec6 );   /* 41 */

+    HH( d, a, b, c, x[0], S32, 0xeaa127fa );    /* 42 */

+    HH( c, d, a, b, x[3], S33, 0xd4ef3085 );    /* 43 */

+    HH( b, c, d, a, x[6], S34, 0x4881d05 ); /* 44 */

+    HH( a, b, c, d, x[9], S31, 0xd9d4d039 );    /* 45 */

+    HH( d, a, b, c, x[12], S32, 0xe6db99e5 );   /* 46 */

+    HH( c, d, a, b, x[15], S33, 0x1fa27cf8 );   /* 47 */

+    HH( b, c, d, a, x[2], S34, 0xc4ac5665 );    /* 48 */

+

+    /*

+       Round 4

+     */

+    II( a, b, c, d, x[0], S41, 0xf4292244 );    /* 49 */

+    II( d, a, b, c, x[7], S42, 0x432aff97 );    /* 50 */

+    II( c, d, a, b, x[14], S43, 0xab9423a7 );   /* 51 */

+    II( b, c, d, a, x[5], S44, 0xfc93a039 );    /* 52 */

+    II( a, b, c, d, x[12], S41, 0x655b59c3 );   /* 53 */

+    II( d, a, b, c, x[3], S42, 0x8f0ccc92 );    /* 54 */

+    II( c, d, a, b, x[10], S43, 0xffeff47d );   /* 55 */

+    II( b, c, d, a, x[1], S44, 0x85845dd1 );    /* 56 */

+    II( a, b, c, d, x[8], S41, 0x6fa87e4f );    /* 57 */

+    II( d, a, b, c, x[15], S42, 0xfe2ce6e0 );   /* 58 */

+    II( c, d, a, b, x[6], S43, 0xa3014314 );    /* 59 */

+    II( b, c, d, a, x[13], S44, 0x4e0811a1 );   /* 60 */

+    II( a, b, c, d, x[4], S41, 0xf7537e82 );    /* 61 */

+    II( d, a, b, c, x[11], S42, 0xbd3af235 );   /* 62 */

+    II( c, d, a, b, x[2], S43, 0x2ad7d2bb );    /* 63 */

+    II( b, c, d, a, x[9], S44, 0xeb86d391 );    /* 64 */

+

+    state[0] += a;

+    state[1] += b;

+    state[2] += c;

+    state[3] += d;

+

+    /*

+       Zeroize sensitive information.

+     */

+    MD5_memset( ( POINTER ) x, 0, sizeof( x ) );

+

+}

+

+/*

+   Encodes input (UINT4) into output (unsigned char). Assumes len is

+   a multiple of 4.

+ */

+static void

+Encode( output,

+        input,

+        len )

+unsigned char *output;

+UINT4 *input;

+unsigned int len;

+{

+    unsigned int i,

+    j;

+

+    for ( i = 0, j = 0; j < len; i++, j += 4 )

+    {

+        output[j] = ( unsigned char )( input[i] & 0xff );

+        output[j + 1] = ( unsigned char )( ( input[i] >> 8 ) & 0xff );

+        output[j + 2] = ( unsigned char )( ( input[i] >> 16 ) & 0xff );

+        output[j + 3] = ( unsigned char )( ( input[i] >> 24 ) & 0xff );

+    }

+

+}

+

+/*

+   Decodes input (unsigned char) into output (UINT4). Assumes len is

+   a multiple of 4.

+ */

+

+static void

+Decode( output,

+        input,

+        len )

+UINT4 *output;

+unsigned char *input;

+unsigned int len;

+{

+    unsigned int i,

+    j;

+

+    for ( i = 0, j = 0; j < len; i++, j += 4 )

+        output[i] =

+            ( ( UINT4 ) input[j] ) | ( ( ( UINT4 ) input[j + 1] ) << 8 ) |

+            ( ( ( UINT4 ) input[j + 2] ) << 16 ) |

+            ( ( ( UINT4 ) input[j + 3] ) << 24 );

+}

+

+/*

+   Note: Replace &quot;for loop&quot; with standard memcpy if possible.

+ */

+static void

+MD5_memcpy( output,

+            input,

+            len )

+POINTER output;

+POINTER input;

+unsigned int len;

+{

+    unsigned int i;

+

+    for ( i = 0; i < len; i++ )

+        output[i] = input[i];

+

+}

+

+/*

+   Note: Replace &quot;for loop&quot; with standard memset if possible.

+ */

+static void

+MD5_memset( output,

+            value,

+            len )

+POINTER output;

+int value;

+unsigned int len;

+{

+    unsigned int i;

+

+    for ( i = 0; i < len; i++ )

+        ( ( char * )output )[i] = ( char )value;

+

+}

+

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/memory.c b/ap/app/cwmp/netcwmp/libcwmp/src/memory.c
new file mode 100755
index 0000000..3378157
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/memory.c
@@ -0,0 +1,366 @@
+/************************************************************************

+ * Id: memory.c                                                         *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *  

+ ***********************************************************************/

+ 

+#include <stdio.h>

+#include <stdlib.h>

+#include <string.h>

+#include "cwmp/memory.h"

+#include "cwmp/log.h"

+

+#ifdef USE_CWMP_MEMORY

+

+#define TRLOCK()

+#define TRUNLOCK()

+

+

+enum {TR_ALIGN = 8};

+enum {TR_MAX_BYTES = 128};

+enum {TR_FREELISTS = 32}; // TR_MAX_BYTES/TR_ALIGN

+

+

+

+#define TR_THROW_ERROR_ALLOC()	do{ cwmp_log_error("TRC throw Alloc error. out of memory."); } while(0)

+#define TR_THROW_ERROR_MALLOC()	do{ cwmp_log_error("TRC throw Malloc error. out of memory."); } while(0)

+

+

+typedef union TRObj TRObj;

+

+union TRObj

+{

+    union TRObj* freeListLink;

+    char sessionData[1];    /* The session sees this. */

+};

+

+

+void (* TriMallocAllocSafeHandler)() = 0;

+

+

+static char*		TR_start_free;

+static char*		TR_end_free;

+static size_t		TR_heap_size;

+static TRObj*		TR_free_list[TR_FREELISTS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

+

+

+

+void * TRSafeRefill(size_t n);

+

+

+static INLINE void * TRMemStrdup(const char * src)

+{

+    size_t len;

+    char * dest;

+    if (!src)

+        return 0;

+    len = strlen(src);

+    dest = XMALLOC(len);

+    if (!dest)

+        return 0;

+    strncpy(dest,src, len);

+    return dest;

+}

+

+static void* TRSafeMalloc(size_t n)

+{

+    void (* TriMallocHandler)();

+    void * result;

+

+    for (;;)

+    {

+        TriMallocHandler = TriMallocAllocSafeHandler;

+        if (TriMallocHandler == 0)

+        {

+            TR_THROW_ERROR_ALLOC();

+        }

+        else

+        {

+            (*TriMallocHandler)();

+        }

+        result = TRmalloc(n);

+        if (result)

+        {

+            return result;

+        }

+        else

+        {

+            TR_THROW_ERROR_MALLOC();

+        }

+    }

+

+}

+

+

+static void* TRSafeRealloc(void * ptr, size_t n)

+{

+    void (* TriMallocHandler)();

+    void * result;

+

+    for (;;)

+    {

+        TriMallocHandler = TriMallocAllocSafeHandler;

+        if (TriMallocHandler == 0)

+        {

+            TR_THROW_ERROR_ALLOC();

+        }

+        else

+        {

+            (*TriMallocHandler)();

+        }

+        result = realloc(ptr, n);

+        if (result)

+            return result;

+    }

+}

+

+

+static void* TRMemAllocate(size_t n)

+{

+    void * result = TRmalloc(n);

+    if (0 == result)

+        result = TRSafeMalloc(n);

+    return result;

+}

+

+

+static void  TRMemDeallocate(void* ptr, size_t n )

+{

+    TRfree(ptr);

+}

+

+

+static void* TRMemReallocate(void * ptr, size_t old_size, size_t new_size)

+{

+    void * result = realloc(ptr, new_size);

+    if (0 == result) result = TRSafeRealloc(ptr, new_size);

+    return result;

+}

+

+

+static size_t	TRSafeRoundUp(size_t bytes)

+{

+    return (((bytes) + (size_t) TR_ALIGN-1) & ~((size_t) TR_ALIGN - 1));

+}

+

+

+

+static size_t	TRSafeFreeListIndex(size_t  bytes)

+{

+    return (((bytes) + (size_t)TR_ALIGN-1)/(size_t)TR_ALIGN - 1);

+}

+

+

+

+char * TRSafecwmp_chunk_tAlloc(size_t size, int * nobjs)

+{

+    char * result = 0;

+    size_t total_bytes = size * (*nobjs);

+    size_t bytes_left = TR_end_free - TR_start_free;

+

+    if (bytes_left > total_bytes)

+    {

+        result = TR_start_free;

+        TR_start_free += total_bytes;

+        return result;

+    }

+    else if (bytes_left > size)

+    {

+        (*nobjs) = (int)(bytes_left / size);

+        total_bytes = size * (*nobjs);

+        result = TR_start_free;

+        TR_start_free += total_bytes;

+        return result;

+    }

+    else

+    {

+        size_t bytes_to_get = 2 * total_bytes + TRSafeRoundUp(TR_heap_size >> 4);

+        if (bytes_left > 0)

+        {

+            TRObj ** my_free_list = TR_free_list + TRSafeFreeListIndex(bytes_left);

+            ((TRObj*)TR_start_free)->freeListLink = *my_free_list;

+            *my_free_list = (TRObj*)TR_start_free;

+        }

+        TR_start_free = (char *)TRmalloc(bytes_to_get);

+        if (TR_start_free == 0)

+        {

+            size_t i;

+            TRObj ** my_free_list;

+            TRObj * p;

+

+            for (i = size; i <= (size_t)TR_MAX_BYTES; i += (size_t)TR_ALIGN)

+            {

+                my_free_list = TR_free_list + TRSafeFreeListIndex(i);

+                p = *my_free_list;

+                if (p != 0)

+                {

+                    *my_free_list = p->freeListLink;

+                    TR_start_free = (char *)p;

+                    TR_end_free = TR_start_free + i;

+                    return TRSafecwmp_chunk_tAlloc(size, nobjs);

+                }

+            }

+            TR_end_free = 0;

+            TR_start_free = (char *)TRMemAllocate(bytes_to_get);

+

+        }

+        TR_heap_size += bytes_to_get;

+        TR_end_free = TR_start_free + bytes_to_get;

+        return(TRSafecwmp_chunk_tAlloc(size, nobjs));

+    }

+

+}

+

+

+void * TRSafeRefill(size_t n)

+{

+    int nobjs = 10;

+    char* chunk = TRSafecwmp_chunk_tAlloc(n, &nobjs);

+    TRObj** my_free_list;

+    TRObj* result;

+    TRObj* current_obj;

+    TRObj* next_obj;

+    int i;

+

+    if (1 == nobjs) return(chunk);

+    my_free_list = TR_free_list + TRSafeFreeListIndex(n);

+

+    /* Build free list in chunk */

+    result = (TRObj*)chunk;

+    *my_free_list = next_obj = (TRObj*)(chunk + n);

+    for (i = 1; ; i++)

+    {

+        current_obj = next_obj;

+        next_obj = (TRObj*)((char*)next_obj + n);

+        if (nobjs - 1 == i)

+        {

+            current_obj -> freeListLink = 0;

+            break;

+        }

+        else

+        {

+            current_obj -> freeListLink = next_obj;

+        }

+    }

+

+    return(result);

+}

+

+

+

+static void* TRAllocate(size_t n)

+{

+    void * result = 0;

+

+    if (n > (size_t)TR_MAX_BYTES)

+    {

+        result = TRMemAllocate(n);

+    }

+    else

+    {

+        TRObj ** my_free_list = TR_free_list + TRSafeFreeListIndex(n);

+        TRObj * res ;

+#ifndef __TRI_NOTHREADS__

+        TRLOCK();

+#endif

+

+        res = * my_free_list;

+        if (res == 0)

+            result = TRSafeRefill(TRSafeRoundUp(n));

+        else

+        {

+            (*my_free_list) = res->freeListLink;

+            result = res;

+        }

+#ifndef __TRI_NOTHREADS__

+        TRUNLOCK();

+#endif

+

+    }

+

+    return result;

+}

+

+/*

+ *

+ */

+

+static void TRDeallocate(void* ptr, size_t n)

+{

+    if (n > (size_t)TR_MAX_BYTES)

+    {

+        TRMemDeallocate(ptr,n);

+    }

+    else

+    {

+        TRObj ** my_free_list = TR_free_list + TRSafeFreeListIndex(n);

+        TRObj * q = (TRObj *)ptr;

+

+#ifndef __TRI_NOTHREADS__

+        TRLOCK();

+#endif

+

+        q->freeListLink = *my_free_list;

+        (*my_free_list) = q;

+

+#ifndef __TRI_NOTHREADS__

+        TRUNLOCK();

+#endif

+

+    }

+}

+

+/*

+ *

+ */

+

+static void* TRReallocate(void * ptr, size_t old_size, size_t new_size)

+{

+    void * result;

+    size_t copy_size;

+    if (old_size > (size_t) TR_MAX_BYTES && new_size > (size_t) TR_MAX_BYTES)

+    {

+        return realloc(ptr, new_size);

+    }

+    if (TRSafeRoundUp(old_size) == TRSafeRoundUp(new_size))

+        return ptr;

+    result = TRAllocate(new_size);

+    copy_size = new_size > old_size ? old_size : new_size;

+    memcpy(result,ptr, copy_size);

+    TRDeallocate(ptr, old_size);

+    return result;

+}

+

+/*

+ *

+ */

+

+

+

+void *	MemoryMalloc(size_t size)

+{

+    return TRAllocate(size);

+}

+void *	MemoryCalloc(size_t count, size_t size)

+{

+    return calloc(count , size);

+}

+void *	MemoryRealloc(void * ptr,size_t olds, size_t news)

+{

+    return TRReallocate(ptr,olds,news);

+}

+

+void 	MemoryFree(void* ptr, size_t size)

+{

+    TRDeallocate(ptr,size);

+}

+

+

+

+

+#endif

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/model.c b/ap/app/cwmp/netcwmp/libcwmp/src/model.c
new file mode 100755
index 0000000..411da04
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/model.c
@@ -0,0 +1,573 @@
+#include "cwmp/model.h"

+#include "cwmp/xmlet.h"

+#include "cwmp/log.h"

+#include "cwmp_private.h"

+

+

+

+

+int cwmp_model_copy_parameter_object(parameter_node_t * object_param, parameter_node_t * new_node, int instance_number)

+{

+    parameter_node_t * tmp_node, *curr_node, *next_newnode;

+

+//	FUNCTION_TRACE();

+	

+    if(!object_param || !new_node)

+    {

+        return CWMP_ERROR;

+    }

+    if(instance_number > 0)

+    {

+        memcpy(new_node, object_param, sizeof(parameter_node_t));

+        new_node->name = TRstrdup(TRitoa(instance_number));

+

+        new_node->child = NULL;

+        new_node->next_sibling = NULL;

+        for(tmp_node=object_param; tmp_node; tmp_node = tmp_node->next_sibling)

+        {

+            if(!tmp_node->next_sibling)

+            {

+                tmp_node->next_sibling = new_node;

+                break;

+            }

+

+        }

+    }

+

+    for(tmp_node=object_param->child, curr_node=NULL; tmp_node; tmp_node=tmp_node->next_sibling)

+    {

+        next_newnode = MALLOC(sizeof(parameter_node_t));

+		if(NULL == next_newnode)

+		{

+		    cwmp_log_error("next_newnode malloc return NULL");

+			goto fail;

+		}

+		memset(next_newnode, 0, sizeof(parameter_node_t));

+        memcpy(next_newnode, tmp_node, sizeof(parameter_node_t));

+        next_newnode->name = TRstrdup(tmp_node->name);

+        next_newnode->parent = new_node;

+        next_newnode->child = NULL;

+        next_newnode->next_sibling = NULL;

+

+        if(!curr_node)

+        {

+            new_node->child = next_newnode;

+        }

+        else

+        {

+            curr_node->next_sibling = next_newnode;

+        }

+

+        curr_node = next_newnode;

+

+        cwmp_model_copy_parameter_object(tmp_node, next_newnode, 0);

+

+

+    }

+

+    return CWMP_OK;

+

+fail:

+	//todo: if need to free other node ???

+	return CWMP_ERROR;

+}

+

+

+int cwmp_model_copy_parameter(parameter_node_t * param, parameter_node_t ** new_param, int instance_number)

+{

+    int rv;

+    if(!param)

+        return CWMP_OK;

+

+//	FUNCTION_TRACE();

+

+    parameter_node_t * object_param = param->child; //object param like InternetGatewayDevice.WANDevice.{i}

+

+    parameter_node_t * new_node = MALLOC(sizeof(parameter_node_t));

+

+    if (!new_node)

+    {

+        cwmp_log_error("cwmp model copy parameter malloc new node failed ...\n");

+        return CWMP_ERROR;

+    }

+

+    new_node->name = NULL;

+    new_node->rw = 0;

+    new_node->type = 0;

+    new_node->value = NULL;

+    new_node->parent = param;

+

+    rv = cwmp_model_copy_parameter_object(object_param, new_node, instance_number);

+    *new_param = new_node;

+    if(rv != CWMP_OK)

+    {

+        cwmp_log_error("cwmp_model: copy parameter failed");

+        cwmp_model_free_parameter(new_node);

+        *new_param = NULL;

+    }

+    return rv;

+}

+

+int cwmp_model_free_parameter(parameter_node_t * param)

+{

+    parameter_node_t     *tmp_param = NULL;

+    parameter_node_t     *next_param = NULL;

+

+    if(!param)

+    {

+        return CWMP_OK;

+    }

+

+    for(tmp_param=param->child; tmp_param; tmp_param=next_param)

+    {

+        next_param = tmp_param->next_sibling;

+        cwmp_model_delete_parameter(tmp_param);

+    }

+

+    FREE(param->name);

+    FREE(param);

+

+    return CWMP_OK;

+}

+

+int cwmp_model_delete_parameter(parameter_node_t * param)

+{

+    parameter_node_t     *tmp_param = NULL;

+    parameter_node_t     *next_param = NULL;

+    parameter_node_t     *parent = NULL;

+

+//    FUNCTION_TRACE();

+	

+    if(!param)

+    {

+        return CWMP_OK;

+    }

+    parent = param->parent;

+    for(tmp_param=parent->child; tmp_param; tmp_param=tmp_param->next_sibling)

+    {

+        if(tmp_param->next_sibling == param)

+        {

+            tmp_param->next_sibling = param->next_sibling;

+            break;

+        }

+    }

+

+    return cwmp_model_free_parameter(param);

+}

+

+void* cwmp_model_find_func(model_func_t * func_list, int func_count, const char * func_name)

+{

+//    FUNCTION_TRACE();

+

+    if(!func_name || func_count <= 0)

+        return NULL;

+

+    model_func_t * f = func_list;

+    int i;

+

+//	FUNCTION_TRACE();

+	

+    for(i=0; i<func_count; i++, f++)

+    {

+        if(TRstrcasecmp(func_name, f->name) == 0)

+        {

+            return f->func;

+        }

+    }

+    return NULL;

+

+

+}

+

+int cwmp_model_create_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count,  pool_t * pool);

+

+

+int cwmp_model_init_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count, pool_t * pool)

+{

+//    FUNCTION_TRACE();

+

+    param->get = NULL;

+    param->set = NULL;

+    param->notify = NULL;

+    param->add = NULL;

+    param->del = NULL;

+    param->refresh = NULL;

+

+    if(!node)

+    {

+        return CWMP_OK;

+    }    

+

+    char *nodename = node->nodeName;

+    char *name, *value, *attr_nc;

+    if(! TRstrcasecmp(nodename, DEVICE_MODEL_OBJECT_NODE))

+    {

+        param->type = TYPE_OBJECT;

+    }

+    else

+    {

+        char * type = cwmp_xml_get_node_attribute(node, "type");

+        if(!type)

+        {

+            param->type = TYPE_STRING;

+        }

+        else

+        {

+            param->type = cwmp_get_type_value(type);

+        } 

+    }

+    name = cwmp_xml_get_node_attribute(node, "name");

+    if(!name)

+    {

+        return CWMP_ERROR;

+    }

+

+    param->name = TRstrdup(name);

+

+    value = cwmp_xml_get_node_attribute(node, "rw");

+    if(value)

+    {

+        param->rw = TRatoi(value);

+    }

+

+    memset(&(param->attr), 0, sizeof(parameter_node_attr_t));

+    attr_nc = cwmp_xml_get_node_attribute(node, "noti_rw");

+    if(attr_nc)

+    {

+        param->attr.nc = TRatoi(attr_nc);

+    }

+

+    //value = cwmp_xml_get_node_attribute(node, "value");

+    //if(value)

+    //{

+    //    param->value = pool_pstrdup(pool, value);

+    //}

+

+    if(param->type == TYPE_OBJECT)

+    {

+        value = cwmp_xml_get_node_attribute(node, "add_func");

+        if(value)

+        {

+            param->add = (parameter_add_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+

+

+        }

+

+        value = cwmp_xml_get_node_attribute(node, "del_func");

+        if(value)

+        {

+            param->del = (parameter_del_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+        }

+

+        value = cwmp_xml_get_node_attribute(node, "refresh_func");

+        if(value)

+        {

+            param->refresh = (parameter_refresh_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+        }

+    }

+    else

+    {

+        value = cwmp_xml_get_node_attribute(node, "get_func");

+        if(value)

+        {

+            param->get = (parameter_get_handler_pt) cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+        }

+

+        value = cwmp_xml_get_node_attribute(node, "set_func");

+        if(value)

+        {

+            param->set = (parameter_set_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+        }

+

+        value = cwmp_xml_get_node_attribute(node, "notify_func");

+        if(value)

+        {

+            param->notify = (parameter_notify_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;

+        }

+    }

+

+    return CWMP_OK;

+

+}

+

+int cwmp_model_create_child_parameter(parameter_node_t * child_param, xmlnode_t * child_node, model_func_t * func_list, int func_count, pool_t * pool)

+{

+//    FUNCTION_TRACE();

+    cwmp_model_create_parameter(child_param, child_node, func_list, func_count, pool);

+

+

+    xmlnode_t * next_node =  XmlNodeGetNextSibling(child_node);

+    parameter_node_t * last_sibling = child_param;

+    while(next_node)

+    {

+        parameter_node_t * next_param = (parameter_node_t *)pool_pcalloc(pool, sizeof(parameter_node_t));

+        if(NULL == next_param)

+		{

+            cwmp_log_error("next_param pool pcalloc return null");

+			return CWMP_ERROR;

+        }

+        cwmp_model_create_parameter(next_param, next_node, func_list, func_count, pool);

+

+        next_node = XmlNodeGetNextSibling(next_node);

+

+        next_param->parent = child_param;

+        last_sibling->next_sibling = next_param;

+        last_sibling = next_param;

+    }

+

+    return CWMP_OK;

+}

+

+

+int cwmp_model_create_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count,  pool_t * pool)

+{

+//    FUNCTION_TRACE(); 

+    cwmp_model_init_parameter(param, node, func_list, func_count, pool);

+

+    cwmp_log_debug("name %s: %p,%p", param->name, param->get, param->set);

+

+    xmlnode_t * child_node = XmlNodeGetFirstChild(node);

+    if(!child_node)

+    {

+        return CWMP_OK;

+    }

+

+    parameter_node_t * child_param = (parameter_node_t *)pool_pcalloc(pool, sizeof(parameter_node_t));

+	if(NULL == child_param)

+	{

+	    cwmp_log_error("child_param pool_pcalloc return NULL");

+		return CWMP_ERROR;

+	}

+	

+    cwmp_model_create_child_parameter(child_param, child_node, func_list, func_count,  pool);

+    param->child = child_param;

+    child_param->parent = param;

+

+    return CWMP_OK;

+}

+

+

+static int cwmp_model_init_object(cwmp_t * cwmp, parameter_node_t *param)

+{ 

+    parameter_node_t     *node = NULL;

+

+//	FUNCTION_TRACE();

+

+    if(!param)

+    {        

+        cwmp_log_error("input param is null");

+        return CWMP_ERROR;

+    }

+

+    if(param->type == TYPE_OBJECT && param->refresh)

+    {

+        param->refresh(cwmp, param, callback_register_task);

+    }

+

+    for (node = param->child; node; node = node->next_sibling)

+    {

+        cwmp_model_init_object(cwmp, node);

+    }

+

+    return CWMP_OK;

+}

+

+

+int cwmp_model_refresh_object(cwmp_t * cwmp, parameter_node_t *param, int flag, callback_register_func_t callback_reg)

+{

+    parameter_node_t     *node = NULL;

+

+//	FUNCTION_TRACE();

+

+    if(!param)

+    {        

+        return CWMP_ERROR;

+    }

+

+    if(param->refresh && flag == 1)

+    {

+         param->refresh(cwmp, param, callback_reg);

+    }

+

+    for (node = param->child; node; node = node->next_sibling)

+    {

+        if(TRstrcmp(param->name, "{i}") != 0)

+        {

+            cwmp_model_refresh_object(cwmp, node, 1, callback_reg);

+        }

+    }

+

+    return CWMP_OK;

+}

+

+

+

+

+int cwmp_model_load_parameter(cwmp_t * cwmp, xmldoc_t * doc, model_func_t * func_list, int func_count)

+{

+    pool_t * pool = cwmp->pool;

+

+

+    xmlnode_t *  root_node;

+    xmlnode_t *  model_node;

+    ASSERT(doc != NULL);

+    FUNCTION_TRACE();

+    root_node = XmlNodeGetFirstChild(& doc->node);

+    if (! root_node)

+    {

+        cwmp_log_error("xml document root is null!");

+        return CWMP_ERROR;

+    }

+

+    cwmp_log_debug("model load: xml node name is %s\n", root_node->nodeName); 

+    model_node = cwmp_xml_get_child_with_name(root_node, DEVICE_MODEL_NODE);

+    if (model_node == NULL)

+    {

+        cwmp_log_error("device model node not found!");

+        return CWMP_ERROR;

+    }

+

+    parameter_node_t * root_param = pool_pcalloc(pool, sizeof(parameter_node_t));

+    if(NULL == root_param)

+    {

+        cwmp_log_error("root_param pool_pcalloc return NULL!");

+        return CWMP_ERROR;        

+    }

+

+    cwmp_model_create_parameter(root_param, model_node, func_list, func_count, pool);

+

+    cwmp->root = root_param->child;

+

+	cwmp_log_info("init model object");

+

+    cwmp_model_init_object(cwmp, cwmp->root);

+

+	return CWMP_OK;

+}

+

+int cwmp_model_load_xml(cwmp_t * cwmp, const char * xmlfile, model_func_t * func_list, int func_count)

+{

+

+    xmldoc_t *  doc;

+    size_t xmllen, nread ;

+

+	FUNCTION_TRACE();

+

+#if 0

+    FILE * fp = fopen(xmlfile, "rb");

+    if(!fp)

+    {

+        cwmp_log_error("xmlfile is NULL\n");

+        return CWMP_ERROR;

+    }

+

+    fseek(fp, 0, SEEK_END);

+    xmllen = ftell(fp);

+	if(xmllen <= 0)

+	{

+        cwmp_log_error("ftell return %d!\n", xmllen);

+        fclose(fp); 

+		return CWMP_ERROR;	

+	}

+

+    char * buf = (char*)MALLOC(sizeof(char)*(xmllen+1));

+    if(!buf)

+    {

+        cwmp_log_error("model load: malloc fail\n");

+        fclose(fp); 

+		return CWMP_ERROR;

+    }

+

+	memset(buf, 0, sizeof(char)*(xmllen+1));

+	

+    fseek(fp, 0, SEEK_SET);

+	

+    nread = fread(buf, 1, xmllen, fp);

+    if(nread <= 0)

+    {

+		fclose(fp);

+		FREE(buf);

+		return CWMP_ERROR;       

+    }

+#else

+    struct stat statbuf;

+    if(stat(xmlfile,&statbuf) < 0)

+    {

+    	cwmp_log_info("Get file len fail! file path:%s", xmlfile);

+    	return CWMP_ERROR;

+    }

+    int size=statbuf.st_size;

+    

+    char * buf = (char*)MALLOC(sizeof(char)*(size+1));

+    if(!buf)

+    {

+    	cwmp_log_error("model load: malloc fail\n");

+    	return CWMP_ERROR;

+    }

+    

+    memset(buf, 0, sizeof(char)*(size+1));

+    

+    FILE * fp = fopen(xmlfile, "rb");

+    if(!fp)

+    {

+    	cwmp_log_error("xmlfile is NULL\n");

+    	FREE(buf);

+    	return CWMP_ERROR;

+    }

+    

+    fseek(fp, 0, SEEK_SET);

+    

+    nread = fread(buf, 1, size, fp);

+    if(nread <= 0)

+    {

+    	fclose(fp);

+    	FREE(buf);

+    	return CWMP_ERROR;		 

+    }

+

+

+#endif

+

+    buf[nread] = 0;

+	

+    pool_t * pool = pool_create(POOL_DEFAULT_SIZE);

+    if(NULL == pool)

+    {

+        cwmp_log_error("pool create return null");

+		fclose(fp);

+		FREE(buf);

+		return CWMP_ERROR;

+    }

+	

+    doc = XmlParseBuffer(pool, buf);

+    if (!doc)

+    {

+        cwmp_log_error("model load: create doc faild\n");

+        goto finish;

+    }

+

+    cwmp_model_load_parameter(cwmp, doc, func_list, func_count);

+    //cwmp_model_load_inform_info(cwmp, doc);

+    //cwmp_model_load_device_info(cwmp, doc);

+

+finish:

+

+    fclose(fp);

+	

+//    if(NULL != buf)

+    {

+        FREE(buf);

+    }

+	

+//	if(NULL != pool)

+	{

+        pool_destroy(pool);

+	}

+	

+	return CWMP_OK;

+}

+

+

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/periodic.c b/ap/app/cwmp/netcwmp/libcwmp/src/periodic.c
new file mode 100755
index 0000000..c4e7a5b
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/periodic.c
@@ -0,0 +1,139 @@
+

+#include "cwmp/periodic.h"

+#include "cwmp/cwmp.h"

+#include "cwmp/log.h"

+#include "soft_timer.h"

+

+extern cwmp_t *g_cwmp;

+

+#define PERIODIC_INFORM_TIMER   (30)

+

+#define PERIODIC_ENABLE_INI_TAG       "periodic:enable"

+#define PERIODIC_INTERVAL_INI_TAG     "periodic:interval"

+

+

+static void *periodic_inform_timer(void *arg)

+{

+    FUNCTION_TRACE();

+

+	cwmp_t *cwmp = g_cwmp;

+	

+    cwmp_log_info("push begin");

+

+    cwmp->new_request = CWMP_YES;

+    cwmp_event_set_value(g_cwmp, INFORM_PERIODIC, 1, 0, 0, 0, 0);

+

+    cwmp_log_info("push ok");

+

+    return NULL;

+}

+

+

+int cwmp_reset_periodic_inform(cwmp_t * cwmp)

+{

+    long result = 0;

+	

+	periodic_info_st * pperiodic = &(cwmp->periodic_info);

+

+	cwmp_log_info("Enable is: %d, interval:%d", pperiodic->PeriodicInformEnable, pperiodic->PeriodicInformInterval);

+

+	if(pperiodic->PeriodicInformEnable)

+	{

+	

+	    sc_timer_delete(PERIODIC_INFORM_TIMER);

+	    result = sc_timer_create(PERIODIC_INFORM_TIMER, 0, pperiodic->PeriodicInformInterval*1000, periodic_inform_timer, &cwmp);

+	}

+	

+	return result;

+}

+

+

+int cwmp_clean_periodic_inform(cwmp_t * cwmp)

+{

+    sc_timer_delete(PERIODIC_INFORM_TIMER);

+

+	return CWMP_OK;

+}

+

+

+int get_periodic_info_enable()

+{

+    int cwmp_enable = 0;

+    cwmp_enable = cwmp_conf_get_int(PERIODIC_ENABLE_INI_TAG);

+

+	if(0 == cwmp_enable)

+	{

+	    return 0;

+	}

+	else

+	{

+	    return 1;

+	}

+

+}

+

+

+int get_periodic_info_interval()

+{

+    int interval = 0;

+	

+	interval = cwmp_conf_get_int(PERIODIC_INTERVAL_INI_TAG);

+

+    if(0 != interval)

+    {

+        return interval;

+    }

+	else

+	{

+	    return 60;

+	}		

+}

+

+

+

+int set_periodic_info_enable(const char* value)

+{

+    return cwmp_conf_set(PERIODIC_ENABLE_INI_TAG, value);

+}

+

+

+int set_periodic_info_interval(const char* value)

+{

+    return cwmp_conf_set(PERIODIC_INTERVAL_INI_TAG, value);	

+}

+

+

+

+int update_periodic_info_enable(cwmp_t* cwmp)

+{

+    periodic_info_st *pperiodic_info = &(cwmp->periodic_info);

+

+	pperiodic_info->PeriodicInformEnable = get_periodic_info_enable();

+

+	return CWMP_OK;

+}

+

+

+

+int update_periodic_info_interval(cwmp_t* cwmp)

+{

+    periodic_info_st *pperiodic_info = &(cwmp->periodic_info);

+

+    pperiodic_info->PeriodicInformInterval = get_periodic_info_interval();

+	

+	return CWMP_OK;

+}

+

+

+

+int ini_periodic_info(cwmp_t* cwmp)

+{

+    update_periodic_info_enable(cwmp);

+

+    update_periodic_info_interval(cwmp);

+

+	return CWMP_OK;

+}

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/queue.c b/ap/app/cwmp/netcwmp/libcwmp/src/queue.c
new file mode 100755
index 0000000..0317708
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/queue.c
@@ -0,0 +1,155 @@
+/* Papastefanos Serafeim */

+/* Ulopoihsh sunarthsewn ouras */

+

+#include <cwmp/cwmp.h>

+#include "cwmp/queue.h"

+#include "cwmp/log.h"

+

+

+

+void queue_add(queue_t *q, void * data, int type, int priority, void * arg1, void *arg2) {

+	qnode_t *node;

+	

+	node = (qnode_t *)MALLOC(sizeof(qnode_t));

+

+	if(node == NULL) {

+		cwmp_log_error("malloc null");

+		return ;

+	}

+

+	node->data = data;

+	node->arg1 = arg1;

+	node->arg2 = arg2;

+	node->datatype = type;

+	node->priority = priority;

+	node->next = NULL;

+

+	pthread_mutex_lock(&q->mutex);

+

+	q->size += 1;

+

+	if(q->first==NULL) {

+		q->first = node;

+		q->last = node;

+	} else {

+		qnode_t * first = q->first;

+		if(priority >= first->priority)

+		{

+			node->next = first->next;

+			q->first = node;

+		}

+		else

+		{

+			q->last->next = node;

+			q->last = node;

+		}

+	}

+

+	pthread_mutex_unlock(& q->mutex);

+	

+}

+

+void queue_push(queue_t *q, void * data, int type) {

+	qnode_t *node;

+	

+	node = (qnode_t *)MALLOC(sizeof(qnode_t));

+

+	if(node == NULL) {

+		cwmp_log_error("malloc null");

+		return ;

+	}

+

+	node->data = data;

+	node->arg1 = NULL;

+	node->arg2 = NULL;

+	node->datatype = type;

+	node->priority = QUEUE_PRIORITY_COMMON;

+	node->next = NULL;

+

+	pthread_mutex_lock(&q->mutex);

+

+	q->size += 1;

+

+	if(q->first==NULL) {

+		q->first = node;

+		q->last = node;

+	} else {

+		q->last->next = node;

+		q->last = node;

+		

+	}

+

+	pthread_mutex_unlock(& q->mutex);

+	

+}

+

+

+void queue_view(queue_t *q) {

+	qnode_t *p;

+	p=q->first;

+	if(p==NULL) {

+		cwmp_log_debug("queue is empty.");

+		return; 

+	} else {

+		cwmp_log_debug("queue size = %d. ", q->size);

+		while(p->next!=NULL) {

+			cwmp_log_debug(" %s ", (char *)p->data);

+			p=p->next;

+		}

+		cwmp_log_debug(" %s ", (char *)p->data);

+	}

+}

+

+

+int queue_pop(queue_t *q, void ** data) {

+	if(q->first == NULL) { 

+		cwmp_log_debug("queue is empty.");

+		return -1;

+	}

+	qnode_t *p;

+	int type ;

+	pthread_mutex_lock(& q->mutex);

+	

+	void *c=q->first->data;

+	p=q->first;

+	type = p->datatype;

+	q->first=q->first->next;

+	if(q->first == NULL) q->last = NULL;

+	free(p);

+	q->size--;

+	pthread_mutex_unlock(& q->mutex);

+	

+	*data = c;

+	return type;

+}

+

+

+queue_t *queue_create(pool_t * pool) {

+	queue_t *queue = MALLOC(sizeof(queue_t));//(queue_t *)pool_pcalloc(pool, sizeof(queue_t) );

+	if(queue == NULL) return NULL;

+	queue->first = NULL;

+	queue->last = NULL;

+	queue->size = 0;

+	 

+	

+	pthread_mutex_init(& queue->mutex ,NULL);

+	

+	return queue;

+}

+

+/* Elegxei an h oura einai adeia */

+int queue_is_empty(queue_t *q) {

+	return (q->first == NULL);	

+}

+

+void queue_free(pool_t * pool, queue_t *q) {

+	pthread_mutex_lock(& q->mutex);

+	qnode_t *p = q->first;

+	while(p->next != NULL) {

+		qnode_t *r = p;

+		p=p->next;

+		free(r);

+	}

+	pthread_mutex_unlock(& q->mutex);

+	pool_pfree(pool, q);

+}

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/session.c b/ap/app/cwmp/netcwmp/libcwmp/src/session.c
new file mode 100755
index 0000000..3d14ec9
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/session.c
@@ -0,0 +1,1580 @@
+/************************************************************************

+ * Id: session.c                                                        *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014 netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *

+ ***********************************************************************/

+#include "cwmp/session.h"

+#include "cwmp/cfg.h"

+#include "cwmp/log.h"

+#include "cwmp/cwmp.h"

+#include "cwmp_private.h"

+

+#include "cfg_api.h"

+

+#include <stdlib.h>

+#include <stdarg.h>

+

+

+

+

+static cwmp_uint32_t g_cwmp_session_sequence = 0;

+

+static char g_cwmp_session_sequence_buffer[64];

+

+

+

+//static parameter_node_t * g_cwmp_session_root_parameters = NULL;

+

+

+#define ParameterFormatEnd  ".%s"

+#define ParameterFormat1    "%s"

+#define ParameterFormat2 ParameterFormat1 ParameterFormatEnd

+#define ParameterFormat3 ParameterFormat2 ParameterFormatEnd

+#define ParameterFormat4 ParameterFormat3 ParameterFormatEnd

+#define ParameterFormat5 ParameterFormat4 ParameterFormatEnd

+#define ParameterFormat6 ParameterFormat5 ParameterFormatEnd

+#define ParameterFormat7 ParameterFormat6 ParameterFormatEnd

+#define ParameterFormat8 ParameterFormat7 ParameterFormatEnd

+#define ParameterFormat9 ParameterFormat8 ParameterFormatEnd

+#define ParameterFormat10 ParameterFormat9 ParameterFormatEnd

+#define ParameterFormat11 ParameterFormat10 ParameterFormatEnd

+#define ParameterFormat12 ParameterFormat11 ParameterFormatEnd

+#define ParameterFormat13 ParameterFormat12 ParameterFormatEnd

+#define ParameterFormat14 ParameterFormat13 ParameterFormatEnd

+#define ParameterFormat15 ParameterFormat14 ParameterFormatEnd

+

+#define CWMP_PARAMETER_FORMATS_MAX 15

+

+static char * cwmp_parameter_formats_string[] =

+{

+    ParameterFormat1,

+    ParameterFormat2,

+    ParameterFormat3,

+    ParameterFormat4,

+    ParameterFormat5,

+    ParameterFormat6,

+    ParameterFormat7,

+    ParameterFormat8,

+    ParameterFormat9,

+    ParameterFormat10,

+    ParameterFormat11,

+    ParameterFormat12,

+    ParameterFormat13,

+    ParameterFormat14,

+    ParameterFormat15

+};

+

+

+

+

+static char * rpc_methods[] =

+{

+    "GetRPCMethods",

+    "SetParameterValues",

+    "GetParameterValues",

+    "GetParameterNames",

+    "SetParameterAttributes",

+    "GetParameterAttributes",

+    "AddObject",

+    "DeleteObject",

+    "Download",

+    "Upload",

+    "Reboot",

+    "FactoryReset",

+    "ScheduleInform"

+};

+

+

+

+#if 1

+

+char * cwmp_data_append_parameter_name(pool_t * pool, int count, ...)

+{

+	char buffer[1024] = {0};

+    char * p = NULL;

+    va_list ap = {0};

+

+	char * temp = NULL;

+	int total = 0;

+	int i = 0;

+

+

+    cwmp_log_info("count is :%d", count);

+

+

+	va_start(ap, count);

+	

+    for(i = 0; i<count && total < 1023; i++)

+    {

+        temp = va_arg(ap, char*);

+		

+        if(i == 0)

+        {

+            snprintf(buffer+total, 1024-total, "%s", temp);	

+			total += strlen(temp);

+        }

+		else

+		{

+		    snprintf(buffer+total, 1024-total, ".%s", temp);

+			total += strlen(temp)+1;

+		}

+    }

+

+	buffer[total] = '\0';

+	

+    va_end(ap);

+

+    p = pool_pcalloc(pool, strlen(buffer)+1);

+    if(NULL == p)

+	{

+		cwmp_log_error("p pool_pcalloc return null");

+	    return NULL;

+	}

+

+    memset(p, 0, strlen(buffer)+1 );

+    strncpy(p, buffer, strlen(buffer));		

+	

+    return p;

+}

+

+

+int cwmp_data_sprintf_parameter_name(char * buffer, int count, ...)

+{

+    char * format;

+    va_list ap = {0};

+

+	char * temp = NULL;

+	int total = 0;	

+	int i = 0;

+

+	

+    cwmp_log_info("count is :%d", count);

+

+	va_start(ap, count);

+	

+    for(i = 0; i<count && total < 1024; i++)

+    {

+        temp = va_arg(ap, char*);

+		

+        if(i == 0)

+        {

+            snprintf(buffer+total, 1024-total, "%s", temp);	

+			total += strlen(temp);

+        }

+		else

+		{

+		    snprintf(buffer+total, 1024-total, ".%s", temp);

+			total += strlen(temp)+1;

+		}

+    }

+

+	va_end(ap);	

+

+	buffer[total] = '\0';

+	

+    return total;

+}

+

+

+#else

+

+char * cwmp_data_append_parameter_name(pool_t * pool, int count, ...)

+{

+	char buffer[1024+1] = {0};

+    char * p = NULL;

+    char * format;

+    va_list ap = {0};

+	

+    if (count >0 && count <= CWMP_PARAMETER_FORMATS_MAX)

+    {

+        format = cwmp_parameter_formats_string[count-1];

+        va_start(ap, count);

+//        vsprintf(buffer, format, ap);

+		vsnprintf(buffer, 1024, format, ap);

+        va_end(ap);

+

+        p = pool_pcalloc(pool, strlen(buffer)+1);

+		if(NULL == p)

+		{

+		    cwmp_log_error("p pool_pcalloc return null");

+			return NULL;

+		}

+		

+        strncpy(p, buffer, strlen(buffer));	

+    }	

+	

+    return p;

+}

+

+int cwmp_data_sprintf_parameter_name(char * buffer, int count, ...)

+{

+    int rc = 0;

+    char * format;

+    va_list ap = {0};

+    if (count >0 && count <= CWMP_PARAMETER_FORMATS_MAX)

+    {

+        format = cwmp_parameter_formats_string[count-1];

+        va_start(ap, count);

+        //rc = vsprintf(buffer, format, ap);

+        rc = vsnprintf(buffer, 1024, format, ap);

+        va_end(ap);

+        buffer[rc] = 0;

+    }

+    return rc;

+}

+

+

+#endif

+

+

+

+

+

+

+#if 0

+char * cwmp_data_append_parameter_name(pool_t * pool, const char * format, ...)

+{

+

+    char buffer[1024] = {0};

+    char * p;

+    va_list ap;

+    va_start(ap, format);

+    vsprintf(buffer, format, ap);

+    va_end(ap);

+

+    p = pool_pcalloc(pool, strlen(buffer)+1);

+    strcpy(p, buffer);

+

+    return p;

+}

+

+int cwmp_data_sprintf_parameter_name(char * buffer, const char * format, ...)

+{

+    int rc;

+    va_list ap;

+    va_start(ap, format);

+    rc = vsprintf(buffer, format, ap);

+    va_end(ap);

+    buffer[rc] = 0;

+

+    return rc;

+}

+#endif

+

+

+

+char * cwmp_data_get_parameter_value(cwmp_t * cwmp, parameter_node_t * root, const char * name, pool_t * pool)

+{

+    parameter_node_t * node;

+    char * value = NULL;

+    int rc;

+

+

+    node = cwmp_get_parameter_node(root, name);

+    if (!node)

+    {

+        return NULL;

+    }

+

+    rc = cwmp_get_parameter_node_value(cwmp, node, name, &value, pool);

+    if(rc == 0)

+    {

+	    return value;

+    }

+    else

+    {

+	    return node->value;

+    }

+

+}

+

+int cwmp_data_set_parameter_value(cwmp_t * cwmp, parameter_node_t * root, const char * name, const char * value, int value_length, pool_t * pool)

+{

+    parameter_node_t * node;

+

+    node = cwmp_get_parameter_node(root, name);

+    if (!node)

+        return CWMP_ERROR;

+    return cwmp_set_parameter_node_value(cwmp, node, name, value, value_length);

+

+}

+

+char * cwmp_session_get_sequence(pool_t * pool)

+ {

+    g_cwmp_session_sequence++;

+    TRsnprintf(g_cwmp_session_sequence_buffer, 63, "%d", g_cwmp_session_sequence);

+    return g_cwmp_session_sequence_buffer;

+}

+

+int cwmp_session_get_localip(char *hostip)

+{

+#if 1

+

+    int family  = get_family_type();

+    char buf[128] = {0};

+    

+    if(family == AF_INET)

+    {

+        sc_cfg_get("wan1_ip", hostip, 256);

+    }

+    else if(family == AF_INET6)

+    {

+        sc_cfg_get("ipv6_br0_addr", buf, sizeof(buf));

+        snprintf(hostip, 256, "[%s]", buf);

+    }

+    else

+    {

+        sc_cfg_get("wan1_ip", hostip, 256);

+    }

+

+    cwmp_log_info("family type:%d, hostip:%s", family, hostip);

+

+    return CWMP_OK;

+

+#else

+#ifdef WIN32

+    /*    struct sockaddr addr;

+    	SOCKET fd;

+    	char local_ip_addr[20] = {0};

+    	int len = sizeof(addr);

+        ZeroMemory( &addr, sizeof(addr) );

+

+

+    	if(!hostip)

+                return -1;

+

+    	if((fd=socket(AF_INET,SOCK_DGRAM,0))>=0)

+        {

+    		if( getsockname( fd, &addr, &len ) )

+    		{

+    			len = WSAGetLastError();

+    		}

+

+    		TRsnprintf(local_ip_addr, 20, "%s", inet_ntoa( ((struct sockaddr_in*)&addr)->sin_addr ));

+    		TRstrcpy(hostip, local_ip_addr);

+    	}

+    */

+

+    char hostname[256];

+

+    struct hostent* pHostent;

+

+    struct sockaddr_in sa;

+

+    struct hostent he;

+    int i;

+

+    int res = gethostname(hostname, sizeof(hostname));

+    if (res != 0)

+    {

+        cwmp_log_error("Error: %u\n", WSAGetLastError());

+        return -1;

+    }

+    cwmp_log_debug("hostname=%s\n", hostname);

+    ////////////////

+    // ������������ȡ������Ϣ.

+    //

+

+

+    pHostent = gethostbyname(hostname);

+

+

+    if (pHostent==NULL)

+    {

+        cwmp_log_error("Error: %u\n", WSAGetLastError());

+        return -1;

+    }

+    //////////////////

+    // ���򷵻ص�hostent��Ϣ.

+    //

+

+    he = *pHostent;

+

+

+    cwmp_log_debug("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",

+                   he.h_name, he.h_aliases, he.h_addrtype, he.h_length);

+

+

+    for (i=0; he.h_addr_list[i]; i++)

+    {

+        memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[i],he.h_length);

+        // ����������IP��ַ.

+        cwmp_log_debug("Address: %s\n", inet_ntoa(sa.sin_addr)); // ��ʾ��ַ��

+        TRsnprintf(hostip, 20, "%s", inet_ntoa(sa.sin_addr));

+        break;

+    }

+

+#else

+    register int fd,intrface,retn=0;

+    struct ifreq buf[32];

+    struct ifconf ifc;

+    char domain_host[100] = {0};

+    char local_ip_addr[20] = {0};

+    char local_mac[20] = {0};

+    //Get Domain Name --------------------------------------------------

+    if (!hostip)

+        return -1;

+    if (getdomainname(&domain_host[0], 100) != 0)

+    {

+        cwmp_log_error("getdomainname  fail!");

+        return -1;

+    }

+    //------------------------------------------------------------------

+    //Get IP Address & Mac Address ----------------------------------------

+    if ((fd=socket(AF_INET,SOCK_DGRAM,0))>=0)

+    {

+        ifc.ifc_len=sizeof buf;

+        ifc.ifc_buf=(caddr_t)buf;

+        if (!ioctl(fd,SIOCGIFCONF,(char*)&ifc))

+        {

+            intrface=ifc.ifc_len/sizeof(struct ifreq);

+            while (intrface-->0)

+            {

+                if (!(ioctl(fd,SIOCGIFFLAGS,(char*)&buf[intrface])))

+                {

+                    if (buf[intrface].ifr_flags&IFF_PROMISC)

+                    {

+                        retn++;

+                    }

+                }

+                //Get IP Address

+                if (!(ioctl(fd,SIOCGIFADDR,(char*)&buf[intrface])))

+                {

+                    snprintf(local_ip_addr, sizeof(local_ip_addr), "%s", inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));

+                }

+                //Get Hardware Address

+#if 0

+                if (!(ioctl(fd,SIOCGIFHWADDR,(char*)&buf[intrface])))

+                {

+

+

+		    sprintf(local_mac,"%02x:%02x:%02x:%02x:%02x:%02x",

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],

+                            (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);

+

+                    break;

+                }

+#endif

+            }//While

+        }

+    }

+    if ( fd >= 0 )

+    {

+        close(fd);

+    }

+

+    strncpy(hostip, local_ip_addr, sizeof(local_ip_addr));

+

+	cwmp_log_info("hostip is :%s", hostip);

+#endif

+

+

+    return CWMP_OK;

+#endif

+}

+

+cwmp_session_t * cwmp_session_create(cwmp_t * cwmp)

+{

+    int rc1 = 0;

+	int rc2 = 0;

+	

+    pool_t * pool = pool_create(POOL_MIN_SIZE);

+    if(NULL == pool)

+    {

+        cwmp_log_error("pool create return null");

+		return NULL;

+    }

+	

+    cwmp_session_t * session = pool_pcalloc(pool, sizeof(cwmp_session_t));

+    if(NULL == session)

+    {

+        cwmp_log_error("session pool pcalloc return null");

+		pool_destroy(pool);

+		return NULL;

+    }

+

+	

+    session->env = pool_pcalloc(pool, sizeof(env_t));

+    if(NULL == session->env)

+    {

+        cwmp_log_error("session->env pool pcalloc return null");

+		pool_destroy(pool);

+		return NULL;

+    }

+	

+    session->env->cwmp = cwmp;

+    session->cwmp = cwmp;

+    rc1 = cwmp_chunk_create( &session->writers, pool);

+    rc2 = cwmp_chunk_create(&session->readers, pool);

+	if(CWMP_ENOMEM == rc1 || CWMP_ENOMEM == rc2)

+	{

+        cwmp_log_error("session->env pool pcalloc return null");

+		pool_destroy(pool);

+		return NULL;	    

+	}	

+

+    session->pool = pool;

+    session->status = 0;

+    session->newdata = 0;

+    session->timeout = 0;

+    session->envpool = NULL;

+    session->connpool = NULL;

+

+    session->root = cwmp->root;

+    session->retry_count = 0;

+

+	cwmp->session_running = SESSION_RUNNING;

+

+    return session;

+}

+

+void cwmp_session_free(cwmp_session_t * session)

+{

+    pool_t * pool = session->pool;

+

+    if (session->envpool)

+    {

+        pool_destroy(session->envpool);

+        session->envpool = NULL;

+    }

+    if (session->connpool)

+    {

+        pool_destroy(session->connpool);

+        session->connpool = NULL;

+    }

+    pool_destroy(pool);

+

+}

+

+int cwmp_session_close(cwmp_session_t * session)

+{

+    pool_destroy(session->envpool);

+    pool_destroy(session->connpool);

+    session->envpool = NULL;

+    session->connpool = NULL;

+

+    if(1 == session->cwmp->boot_flag)

+    {

+        session->cwmp->new_request = CWMP_YES;

+        cwmp_event_set_value(session->cwmp, INFORM_BOOT, 1, NULL, 0, 0, 0);

+		session->cwmp->boot_flag = 0;

+    }

+	

+    return 0;

+}

+

+int cwmp_session_open(cwmp_session_t * session)

+{

+

+    pool_t *envpool = pool_create(POOL_MIN_SIZE);

+	if(NULL == envpool)

+	{

+	    cwmp_log_error("pool create return null, size: POOL_MIN_SIZE");

+		return CWMP_ERROR;

+	}	

+

+    session->connpool = pool_create(POOL_MIN_SIZE);

+    if (!session->connpool)

+    {

+        cwmp_log_error("session init: create connection pool null.");

+		pool_destroy(envpool);

+        return CWMP_ERROR;

+    }

+    session->envpool = envpool;

+    session->env->pool = envpool;

+

+    //pool_cleanup_add(envpool, cwmp_chunk_clear, session->writers);

+    //pool_cleanup_add(envpool, cwmp_chunk_clear, session->readers);

+

+    return CWMP_OK;

+}

+

+static size_t cwmp_session_write_callback(char *data, size_t size, size_t nmemb, void * calldata)

+{

+    cwmp_session_t * session = (cwmp_session_t *)calldata;

+

+    cwmp_chunk_write_string(session->readers, data, size * nmemb, session->envpool);

+

+    return size * nmemb;

+}

+

+int cwmp_session_connect(cwmp_session_t * session, const char * url)

+{

+    http_dest_t *  dest;

+    int rv;

+

+    http_dest_create(&dest, url, session->connpool);

+    session->dest = dest;

+    cwmp_log_debug("session connect: dest url is %s, acs url is %s", dest->url, url);

+    rv = cwmp_session_create_connection(session);

+    if(rv != CWMP_OK)

+    {

+        return rv;

+    }

+    cwmp_session_set_headers(session, 0);

+

+    return CWMP_OK;

+}

+

+int cwmp_session_set_auth(cwmp_session_t * session, const char * user, const char * pwd)

+{

+    char buffer[256] = {0};

+    TRsnprintf(buffer, 255, "%s:%s", user==NULL?"":user, pwd==NULL?"":pwd);

+

+    session->dest->auth_type = HTTP_DIGEST_AUTH;

+    session->dest->auth.active = CWMP_FALSE;

+    TRstrncpy(session->dest->user, user, URL_USER_LEN);

+    TRstrncpy(session->dest->password, pwd, URL_PWD_LEN);

+

+    return CWMP_OK;

+}

+

+

+int cwmp_session_set_headers(cwmp_session_t * session, int postempty)

+{

+    return 0;

+}

+

+

+int cwmp_session_create_connection(cwmp_session_t * session)

+{

+    cwmp_t * cwmp = session->cwmp;

+    http_socket_t * sock;

+    int use_ssl = 0;

+    http_dest_t *  dest = session->dest;

+    int family = -1;

+    int rc = -1;

+    

+    if(dest)

+    {

+        if(strncmp(dest->scheme, "https", 5) == 0)

+        {

+            use_ssl = 1;

+        }

+    }

+	else

+	{

+	    cwmp_log_error("session->dest is null");

+	    return CWMP_ERROR;

+	}

+	

+    cwmp_log_info("session connect using ssl?(%s)\n", use_ssl==1?"yes":"no");

+

+    family = get_family_type();

+

+    if(family == -1)

+    {

+        cwmp_log_error("family error:%d.", family);

+        return CWMP_ERROR;;

+    }

+    

+    rc = http_socket_create(&sock, family, SOCK_STREAM, 0, session->connpool);

+    if (rc != CWMP_OK)

+    {

+        cwmp_log_error("session connect: create socket error.");

+        return rc;

+    }

+    

+    cwmp_log_debug("dest host: %s, dest port: %d", dest->host, dest->port);

+    

+    http_socket_set_sendtimeout(sock, 10);

+    

+    rc = http_socket_connect(sock, family, session->dest->host, session->dest->port);

+    if(rc != CWMP_OK)

+    {

+        cwmp_log_alert("connect to ACS faild. Host is %s:%d.", session->dest->host, session->dest->port);

+        return rc;

+    }

+

+

+    if(use_ssl)

+    {

+#ifdef USE_CWMP_OPENSSL

+        SSL *ssl = openssl_connect(cwmp->ssl_ctx, sock->sockdes);

+        if(ssl)

+        {

+           sock->ssl = ssl;

+           sock->use_ssl = 1;

+        }

+#endif

+

+        //check_cert(ssl,host);

+    }

+

+    http_socket_set_writefunction(sock, cwmp_session_write_callback, session);

+    if(session->timeout > 0)

+    {

+        http_socket_set_recvtimeout(sock, session->timeout);

+    }

+

+    session->sock = sock;

+

+    return CWMP_OK;

+}

+

+header_t * cwmp_session_create_header(cwmp_session_t * session, pool_t * pool)

+{

+    header_t * header;

+    FUNCTION_TRACE();

+

+    header = pool_palloc(pool, sizeof(header_t));

+    if(NULL == header)

+    {

+        cwmp_log_error("header pool palloc return null");

+		return NULL;

+    }

+	

+    header->hold_requests = 0;

+    header->id = cwmp_session_get_sequence(pool);

+    header->no_more_requests = 0;

+

+    // header->id array max length is 64, and get_Sequence get max length is:63

+    strncpy(session->id, header->id, 128-1);

+

+    return header;

+}

+

+device_id_t * cwmp_session_create_inform_device(cwmp_session_t * session, pool_t * pool)

+{

+    device_id_t * device;

+

+    char * name;

+    char * value;    

+

+    FUNCTION_TRACE();

+

+    device = pool_palloc(pool, sizeof(device_id_t));

+    if(NULL == device)

+    {

+        cwmp_log_error("device pool palloc return null");

+		return NULL;

+    }

+		

+    device->manufactorer = session->cwmp->cpe_mf;  //cwmp_get_parameter_value(InternetGatewayDeviceModule, DeviceInfoModule,ManufacturerModule);

+    device->oui          = session->cwmp->cpe_oui; //cwmp_get_parameter_value(InternetGatewayDeviceModule, DeviceInfoModule, ManufacturerOUIModule);

+    device->product_class = session->cwmp->cpe_pc; //cwmp_get_parameter_value(InternetGatewayDeviceModule, DeviceInfoModule, ProductClassModule);

+//    device->serial_number = session->cwmp->cpe_sn; //cwmp_get_parameter_value(InternetGatewayDeviceModule, DeviceInfoModule, SerialNumberModule);

+    device->name = session->cwmp->cpe_name;

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, SerialNumberModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    device->serial_number = value;

+    cwmp_log_info("serial_number is:%s:%s", value, device->serial_number);

+

+    return device;

+}

+

+parameter_list_t * cwmp_session_create_inform_parameters(cwmp_session_t * session, pool_t * pool)

+{

+    parameter_list_t * pl;

+

+    parameter_t * parameter;

+	

+    //parameter_node_t * parameterNode;

+    char * name;

+    char * value;

+

+    FUNCTION_TRACE();

+

+	if(NULL == pool)

+	{

+	    cwmp_log_error("pool is null");

+		return NULL;

+	}

+

+

+    pl = cwmp_create_parameter_list(session->env);

+	if(NULL == pl)

+	{

+	    cwmp_log_error("pl is null");

+		return NULL;

+	}	

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 2, InternetGatewayDeviceModule, DeviceSummaryModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, ManufacturerModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, ManufacturerOUIModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, ProductClassModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, SerialNumberModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, SpecVersionModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, HardwareVersionModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, SoftwareVersionModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, DeviceInfoModule, ProvisioningCodeModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, ConnectionRequestURLModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+	cwmp_log_debug("get request name:%s, value:%s", name, value);

+	

+    parameter = cwmp_create_parameter(session->env,  name,	value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, ParameterKeyModule);

+	value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, UsernameModule);

+	value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, PasswordModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, ConnectionRequestUsernameModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, ConnectionRequestPasswordModule);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    name    = CWMP_APPEND_PARAMETER_NAME(pool, 3, InternetGatewayDeviceModule, ManagementServerModule, URLModule);

+	value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+

+    name    = cwmp_conf_pool_get(pool, "cwmp:wan_interface");

+    if (name == NULL || TRstrcmp(name, "") == 0 )

+    {

+        cwmp_log_debug("cwmp:wan_interface name is: [%s]\n", name);

+        name    = CWMP_APPEND_PARAMETER_NAME(pool, 8, InternetGatewayDeviceModule, WANDeviceModule, "1", WANConnectionDeviceModule, "1", WANIPConnectionModule, "1", ExternalIPAddressModule);

+    }

+    cwmp_log_debug("cwmp:wan_interface new name is: [%s]\n", name);

+    value   = cwmp_data_get_parameter_value(session->cwmp, session->root, name, pool);

+    parameter = cwmp_create_parameter(session->env,  name, value, 0, TYPE_STRING);

+    cwmp_add_parameter_to_list(session->env,  pl, parameter);

+

+    return pl;

+

+}

+

+

+event_list_t * cwmp_session_create_inform_events(cwmp_session_t * session, pool_t * pool)

+{

+    event_list_t * el;

+    event_code_t * ev;

+//    int i = 0;

+

+    FUNCTION_TRACE();

+

+    el = cwmp_create_event_list(session->env, INFORM_MAX);

+	if(NULL == el)

+	{

+	    cwmp_log_error("create event list return null");

+		return NULL;

+	}

+

+

+#if 0

+    while (i<INFORM_MAX)

+    {

+

+        if (cwmp_conf_get_int(inf->key) == 1)

+        {

+            ev = cwmp_create_event_code(session->env);

+            ev->event = i;

+            ev->code = inf->code;

+

+            ev->command_key = 0;

+            if (i == INFORM_MREBOOT || i == INFORM_BOOTSTRAP)

+            {

+                ev->command_key = cwmp_conf_pool_get(session->env->pool, inf->command);

+            }

+

+            //cwmp_add_event_to_list(pool, el, ev);

+            el->events[el->count++] = ev;

+            ev = NULL;

+

+        }

+        i++;

+    }

+#endif

+

+    if (el->count == 0)

+    {

+        ev = cwmp_create_event_code(session->env);

+		if(NULL == ev)

+		{

+		    cwmp_log_error("cwmp_create_event_code return NULL");

+			return NULL;

+		}

+        ev->event = 1;

+        ev->code = CWMP_INFORM_EVENT_CODE_1;

+        el->events[el->count++] = ev;

+    }

+

+    return el;

+}

+

+

+

+datatime_t *cwmp_session_create_inform_datetimes(cwmp_session_t * session, pool_t * pool)

+{

+    struct tm *t;

+    time_t tn;

+    datatime_t *now;

+

+    //FUNCTION_TRACE();

+    tn = time(NULL);

+#ifdef WIN32

+    cwmp_log_debug("inform datatime");

+    //localtime_s(t, &tn);

+    memset(t, 0, sizeof(struct tm));

+#else

+    t = localtime(&tn);

+    if(NULL == t)

+    {

+        cwmp_log_error("localtime return NULL");

+		return NULL;

+    }

+#endif

+

+    now = pool_palloc(pool, sizeof(datatime_t));

+    if(NULL == now)

+    {

+        cwmp_log_error("pool_palloc return NULL");

+		return NULL;

+    }

+

+    now->year = t->tm_year + 1900;

+    now->month = t->tm_mon + 1;

+    now->day = t->tm_mday;

+    now->hour = t->tm_hour;

+    now->min = t->tm_min;

+    now->sec = t->tm_sec;

+

+    return now;

+}

+

+

+

+

+xmldoc_t *  cwmp_session_create_inform_message(cwmp_session_t * session, event_list_t * evtlist,  pool_t * pool)

+{

+    header_t * header;

+    device_id_t * device;

+    event_list_t * el;

+    datatime_t *now;

+    parameter_list_t * pl;

+

+    FUNCTION_TRACE();

+

+

+    header = cwmp_session_create_header(session, pool);

+    device  = cwmp_session_create_inform_device(session, pool);

+    if(NULL == device)

+    {

+        cwmp_log_error("cwmp_session_create_inform_device return null device");

+		return NULL;

+    }

+	

+    pl = cwmp_session_create_inform_parameters(session, pool);

+    if(NULL == pl)

+    {

+        cwmp_log_error("cwmp_session_create_inform_parameters return null pl");

+		return NULL;

+    }	

+

+    now = cwmp_session_create_inform_datetimes(session, pool);

+    if(NULL == now)

+    {

+        cwmp_log_error("cwmp_session_create_inform_datetimes return null now");

+		return NULL;

+    }		

+

+    return  cwmp_create_inform_message(session->env, header, device, evtlist, now, 1, session->retry_count, pl);

+

+#if 0

+    xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\" xmlns:cwmp=\"urn:dslforum-org:cwmp-1-0\"><SOAP-ENV:Header><cwmp:ID SOAP-ENV:mustUnderstand=\"1\">1</cwmp:ID></SOAP-ENV:Header><SOAP-ENV:Body><cwmp:Inform> <DeviceId xsi:type=\"cwmp:DeviceIdStruct\"> <Manufacturer xsi:type=\"xsd:string\">ZTE</Manufacturer> <OUI xsi:type=\"xsd:string\">00AA11</OUI> <ProductClass xsi:type=\"xsd:string\">ZXECS EBG2100</ProductClass> <SerialNumber xsi:type=\"xsd:string\">0410400AA11AA2255</SerialNumber> </DeviceId> <Event SOAP-ENC:arrayType=\"cwmp:EventStruct[1]\"> <EventStruct><EventCode xsi:type=\"xsd:string\">1 BOOT</EventCode><CommandKey></CommandKey> </EventStruct> </Event> <MaxEnvelopes xsi:type=\"xsd:unsignedInt\">1</MaxEnvelopes> <CurrentTime xsi:type=\"xsd:dateTime\">2009-05-14T14:05:00</CurrentTime> <RetryCount xsi:type=\"xsd:unsignedInt\">0</RetryCount> <ParameterList SOAP-ENC:arrayType=\"cwmp:ParameterValueStruct[13]\"> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceSummary</Name><Value xsi:type=\"xsd:string(1024)\">InternetGatewayDevice:1.0[](Baseline:1, EthernetLAN:1, WiFiLAN:1, EthernetWAN:1, Time:1, IPPing:1)</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.SpecVersion</Name><Value xsi:type=\"xsd:string(16)\">1.0</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.HardwareVersion</Name><Value xsi:type=\"xsd:string(64)\">2.2.0</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name><Value xsi:type=\"xsd:string(64)\">2.2.0.4</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name><Value xsi:type=\"xsd:string(64)\">cpea</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name><Value xsi:type=\"xsd:string(256)\">http://58.211.149.43:25100</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ParameterKey</Name><Value xsi:type=\"xsd:string(32)\">parameterkey</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.ExternalIPAddress</Name><Value xsi:type=\"xsd:string\">58.211.149.43</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.Username</Name><Value xsi:type=\"xsd:string(256)\">Navigator</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.Password</Name><Value xsi:type=\"xsd:string(256)\">navigator</Value> </ParameterValueStruct>  <ParameterValueStruct>  <Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestUsername</Name><Value xsi:type=\"xsd:string(256)\">bbms</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestPassword</Name><Value xsi:type=\"xsd:string(256)\">bbms</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.URL</Name><Value xsi:type=\"xsd:string(256)\">http://192.168.0.69:8000/otnms/acs/webservice.action</Value> </ParameterValueStruct> </ParameterList> </cwmp:Inform></SOAP-ENV:Body> </SOAP-ENV:Envelope>"

+#endif

+}

+

+xmldoc_t *  cwmp_session_create_transfercomplete_message(cwmp_session_t * session, event_code_t * evcode,  pool_t * pool)

+{

+    header_t * header;

+    device_id_t * device;

+    event_list_t * el;

+    datatime_t *now;

+    parameter_list_t * pl;

+

+    FUNCTION_TRACE();

+

+    header = cwmp_session_create_header(session, pool);

+

+    return  cwmp_create_transfercomplete_message(session->env, header, evcode);

+

+#if 0

+    xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\" xmlns:cwmp=\"urn:dslforum-org:cwmp-1-0\"><SOAP-ENV:Header><cwmp:ID SOAP-ENV:mustUnderstand=\"1\">1</cwmp:ID></SOAP-ENV:Header><SOAP-ENV:Body><cwmp:Inform> <DeviceId xsi:type=\"cwmp:DeviceIdStruct\"> <Manufacturer xsi:type=\"xsd:string\">ZTE</Manufacturer> <OUI xsi:type=\"xsd:string\">00AA11</OUI> <ProductClass xsi:type=\"xsd:string\">ZXECS EBG2100</ProductClass> <SerialNumber xsi:type=\"xsd:string\">0410400AA11AA2255</SerialNumber> </DeviceId> <Event SOAP-ENC:arrayType=\"cwmp:EventStruct[1]\"> <EventStruct><EventCode xsi:type=\"xsd:string\">1 BOOT</EventCode><CommandKey></CommandKey> </EventStruct> </Event> <MaxEnvelopes xsi:type=\"xsd:unsignedInt\">1</MaxEnvelopes> <CurrentTime xsi:type=\"xsd:dateTime\">2009-05-14T14:05:00</CurrentTime> <RetryCount xsi:type=\"xsd:unsignedInt\">0</RetryCount> <ParameterList SOAP-ENC:arrayType=\"cwmp:ParameterValueStruct[13]\"> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceSummary</Name><Value xsi:type=\"xsd:string(1024)\">InternetGatewayDevice:1.0[](Baseline:1, EthernetLAN:1, WiFiLAN:1, EthernetWAN:1, Time:1, IPPing:1)</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.SpecVersion</Name><Value xsi:type=\"xsd:string(16)\">1.0</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.HardwareVersion</Name><Value xsi:type=\"xsd:string(64)\">2.2.0</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.SoftwareVersion</Name><Value xsi:type=\"xsd:string(64)\">2.2.0.4</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.DeviceInfo.ProvisioningCode</Name><Value xsi:type=\"xsd:string(64)\">cpea</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name><Value xsi:type=\"xsd:string(256)\">http://58.211.149.43:25100</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ParameterKey</Name><Value xsi:type=\"xsd:string(32)\">parameterkey</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANIPConnection.1.ExternalIPAddress</Name><Value xsi:type=\"xsd:string\">58.211.149.43</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.Username</Name><Value xsi:type=\"xsd:string(256)\">Navigator</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.Password</Name><Value xsi:type=\"xsd:string(256)\">navigator</Value> </ParameterValueStruct>  <ParameterValueStruct>  <Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestUsername</Name><Value xsi:type=\"xsd:string(256)\">bbms</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.ConnectionRequestPassword</Name><Value xsi:type=\"xsd:string(256)\">bbms</Value> </ParameterValueStruct> <ParameterValueStruct><Name xsi:type=\"xsd:string\">InternetGatewayDevice.ManagementServer.URL</Name><Value xsi:type=\"xsd:string(256)\">http://192.168.0.69:8000/otnms/acs/webservice.action</Value> </ParameterValueStruct> </ParameterList> </cwmp:Inform></SOAP-ENV:Body> </SOAP-ENV:Envelope>";

+#endif

+}

+

+xmldoc_t *  cwmp_session_create_getrpcmethods_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+    return cwmp_create_getrpcmethods_response_message(session->env, header, rpc_methods, sizeof(rpc_methods)/sizeof(rpc_methods[0]));

+}

+

+xmldoc_t *  cwmp_session_create_getparameternames_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    char * path;

+    unsigned int next_level;

+    unsigned int next_subset;

+    parameter_node_t * node;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+		return NULL;

+    }

+

+    rv = cwmp_parse_getparameternames_message(session->env, doc, &path, &next_level, &fault);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("cwmp_parse_getparameternames_message fail \n");

+		return NULL;

+    }

+

+    cwmp_log_info("path:%s", path);

+

+    if (path != NULL && strlen(path) > 0 && path[strlen(path)-1] == '.')

+    {

+        next_subset = CWMP_YES;

+    }

+    else

+    {

+        next_subset = CWMP_NO;

+    }

+

+    node = cwmp_get_parameter_path_node(session->root, path);

+

+    return cwmp_create_getparameternames_response_message(session->env, header, path, node, next_subset, next_level);

+}

+

+

+xmldoc_t *  cwmp_session_create_getparametervalues_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    parameter_list_t * pl;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_getparametervalues_message(session->env, doc, session->root, &pl, &fault);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("cwmp_parse_getparametervalues_message fail \n");

+    }	

+

+    return cwmp_create_getparametervalues_response_message(session->env, header, pl);

+}

+

+xmldoc_t *  cwmp_session_create_setparametervalues_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    parameter_list_t * pl;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_setparametervalues_message(session->env, doc, session->root, &pl, &fault);

+

+    if(rv != CWMP_OK)

+    {

+        return cwmp_create_faultcode_setparametervalues_response_message(session->env, header, pl, &fault);

+    }

+

+    return cwmp_create_setparametervalues_response_message(session->env, header, 0);

+}

+

+xmldoc_t *  cwmp_session_create_download_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    char * key;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    download_arg_t * dlarg;

+

+    rv = cwmp_parse_download_message(session->env, doc, &dlarg, &fault);

+

+    //add download arg to taskqueue

+    //begin download process

+

+    if(rv == CWMP_OK)

+    {

+        download_arg_t * newdlarg = cwmp_clone_download_arg(dlarg);

+        if(newdlarg != NULL)

+        {

+            cwmp_t * cwmp = session->cwmp;

+             

+            queue_push(cwmp->queue, newdlarg, TASK_DOWNLOAD_TAG);

+             

+            cwmp_log_debug("push new download task to queue! url: %s ", newdlarg->url);

+        }

+    }

+ 

+    int status = 1;

+ 

+    return cwmp_create_download_response_message(session->env, header, status);

+}

+

+xmldoc_t *  cwmp_session_create_upload_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    char * key;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    upload_arg_t * uparg;

+

+    rv = cwmp_parse_upload_message(session->env, doc, &uparg, &fault);

+

+    if(rv == CWMP_OK)

+    {

+	    upload_arg_t * newularg = cwmp_clone_upload_arg(uparg);

+	    if(newularg)

+    	{

+    		cwmp_t * cwmp = session->cwmp;

+    		queue_push(cwmp->queue, newularg, TASK_UPLOAD_TAG);

+    		cwmp_log_debug("push new upload task to queue! url: %s ", newularg->url);

+    	}

+    }

+

+    int status = 1;

+    return cwmp_create_upload_response_message(session->env, header, status);

+

+}

+

+xmldoc_t *  cwmp_session_create_addobject_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    int instances = 0;

+	int status = 0;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+    rv = cwmp_parse_addobject_message(session->env, doc, session->root, &instances, &status,  &fault);

+    if(rv != CWMP_OK)

+    {

+        cwmp_log_error("cwmp_parse_addobject_message fail(%d) \n", rv);

+	    return cwmp_create_faultcode_response_message(session->env, header, &fault);

+    }

+

+    return cwmp_create_addobject_response_message(session->env, header, instances, status);

+}

+

+

+xmldoc_t *  cwmp_session_create_deleteobject_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv = CWMP_OK; 

+	int status = 0;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_deleteobject_message(session->env, doc, session->root, &status, &fault);

+

+    if(rv != CWMP_OK)

+    {

+	    return cwmp_create_faultcode_response_message(session->env, header, &fault);

+    }

+

+    return cwmp_create_deleteobject_response_message(session->env, header, status);

+}

+

+

+

+xmldoc_t *  cwmp_session_create_reboot_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    char * key;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_reboot_message(session->env, doc, &key, &fault);

+

+    cwmp_t * cwmp = session->cwmp;

+    queue_push(cwmp->queue, NULL, TASK_REBOOT_TAG);

+

+    return cwmp_create_reboot_response_message(session->env, header);

+}

+

+

+xmldoc_t *  cwmp_session_create_factoryreset_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    char * key;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    cwmp_t * cwmp = session->cwmp;

+    queue_push(cwmp->queue, NULL, TASK_FACTORYRESET_TAG);

+

+    return cwmp_create_factoryreset_response_message(session->env, header);

+}

+

+

+xmldoc_t *  cwmp_session_create_getparamterattributes_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    parameter_list_t * pl;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+		return NULL;

+    }

+

+    rv = cwmp_parse_getparameterattributes_message(session->env, doc, session->root, &pl, &fault);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("cwmp_parse_getparameterattributes_message fail\n");

+		return NULL;

+    }	

+

+    return cwmp_create_getparameterattributes_response_message(session->env, header, pl);

+}

+

+xmldoc_t *  cwmp_session_create_setparamterattributes_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool)

+{

+    header_t * header;

+    int rv;

+    parameter_list_t * pl;

+    fault_code_t fault;

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_setparameterattributes_message(session->env, doc, session->root, &pl, &fault);

+    if(rv != CWMP_OK)

+    {

+        return cwmp_create_faultcode_setparameterattributes_response_message(session->env, header, pl, &fault);

+    }

+

+    return cwmp_create_setparameterattributes_response_message(session->env, header, 0);

+}

+

+xmldoc_t *  cwmp_session_create_scheduleinform_response_message(cwmp_session_t * session, xmldoc_t * doc, pool_t * pool, schedule_inform_info_st *psi)

+{

+    header_t * header;

+    int rv;

+	fault_code_t fault;

+	

+    FUNCTION_TRACE();

+    rv = cwmp_parse_header_node(cwmp_get_header_node(doc), &header, pool);

+    if (rv != CWMP_OK)

+    {

+        cwmp_log_error("no header node \n");

+    }

+

+    rv = cwmp_parse_scheduleinform_message(session->env, doc, session->root, psi, &fault);

+

+	if(rv == CWMP_OK)

+	{

+	    cwmp_log_info("get schedule infom info ok");

+	}

+	else

+	{

+	    cwmp_log_info("get schedule inform error!");

+	}

+

+    return cwmp_create_scheduleinform_response_message(session->env, header);

+}

+

+

+

+

+int cwmp_session_send_request(cwmp_session_t * session)

+{

+    //    size_t length = cwmp_chunk_length(session->writers);

+    //    http_request_t * request;

+    //    http_request_create(&request, session->env->pool);

+    //    request->dest = session->dest;

+    //

+    //    http_post(session->sock, request, session->writers, session->env->pool);

+

+    int rv;

+    http_request_t * request;

+    FUNCTION_TRACE();

+

+    cwmp_log_debug("session dest url: %s", session->dest->url);

+

+

+    http_request_create(&request, session->envpool);

+    request->dest = session->dest;

+

+	cwmp_log_info("session->dest->auth_type:%d, session->dest->auth.active:%d", session->dest->auth_type, session->dest->auth.active);

+

+

+#if 0

+    if(session->dest->auth_type == HTTP_DIGEST_AUTH && !session->dest->auth.active)

+    {

+        cwmp_session_auth(session);

+    }

+

+    if(session->dest->auth_type == HTTP_DIGEST_AUTH)

+    {

+        if(!session->dest->auth.active)

+        {

+            //post empty

+            //receive msg

+            http_post(session->sock, request, NULL, session->envpool);

+            rv = cwmp_session_recv_response(session);

+			if(CWMP_OK != rv)

+			{

+			    cwmp_log_debug("cwmp_session_recv_reponse:%d", rv);

+			}

+        }

+    }

+#endif	

+

+    rv = http_post(session->sock, request, session->writers, session->envpool);

+

+    if (rv <= 0)

+    {

+        return CWMP_ERROR;

+    }

+    else

+    {

+        return CWMP_OK;

+    }

+}

+

+

+

+int cwmp_session_recv_response(cwmp_session_t * session)

+{

+    int respcode;

+    http_response_t * response, *response_auth;

+    char * auth;

+    char * cookie;

+	int rc;

+    //select session->sock

+    //if have new data, then read it

+

+	http_request_t * request;

+	int rv = 0;

+

+	FUNCTION_TRACE();

+

+    rc = http_response_create(&response, session->envpool);

+	if(CWMP_OK != rc)

+	{

+	    cwmp_log_error("http_response_create return :%d", rc);

+		return CWMP_ERROR;

+	}

+	

+    response->readers = session->readers;

+    respcode= http_read_response(session->sock, response, session->envpool);

+

+    session->last_code = response->status;

+

+    if(respcode != HTTP_200)

+    {

+        cwmp_log_error("http read response failed. return code is %d, %d", respcode, response->status);

+

+        if(response->status == 401 ||response->status == 407)

+        {

+            auth = http_get_variable(response->parser, "WWW-Authenticate");

+			

+            if(auth)

+            {

+                session->dest->auth.active = CWMP_FALSE;

+

+                http_parse_digest_auth(auth, &session->dest->auth, &session->dest->auth_type);

+

+				if(session->last_method == CWMP_INFORM_METHOD)

+				{

+					cookie = http_get_variable(response->parser, "Set-Cookie");

+					if(cookie)

+					{

+						http_parse_cookie(cookie, session->dest->cookie);

+						cwmp_log_info("cookie:%s", session->dest->cookie);

+					}

+				}

+

+//                if((session->dest->auth_type == HTTP_DIGEST_AUTH) &&(0 != TRstrcmp(session->dest->auth.realm, "")))

+                {

+                    

+        			http_request_create(&request, session->envpool);

+        			request->dest = session->dest;

+        

+        			rv = http_post(session->sock, request, session->writers, session->envpool);

+

+

+                    rc = http_response_create(&response_auth, session->envpool);

+                 	if(CWMP_OK != rc)

+                 	{

+                 	    cwmp_log_error("http_response_create return :%d", rc);

+                 		return CWMP_ERROR;

+                 	}					

+	     

+        			respcode= http_read_response(session->sock, response_auth, session->envpool);

+

+					session->last_code = response_auth->status;

+

+    				if(session->last_method == CWMP_INFORM_METHOD)

+    				{

+    					cookie = http_get_variable(response_auth->parser, "Set-Cookie");

+    					if(cookie)

+    					{

+    						http_parse_cookie(cookie, session->dest->cookie);

+    						cwmp_log_info("cookie:%s", session->dest->cookie);

+    					}

+    				}

+

+					if(respcode == HTTP_200)

+					{

+					    cwmp_log_info("auth response is :%d, status:%d", respcode, response_auth->status);

+					    session->dest->auth.active = CWMP_TRUE;

+

+						return CWMP_OK;

+					}					

+			

+                }			

+

+            }

+        }

+

+    }

+    else

+    {

+        cwmp_log_info("session->dest->auth.active true");

+        session->dest->auth.active = CWMP_TRUE;

+    }

+

+    if(session->last_method == CWMP_INFORM_METHOD)

+    {

+        cookie = http_get_variable(response->parser, "Set-Cookie");

+        if(cookie)

+        {

+            http_parse_cookie(cookie, session->dest->cookie);

+			cwmp_log_info("cookie:%s", session->dest->cookie);

+        }

+    }

+

+    if(respcode == HTTP_200)

+    {

+		return CWMP_OK;

+    }

+    else

+    {

+		return CWMP_ERROR;

+    }

+}

+

+

+

+

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/ssl.c b/ap/app/cwmp/netcwmp/libcwmp/src/ssl.c
new file mode 100755
index 0000000..888d433
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/ssl.c
@@ -0,0 +1,286 @@
+ 

+#include "cwmp/http.h"

+#include "cwmp/log.h"

+//#include "cwmp_private.h"

+#include <cwmp/md5.h>

+

+

+

+

+

+#ifdef USE_CWMP_OPENSSL

+

+static char openssl_password[32];

+

+

+BIO *bio_err=0;

+static char *pass;

+static int password_cb(char *buf,int num,

+  int rwflag,void *userdata);

+static void sigpipe_handle(int x);

+

+/* A simple error and exit routine*/

+int err_exit(string)

+  char *string;

+  {

+    cwmp_log_error("%s\n",string);

+    //exit(0);

+  }

+

+/* Print SSL errors and exit*/

+int berr_exit(string)

+  char *string;

+  {

+    cwmp_log_error("%s\n",string);

+    ERR_print_errors(bio_err);

+    //exit(0);

+  }

+

+/*The password code is not thread safe*/

+static int password_cb(char *buf,int num,

+  int rwflag,void *userdata)

+  {

+    if(num<strlen(pass)+1)

+      return(0);

+

+    strcpy(buf,pass);

+    return(strlen(pass));

+  }

+

+static void sigpipe_handle(int x){

+}

+

+SSL_CTX *openssl_initialize_ctx(char *keyfile,char *password)

+{

+    SSL_METHOD *meth;

+    SSL_CTX *ctx;

+    

+    if(!bio_err){

+      /* Global system initialization*/

+      SSL_library_init();

+      SSL_load_error_strings();

+      

+      /* An error write context */

+      bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);

+    }

+

+    /* Set up a SIGPIPE handler */

+    //signal(SIGPIPE,sigpipe_handle);

+    

+    /* Create our context*/

+    meth=SSLv23_method();

+    ctx=SSL_CTX_new(meth);

+

+    /* Load our keys and certificates*/

+    if(!(SSL_CTX_use_certificate_chain_file(ctx,

+      keyfile)))

+      berr_exit("Can't read certificate file");

+

+    pass=password;

+    SSL_CTX_set_default_passwd_cb(ctx,

+      password_cb);

+    if(!(SSL_CTX_use_PrivateKey_file(ctx,

+      keyfile,SSL_FILETYPE_PEM)))

+      berr_exit("Can't read key file");

+

+    /* Load the CAs we trust*/

+    /*

+    if(!(SSL_CTX_load_verify_locations(ctx,

+      CA_LIST,0)))

+      berr_exit("Can't read CA list");

+    */

+    

+#if (OPENSSL_VERSION_NUMBER < 0x00905100L)

+    SSL_CTX_set_verify_depth(ctx,1);

+#endif

+    

+    return ctx;

+  }

+     

+void openssl_destroy_ctx(ctx)

+  SSL_CTX *ctx;

+  {

+    SSL_CTX_free(ctx);

+  }

+

+

+SSL * openssl_connect(SSL_CTX * ctx, int fd)

+{

+    BIO *sbio;

+    SSL * ssl=SSL_new(ctx);

+            sbio=BIO_new_socket(fd,BIO_NOCLOSE);

+            SSL_set_bio(ssl,sbio,sbio);

+        

+            if(SSL_connect(ssl)<=0)

+            {

+              cwmp_log_alert("SSL connect error");

+              SSL_free(ssl);

+              return NULL;

+            }

+            else

+                {

+                cwmp_log_info("SSL connect to host ok.\n");  

+                     

+            }

+            

+      return ssl;

+    

+}

+

+

+

+/*

+

+static int openssl_password_cb(char *buf, int num, int rwflag, void *userdata)

+{    

+    if (num < strlen(openssl_password)+1)

+	    return 0;

+    

+    strcpy(buf,openssl_password);

+    return(strlen(buf));

+}

+

+

+void openssl_init(void) {

+    

+}

+

+

+

+int openssl_verify_callback(int ok, X509_STORE_CTX *store) {

+    char data[256];

+ 

+    if (!ok) {

+        X509 *cert = X509_STORE_CTX_get_current_cert(store);

+        int  depth = X509_STORE_CTX_get_error_depth(store);

+        int  err = X509_STORE_CTX_get_error(store);

+ 

+        cwmp_log_error("Error with certificate at depth: %i\n", depth);

+        X509_NAME_oneline(X509_get_issuer_name(cert), data, 256);

+        cwmp_log_error("issuer   = %s\n", data);

+        X509_NAME_oneline(X509_get_subject_name(cert), data, 256);

+        cwmp_log_error("subject  = %s\n", data);

+        cwmp_log_error("err %i:%s\n", err, X509_verify_cert_error_string(err));

+    }

+ 

+    return ok;

+}

+

+*/

+

+//SSL_CTX *openssl_setup_client_ctx(const char * cafile, const char * password) 

+//{

+//    SSL_CTX *ctx;

+//    

+//    if (!SSL_library_init()) {

+//        fprintf(stderr, "OpenSSL initialization failed!\n");

+//        cwmp_log_error("OpenSSL initialization failed!\n");

+//        exit(-1);

+//    }

+//    SSL_load_error_strings();

+// 

+//    ctx = SSL_CTX_new(SSLv3_client_method());

+//    

+//    /* Load our keys and certificates*/

+//    if(!(SSL_CTX_use_certificate_chain_file(ctx, cafile)))

+//    {

+//      cwmp_log_error("Can't read certificate file");

+//      

+//      //return NULL;

+//    }

+// 

+//    strncpy(openssl_password, password, 32);

+// 

+//    SSL_CTX_set_default_passwd_cb(ctx, openssl_password_cb);

+//    if(!(SSL_CTX_use_PrivateKey_file(ctx,

+//      cafile,SSL_FILETYPE_PEM)))

+//    {

+//      cwmp_log_error("Can't read key file");

+//      //return NULL

+//    }

+//

+//    /* Load the CAs we trust*/

+//    /*if(!(SSL_CTX_load_verify_locations(ctx, CA_LIST,0)))

+//    {

+//      cwmp_log_error("Can't read CA list");

+//      //return NULL

+//    }

+//    */

+//    SSL_CTX_set_verify_depth(ctx,4);

+//    SSL_CTX_set_options(ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);

+//    

+//    

+///*     

+//    

+//    if (SSL_CTX_load_verify_locations(ctx, cafile, cadir) != 1)

+//        cwmp_log_error("Error loading CA file and/or directory");

+//    if (SSL_CTX_set_default_verify_paths(ctx) != 1) 

+//        cwmp_log_error("Error loading default CA file and/or directory");

+//        

+//    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, openssl_verify_callback);

+//    SSL_CTX_set_verify_depth(ctx, 4);

+//    SSL_CTX_set_options(ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);

+//    if (SSL_CTX_set_cipher_list(ctx, CIPHER_LIST) != 1) 

+//        cwmp_log_error("Error setting cipher list (no valid ciphers)");

+//        

+//*/        

+//    cwmp_log_debug("init openssl success.\n");

+//    return ctx;

+//}

+

+

+

+int openssl_check_cert(SSL *ssl, char *host)  

+  {

+    X509 *peer;

+    char peer_CN[256];

+    

+    if(SSL_get_verify_result(ssl)!=X509_V_OK)

+    {

+      cwmp_log_error("Certificate doesn't verify");

+      //return CWMP_ERROR;

+    }

+

+    /*Check the cert chain. The chain length

+      is automatically checked by OpenSSL when

+      we set the verify depth in the ctx */

+

+    /*Check the common name*/

+    peer=SSL_get_peer_certificate(ssl);

+    X509_NAME_get_text_by_NID

+      (X509_get_subject_name(peer),

+      NID_commonName, peer_CN, 256);

+    if(strcasecmp(peer_CN,host))

+    {

+       cwmp_log_error("Common name doesn't match host name");

+       //return CWMP_ERROR;

+    }

+    

+    return CWMP_OK;

+    

+  }

+  

+

+//int http_socket_ssl_create(http_socket_t **news, SSL_CTX *ctx, pool_t * pool)

+//{

+//    int stat;

+//    stat = http_socket_calloc(news, pool);

+//    if (stat == CWMP_ERROR)

+//    {

+//        return CWMP_ERROR;

+//    }

+//    

+//    if(! (*news)->use_ssl)

+//    { 

+//        return CWMP_ERROR;    

+//    }

+//   

+//    (*news)->ssl = SSL_new(ctx);

+//    return CWMP_OK;

+//}

+//

+

+

+

+#endif

+

diff --git a/ap/app/cwmp/netcwmp/libcwmp/src/util.c b/ap/app/cwmp/netcwmp/libcwmp/src/util.c
new file mode 100755
index 0000000..74232bc
--- /dev/null
+++ b/ap/app/cwmp/netcwmp/libcwmp/src/util.c
@@ -0,0 +1,149 @@
+/************************************************************************

+ * Id: util.c                                                           *

+ *                                                                      *

+ * TR069 Project:  A TR069 library in C                                 *

+ * Copyright (C) 2013-2014  netcwmp.netcwmp group                                *

+ *                                                                      *

+ *                                                                      *

+ * Email: netcwmp ( & ) gmail dot com                                *

+ *                                                                      *  

+ ***********************************************************************/

+

+ 

+#include "cwmp/util.h"

+#include "cwmp/md5.h"

+

+static const char base64[] =

+    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

+    "abcdefghijklmnopqrstuvwxyz"

+    "0123456789+/";

+

+static char * base64_encode(const char *src)

+{

+    char *str, *dst;

+    size_t l;

+    int t, r;

+

+    l = strlen(src);

+    if ((str = malloc(((l + 2) / 3) * 4 + 1)) == 0)

+        return (void*)0;

+    dst = str;

+    r = 0;

+

+    while (l >= 3)

+    {

+        t = (src[0] << 16) | (src[1] << 8) | src[2];

+        dst[0] = base64[(t >> 18) & 0x3f];

+        dst[1] = base64[(t >> 12) & 0x3f];

+        dst[2] = base64[(t >> 6) & 0x3f];

+        dst[3] = base64[(t >> 0) & 0x3f];

+        src += 3;

+        l -= 3;

+        dst += 4;

+        r += 4;

+    }

+

+    switch (l)

+    {

+    case 2:

+        t = (src[0] << 16) | (src[1] << 8);

+        dst[0] = base64[(t >> 18) & 0x3f];

+        dst[1] = base64[(t >> 12) & 0x3f];

+        dst[2] = base64[(t >> 6) & 0x3f];

+        dst[3] = '=';

+        dst += 4;

+        r += 4;

+        break;

+    case 1:

+        t = src[0] << 16;

+        dst[0] = base64[(t >> 18) & 0x3f];

+        dst[1] = base64[(t >> 12) & 0x3f];

+        dst[2] = dst[3] = '=';

+        dst += 4;

+        r += 4;

+        break;

+    case 0:

+        break;

+    }

+

+    *dst = 0;

+    return (str);

+}

+

+static void cwmp_hex_to_string(char *to, const unsigned char *p, size_t len)

+{

+    const char  *hex = "0123456789abcdef";

+

+    for (;len--; p++)

+    {

+        *to++ = hex[p[0] >> 4];

+        *to++ = hex[p[0] & 0x0f];

+

+    }

+    *to = '\0';

+}

+

+

+

+void cwmp_MD5(char *buf, ...)

+{

+    unsigned char   hash[HASHLEN];

+    unsigned char   *p;

+    va_list ap = {0};

+    cwmp_MD5_CTX ctx;

+

+    cwmp_MD5Init(&ctx);

+

+    va_start(ap, buf);

+    while ((p = va_arg(ap, unsigned char *)) != NULL)

+    {

+        cwmp_MD5Update(&ctx, p, strlen((char *) p));

+    }

+    va_end(ap);

+

+    cwmp_MD5Final(hash, &ctx);

+

+    cwmp_hex_to_string(buf, hash, sizeof(hash));

+}

+

+

+

+void convert_to_hex(const char *Bin, char *Hex)

+{

+    unsigned short i;

+    unsigned char j;

+    for (i = 0; i < 16; i++)

+    {

+        j = (Bin[i] >> 4) & 0xf;

+        if (j <= 9)

+        {

+            Hex[i*2] = (j + '0');

+        }

+        else

+        {

+            Hex[i*2] = (j + 'a'-10);

+        }

+		

+        j = Bin[i] & 0xf;

+		

+        if (j <= 9)

+        {

+            Hex[i*2+1] = (j + '0');

+        }

+        else

+        {

+            Hex[i*2+1] = (j + 'a'-10);

+        }

+    }

+    Hex[32] = '\0';

+}

+

+

+

+

+

+

+

+

+

+