d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Deserve A Topic Too! > Random Dev Work/scipts/ideas
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 29 2014 10:01pm
I guess I should make a blog log shit post displaying my awesome google programming skills because everyone else was making one and I feel left out.

I already have a hardware development thread as located here: http://forums.d2jsp.org/topic.php?t=70112375&f=188 which covers many random projects I have or had worked on. It included programming as well as actual hardware work, where as this topic will be more programming specific, although many of the code snippets will be carried over from that thread as that is my main development thread.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 29 2014 10:10pm
I love how you started your thread, but didn't actually post anything. Lol
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 29 2014 10:34pm


Edit:: I guess sample code is needed in order to test if your toolchain is working:

Code
#include <iostream>
#include <oslib/oslib.h>

using namespace std;

PSP_MODULE_INFO("Project", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_MAX();

int main()
{
return 0;
}


oslib.h is a wrapper library which gives you a lot of functionality that you would have to create yourself using the raw PSP API. Some of the functionality is for sound effects and sprite images as well as wrappers around networking and such. The PSP_* declarations are so that the PSP knows some basic information about the executable such as project version, it's maximum heap size, and which thread attributes to give priority to. In the current state of this application it will run indefinitely with a black screen as we are not printing or doing anything to exit the game, but it serves well enough as a test to check if the tool chain is compiling properly.

This post was edited by AbDuCt on Oct 29 2014 10:44pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 29 2014 10:39pm
brb finding a magnifying glass to read that ridiculously small font

This post was edited by carteblanche on Oct 29 2014 10:40pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 29 2014 10:45pm
Quote (carteblanche @ Oct 30 2014 12:39am)
brb finding a magnifying glass to read that ridiculously small font


Don't hate tha playa hate the game!

Tell jsp not to restrict my post size and it wouldn't be a problem :/ Just right click and open the image in a new tab and enlarge lol.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 30 2014 11:08pm
Refactoring my PSP game while I decide how I want to go about my next project which incorporates bluez (bluetooth drivers) on Linux and what language I want to create the project in. I want to do it in ruby, but there seems to be no gems/libraries that support RFCOMM bluetooth communications, so my only other option is C/C++ which would be a pain.

Anyways, the game is a bit of a mess but right now I am refactoring the game into "scenes" where each portion of the game has a dedicated scene which the main running loop calls when the user selects an option. At first the game starts by calling the homeScene() method which simply draws a menu to the screen which changes color based on selection and has support for scrolling through the menus. This scene will be called any time the game scene finishes or the user goes back to the main menu.



I am sure I should refactor this further so that I separate the actual drawing of the scene and the reading of the controls to outside functions, but I am still deciding if I am going to keep this method of designing the game. I think it will be more complicated but having a dedicated drawScene() method which accepts a structure of pointers for different game objects and such would be better than having each specific scene have its own drawing function.

The basic premise of such a function would be consisting of on a structure which holds the current scene to draw to the screen. This structure might look something like this:

Code
int drawScene(struct drawObjects scene);

struct drawObjects {
struct textObjects *texts;
int numberOfTextObjects;
struct imageObjects *images;
int numberOfImageObjects;
};

struct textObjects {
OSL_COLOR color;
char *text;
int xPosition;
int yPosition;
};

struct textObjects {
char *imagePath;
int xPosition;
int yPosition;
};


This is basic and only based off the needs of my current homeScene, but it essentially allows me to pass a structure drawObjects; into the drawScene() method and it will simply check against nulls for the pointers. If the pointer is null than it will skip drawing that object. If it is not null, than check the number of resources at that pointer and draw/load them onto the screen. I am not sure if this will actually cut down on code or not seeing how each object being dawn needs to have its structure declared then appended to array of structures in the drawing object. This method might get way to complicated once it comes down to drawing a scene that has many many images moving around the screen per scene draw. I'll probably get started on a proof of concept soonish but I might not use it because of its complexity. I wonder how directx does it... For now have a peek at this ugly ass code :3

Code
#include "includes.h"

int homeScene() {
int frameSkip = 0;
int menuSelection = 0;
int menuLoop = 0;
char *menuItems[maxMenus] = {"Start", "Options", "About", "Exit"};
OSL_IMAGE *background = NULL;
OSL_COLOR cyan = RGB(151,246 ,220);
OSL_COLOR white = RGB(255, 255, 255);

background = oslLoadImageFilePNG("resources/images/background.png", OSL_IN_RAM, OSL_PF_8888);

while(1) {
if (!frameSkip) {
oslStartDrawing();

oslDrawImageXY(background, 0, 0);

for(menuLoop = 0; menuLoop < maxMenus; menuLoop++) {
if(menuLoop == menuSelection) {
printText(menuLoop * (OSL_SCREEN_WIDTH/maxMenus) - (oslGetStringWidth(menuItems[menuLoop])/2) + ((OSL_SCREEN_WIDTH/maxMenus)/2), 250 , menuItems[menuLoop], cyan);
} else {
printText(menuLoop * (OSL_SCREEN_WIDTH/maxMenus) - (oslGetStringWidth(menuItems[menuLoop])/2) + ((OSL_SCREEN_WIDTH/maxMenus)/2), 250 , menuItems[menuLoop], white);
}
}

oslEndDrawing();
}

oslEndFrame();
frameSkip = oslSyncFrame();

oslReadKeys();
if ((osl_keys->pressed.left) && (menuSelection > 0))
menuSelection--;
else if((osl_keys->pressed.right) && (menuSelection < (maxMenus - 1)))
menuSelection++;
else if (osl_keys->pressed.cross) {
oslDeleteImage(background);

switch(menuSelection) {
case homeSceneStart:
return homeSceneStart;
case homeSceneOptions:
return homeSceneOptions;
case homeSceneAbout:
return homeSceneAbout;
case homeSceneExit:
return homeSceneExit;
}
}
}

return 0;
}


This post was edited by AbDuCt on Oct 30 2014 11:16pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 2 2014 02:02pm
Going to start writing this other project from scratch dealing with a piece of hardware over bluetooth. Ruby would of been nice but I am far to lazy to try to fix or implement the bluez/rfcomm library into it so I am just going to use C++ as I just have to include the already built header to use everything.

This is the project I will be porting over to C++ so I can add further functionality (https://code.google.com/p/adqmisc/source/browse/#svn%2Ftrunk%2Fliveview). There is no reason for it other than I really do not like working in python for what ever reason. If I had my way I would use ruby but fuck it.

Anyways If you view the LiveViewMessages.py file, it uses the python struct library to contruct packets of specific size (https://docs.python.org/2/library/struct.html#format-characters), now I know that I can replicate these sizes in C++ by using char, short int, long int, long long int, but will these sizes change between systems? Would I be better off using the stdint header so I can define int8_t, int16_t, int32_t, int64_t or are these just a typedef for the other set of variables which I have stated before? How would I also maintain their size when sending the data? I was going to create a struct, but that means I have to create a struct for each different packet type (they varry a tiny bit). I wonder if it would just be easier to calloc a memory chunk consisting of the types I need then fill them in using memset. Or maybe create my own sprintf wrapper that uses va_lists because sprintf already has unsigned int, unsigned short int, and unsigned long long int capabilities into a byte buffer. Might not even have to make a wrapper.

On my system they are the same size, and I assume that the purpose for the stdint header is so that you are granted that specific size, where as the other variables might be larger or smaller based on platform. Is this correct?

Code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main()
{
printf("%i\n", sizeof(unsigned char));
printf("%i\n", sizeof(unsigned short int));
printf("%i\n", sizeof(unsigned long int));
printf("%i\n", sizeof(unsigned long long int));

printf("\n");

printf("%i\n", sizeof(uint8_t));
printf("%i\n", sizeof(uint16_t));
printf("%i\n", sizeof(uint32_t));
printf("%i\n", sizeof(uint64_t));
return 1;
}


//output
1
2
4
8

1
2
4
8

Process returned 1 (0x1) execution time : 0.066 s
Press any key to continue.


This post was edited by AbDuCt on Nov 2 2014 02:20pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 2 2014 03:42pm
I gave up with creating a packet function which takes a va_list of arguments to create a packet with specific byte sizes. The main reason for this is that va_list automatically converts everything to type int so that defeats the entire purpose.

In my last attempt I attempted to:
1) calculate the memory space needed for the specific packet based from the format string
2) calloc the memory required in bytes
3) start the va_list and loop inserting them into the memory

This is where it falls apart. va_arg converts everything to int even if you tell it otherwise. I tried to bypass this by using memcpy to place the variable inside the buffer, but this was a failed since va_arg converted all our data into one size, int. Here is the fail code for that lol

Code
char *createPacket(const char* formats, ...) {
va_list argument_list;
int formatsLength = sizeof(formats);
int bufferLength = 0;
int i = 0;
int holder = 0;
char *buffer = NULL;

for(i = 0; i < formatsLength; i++) {
if(formats[i] == 'B')
bufferLength += 1;
else if (formats[i] == 'H')
bufferLength += 2;
else if (formats[i] == 'L')
bufferLength += 4;
else if (formats[i] == 'Q')
bufferLength += 8;
}

buffer = malloc(bufferLength);
bufferLength = 0;

va_start(argument_list, formats);

for(i = 0; i < formatsLength; i++) {
holder = va_arg(argument_list, int);
if(formats[i] == 'B') {
memcpy(buffer+bufferLength, &holder, sizeof(uint8_t));
bufferLength += 1;
} else if (formats[i] == 'H') {
memcpy(buffer+bufferLength, &holder, sizeof(uint16_t));
bufferLength += 2;
} else if (formats[i] == 'L') {
memcpy(buffer+bufferLength, &holder, sizeof(uint32_t));
bufferLength += 4;
} else if (formats[i] == 'Q') {
memcpy(buffer+bufferLength, &holder, sizeof(uint64_t));
bufferLength += 8;
}
}

va_end(argument_list);

return buffer;
}


I also tried using sprintf() but it pads the data with nullbytes, although easy to remove, it also made it a pain to use. Not to mention that it doesn't quite generate the same data has a struct memory blob would. Also it's a pain to have to prefix everything thing with their type size: u, hu, lu, llu.

The method I think will be easiest and actually work now is to just define a structure per packet inside its decoding or encoding function. This way I know it's going to work because I use this method all the time for other data projects, and I have easy to set/get access to all variables in the packet by way of overlaying received data over the packet structure:

Code
int main() {

struct packet {
uint16_t hmm;
uint16_t hmm2;
uint8_t hmm3;
uint64_t hmm4;
};

char *data = malloc(sizeof(struct packet));
struct packet *pSend = malloc(sizeof(struct packet));

pSend->hmm = 1;
pSend->hmm2 = 2;
pSend->hmm3 = 3;
pSend->hmm4 = 4;

memcpy(data, pSend, sizeof(struct packet));

struct packet *pGet = (struct packet*)data;

free(pSend);
free(pGet);
free(data);

return 1;
}




Using this method may make the code longer since I have to define every packet structure for each packet type, but it maintains the ease of use quality which is nice. The only thing better would be if I could create the original packet creator function so that it actually accepts multi arguments and doesn't jew over their types.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 6 2014 01:47am
Hacked on some ruby this past couple days while on the go. It is a wrapper around hashcat which provides monitored "work as you please" distributed hash cracking functionality. This is basically xmlrpc as the communication protocol with functions built to monitor its nodes.

Essentially the protocol is as follows:

1) Client announces their presence to the server with a client handle
2) Client checks if work can be done before asking for work
3) If work can be done it requests the details as to what work needs to be done (hash, algro, other details)
4) Client asks for the data set to be worked on (aka the job) (the server sends a range of the total keyspace to the client to calculate every time it asks for data)
5) While the client works it sends a keep alive packet to the server in order for the server to keep track of lost jobs
6) Once the client is done working the job it submits a response stating if it has found the hash or not within its job
7) This loops from steps 2 through 6 until the server states no more work has to be done

The server keeps track of which client has which job assigned to it and wither or not they have checked in or not. If a client pings out (does not check in within a timeout) the server assume all work done by the client is lost and reassigns it into a job pool which will then be assigned to the next client requesting work.

It's a pretty simple idea, but there wasn't really any projects out there that made use of this, or that I found useful. Most of them simply split the work equally among X computers and left it at that and didn't keep track of work or if it was lost or not. This was meant to be a scalable "work as you please" type of protocol where clients can contribute as much as they want to completing the jobs at hand and if one leaves the client pool it wont effect the jobs or the other nodes.

I need to refactor the code a bit but I haven't had time.

Server.rb
Code
require 'xmlrpc/server'
require 'trollop'
require 'base64'
require 'thread'
require_relative 'NodeCache'
require_relative 'NodeSession'
require_relative 'NodeWorkQueue'

@hashFound = false
@hashFile = nil

@opts = Trollop::options do
version 'Dcat 1.0.0'
banner 'Dcat is a distrubted hash cracking fronted for HashCat programmed in ruby using a work as you please model.'
opt :hash_file, 'Path to hash file', :type => :string
opt :mask, 'The mask to distribute', :type => :string
opt :md5, 'Toggle md5 hash cracking'
opt :wpa_wpa2, 'Toggle wpa/wpa2 cracking'
end

Trollop::die :hash_file, 'must include the path to the hash file' if @opts[:hash_file].to_s.empty? and File.exists? @opts[:hash_file]
Trollop::die :mask, 'must include a mask to distribute' if @opts[:mask].to_s.empty?
Trollop::die 'must choose one cracking method (md5, wpa/wpa2)' if not @opts[:md5] and not @opts[:wpa_wpa2]


open(@opts[:hash_file], 'rb') do |file|
@hashFile = file.read
@hashFile = Base64.strict_encode64 @hashFile
end

sessionCheckerThread = Thread.new { sessionStatusThread }
queueCreateWorkThread = Thread.new { queueGenerateWorkThread }

server = XMLRPC::Server.new(1234)

server.add_handler('node.announce') do |nodeName|
if not nodeExists(nodeName)
nodeAdd(nodeName)
'announce: OK'
else
'announce: Node already exists'
end
end

server.add_handler('node.requestHash') do |nodeName|
if nodeExists(nodeName)
nodeRequestHash
else
'requestHash: Node does not exist'
end
end

server.add_handler('node.requestData') do |nodeName|
if nodeExists(nodeName)
nodeRequestData(nodeName)
else
'requestData: Node does not exist'
end
end

server.add_handler('node.submitData') do |nodeName, hash|
if nodeExists(nodeName)
nodeSubmitData(hash)
else
'submitData: Node does not exist'
end
end

server.add_handler('node.canContinue') do |nodeName|
if nodeExists(nodeName)
nodeCanContinue
else
'canContinue: Node does not exist'
end
end

server.add_handler('node.keepAlive') do |nodeName|
if nodeExists(nodeName)
if nodeKeepAlive(nodeName)
'keepAlive: OK'
else
'keepAlive: Failed to keep alive'
end
else
'keepAlive: Node does not exist'
end
end

server.serve


This post was edited by AbDuCt on Nov 6 2014 01:55am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Nov 6 2014 01:48am
Project continued:

NodeWorkQueue.rb
Code
@workQueue = Queue.new
@currentSkip = 0

def queueGenerateWorkThread #needs to find max work via hashcat --keyspace
while true
if @workQueue.length < 20
@workQueue.push "-s #{@currentSkip} -l 100"
@currentSkip += 100
else
#puts "workQueue is full #{@workQueue.length}"
sleep 1
end
end
end

def queueGetWork #needs to loop until work is available, not blindly return work after a timeout
if @workQueue.empty?
puts "workQueue is empty sleeping a bit to let it generate more work"
sleep 1
end
return @workQueue.pop
end

def queueReaddWork(work)
@workQueue.push work
return true
end



NodeCache.rb
Code
#nodeHast = {:nodeName => 'abduct', :skip => 0, :length => 100, :session => session}
@nodeCache = []

def nodeAdd(nodeName)
nodeHash = {:nodeName => nodeName, :currentWork => '', :session => sessionCreate(nodeName)}
@nodeCache.push nodeHash

return true
end

def nodeExists(nodeName)
return false if @nodeCache.empty?

@nodeCache.each do |node|
if node[:nodeName].eql? nodeName
return true
end
end

return false
end

def nodeRequestData(nodeName)
@nodeCache.each do |node|
if node[:nodeName].eql? nodeName
node[:currentWork] = queueGetWork
if @opts[:md5]
return "-m 0 -a 3 -o cracked.txt #{node[:currentWork]} tocrack.txt #{@opts[:mask]}"
elsif @opts[:wpa_wpa2]
return "-m 2500 -a 3 -o cracked.txt #{node[:currentWork]} tocrack.txt #{@opts[:mask]}"
end

return 'requestData: Unknown hash type'
end
end

return false
end

def nodeRequestHash
if not @hashFile.empty?
return @hashFile
else
return "requestHash: No hashes to crack"
end
end

def nodeSubmitData(hash)
if not hash.empty?
@hashFound = true
open('./found-hashes.txt', 'wb') do |file|
file.write Base64.strict_decode64 hash
end
puts "HASH FOUND: #{hash}"
end

return "submitData: OK"
end

def nodeCanContinue
return "canContinue: NO" if @hashFound
return "canContinue: YES"
end

def nodeKeepAlive(nodeName)
@nodeCache.each do |node|
if node[:nodeName].eql? nodeName
node[:session].timer.thread_variable_set("lastKeepAlive", Time.now)
return true
end
end

return false
end


NodeSession.rb
Code
class Session
attr_reader :nodeName
attr_accessor :expire, :timer

def initialize(nodeName, expire, timer)
@nodeName = nodeName
@expire = expire
@timer = timer
end
end

def sessionStatusThread
while true
if @nodeCache.empty?
#puts "nodeCache empty, sleeping 1 second"
sleep 1
next
end

@nodeCache.each do |node|
if not node[:session].timer.thread_variable_get("sessionAlive").nil? and not node[:session].timer.thread_variable_get("sessionAlive") and not @hashFound
puts "#{node[:nodeName]} is dead, adding #{node[:skip]} to queue"
p node[:currentWork]
queueReaddWork node[:currentWork]

puts "removing #{node[:nodeName]} from node list"
@nodeCache.delete node
end
end
end
end

def sessionStart(session)
Thread.current.thread_variable_set("sessionAlive", true)
Thread.current.thread_variable_set("lastKeepAlive", Time.now)

while Time.now < (Thread.current.thread_variable_get("lastKeepAlive") + session.expire)
#puts "tick"
sleep(1)
end

puts "expire"
Thread.current.thread_variable_set("sessionAlive", false)
end

def sessionCreate(nodeName)
session = Session.new(nodeName, 10, Thread.new { sessionStart(session) })
return session
end


Example test client:

Code
require "xmlrpc/client"
require 'base64'
client = XMLRPC::Client.new("localhost", "/", 1234)

s = client.call('node.announce', 'abduct')
puts s
sleep 2

s = client.call('node.canContinue', 'abduct')
puts s

if s.eql? 'canContinue: YES'
s = client.call('node.requestHash', 'abduct')
open('./tocrack.txt', 'wb') do |fo|
fo.write Base64.strict_decode64 s
end


s = client.call('node.requestData', 'abduct')
puts s

3.times do
s = client.call('node.keepAlive', 'abduct')
puts s
sleep 4
end

s = client.call('node.submitData', 'abduct', '')
puts s
else
puts 'can not continue, hash found'
end
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll