basics
This commit is contained in:
		
						commit
						009294ba4d
					
				
					 13 changed files with 696 additions and 0 deletions
				
			
		
							
								
								
									
										58
									
								
								src/Program.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/Program.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,58 @@ | |||
| /*
 | ||||
|  * Program.cpp | ||||
|  * | ||||
|  *  Created on: 04.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| #include "Program.h" | ||||
| #include "Device.h" | ||||
| #include "Host.h" | ||||
| #include "Socket.h" | ||||
| #include "Packet.h" | ||||
| 
 | ||||
| Program::Program() { | ||||
| 	// TODO Auto-generated constructor stub
 | ||||
| } | ||||
| 
 | ||||
| Program::~Program() { | ||||
| 	// TODO Auto-generated destructor stub
 | ||||
| } | ||||
| 
 | ||||
| int Program::run() { | ||||
| 	Device d = Device(); | ||||
| 	printf(" %d", d.getName()); | ||||
| 
 | ||||
| 	bytes b = { 255, 255, 0, 0}; | ||||
| 
 | ||||
| 	Host h = Host(); | ||||
| 	Packet p = Packet(Packet::DISCOVERY); | ||||
| 	p.setBody(b); | ||||
| 	p.setHostMac(h.getMac()); | ||||
| 	bytes a = p.getBytes(); | ||||
| 	printf("\na ="); | ||||
| 	for (unsigned i = 0; i < sizeof(a); i++) | ||||
| 		printf(" %d", a[i]); | ||||
| 	p.encode(a); | ||||
| 	printf("\nb ="); | ||||
| 	for (unsigned i = 0; i < sizeof(a); i++) | ||||
| 		printf(" %d", a[i]); | ||||
| 	printf("\n"); | ||||
| 
 | ||||
| 	try { | ||||
| 		asio::io_service io_service; | ||||
| 		Socket s(io_service, dst_port, src_port); | ||||
| 		//s.listen();
 | ||||
| 		s.send(a); | ||||
| 		io_service.run(); | ||||
| 	} catch (std::exception& e) { | ||||
| 		std::cerr << "Exception: " << e.what() << "\n"; | ||||
| 	} | ||||
| 
 | ||||
| 	return 1; | ||||
| } | ||||
| 
 | ||||
| int Program::discover() { | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										24
									
								
								src/Program.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/Program.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,24 @@ | |||
| /*
 | ||||
|  * Program.h | ||||
|  * | ||||
|  *  Created on: 04.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef PROGRAM_H_ | ||||
| #define PROGRAM_H_ | ||||
| 
 | ||||
| class Program { | ||||
| public: | ||||
| 	Program(); | ||||
| 	virtual ~Program(); | ||||
| 	int run(); | ||||
| 	void setPort(int); | ||||
| 	void setPort(); | ||||
| 	int src_port = 29809; | ||||
| 	int dst_port = 29808; | ||||
| private: | ||||
| 	int discover(); | ||||
| }; | ||||
| 
 | ||||
| #endif /* PROGRAM_H_ */ | ||||
							
								
								
									
										42
									
								
								src/Types.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/Types.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | |||
| /*
 | ||||
|  * Types.h | ||||
|  * | ||||
|  *  Created on: 11.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef TYPES_H_ | ||||
| #define TYPES_H_ | ||||
| 
 | ||||
| #include <vector> | ||||
| 
 | ||||
| template <typename T> | ||||
| std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) | ||||
| { | ||||
|     std::vector<T> AB; | ||||
|     AB.reserve( A.size() + B.size() );                // preallocate memory
 | ||||
|     AB.insert( AB.end(), A.begin(), A.end() );        // add A;
 | ||||
|     AB.insert( AB.end(), B.begin(), B.end() );        // add B;
 | ||||
|     return AB; | ||||
| } | ||||
| 
 | ||||
| template <typename T> | ||||
| std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) | ||||
| { | ||||
|     A.reserve( A.size() + B.size() );                // preallocate memory without erase original data
 | ||||
|     A.insert( A.end(), B.begin(), B.end() );         // add B;
 | ||||
|     return A;                                        // here A could be named AB
 | ||||
| } | ||||
| 
 | ||||
| typedef std::vector<unsigned char> bytes; | ||||
| typedef unsigned char byte; | ||||
| 
 | ||||
| struct dataset{ | ||||
|  short type; | ||||
|  short len; | ||||
|  bytes value; | ||||
| }; | ||||
| 
 | ||||
| typedef std::vector<dataset> datasets; | ||||
| 
 | ||||
| #endif /* TYPES_H_ */ | ||||
							
								
								
									
										18
									
								
								src/device/Device.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/device/Device.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,18 @@ | |||
| /*
 | ||||
|  * Device.cpp | ||||
|  * | ||||
|  *  Created on: 01.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #include "Device.h" | ||||
| 
 | ||||
| Device::Device() { | ||||
| 	// TODO Auto-generated constructor stub
 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| Device::~Device() { | ||||
| 	// TODO Auto-generated destructor stub
 | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										23
									
								
								src/device/Device.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/device/Device.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,23 @@ | |||
| /*
 | ||||
|  * Device.h | ||||
|  * | ||||
|  *  Created on: 01.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef DEVICE_H_ | ||||
| #define DEVICE_H_ | ||||
| 
 | ||||
| #include <string> | ||||
| 
 | ||||
| class Device { | ||||
| public: | ||||
| 	Device(); | ||||
| 	virtual ~Device(); | ||||
| 
 | ||||
| 	int getName(void) { | ||||
| 		return 5; | ||||
| 	} | ||||
| }; | ||||
| 
 | ||||
| #endif /* DEVICE_H_ */ | ||||
							
								
								
									
										22
									
								
								src/device/Host.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/device/Host.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,22 @@ | |||
| /*
 | ||||
|  * Host.cpp | ||||
|  * | ||||
|  *  Created on: 11.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #include "Host.h" | ||||
| 
 | ||||
| Host::Host() { | ||||
| 	// TODO Auto-generated constructor stub
 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| bytes Host::getMac() { | ||||
| 	return {0x08,0x3e,0x8e,0x16,0x17,0x2c}; | ||||
| } | ||||
| 
 | ||||
| bytes Host::getIp() { | ||||
| 	return {0,0,0,0}; | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										21
									
								
								src/device/Host.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/device/Host.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,21 @@ | |||
| /*
 | ||||
|  * Host.h | ||||
|  * | ||||
|  *  Created on: 11.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef HOST_H_ | ||||
| #define HOST_H_ | ||||
| 
 | ||||
| #include "../Types.h" | ||||
| 
 | ||||
| class Host { | ||||
| public: | ||||
| 	Host(); | ||||
| 	virtual ~Host() {} | ||||
| 	bytes getMac(); | ||||
| 	bytes getIp(); | ||||
| }; | ||||
| 
 | ||||
| #endif /* HOST_H_ */ | ||||
							
								
								
									
										44
									
								
								src/encode.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/encode.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,44 @@ | |||
| /*
 | ||||
|  * encode.cpp | ||||
|  * | ||||
|  *  Created on: 15.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <string> | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <algorithm> | ||||
| 
 | ||||
| #include "Packet.h" | ||||
| 
 | ||||
| int encode(int argc, char *argv[]) { | ||||
| 	std::string s(argv[1]); | ||||
| 	std::string delimiter = ":"; | ||||
| 	std::string token; | ||||
| 	size_t pos = 0; | ||||
| 	bytes arr = { }; | ||||
| 	int hex; | ||||
| 	byte b; | ||||
| 	while ((pos = s.find(delimiter)) != std::string::npos) { | ||||
| 		token = s.substr(0, pos); | ||||
| 		sscanf(token.c_str(), "%x", &hex); | ||||
| 		s.erase(0, pos + delimiter.length()); | ||||
| 		b = hex & 0xFF; | ||||
| 		arr.push_back(b); | ||||
| 	} | ||||
| 	sscanf(s.c_str(), "%x", &hex); | ||||
| 	b = hex & 0xFF; | ||||
| 	arr.push_back(b); | ||||
| 
 | ||||
| 	Packet p = Packet(Packet::DISCOVERY); | ||||
| 	p.encode(arr); | ||||
| 	printf("%x", arr[0]); | ||||
| 	for (unsigned i = 1; i < arr.size(); i++) { | ||||
| 		printf(":%x", arr[i]); | ||||
| 	} | ||||
| 	printf("\n"); | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										100
									
								
								src/smrtlink.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								src/smrtlink.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,100 @@ | |||
| //============================================================================
 | ||||
| // Name        : smrtlink.cpp
 | ||||
| // Author      : jdi
 | ||||
| // Version     :
 | ||||
| // Copyright   : GPL v2
 | ||||
| // Description : Hello World in C++, Ansi-style
 | ||||
| //============================================================================
 | ||||
| 
 | ||||
| #include <cstring> | ||||
| #include <getopt.h> | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <string> | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| #include "Program.h" | ||||
| 
 | ||||
| #define USAGE "Usage: smrtlink [-p n|--port n] [-h|--help] [-v|--version] name\n" | ||||
| 
 | ||||
| #define flag_version 1 | ||||
| #define flag_help 2 | ||||
| 
 | ||||
| #define no_argument 0 | ||||
| #define required_argument 1 | ||||
| #define optional_argument 2 | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
| 	int opt, index, option_flags; | ||||
| 
 | ||||
| 	option_flags = 0; | ||||
| 
 | ||||
| 	const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { | ||||
| 			"help", no_argument, 0, 'h' }, | ||||
| 			{ "port", required_argument, 0, 'p' }, | ||||
| 			{ "srcport", required_argument, 0, 's' }, | ||||
| 			{ "dstport", required_argument, 0, 'p' }, { 0, 0, 0, 0 }, }; | ||||
| 
 | ||||
| 	Program p = Program(); | ||||
| 
 | ||||
| 	while ((opt = getopt_long(argc, argv, "vhp:s:", longopts, &index)) != -1) { | ||||
| 		switch (opt) { | ||||
| 
 | ||||
| 		case 'h': | ||||
| 			std::cout << "You hit help" << std::endl; | ||||
| 			option_flags |= flag_version; | ||||
| 			break; | ||||
| 
 | ||||
| 		case 'v': | ||||
| 			std::cout << "You hit version" << std::endl; | ||||
| 			option_flags |= flag_help; | ||||
| 			break; | ||||
| 
 | ||||
| 		case 'p': | ||||
| 			p.dst_port = atoi(optarg); | ||||
| 			option_flags |= 4; | ||||
| 			break; | ||||
| 
 | ||||
| 		case 's': | ||||
| 			p.src_port = atoi(optarg); | ||||
| 			option_flags |= 4; | ||||
| 			break; | ||||
| 
 | ||||
| 		default: /* '?' */ | ||||
| 			fprintf(stderr, "Unknown option\n"); | ||||
| 			fprintf(stderr, USAGE); | ||||
| 			exit(EXIT_FAILURE); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if (optind >= argc && !option_flags) { | ||||
| 		//fprintf(stderr, "Expected argument\n");
 | ||||
| 		fprintf(stderr, USAGE); | ||||
| 		exit(EXIT_FAILURE); | ||||
| 	} | ||||
| 
 | ||||
| 	while (optind < argc) { | ||||
| 		if (strcmp(argv[optind], "list") == 0) { | ||||
| 			printf("List:\n\tA B C\n"); | ||||
| 		}else { | ||||
| 			printf("->%s\n", argv[optind]); | ||||
| 		} | ||||
| 		optind++; | ||||
| 	} | ||||
| 
 | ||||
| 	if (p.src_port != 0) { | ||||
| 		printf("src_port = %d\n", p.src_port); | ||||
| 	} | ||||
| 
 | ||||
| 	if (p.dst_port != 0) { | ||||
| 		printf("dst_port = %d\n", p.dst_port); | ||||
| 	} | ||||
| 
 | ||||
| 	if (p.run()) | ||||
| 		exit(EXIT_SUCCESS); | ||||
| 	else | ||||
| 		exit(EXIT_FAILURE); | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										157
									
								
								src/transfer/Packet.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								src/transfer/Packet.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,157 @@ | |||
| /*
 | ||||
|  * Packet.cpp | ||||
|  * | ||||
|  *  Created on: 03.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #include <string.h> | ||||
| #include <cstdlib> | ||||
| #include <time.h> | ||||
| #include "Packet.h" | ||||
| #include "../Types.h" | ||||
| 
 | ||||
| Packet::Packet(OpCode c) { | ||||
| 	srand(time(NULL)); | ||||
| 	sequenceId = rand() % 1000; | ||||
| 	opCode = c; | ||||
| } | ||||
| 
 | ||||
| Packet::~Packet() { | ||||
| 	// TODO Auto-generated destructor stub
 | ||||
| } | ||||
| 
 | ||||
| bytes Packet::getBytes() { | ||||
| 	int i = 0; | ||||
| 	push(body, i, payload); | ||||
| 	push(body, i, (int) PACKET_END); | ||||
| 	i = 0; | ||||
| 	push(head, i, version); | ||||
| 	push(head, i, opCode); | ||||
| 	push(head, i, switchMac); | ||||
| 	push(head, i, hostMac); | ||||
| 	push(head, i, sequenceId); | ||||
| 	push(head, i, err); | ||||
| 	push(head, i, this->getLength()); | ||||
| 	push(head, i, fragmentOffset); | ||||
| 	push(head, i, flag); | ||||
| 	push(head, i, tokenId); | ||||
| 	push(head, i, checkSum); | ||||
| 	bytes data = head + body; | ||||
| 	return data; | ||||
| } | ||||
| 
 | ||||
| void Packet::setBody(bytes data) { | ||||
| 	this->body = data; | ||||
| } | ||||
| 
 | ||||
| void Packet::setHostMac(bytes mac) { | ||||
| 	this->hostMac = mac; | ||||
| } | ||||
| 
 | ||||
| short Packet::getLength() const { | ||||
| 	return HEADER_LEN + body.size(); | ||||
| } | ||||
| 
 | ||||
| int Packet::getCheckSum() const { | ||||
| 	return checkSum; | ||||
| } | ||||
| 
 | ||||
| void Packet::setCheckSum(int checkSum) { | ||||
| 	this->checkSum = checkSum; | ||||
| } | ||||
| 
 | ||||
| short Packet::getSequenceId() const { | ||||
| 	return sequenceId; | ||||
| } | ||||
| 
 | ||||
| void Packet::setSequenceId(short sequenceId) { | ||||
| 	this->sequenceId = sequenceId; | ||||
| } | ||||
| 
 | ||||
| const bytes& Packet::getSwitchMac() const { | ||||
| 	return switchMac; | ||||
| } | ||||
| 
 | ||||
| void Packet::setSwitchMac(bytes switchMac) { | ||||
| 	this->switchMac = switchMac; | ||||
| } | ||||
| 
 | ||||
| const datasets& Packet::getPayload() const { | ||||
| 	return payload; | ||||
| } | ||||
| 
 | ||||
| void Packet::setPayload(const datasets& payload) { | ||||
| 	this->payload = payload; | ||||
| } | ||||
| 
 | ||||
| void Packet::encode(bytes &data) { | ||||
| 	int len = data.size(); | ||||
| 	bytes key = { 191, 155, 227, 202, 99, 162, 79, 104, 49, 18, 190, 164, 30, | ||||
| 			76, 189, 131, 23, 52, 86, 106, 207, 125, 126, 169, 196, 28, 172, 58, | ||||
| 			188, 132, 160, 3, 36, 120, 144, 168, 12, 231, 116, 44, 41, 97, 108, | ||||
| 			213, 42, 198, 32, 148, 218, 107, 247, 112, 204, 14, 66, 68, 91, 224, | ||||
| 			206, 235, 33, 130, 203, 178, 1, 134, 199, 78, 249, 123, 7, 145, 73, | ||||
| 			208, 209, 100, 74, 115, 72, 118, 8, 22, 243, 147, 64, 96, 5, 87, 60, | ||||
| 			113, 233, 152, 31, 219, 143, 174, 232, 153, 245, 158, 254, 70, 170, | ||||
| 			75, 77, 215, 211, 59, 71, 133, 214, 157, 151, 6, 46, 81, 94, 136, | ||||
| 			166, 210, 4, 43, 241, 29, 223, 176, 67, 63, 186, 137, 129, 40, 248, | ||||
| 			255, 55, 15, 62, 183, 222, 105, 236, 197, 127, 54, 179, 194, 229, | ||||
| 			185, 37, 90, 237, 184, 25, 156, 173, 26, 187, 220, 2, 225, 0, 240, | ||||
| 			50, 251, 212, 253, 167, 17, 193, 205, 177, 21, 181, 246, 82, 226, | ||||
| 			38, 101, 163, 182, 242, 92, 20, 11, 95, 13, 230, 16, 121, 124, 109, | ||||
| 			195, 117, 39, 98, 239, 84, 56, 139, 161, 47, 201, 51, 135, 250, 10, | ||||
| 			19, 150, 45, 111, 27, 24, 142, 80, 85, 83, 234, 138, 216, 57, 93, | ||||
| 			65, 154, 141, 122, 34, 140, 128, 238, 88, 89, 9, 146, 171, 149, 53, | ||||
| 			102, 61, 114, 69, 217, 175, 103, 228, 35, 180, 252, 200, 192, 165, | ||||
| 			159, 221, 244, 110, 119, 48 }; | ||||
| 	bytes s = key; | ||||
| 	int i, t, j = 0; | ||||
| 	for (int k = 0; k < len; k++) { | ||||
| 		i = (k + 1) % 256; | ||||
| 		j = (j + s[i]) % 256; | ||||
| 		t = s[i]; | ||||
| 		s[i] = s[j]; | ||||
| 		s[j] = t; | ||||
| 		data[k] = ((data[k] ^ s[(s[i] + s[j]) % 256])); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, byte data) { | ||||
| 	arr[index] = data; | ||||
| 	index++; | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, bytes data) { | ||||
| 	for (unsigned j = 0; j < data.size(); j++) | ||||
| 		push(arr, index, data[j]); | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, short data) { | ||||
| 	byte a = (data >> 8) & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| 	a = data & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, int data) { | ||||
| 	byte a = (data >> 24) & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| 	a = (data >> 16) & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| 	a = (data >> 8) & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| 	a = data & 0xFF; | ||||
| 	push(arr, index, a); | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, dataset data) { | ||||
| 	push(arr, index, data.type); | ||||
| 	push(arr, index, data.len); | ||||
| 	push(arr, index, data.value); | ||||
| } | ||||
| 
 | ||||
| void Packet::push(bytes &arr, int &index, datasets data) { | ||||
| 	for (unsigned j = 0; j < data.size(); j++) | ||||
| 		push(arr, index, data[j]); | ||||
| } | ||||
							
								
								
									
										65
									
								
								src/transfer/Packet.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/transfer/Packet.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,65 @@ | |||
| /*
 | ||||
|  * Packet.h | ||||
|  * | ||||
|  *  Created on: 03.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef PACKET_H_ | ||||
| #define PACKET_H_ | ||||
| 
 | ||||
| #define HEADER_LEN 32 | ||||
| #define PACKET_END -65536 | ||||
| 
 | ||||
| #include "../Types.h" | ||||
| 
 | ||||
| class Packet { | ||||
| public: | ||||
| 	enum OpCode { | ||||
| 		DISCOVERY, GET, SET | ||||
| 	}; | ||||
| 	Packet(OpCode); | ||||
| 	virtual ~Packet(); | ||||
| 	void encode(bytes&); | ||||
| 	bytes getBytes(); | ||||
| 	short getLength() const ; | ||||
| 	int getCheckSum() const; | ||||
| 	short getSequenceId() const; | ||||
| 	const bytes& getSwitchMac() const; | ||||
| 	const datasets& getPayload() const; | ||||
| 	void setBody(bytes); | ||||
| 	void setHostMac(bytes); | ||||
| 	void setCheckSum(int); | ||||
| 	void setSequenceId(short); | ||||
| 	void setSwitchMac(bytes); | ||||
| 	void setPayload(const datasets& payload); | ||||
| 
 | ||||
| private: | ||||
| 	bytes head = bytes(32); | ||||
| 	bytes body; | ||||
| 	datasets payload; | ||||
| 
 | ||||
| 	byte version = 1; | ||||
| 	byte opCode; | ||||
| 	bytes switchMac = { 0, 0, 0, 0, 0, 0 }; | ||||
| 	bytes hostMac = { 0, 0, 0, 0, 0, 0 };// TODO set Mac
 | ||||
| 	bytes broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; | ||||
| 	short sequenceId; | ||||
| 	short tokenId = 0; | ||||
| 	short fragmentOffset = 0; | ||||
| 	int err = 0; | ||||
| 	int checkSum = 0; | ||||
| 	short flag = 0; | ||||
| 
 | ||||
| 	void buildHead(); | ||||
| 	void buildBody(); | ||||
| 
 | ||||
| 	void push(bytes&, int&, short); | ||||
| 	void push(bytes&, int&, int); | ||||
| 	void push(bytes&, int&, byte); | ||||
| 	void push(bytes&, int&, bytes); | ||||
| 	void push(bytes&, int&, dataset); | ||||
| 	void push(bytes&, int&, datasets); | ||||
| }; | ||||
| 
 | ||||
| #endif /* PACKET_H_ */ | ||||
							
								
								
									
										90
									
								
								src/transfer/Socket.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								src/transfer/Socket.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,90 @@ | |||
| /*
 | ||||
|  * Socket.cpp | ||||
|  * | ||||
|  *  Created on: 02.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| #include <sys/types.h> | ||||
| #include <sys/socket.h> | ||||
| #include <netinet/in.h> | ||||
| #include <arpa/inet.h> | ||||
| #include <stdio.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <iostream> | ||||
| #include <asio.hpp> | ||||
| #include "Socket.h" | ||||
| #include "../Types.h" | ||||
| 
 | ||||
| Socket::Socket(asio::io_service& io_service, short dst_port, short src_port) : | ||||
| 		socket_(io_service) { | ||||
| 
 | ||||
| 	local_endpoint_ = asio::ip::udp::endpoint( | ||||
| 			asio::ip::address::from_string("192.168.0.3"), src_port); //a
 | ||||
| 	remote_endpoint_ = asio::ip::udp::endpoint( | ||||
| 			asio::ip::address_v4::from_string("255.255.255.255"), dst_port); | ||||
| 	broadcast_endpoint_ = asio::ip::udp::endpoint( | ||||
| 			asio::ip::address_v4::from_string("255.255.255.255"), dst_port); | ||||
| 	asio::ip::address_v4 local_addr = asio::ip::address_v4::from_string( | ||||
| 			"192.168.0.3"); | ||||
| 
 | ||||
| 	socket_.open(asio::ip::udp::v4()); | ||||
| 	socket_.bind(local_endpoint_); //TODO reuse Address
 | ||||
| 
 | ||||
| 	//socket_.open(asio::ip::udp::v4());
 | ||||
| 
 | ||||
| 	//socket_.set_option(asio::ip::multicast::outbound_interface(local_addr)); // TODO broadcast outbound interface
 | ||||
| 	socket_.set_option(asio::socket_base::broadcast(true)); | ||||
| 	socket_.set_option(asio::socket_base::reuse_address(true)); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| Socket::~Socket() { | ||||
| // TODO Auto-generated destructor stub
 | ||||
| } | ||||
| void Socket::send(bytes data) { | ||||
| 
 | ||||
| 	unsigned char * a = &data[0]; | ||||
| 	/*socket_.async_send_to(asio::buffer(a, data.size()), broadcast_endpoint_,
 | ||||
| 	 [this](asio::error_code ec, std::size_t bytes_sent) | ||||
| 	 { | ||||
| 	 listen(); | ||||
| 	 }); | ||||
| 	 */ | ||||
| 	remote_endpoint_ = broadcast_endpoint_; | ||||
| 	printf("Send\n"); | ||||
| 	socket_.send_to(asio::buffer(a, data.size()), remote_endpoint_); | ||||
| 
 | ||||
| 	char reply[max_length]; | ||||
| 	size_t reply_length = socket_.receive_from(asio::buffer(reply, max_length), | ||||
| 			remote_endpoint_); | ||||
| 	printf("Received %d\n", reply_length); | ||||
| 
 | ||||
| 	reply_length = socket_.receive_from(asio::buffer(reply, max_length), | ||||
| 			remote_endpoint_); | ||||
| 	printf("Received %d\n", reply_length); | ||||
| //asio::buffer(data_, length)
 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| void Socket::listen() { | ||||
| 	socket_.async_receive_from(asio::buffer(data_, max_length), | ||||
| 			remote_endpoint_, | ||||
| 			[this](asio::error_code ec, std::size_t bytes_recvd) | ||||
| 			{ | ||||
| 				listen(); | ||||
| 
 | ||||
| 				if (!ec && bytes_recvd > 0) | ||||
| 				{ | ||||
| 					printf("Receive %s\n", data_); | ||||
| 					bytes a = {72, 97, 108, 108, 111, 32, 87, 101, 108, 116, 33}; | ||||
| 					send(a); //bytes_recvd
 | ||||
| 				} | ||||
| 
 | ||||
| 			}); | ||||
| 
 | ||||
| 	printf("Listen\n"); | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										32
									
								
								src/transfer/Socket.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/transfer/Socket.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,32 @@ | |||
| /*
 | ||||
|  * Socket.h | ||||
|  * | ||||
|  *  Created on: 02.09.2015 | ||||
|  *      Author: jdi | ||||
|  */ | ||||
| 
 | ||||
| #ifndef SOCKET_H_ | ||||
| #define SOCKET_H_ | ||||
| 
 | ||||
| #include <asio.hpp> | ||||
| #include "../Types.h" | ||||
| 
 | ||||
| class Socket { | ||||
| public: | ||||
| 	Socket(asio::io_service&, short, short); | ||||
| 	virtual ~Socket(); | ||||
| 	void send(bytes); | ||||
| 	void listen(); | ||||
| private: | ||||
| 	asio::ip::udp::socket socket_; | ||||
| 	asio::ip::udp::endpoint broadcast_endpoint_; | ||||
| 	asio::ip::udp::endpoint remote_endpoint_; | ||||
| 	asio::ip::udp::endpoint local_endpoint_; | ||||
| 	enum { | ||||
| 		max_length = 1024 | ||||
| 	}; | ||||
| 	char data_[max_length]; | ||||
| 
 | ||||
| }; | ||||
| 
 | ||||
| #endif /* SOCKET_H_ */ | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue