diff options
-rw-r--r-- | .cproject | 351 | ||||
-rw-r--r-- | .project | 110 | ||||
-rw-r--r-- | CMakeLists.txt | 68 | ||||
-rw-r--r-- | Kbuild.in | 2 | ||||
-rw-r--r-- | src/client/client.bak | 75 | ||||
-rw-r--r-- | src/client/client.c | 95 | ||||
-rw-r--r-- | src/config.h | 9 | ||||
-rw-r--r-- | src/include/types.h | 30 | ||||
-rw-r--r-- | src/kernel/main.c | 216 | ||||
-rw-r--r-- | src/kernel/main.c.bak | 322 | ||||
-rw-r--r-- | src/server/file.c | 58 | ||||
-rw-r--r-- | src/server/file.h | 7 | ||||
-rw-r--r-- | src/server/server.c | 173 | ||||
-rw-r--r-- | src/version.h | 7 |
14 files changed, 1523 insertions, 0 deletions
diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..f3c17a4 --- /dev/null +++ b/.cproject @@ -0,0 +1,351 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<?fileVersion 4.0.0?> + +<cproject> + <storageModule moduleId="org.eclipse.cdt.core.settings"> + <cconfiguration id="org.eclipse.cdt.core.default.config.1"> + <storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.1" moduleId="org.eclipse.cdt.core.settings" name="Configuration"> + <externalSettings/> + <extensions> + <extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/> + <extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/> + </extensions> + </storageModule> + <storageModule moduleId="org.eclipse.cdt.core.language.mapping"> + <project-mappings/> + </storageModule> + <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/> + <storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"> + <buildTargets> + <target name="[exe] dnbd3-server" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>dnbd3-server</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[exe] dnbd3-server/fast" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>dnbd3-server/fast</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name=": rebuild_cache" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>rebuild_cache</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name=": all" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>all</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name=": clean" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>clean</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[obj] src/server/file.c.o" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/file.c.o</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[pre] src/server/file.c.i" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/file.c.i</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[to asm] src/server/file.c.s" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/file.c.s</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[obj] src/server/main.c.o" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/main.c.o</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[pre] src/server/main.c.i" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/main.c.i</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + <target name="[to asm] src/server/main.c.s" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder"> + <buildCommand>/usr/bin/make</buildCommand> + <buildArguments/> + <buildTarget>src/server/main.c.s</buildTarget> + <stopOnError>true</stopOnError> + <useDefaultCommand>false</useDefaultCommand> + </target> + </buildTargets> + </storageModule> + </cconfiguration> + </storageModule> + <storageModule moduleId="cdtBuildSystem" version="4.0.0"> + <project id="dnbd3.null.1" name="dnbd3"/> + </storageModule> + <storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/> + <storageModule moduleId="scannerConfiguration"> + <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile"/> + <profile id="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile"> + <buildOutputProvider> + <openAction enabled="true" filePath=""/> + <parser enabled="true"/> + </buildOutputProvider> + <scannerInfoProvider id="specsFile"> + <runAction arguments="-E -P -v -dD ${plugin_state_location}/${specs_file}" command="/usr/bin/gcc" useDefault="true"/> + <parser enabled="true"/> + </scannerInfoProvider> + </profile> + <profile id="org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile"> + <buildOutputProvider> + <openAction enabled="true" filePath=""/> + <parser enabled="true"/> + </buildOutputProvider> + <scannerInfoProvider id="makefileGenerator"> + <runAction arguments="-f ${project_name}_scd.mk" command="/usr/bin/make" useDefault="true"/> + <parser enabled="true"/> + </scannerInfoProvider> + </profile> + </storageModule> + <storageModule moduleId="refreshScope"/> + <storageModule moduleId="org.eclipse.cdt.core.pathentry"> + <pathentry kind="mac" name="__STDC__" path="" value="1"/> + <pathentry kind="mac" name="__STDC_HOSTED__" path="" value="1"/> + <pathentry kind="mac" name="__GNUC__" path="" value="4"/> + <pathentry kind="mac" name="__GNUC_MINOR__" path="" value="5"/> + <pathentry kind="mac" name="__GNUC_PATCHLEVEL__" path="" value="2"/> + <pathentry kind="mac" name="__SIZE_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__PTRDIFF_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__WCHAR_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__WINT_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__INTMAX_TYPE__" path="" value="long long int"/> + <pathentry kind="mac" name="__UINTMAX_TYPE__" path="" value="long long unsigned int"/> + <pathentry kind="mac" name="__CHAR16_TYPE__" path="" value="short unsigned int"/> + <pathentry kind="mac" name="__CHAR32_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__SIG_ATOMIC_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__INT8_TYPE__" path="" value="signed char"/> + <pathentry kind="mac" name="__INT16_TYPE__" path="" value="short int"/> + <pathentry kind="mac" name="__INT32_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__INT64_TYPE__" path="" value="long long int"/> + <pathentry kind="mac" name="__UINT8_TYPE__" path="" value="unsigned char"/> + <pathentry kind="mac" name="__UINT16_TYPE__" path="" value="short unsigned int"/> + <pathentry kind="mac" name="__UINT32_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__UINT64_TYPE__" path="" value="long long unsigned int"/> + <pathentry kind="mac" name="__INT_LEAST8_TYPE__" path="" value="signed char"/> + <pathentry kind="mac" name="__INT_LEAST16_TYPE__" path="" value="short int"/> + <pathentry kind="mac" name="__INT_LEAST32_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__INT_LEAST64_TYPE__" path="" value="long long int"/> + <pathentry kind="mac" name="__UINT_LEAST8_TYPE__" path="" value="unsigned char"/> + <pathentry kind="mac" name="__UINT_LEAST16_TYPE__" path="" value="short unsigned int"/> + <pathentry kind="mac" name="__UINT_LEAST32_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__UINT_LEAST64_TYPE__" path="" value="long long unsigned int"/> + <pathentry kind="mac" name="__INT_FAST8_TYPE__" path="" value="signed char"/> + <pathentry kind="mac" name="__INT_FAST16_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__INT_FAST32_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__INT_FAST64_TYPE__" path="" value="long long int"/> + <pathentry kind="mac" name="__UINT_FAST8_TYPE__" path="" value="unsigned char"/> + <pathentry kind="mac" name="__UINT_FAST16_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__UINT_FAST32_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__UINT_FAST64_TYPE__" path="" value="long long unsigned int"/> + <pathentry kind="mac" name="__INTPTR_TYPE__" path="" value="int"/> + <pathentry kind="mac" name="__UINTPTR_TYPE__" path="" value="unsigned int"/> + <pathentry kind="mac" name="__GXX_ABI_VERSION" path="" value="1002"/> + <pathentry kind="mac" name="__SCHAR_MAX__" path="" value="127"/> + <pathentry kind="mac" name="__SHRT_MAX__" path="" value="32767"/> + <pathentry kind="mac" name="__INT_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__LONG_MAX__" path="" value="2147483647L"/> + <pathentry kind="mac" name="__LONG_LONG_MAX__" path="" value="9223372036854775807LL"/> + <pathentry kind="mac" name="__WCHAR_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__WCHAR_MIN__" path="" value="(-__WCHAR_MAX__ - 1)"/> + <pathentry kind="mac" name="__WINT_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__WINT_MIN__" path="" value="0U"/> + <pathentry kind="mac" name="__PTRDIFF_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__SIZE_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__CHAR_BIT__" path="" value="8"/> + <pathentry kind="mac" name="__INTMAX_MAX__" path="" value="9223372036854775807LL"/> + <pathentry kind="mac" name="__INTMAX_C(c)" path="" value="c ## LL"/> + <pathentry kind="mac" name="__UINTMAX_MAX__" path="" value="18446744073709551615ULL"/> + <pathentry kind="mac" name="__UINTMAX_C(c)" path="" value="c ## ULL"/> + <pathentry kind="mac" name="__SIG_ATOMIC_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__SIG_ATOMIC_MIN__" path="" value="(-__SIG_ATOMIC_MAX__ - 1)"/> + <pathentry kind="mac" name="__INT8_MAX__" path="" value="127"/> + <pathentry kind="mac" name="__INT16_MAX__" path="" value="32767"/> + <pathentry kind="mac" name="__INT32_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__INT64_MAX__" path="" value="9223372036854775807LL"/> + <pathentry kind="mac" name="__UINT8_MAX__" path="" value="255"/> + <pathentry kind="mac" name="__UINT16_MAX__" path="" value="65535"/> + <pathentry kind="mac" name="__UINT32_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__UINT64_MAX__" path="" value="18446744073709551615ULL"/> + <pathentry kind="mac" name="__INT_LEAST8_MAX__" path="" value="127"/> + <pathentry kind="mac" name="__INT8_C(c)" path="" value="c"/> + <pathentry kind="mac" name="__INT_LEAST16_MAX__" path="" value="32767"/> + <pathentry kind="mac" name="__INT16_C(c)" path="" value="c"/> + <pathentry kind="mac" name="__INT_LEAST32_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__INT32_C(c)" path="" value="c"/> + <pathentry kind="mac" name="__INT_LEAST64_MAX__" path="" value="9223372036854775807LL"/> + <pathentry kind="mac" name="__INT64_C(c)" path="" value="c ## LL"/> + <pathentry kind="mac" name="__UINT_LEAST8_MAX__" path="" value="255"/> + <pathentry kind="mac" name="__UINT8_C(c)" path="" value="c"/> + <pathentry kind="mac" name="__UINT_LEAST16_MAX__" path="" value="65535"/> + <pathentry kind="mac" name="__UINT16_C(c)" path="" value="c"/> + <pathentry kind="mac" name="__UINT_LEAST32_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__UINT32_C(c)" path="" value="c ## U"/> + <pathentry kind="mac" name="__UINT_LEAST64_MAX__" path="" value="18446744073709551615ULL"/> + <pathentry kind="mac" name="__UINT64_C(c)" path="" value="c ## ULL"/> + <pathentry kind="mac" name="__INT_FAST8_MAX__" path="" value="127"/> + <pathentry kind="mac" name="__INT_FAST16_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__INT_FAST32_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__INT_FAST64_MAX__" path="" value="9223372036854775807LL"/> + <pathentry kind="mac" name="__UINT_FAST8_MAX__" path="" value="255"/> + <pathentry kind="mac" name="__UINT_FAST16_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__UINT_FAST32_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__UINT_FAST64_MAX__" path="" value="18446744073709551615ULL"/> + <pathentry kind="mac" name="__INTPTR_MAX__" path="" value="2147483647"/> + <pathentry kind="mac" name="__UINTPTR_MAX__" path="" value="4294967295U"/> + <pathentry kind="mac" name="__FLT_EVAL_METHOD__" path="" value="2"/> + <pathentry kind="mac" name="__DEC_EVAL_METHOD__" path="" value="2"/> + <pathentry kind="mac" name="__FLT_RADIX__" path="" value="2"/> + <pathentry kind="mac" name="__FLT_MANT_DIG__" path="" value="24"/> + <pathentry kind="mac" name="__FLT_DIG__" path="" value="6"/> + <pathentry kind="mac" name="__FLT_MIN_EXP__" path="" value="(-125)"/> + <pathentry kind="mac" name="__FLT_MIN_10_EXP__" path="" value="(-37)"/> + <pathentry kind="mac" name="__FLT_MAX_EXP__" path="" value="128"/> + <pathentry kind="mac" name="__FLT_MAX_10_EXP__" path="" value="38"/> + <pathentry kind="mac" name="__FLT_MAX__" path="" value="3.40282346638528859812e+38F"/> + <pathentry kind="mac" name="__FLT_MIN__" path="" value="1.17549435082228750797e-38F"/> + <pathentry kind="mac" name="__FLT_EPSILON__" path="" value="1.19209289550781250000e-7F"/> + <pathentry kind="mac" name="__FLT_DENORM_MIN__" path="" value="1.40129846432481707092e-45F"/> + <pathentry kind="mac" name="__FLT_HAS_DENORM__" path="" value="1"/> + <pathentry kind="mac" name="__FLT_HAS_INFINITY__" path="" value="1"/> + <pathentry kind="mac" name="__FLT_HAS_QUIET_NAN__" path="" value="1"/> + <pathentry kind="mac" name="__DBL_MANT_DIG__" path="" value="53"/> + <pathentry kind="mac" name="__DBL_DIG__" path="" value="15"/> + <pathentry kind="mac" name="__DBL_MIN_EXP__" path="" value="(-1021)"/> + <pathentry kind="mac" name="__DBL_MIN_10_EXP__" path="" value="(-307)"/> + <pathentry kind="mac" name="__DBL_MAX_EXP__" path="" value="1024"/> + <pathentry kind="mac" name="__DBL_MAX_10_EXP__" path="" value="308"/> + <pathentry kind="mac" name="__DBL_MAX__" path="" value="((double)1.79769313486231570815e+308L)"/> + <pathentry kind="mac" name="__DBL_MIN__" path="" value="((double)2.22507385850720138309e-308L)"/> + <pathentry kind="mac" name="__DBL_EPSILON__" path="" value="((double)2.22044604925031308085e-16L)"/> + <pathentry kind="mac" name="__DBL_DENORM_MIN__" path="" value="((double)4.94065645841246544177e-324L)"/> + <pathentry kind="mac" name="__DBL_HAS_DENORM__" path="" value="1"/> + <pathentry kind="mac" name="__DBL_HAS_INFINITY__" path="" value="1"/> + <pathentry kind="mac" name="__DBL_HAS_QUIET_NAN__" path="" value="1"/> + <pathentry kind="mac" name="__LDBL_MANT_DIG__" path="" value="64"/> + <pathentry kind="mac" name="__LDBL_DIG__" path="" value="18"/> + <pathentry kind="mac" name="__LDBL_MIN_EXP__" path="" value="(-16381)"/> + <pathentry kind="mac" name="__LDBL_MIN_10_EXP__" path="" value="(-4931)"/> + <pathentry kind="mac" name="__LDBL_MAX_EXP__" path="" value="16384"/> + <pathentry kind="mac" name="__LDBL_MAX_10_EXP__" path="" value="4932"/> + <pathentry kind="mac" name="__DECIMAL_DIG__" path="" value="21"/> + <pathentry kind="mac" name="__LDBL_MAX__" path="" value="1.18973149535723176502e+4932L"/> + <pathentry kind="mac" name="__LDBL_MIN__" path="" value="3.36210314311209350626e-4932L"/> + <pathentry kind="mac" name="__LDBL_EPSILON__" path="" value="1.08420217248550443401e-19L"/> + <pathentry kind="mac" name="__LDBL_DENORM_MIN__" path="" value="3.64519953188247460253e-4951L"/> + <pathentry kind="mac" name="__LDBL_HAS_DENORM__" path="" value="1"/> + <pathentry kind="mac" name="__LDBL_HAS_INFINITY__" path="" value="1"/> + <pathentry kind="mac" name="__LDBL_HAS_QUIET_NAN__" path="" value="1"/> + <pathentry kind="mac" name="__DEC32_MANT_DIG__" path="" value="7"/> + <pathentry kind="mac" name="__DEC32_MIN_EXP__" path="" value="(-94)"/> + <pathentry kind="mac" name="__DEC32_MAX_EXP__" path="" value="97"/> + <pathentry kind="mac" name="__DEC32_MIN__" path="" value="1E-95DF"/> + <pathentry kind="mac" name="__DEC32_MAX__" path="" value="9.999999E96DF"/> + <pathentry kind="mac" name="__DEC32_EPSILON__" path="" value="1E-6DF"/> + <pathentry kind="mac" name="__DEC32_SUBNORMAL_MIN__" path="" value="0.000001E-95DF"/> + <pathentry kind="mac" name="__DEC64_MANT_DIG__" path="" value="16"/> + <pathentry kind="mac" name="__DEC64_MIN_EXP__" path="" value="(-382)"/> + <pathentry kind="mac" name="__DEC64_MAX_EXP__" path="" value="385"/> + <pathentry kind="mac" name="__DEC64_MIN__" path="" value="1E-383DD"/> + <pathentry kind="mac" name="__DEC64_MAX__" path="" value="9.999999999999999E384DD"/> + <pathentry kind="mac" name="__DEC64_EPSILON__" path="" value="1E-15DD"/> + <pathentry kind="mac" name="__DEC64_SUBNORMAL_MIN__" path="" value="0.000000000000001E-383DD"/> + <pathentry kind="mac" name="__DEC128_MANT_DIG__" path="" value="34"/> + <pathentry kind="mac" name="__DEC128_MIN_EXP__" path="" value="(-6142)"/> + <pathentry kind="mac" name="__DEC128_MAX_EXP__" path="" value="6145"/> + <pathentry kind="mac" name="__DEC128_MIN__" path="" value="1E-6143DL"/> + <pathentry kind="mac" name="__DEC128_MAX__" path="" value="9.999999999999999999999999999999999E6144DL"/> + <pathentry kind="mac" name="__DEC128_EPSILON__" path="" value="1E-33DL"/> + <pathentry kind="mac" name="__DEC128_SUBNORMAL_MIN__" path="" value="0.000000000000000000000000000000001E-6143DL"/> + <pathentry kind="mac" name="__REGISTER_PREFIX__" path="" value=" "/> + <pathentry kind="mac" name="__USER_LABEL_PREFIX__" path="" value=" "/> + <pathentry kind="mac" name="__VERSION__" path="" value=""4.5.2""/> + <pathentry kind="mac" name="__GNUC_GNU_INLINE__" path="" value="1"/> + <pathentry kind="mac" name="__NO_INLINE__" path="" value="1"/> + <pathentry kind="mac" name="__FINITE_MATH_ONLY__" path="" value=" "/> + <pathentry kind="mac" name="__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1" path="" value="1"/> + <pathentry kind="mac" name="__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2" path="" value="1"/> + <pathentry kind="mac" name="__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" path="" value="1"/> + <pathentry kind="mac" name="__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8" path="" value="1"/> + <pathentry kind="mac" name="__PRAGMA_REDEFINE_EXTNAME" path="" value="1"/> + <pathentry kind="mac" name="__SSP__" path="" value="1"/> + <pathentry kind="mac" name="__SIZEOF_INT__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_LONG__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_LONG_LONG__" path="" value="8"/> + <pathentry kind="mac" name="__SIZEOF_SHORT__" path="" value="2"/> + <pathentry kind="mac" name="__SIZEOF_FLOAT__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_DOUBLE__" path="" value="8"/> + <pathentry kind="mac" name="__SIZEOF_LONG_DOUBLE__" path="" value="12"/> + <pathentry kind="mac" name="__SIZEOF_SIZE_T__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_WCHAR_T__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_WINT_T__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_PTRDIFF_T__" path="" value="4"/> + <pathentry kind="mac" name="__SIZEOF_POINTER__" path="" value="4"/> + <pathentry kind="mac" name="__i386" path="" value="1"/> + <pathentry kind="mac" name="__i386__" path="" value="1"/> + <pathentry kind="mac" name="i386" path="" value="1"/> + <pathentry kind="mac" name="__i686" path="" value="1"/> + <pathentry kind="mac" name="__i686__" path="" value="1"/> + <pathentry kind="mac" name="__pentiumpro" path="" value="1"/> + <pathentry kind="mac" name="__pentiumpro__" path="" value="1"/> + <pathentry kind="mac" name="__gnu_linux__" path="" value="1"/> + <pathentry kind="mac" name="__linux" path="" value="1"/> + <pathentry kind="mac" name="__linux__" path="" value="1"/> + <pathentry kind="mac" name="linux" path="" value="1"/> + <pathentry kind="mac" name="__unix" path="" value="1"/> + <pathentry kind="mac" name="__unix__" path="" value="1"/> + <pathentry kind="mac" name="unix" path="" value="1"/> + <pathentry kind="mac" name="__ELF__" path="" value="1"/> + <pathentry kind="mac" name="__DECIMAL_BID_FORMAT__" path="" value="1"/> + <pathentry kind="mac" name="__BIGGEST_ALIGNMENT__" path="" value="16"/> + <pathentry kind="mac" name="_FORTIFY_SOURCE" path="" value="2"/> + <pathentry kind="mac" name="__cplusplus" path="" value="1"/> + <pathentry kind="mac" name="__GNUG__" path="" value="4"/> + <pathentry kind="mac" name="__GXX_WEAK__" path="" value="1"/> + <pathentry kind="mac" name="__DEPRECATED" path="" value="1"/> + <pathentry kind="mac" name="__GXX_RTTI" path="" value="1"/> + <pathentry kind="mac" name="__EXCEPTIONS" path="" value="1"/> + <pathentry kind="mac" name="__GCC_HAVE_DWARF2_CFI_ASM" path="" value="1"/> + <pathentry kind="mac" name="_GNU_SOURCE" path="" value="1"/> + <pathentry include="/home/jjl/Workspace/dnbd3" kind="inc" path="" system="true"/> + <pathentry include="/home/jjl/Workspace/dnbd3/build" kind="inc" path="" system="true"/> + <pathentry include="/usr/local/include" kind="inc" path="" system="true"/> + <pathentry include="/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include" kind="inc" path="" system="true"/> + <pathentry include="/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include-fixed" kind="inc" path="" system="true"/> + <pathentry include="/usr/include/i386-linux-gnu" kind="inc" path="" system="true"/> + <pathentry include="/usr/include/c++/4.5" kind="inc" path="" system="true"/> + <pathentry include="/usr/include/c++/4.5/i686-linux-gnu" kind="inc" path="" system="true"/> + <pathentry include="/usr/include/c++/4.5/backward" kind="inc" path="" system="true"/> + <pathentry include="/usr/include" kind="inc" path="" system="true"/> + <pathentry include="/lib/modules/3.0.0-12-generic-pae/build/include" kind="inc" path="" system="true"/> + <pathentry excluding="**/CMakeFiles/" kind="out" path=""/> + <pathentry kind="src" path=""/> + </storageModule> +</cproject> diff --git a/.project b/.project new file mode 100644 index 0000000..453481e --- /dev/null +++ b/.project @@ -0,0 +1,110 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>dnbd3</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.cdt.make.core.makeBuilder</name> + <triggers>clean,full,incremental,</triggers> + <arguments> + <dictionary> + <key>org.eclipse.cdt.core.errorOutputParser</key> + <value>org.eclipse.cdt.core.MakeErrorParser;org.eclipse.cdt.core.GCCErrorParser;org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GLDErrorParser;</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.append_environment</key> + <value>true</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.autoBuildTarget</key> + <value>all</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.arguments</key> + <value>-C /home/jjl/Data/Workspace/cpp/dnbd3/build</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.command</key> + <value>/usr/bin/make</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.location</key> + <value>/home/jjl/Data/Workspace/cpp/dnbd3/build</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.target.auto</key> + <value>all</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.target.clean</key> + <value>clean</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.build.target.inc</key> + <value>all</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.buildArguments</key> + <value></value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.buildLocation</key> + <value>/home/jjl/Workspace/dnbd3/build</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.cleanBuildTarget</key> + <value>clean</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.contents</key> + <value>org.eclipse.cdt.make.core.activeConfigSettings</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.enableAutoBuild</key> + <value>false</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.enableCleanBuild</key> + <value>true</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.enableFullBuild</key> + <value>true</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.enabledIncrementalBuild</key> + <value>true</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.environment</key> + <value>CMAKE_NO_VERBOSE=1|VERBOSE=1|</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.fullBuildTarget</key> + <value>all</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.stopOnError</key> + <value>true</value> + </dictionary> + <dictionary> + <key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key> + <value>false</value> + </dictionary> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.cdt.make.core.ScannerConfigBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.cdt.core.ccnature</nature> + <nature>org.eclipse.cdt.make.core.makeNature</nature> + <nature>org.eclipse.cdt.make.core.ScannerConfigNature</nature> + <nature>org.eclipse.cdt.core.cnature</nature> + </natures> +</projectDescription> diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..369238a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,68 @@ +################################################################################ +# GENERAL # +################################################################################ + +PROJECT(dnbd3) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0) + +SET(CMAKE_BUILD_TYPE Debug) +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64") +SET(CMAKE_C_FLAGS_RELEASE "-O2") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2" ) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) + +FIND_PACKAGE(Threads) + +################################################################################ +# CLIENT # +################################################################################ + +FILE(GLOB_RECURSE CLIENT_SRCS src/client/*.c) +ADD_EXECUTABLE(dnbd3-client ${CLIENT_SRCS}) + + + +################################################################################ +# SERVER # +################################################################################ + +FILE(GLOB_RECURSE SERVER_SRCS src/server/*.c) +ADD_EXECUTABLE(dnbd3-server ${SERVER_SRCS}) +TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT}) + + +################################################################################ +# MODULE # +################################################################################ + +SET(MODULE_NAME dnbd3) +SET(MODULE_FILE ${MODULE_NAME}.ko) +FILE(GLOB_RECURSE MODULE_SOURCE_FILES src/kernel/*.c) + +SET(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build") + +SET(KBUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -C ${KERNEL_DIR} + M=${CMAKE_BINARY_DIR} modules +) + +CONFIGURE_FILE(Kbuild.in ${CMAKE_BINARY_DIR}/Kbuild) + +FOREACH(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES}) + CONFIGURE_FILE(${MODULE_SOURCE_FILE} ${CMAKE_BINARY_DIR} COPYONLY) +ENDFOREACH( MODULE_SOURCE_FILE ) + +CONFIGURE_FILE(src/config.h ${CMAKE_BINARY_DIR} COPYONLY) +CONFIGURE_FILE(src/include/types.h ${CMAKE_BINARY_DIR}/include/types.h COPYONLY) + +ADD_CUSTOM_COMMAND( + OUTPUT ${CMAKE_BINARY_DIR}/${MODULE_FILE} + COMMAND ${KBUILD_COMMAND} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + DEPENDS ${MODULE_SOURCE_FILES} Kbuild.in + VERBATIM +) + +ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE}) + diff --git a/Kbuild.in b/Kbuild.in new file mode 100644 index 0000000..c16df74 --- /dev/null +++ b/Kbuild.in @@ -0,0 +1,2 @@ +obj-m := ${MODULE_NAME}.o +${MODULE_NAME}-objs += main.o
\ No newline at end of file diff --git a/src/client/client.bak b/src/client/client.bak new file mode 100644 index 0000000..bacfa52 --- /dev/null +++ b/src/client/client.bak @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <unistd.h> + +#include <math.h> + +#include "../include/types.h" + +#define FILE_SIZE 721127424 + +int main(int argc, char *argv[]) +{ + struct sockaddr_in server; + unsigned long addr; + int sock; + + // Create socket + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock < 0) + { + printf("ERROR: Socket failure\n"); + return EXIT_FAILURE; + } + + addr = inet_addr(HOST); + memcpy((char *) &server.sin_addr, &addr, sizeof(addr)); + server.sin_family = AF_INET; // IPv4 + server.sin_port = htons(PORT); // set port number + + // Connect to server + if (connect(sock, (struct sockaddr*) &server, sizeof(server)) < 0) + { + printf("ERROR: Connect failure\n"); + return EXIT_FAILURE; + } + + // Set data + struct dnbd3_request request; + struct dnbd3_reply reply; + request.num = 0; + + // Send to server + int i; + off_t blocks = FILE_SIZE / DNBD3_BLOCK_SIZE; + int e = log(DNBD3_BLOCK_SIZE) / log(2); // logarithmus dualis + + + for (i = 0; i < blocks; i++) + { + request.num = i << e; // multiplie by e + send(sock, (char *) &request, sizeof(request), 0); + recv(sock, &reply, DNBD3_BLOCK_SIZE, MSG_WAITALL); + write(STDOUT_FILENO, reply.data, DNBD3_BLOCK_SIZE); + } + + /* Fetch "rest" bytes */ + int rest = FILE_SIZE % DNBD3_BLOCK_SIZE; + if (rest != 0) + { + request.num = i * DNBD3_BLOCK_SIZE; + send(sock, (char *) &request, sizeof(request), 0); + recv(sock, &reply, sizeof(struct dnbd3_reply), MSG_WAITALL); + write(STDOUT_FILENO, reply.data, rest); + } + + close(sock); + return EXIT_SUCCESS; +} diff --git a/src/client/client.c b/src/client/client.c new file mode 100644 index 0000000..87f20d5 --- /dev/null +++ b/src/client/client.c @@ -0,0 +1,95 @@ +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <getopt.h> +#include <sys/ioctl.h> +#include <netinet/in.h> + +#include "../include/types.h" +#include "../version.h" + +void print_help(char* argv_0) +{ + printf("Usage: %s -H <host> -p <port> -d <device>\n", argv_0); + printf("Start the DNBD3 client.\n"); + printf("-H or --host \t\t Host running dnbd3-server.\n"); + printf("-p or --port \t\t Port used by server.\n"); + printf("-d or --device \t\t DNBD3 device name.\n"); + printf("-h or --help \t\t Show this help text and quit.\n"); + printf("-v or --version \t Show version and quit.\n"); + exit(EXIT_SUCCESS); +} + +void print_version() +{ + printf("Version: %s\n", VERSION_STRING); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + char *host = NULL; + char *port = NULL; + char *dev = NULL; + + int opt = 0; + int longIndex = 0; + static const char *optString = "H:p:d:hv?"; + static const struct option longOpts[] = + { + { "host", required_argument, NULL, 'H' }, + { "port", required_argument, NULL, 'p' }, + { "device", required_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, }; + + opt = getopt_long(argc, argv, optString, longOpts, &longIndex); + + while (opt != -1) + { + switch (opt) + { + case 'H': + host = optarg; + break; + case 'p': + port = optarg; + break; + case 'd': + dev = optarg; + break; + case 'h': + print_help(argv[0]); + break; + case 'v': + print_version(); + break; + case '?': + print_help(argv[0]); + } + opt = getopt_long(argc, argv, optString, longOpts, &longIndex); + } + + if (!host || !port || !dev) + { + printf("FATAL: Not enough information specified\n"); + exit(EXIT_FAILURE); + } + + int fd; + fd = open(dev, O_RDONLY); + + if (ioctl(fd, IOCTL_SET_HOST, host) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_PORT, port) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_CONNECT) < 0) + printf("ERROR: ioctl not successful\n"); + + close(fd); + + exit(EXIT_SUCCESS); +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..ec41da2 --- /dev/null +++ b/src/config.h @@ -0,0 +1,9 @@ +#ifndef CONFIG_H_ +#define CONFIG_H_ + +#define PORT 5003 // TODO: make obsolete + +#define KERNEL_SECTOR_SIZE 512 +#define DNBD3_BLOCK_SIZE 4096 + +#endif /* CONFIG_H_ */ diff --git a/src/include/types.h b/src/include/types.h new file mode 100644 index 0000000..ea34616 --- /dev/null +++ b/src/include/types.h @@ -0,0 +1,30 @@ +#ifndef TYPES_H_ +#define TYPES_H_ + +#include "../config.h" + +#define DNBD_MAGIC 'd' + +#define CMD_GET_BLOCK 1 +#define CMD_GET_SIZE 2 + +#define IOCTL_SET_HOST _IO(0xab, 1) +#define IOCTL_SET_PORT _IO(0xab, 2) +#define IOCTL_CONNECT _IO(0xab, 3) + +#pragma pack(1) +typedef struct dnbd3_request { + uint16_t cmd; + uint64_t offset; + uint64_t size; +} dnbd3_request_t; +#pragma pack(0) + +#pragma pack(1) +typedef struct dnbd3_reply { + uint16_t cmd; + uint64_t filesize; +} dnbd3_reply_t; +#pragma pack(0) + +#endif /* TYPES_H_ */ diff --git a/src/kernel/main.c b/src/kernel/main.c new file mode 100644 index 0000000..1802b5f --- /dev/null +++ b/src/kernel/main.c @@ -0,0 +1,216 @@ +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/blkdev.h> +#include <net/sock.h> + +// Own +#include "config.h" +#include "include/types.h" + +static int major; +static struct gendisk *disk; +static struct request_queue *dnbd3_queue; + +DEFINE_SPINLOCK( dnbd3_lock); + +static struct socket *_sock; +static struct dnbd3_request _dnbd3_request; +static struct dnbd3_reply _dnbd3_reply; + +static char* host; +static char* port; + +unsigned int inet_addr(char *str) +{ + int a, b, c, d; + char arr[4]; + sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d); + arr[0] = a; + arr[1] = b; + arr[2] = c; + arr[3] = d; + return *(unsigned int*) arr; +} + +void connect(void) +{ + if (!host || !port) + { + printk("ERROR: Host or port not set."); + return; + } + + // initialize socket + struct sockaddr_in sin; + if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &_sock) < 0) + { + printk("ERROR: dnbd3 couldn't create socket.\n"); + return; + } + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = inet_addr(host); + sin.sin_port = htons(simple_strtol(port, NULL, 10)); + if (kernel_connect(_sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0) + { + printk("ERROR: dnbd3 couldn't connect to given host.\n"); + return; + } + + // prepare message + struct msghdr msg; + struct kvec iov; + _sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE + + // send request + _dnbd3_request.cmd = CMD_GET_SIZE; + iov.iov_base = &_dnbd3_request; + iov.iov_len = sizeof(_dnbd3_request); + kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_request)); + + // receive replay + iov.iov_base = &_dnbd3_reply; + iov.iov_len = sizeof(_dnbd3_reply); + kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_reply), msg.msg_flags); + + // set filesize + printk("INFO: dnbd3 filesize: %llu\n", _dnbd3_reply.filesize); + set_capacity(disk, _dnbd3_reply.filesize >> 9); /* 512 Byte blocks */ +} + +void dnbd3_request(struct request_queue *q) +{ + if (!_sock) + return; + + struct request *req; + struct msghdr msg; + struct kvec iov; + + _sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE + + while ((req = blk_fetch_request(q)) != NULL) + { + if (req->cmd_type != REQ_TYPE_FS) + { + if (!__blk_end_request_cur(req, 0)) + req = blk_fetch_request(q); + continue; + } + + spin_unlock_irq(q->queue_lock); + if (rq_data_dir(req) == READ) + { + _dnbd3_request.cmd = CMD_GET_BLOCK; + _dnbd3_request.offset = blk_rq_pos(req) << 9; // *512 + _dnbd3_request.size = blk_rq_bytes(req); // blk_rq_bytes() Returns bytes left to complete in the entire request + + // send request + iov.iov_base = &_dnbd3_request; + iov.iov_len = sizeof(_dnbd3_request); + kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(_dnbd3_request)); + + // receive replay + struct req_iterator iter; + struct bio_vec *bvec; + rq_for_each_segment(bvec, req, iter) + { + iov.iov_base = kmap(bvec->bv_page) + bvec->bv_offset; + iov.iov_len = bvec->bv_len; + kernel_recvmsg(_sock, &msg, &iov, 1, bvec->bv_len, msg.msg_flags); + kunmap(bvec->bv_page); + } + } + + spin_lock_irq(q->queue_lock); + __blk_end_request_all(req, 0); + } +} + +int dnbd3_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + switch (cmd) + { + case IOCTL_SET_HOST: + host = (char *) arg; + break; + + case IOCTL_SET_PORT: + port = (char *) arg; + break; + + case IOCTL_CONNECT: + connect(); + break; + case BLKFLSBUF: + // TODO: if missing, hdparm tells "BLKFLSBUF failed: Operation not permitted". Figure out what this should do. + break; + + default: + return -1; + + } + return 0; +} + +struct block_device_operations dnbd3_ops = +{ .owner = THIS_MODULE, .ioctl = dnbd3_ioctl, }; + +static int __init dnbd3_init(void) +{ + // Init blkdev + if ((major = register_blkdev(0, "dnbd")) == 0) + { + printk("ERROR: dnbd3 register_blkdev failed.\n"); + return -EIO; + } + if (!(disk = alloc_disk(1))) + { + printk("ERROR: dnbd3 alloc_disk failed.\n"); + return -EIO; + } + disk->major = major; + disk->first_minor = 0; + sprintf(disk->disk_name, "dnbd0"); + set_capacity(disk, 0); + //set_disk_ro(disk, 1); + disk->fops = &dnbd3_ops; + + if ((dnbd3_queue = blk_init_queue(&dnbd3_request, &dnbd3_lock)) == NULL) + { + printk("ERROR: dnbd3 blk_init_queue failed.\n"); + return -EIO; + } + + blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set logical block size for the queue + disk->queue = dnbd3_queue; + + add_disk(disk); + printk("INFO: dnbd3 init successful.\n"); + return 0; +} + +static void __exit dnbd3_exit(void) +{ + if (_sock) + sock_release(_sock); + unregister_blkdev(major, "dnbd"); + del_gendisk(disk); + put_disk(disk); + blk_cleanup_queue(dnbd3_queue); + printk("INFO: dnbd3 exit.\n"); +} + +module_init( dnbd3_init); +module_exit( dnbd3_exit); +MODULE_LICENSE("GPL"); diff --git a/src/kernel/main.c.bak b/src/kernel/main.c.bak new file mode 100644 index 0000000..600ed3c --- /dev/null +++ b/src/kernel/main.c.bak @@ -0,0 +1,322 @@ +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/blkdev.h> + +// Own +#include "config.h" +#include "include/types.h" + +// Network +#include <net/sock.h> +//#include <linux/net.h> +//#include <net/ip.h> + +static int major; +static struct gendisk *disk; +static struct request_queue *dnbd3_queue; + +DEFINE_SPINLOCK( dnbd3_lock); + +struct socket *sock; +struct dnbd3_request r; +struct dnbd3_reply rp; + +uint64_t filesize; + +char* host; +char* port; + +// TODO: schauen obs nicht eine fertige gibt +static unsigned int inet_addr(char *str) +{ + int a, b, c, d; + char arr[4]; + sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d); + arr[0] = a; + arr[1] = b; + arr[2] = c; + arr[3] = d; + return *(unsigned int*) arr; +} + +static void connect() +{ + if (!host || !port) + { + printk("ERROR: Host or port not set."); + return; + } + + // Init kernel-socket +// static unsigned char address[4] = +// { 132, 230, 4, 29 }; + + struct sockaddr_in sin; + + if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock) < 0) + { + printk("DNBD3: Couldn't create socket.\n"); + } + sin.sin_family = AF_INET; + //memcpy(&sin.sin_addr.s_addr, address, 4); + sin.sin_addr.s_addr = inet_addr(host); + sin.sin_port = htons(simple_strtol(port, NULL, 10)); + if (kernel_connect(sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0) + { + printk("DNBD3: Couldn't connect.\n"); + return; + } + + // Ask for filesize + struct msghdr msg; + struct kvec iov; + int size, result; + void *buf = &r; + r.cmd = CMD_GET_SIZE; + size = sizeof(r); + do + { + sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + iov.iov_base = (char *) buf; + iov.iov_len = size; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE + + result = kernel_sendmsg(sock, &msg, &iov, 1, size); + + if (result <= 0) + { + if (result == 0) + result = -EPIPE; /* short read */ + break; + } + size -= result; + buf += result; + } while (size > 0); + buf = &rp; + size = sizeof(rp); + do + { + sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + iov.iov_base = (char *) buf; + iov.iov_len = size; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_NOSIGNAL; // No SIGPIPE + + result = kernel_recvmsg(sock, &msg, &iov, 1, size, msg.msg_flags); + + if (result <= 0) + { + if (result == 0) + result = -EPIPE; /* short read */ + break; + } + size -= result; + buf += result; + } while (size > 0); + + filesize = rp.num; + printk("Filesize: %llu\n", filesize); + + //filesize = 721127424; + set_capacity(disk, filesize >> 9); /* 512 Byte blocks */ + +} + +static void dnbd3_request(struct request_queue *q) +{ + if (!sock) + return; + + struct request *req; + unsigned long start, to_copy; + int size, result; + void *buf; + struct msghdr msg; + struct kvec iov; + + while ((req = blk_fetch_request(q)) != NULL) + { + if (req->cmd_type != REQ_TYPE_FS) + { + if (!__blk_end_request_cur(req, 0)) + req = blk_fetch_request(q); + continue; + } + start = blk_rq_pos(req) << 9; // *512 + + //to_copy = blk_rq_cur_bytes(req); // Returns bytes left to complete in the current segment + to_copy = blk_rq_bytes(req); // blk_rq_bytes() Returns bytes left to complete in the entire request + + spin_unlock_irq(q->queue_lock); + if ((start + to_copy) <= filesize) + { + if (rq_data_dir(req) == READ) + { + // Send msg + //printk("Send...\n"); + buf = &r; + r.cmd = CMD_GET_BLOCK; + r.num = start; + r.to_copy = to_copy; + size = sizeof(r); + do + { + sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + iov.iov_base = (char *) buf; + iov.iov_len = size; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE + + result = kernel_sendmsg(sock, &msg, &iov, 1, size); + + if (result <= 0) + { + if (result == 0) + result = -EPIPE; /* short read */ + break; + } + size -= result; + buf += result; + } while (size > 0); + + // Test - receive msg + struct req_iterator iter; + struct bio_vec *bvec; + rq_for_each_segment(bvec, req, iter) + { + void *kaddr = kmap(bvec->bv_page); + buf = kaddr + bvec->bv_offset; + size = bvec->bv_len; + do + { + sock->sk->sk_allocation = GFP_NOIO; // GFP_NOIO: blocking is possible, but no I/O will be performed. + iov.iov_base = buf; + iov.iov_len = size; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE + + result = kernel_recvmsg(sock, &msg, &iov, 1, size, + msg.msg_flags); + + if (result <= 0) + { + if (result == 0) + result = -EPIPE; /* short read */ + break; + } + size -= result; + buf += result; + + if (size > 0) + printk("SIZE > 0\n"); + + } while (size > 0); + + kunmap(bvec->bv_page); + } + + } + else + { + printk("ERROR: Write not supported."); + } + } + else + { + printk("ERROR: %ld not in range...\n", start + to_copy); + } + spin_lock_irq(q->queue_lock); + __blk_end_request_all(req, 0); + } +} + +static int dnbd3_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + switch (cmd) + { + + case IOCTL_SET_HOST: + host = (char *) arg; + break; + + case IOCTL_SET_PORT: + port = (char *) arg; + break; + + case IOCTL_CONNECT: + connect(); + break; + + default: + return -1; + + } + return 0; +} + +static struct block_device_operations dnbd3_ops = +{ .owner = THIS_MODULE, .ioctl = dnbd3_ioctl, }; + +static int __init dnbd3_init(void) +{ + + // Init blkdev + if ((major = register_blkdev(0, "dnbd")) == 0) + { + printk("dnbd3: can't get majornumber\n"); + return -EIO; + } + printk("major: %d\n", major); + if (!(disk = alloc_disk(1))) + { + printk("alloc_disk failed ...\n"); + goto out; + } + disk->major = major; + disk->first_minor = 0; + sprintf(disk->disk_name, "dnbd0"); + //set_capacity(disk, filesize >> 9); /* 512 Byte blocks */ + set_capacity(disk, 0); + disk->fops = &dnbd3_ops; + + if ((dnbd3_queue = blk_init_queue(&dnbd3_request, &dnbd3_lock)) == NULL) + goto out; + + blk_queue_logical_block_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set logical block size for the queue +// blk_queue_max_hw_sectors(dnbd3_queue, DNBD3_BLOCK_SIZE / KERNEL_SECTOR_SIZE); // set max sectors for a request for this queue, min 8 +// blk_queue_max_segments(dnbd3_queue, 1); // set max hw segments for a request for this queue +// blk_queue_max_segment_size(dnbd3_queue, DNBD3_BLOCK_SIZE); // set max segment size for blk_rq_map_sg, min 4096 + disk->queue = dnbd3_queue; + + add_disk(disk); + return 0; + out: return -EIO; +} + +static void __exit dnbd3_exit(void) +{ + if (sock) + sock_release(sock); + unregister_blkdev(major, "dnbd"); + del_gendisk(disk); + put_disk(disk); + blk_cleanup_queue(dnbd3_queue); +} + +module_init( dnbd3_init); +module_exit( dnbd3_exit); +MODULE_LICENSE("GPL"); diff --git a/src/server/file.c b/src/server/file.c new file mode 100644 index 0000000..c53ca15 --- /dev/null +++ b/src/server/file.c @@ -0,0 +1,58 @@ +#include <fcntl.h> +#include <errno.h> +#include "file.h" + +int file_open(char *filename) +{ + int fd = open(filename, O_RDONLY); + if (fd == -1) + return -1; + + struct stat st; + if (fstat(fd, &st) == -1) + return -1; + + return fd; +} + +int file_getsize(int fd, off_t *size) +{ + *size = lseek64(fd, 0, SEEK_END); + + if (*size == -1) + return -1; + + return 0; +} + +int file_read(int fd, void *buf, size_t size, off_t pos) +{ + off_t newpos = lseek(fd, pos, SEEK_SET); + + if (newpos == -1) + return -1; + + size_t nleft = size; + ssize_t nread; + char *ptr = buf; + + while (nleft > 0) + { + if ((nread = read(fd, ptr, nleft)) < 0) + { + if (errno == EINTR) + continue; + + return -1; + } + if (nread == 0) + { + break; + } + + nleft -= nread; + ptr += nread; + } + + return 0; +} diff --git a/src/server/file.h b/src/server/file.h new file mode 100644 index 0000000..e61849a --- /dev/null +++ b/src/server/file.h @@ -0,0 +1,7 @@ +#include <unistd.h> + +int file_open(char *filename); + +int file_getsize(int fd, off_t *size); + +int file_read(int fd, void *buf, size_t size, off_t pos); diff --git a/src/server/server.c b/src/server/server.c new file mode 100644 index 0000000..e38a0e3 --- /dev/null +++ b/src/server/server.c @@ -0,0 +1,173 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <signal.h> +#include <getopt.h> +#include <netinet/in.h> +#include <sys/sendfile.h> + +#include <pthread.h> + +#include "../include/types.h" +#include "../version.h" +#include "file.h" + +int file; + +void print_help(char* argv_0) +{ + + printf("Usage: %s [OPTIONS]...\n", argv_0); + printf("Start the DNBD3 server.\n"); + printf("-f or --file \t\t File to export.\n"); + printf("-h or --help \t\t Show this help text and quit.\n"); + printf("-v or --version \t Show version and quit.\n"); + exit(0); +} + +void print_version() +{ + printf("Version: %s\n", VERSION_STRING); + exit(0); +} + +void handle_sigpipe(int signum) +{ + printf("Program received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); + return; +} + +void *echo(void *client_socket) +{ + int sock = (int) client_socket; + struct dnbd3_request request; + struct dnbd3_reply reply; + uint16_t cmd; + off_t filesize; + + while (recv(sock, &request, sizeof(struct dnbd3_request), MSG_WAITALL) > 0) + { + cmd = request.cmd; +// char buf[request.size]; + switch (cmd) + { + case CMD_GET_SIZE: + reply.cmd = request.cmd; + file_getsize(file, &filesize); + reply.filesize = filesize; + send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); + break; + + case CMD_GET_BLOCK: +// printf("CMD: %i, Byte: %llu, Size: %llu\n",request.cmd, request.offset, request.size); +// file_read(file, buf, request.size, request.offset); +// send(sock, (char *) buf, request.size, 0); + sendfile(sock, file, (off_t *) &request.offset, request.size); + break; + + default: + ; + } + + } + close(sock); + printf("Client exit.\n"); + pthread_exit((void *)0); +} + +int main(int argc, char* argv[]) +{ + int opt = 0; + int longIndex = 0; + static const char *optString = "f:hv?"; + static const struct option longOpts[] = + { + { "file", required_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' } }; + + opt = getopt_long(argc, argv, optString, longOpts, &longIndex); + if (opt == -1) + print_help(argv[0]); + + while (opt != -1) + { + switch (opt) + { + case 'f': + file = file_open(optarg); + break; + case 'h': + print_help(argv[0]); + break; + case 'v': + print_version(); + break; + case '?': + exit(1); + } + opt = getopt_long(argc, argv, optString, longOpts, &longIndex); + } + + signal(SIGPIPE, handle_sigpipe); + + struct sockaddr_in server; + struct sockaddr_in client[50]; + int sock, fd; + unsigned int len; + pthread_t thread[50]; + int i=1; + + // Create socket + sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock < 0) + { + printf("ERROR: Socket failure\n"); + exit(EXIT_FAILURE); + } + + memset(&server, 0, sizeof(server)); + server.sin_family = AF_INET; // IPv4 + server.sin_addr.s_addr = htonl(INADDR_ANY); // Take all IPs + server.sin_port = htons(PORT); // set port number + + // Bind to socket + if (bind(sock, (struct sockaddr*) &server, sizeof(server)) < 0) + { + printf("ERROR: Bind failure\n"); + exit(EXIT_FAILURE); + } + + // Listen on socket + if (listen(sock, 50) == -1) + { + printf("ERROR: Listen failure\n"); + exit(EXIT_FAILURE); + } + + printf("INFO: Server is ready...\n"); + + // TODO: dyn threads + while (1) + { + len = sizeof(client); + fd = accept(sock, (struct sockaddr*) &client[i], &len); + if (fd < 0) + { + printf("ERROR: Accept failure\n"); + exit(EXIT_FAILURE); + } + + printf("INFO: Client: %s connected\n", inet_ntoa(client[i].sin_addr)); + pthread_create(&(thread[i]), NULL, echo, (void *) fd); + pthread_detach(thread[i++]); + } + return EXIT_SUCCESS; +} diff --git a/src/version.h b/src/version.h new file mode 100644 index 0000000..86e034a --- /dev/null +++ b/src/version.h @@ -0,0 +1,7 @@ +#ifndef VERSION_H_ +#define VERSION_H_ + +#define VERSION_STRING "0.1.0" +#define VERSION_NUMBER 010 + +#endif /* VERSION_H_ */ |