cmte hace 5 años
padre
commit
70eac43101

+ 0 - 3
README.md

@@ -1,3 +0,0 @@
-# music-brainz-go
-
-Go implementation of the API to access music-brainz

+ 0 - 5
go-music/go.mod

@@ -1,5 +0,0 @@
-module go-music
-
-go 1.14
-
-require github.com/stretchr/testify v1.5.1

+ 0 - 10
go-music/go.sum

@@ -1,10 +0,0 @@
-github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
-github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
-github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
-github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
-gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 0 - 171
go-music/mb.go

@@ -1,171 +0,0 @@
-package mb
-
-import (
-	"encoding/json"
-	"fmt"
-	"net/http"
-	"net/url"
-	"sort"
-	"time"
-)
-
-const (
-	apiURI               = "https://musicbrainz.org/ws/2"
-	artistSearchEndpoint = "artist?query"
-)
-
-//MB holds the http client
-type MB struct {
-	Client http.Client
-}
-
-//Search holds the search returns
-type Search struct {
-	artist artistSearch
-}
-
-//NewMusicBrainz returns a new Search object
-func NewMusicBrainz() MB {
-
-	var mb MB
-	timeout := time.Duration(10 * time.Second)
-
-	mb.Client = http.Client{Timeout: timeout}
-	return mb
-}
-
-//SearchForArtist return a list of artists using `query` as parameter if none found returns error
-func (m MB) SearchForArtist(query string) (*Search, error) {
-	var ret Search
-	uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, url.QueryEscape(query))
-
-	req, err := http.NewRequest(
-		"GET",
-		uri,
-		nil)
-
-	req.Header.Set(
-		"User-Agent",
-		"Go Application Development: Create spotify playlist based on artist")
-	req.Header.Set("Content-Type", "application/json")
-	req.Header.Set("Accept", "application/json")
-
-	if err != nil {
-		fmt.Printf("error creating request\n%v", err)
-		return nil, err
-	}
-
-	resp, err := m.Client.Do(req)
-	if err != nil {
-		fmt.Printf("error opening request\n%v", err)
-		return nil, err
-	}
-	defer resp.Body.Close()
-	if err != nil {
-		fmt.Printf("could not read body\n%v", err)
-		return nil, err
-	}
-	err = json.NewDecoder(resp.Body).Decode(&ret.artist)
-	if err != nil {
-		fmt.Printf("could not decode body\n%v", err)
-		return nil, err
-	}
-
-	if ret.artist.Count == 0 {
-		return nil, fmt.Errorf("cound not found artist: %s", query)
-	}
-
-	return &ret, nil
-}
-
-//GetArtistName get the artist with the highest score on the search
-func (s Search) GetArtistName() (string, error) {
-
-	if len(s.artist.Artists) == 0 {
-		return "", fmt.Errorf("could not find any artist")
-	}
-	return s.artist.Artists[0].Name, nil
-}
-
-//GetArtistTags returns the tags in order of relevance of the current artist
-func (s Search) GetArtistTags() []Tags {
-	var t []Tags
-	for _, elem := range s.artist.Artists[0].Tags {
-		if int(elem.Count) > 0 {
-			t = append(t, elem)
-		}
-	}
-	sort.Sort(ByInverseCount(t))
-	return t
-}
-
-type artistSearch struct {
-	Created time.Time `json:"created"`
-	Count   int       `json:"count"`
-	Offset  int       `json:"offset"`
-	Artists []artists `json:"artists"`
-}
-
-type area struct {
-	ID       string `json:"id"`
-	Type     string `json:"type"`
-	TypeID   string `json:"type-id"`
-	Name     string `json:"name"`
-	SortName string `json:"sort-name"`
-	LifeSpan struct {
-		Ended interface{} `json:"ended"`
-	} `json:"life-span"`
-}
-
-type alias struct {
-	SortName  string      `json:"sort-name"`
-	TypeID    string      `json:"type-id,omitempty"`
-	Name      string      `json:"name"`
-	Locale    interface{} `json:"locale"`
-	Type      string      `json:"type"`
-	Primary   interface{} `json:"primary"`
-	BeginDate interface{} `json:"begin-date"`
-	EndDate   interface{} `json:"end-date"`
-}
-
-type artists struct {
-	ID        string `json:"id"`
-	Type      string `json:"type,omitempty"`
-	TypeID    string `json:"type-id,omitempty"`
-	Score     int    `json:"score"`
-	Name      string `json:"name"`
-	SortName  string `json:"sort-name"`
-	Country   string `json:"country,omitempty"`
-	Area      area   `json:"area,omitempty"`
-	BeginArea struct {
-		ID       string `json:"id"`
-		Type     string `json:"type"`
-		TypeID   string `json:"type-id"`
-		Name     string `json:"name"`
-		SortName string `json:"sort-name"`
-		LifeSpan struct {
-			Ended interface{} `json:"ended"`
-		} `json:"life-span"`
-	} `json:"begin-area,omitempty"`
-	Isnis    []string `json:"isnis,omitempty"`
-	LifeSpan struct {
-		Begin string      `json:"begin"`
-		Ended interface{} `json:"ended"`
-	} `json:"life-span,omitempty"`
-	Aliases        []alias `json:"aliases,omitempty"`
-	Tags           []Tags  `json:"tags,omitempty"`
-	Disambiguation string  `json:"disambiguation,omitempty"`
-}
-
-//Tags represent Artist tags (eg. Rock, Punk, Late 2000s)
-type Tags struct {
-	Count int    `json:"count"`
-	Name  string `json:"name"`
-}
-
-//ByInverseCount return the tags in relevance order
-type ByInverseCount []Tags
-
-func (t ByInverseCount) Len() int           { return len(t) }
-func (t ByInverseCount) Less(i, j int) bool { return t[i].Count >= t[j].Count }
-func (t ByInverseCount) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

+ 0 - 33
go-music/mb_test.go

@@ -1,33 +0,0 @@
-package mb
-
-import (
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestShouldBeEqualToArtist(t *testing.T) {
-	m := NewMusicBrainz()
-	s, _ := m.SearchForArtist("Metallica")
-	name, _ := s.GetArtistName()
-
-	assert.Equal(t, name, "Metallica")
-}
-
-func TestErrShouldBeNilIfArtistIsValid(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("Iron Maiden")
-	assert.Nil(t, err, "err should be nil for valid artist")
-}
-
-func TestIfArtistIsValidThenErrIsNil(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("Metallica")
-	assert.Nil(t, err, "err should be nil if found artist")
-}
-
-func TestErrShouldContainErrorIfArtistIsInvalid(t *testing.T) {
-	m := NewMusicBrainz()
-	_, err := m.SearchForArtist("difjapodsifjapdsif adspofijdas flkdsajf pakdsfad")
-	assert.NotNil(t, err, "err should not be nil if found artist")
-}

+ 0 - 3
libpng/.gitignore

@@ -1,3 +0,0 @@
-bin/*
-images/*
-png

+ 0 - 13
libpng/Makefile

@@ -1,13 +0,0 @@
-CC=/usr/bin/gcc
-CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD} -lm -lpng
-OUT=bin
-OBJ=$(OUT)/obj
-OBJS=$(OBJ)/img.o
-
-
-all: $(OBJS)
-	$(CC) $(CFLAGS) $(OBJS) png.c -o bin/png
-
-$(OBJ)/img.o:
-	$(CC) $(CFLAGS)	-c img.c -o $(OBJ)/img.o
-

+ 0 - 13
libpng/Makefile.fbsd-amd64

@@ -1,13 +0,0 @@
-CC=/usr/local/bin/gcc
-CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I/usr/local/include -I${PWD} -lm -lpng
-OUT=bin
-OBJ=$(OUT)/obj
-OBJS=$(OBJ)/img.o
-
-
-all: $(OBJS)
-	$(CC) $(CFLAGS) $(OBJS) png.c -o bin/png
-
-$(OBJ)/img.o:
-	$(CC) $(CFLAGS)	-c img.c -o $(OBJ)/img.o
-

+ 0 - 88
libpng/img.c

@@ -1,88 +0,0 @@
-#include "img.h"
-
-
-image initialize_png(char *title, char *filename, int width, int height)
-{
-	image ret;
-	FILE *fp = NULL;
-
-	fp = fopen(filename, "wb");
-	if (fp == NULL) {
-		fprintf(stderr, "could not create %s", filename);
-		exit(EXIT_FAILURE);
-	}
-
-	png_structp png_ptr =
-	    png_create_write_struct(PNG_LIBPNG_VER_STRING,
-				    NULL,
-				    NULL,
-				    NULL);
-
-	if (png_ptr == NULL) {
-		fprintf(stderr, "Could not create write struct. libpng\n");
-	}
-
-
-	png_infop info_ptr = png_create_info_struct(png_ptr);
-	if (info_ptr == NULL) {
-		fprintf(stderr, "Could not create info struct. libpng\n");
-	}
-
-	if (setjmp(png_jmpbuf(png_ptr))) {
-		fprintf(stderr, "Error during png creation\n");
-	}
-
-	png_init_io(png_ptr, fp);
-
-	//Write header
-	png_set_IHDR(png_ptr,
-		     info_ptr,
-		     width,
-		     height,
-		     8,
-		     PNG_COLOR_TYPE_RGB,
-		     PNG_INTERLACE_NONE,
-		     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-	if (title != NULL) {
-		png_text title_text;
-		title_text.compression = PNG_TEXT_COMPRESSION_NONE;
-		title_text.key = "Title";
-		title_text.text = title;
-		png_set_text(png_ptr, info_ptr, &title_text, 1);
-	}
-
-	png_write_info(png_ptr, info_ptr);
-
-	ret.png_ptr = png_ptr;
-	ret.info_ptr = info_ptr;
-	ret.width = width;
-	ret.height = height;
-	ret.fp = fp;
-	return ret;
-}
-
-void write_image(image * img, pix_row * img_array)
-{
-
-	png_bytep row = NULL;
-	int x = 0, y = 0;
-	row = (png_bytep) malloc(3 * img->width * sizeof(png_byte));
-	for (y = 0; y < img->height; y++) {
-		for (x = 0; x < img->width; x++) {
-			row[x * 3 + 0] = img_array[x].p[y].r;	//red
-			row[x * 3 + 1] = img_array[x].p[y].g;	// green
-			row[x * 3 + 2] = img_array[x].p[y].b;	// blue
-		}
-		png_write_row(img->png_ptr, row);
-	}
-	png_write_end(img->png_ptr, img->info_ptr);
-	free(row);
-}
-
-void finish_image(image * img)
-{
-	fclose(img->fp);
-	png_free_data(img->png_ptr, img->info_ptr, PNG_FREE_ALL, -1);
-	png_destroy_write_struct(&img->png_ptr, NULL);
-
-}

+ 0 - 33
libpng/img.h

@@ -1,33 +0,0 @@
-#ifndef IMG_H
-#define IMG_H
-#include <png.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-typedef struct pix {
-	int r;
-	int g; 
-	int b;
-	int a;
-}pix;
-
-typedef struct pix_row {
-	int lenght;
-	pix* p;
-}pix_row;
-
-typedef struct image {
-
-	png_structp png_ptr;
-	png_infop info_ptr;
-	int width;
-	int height;
-	FILE *fp;
-} image;
-
-
-image initialize_png(char* title, char* filename, int width, int height);
-void write_image(image * img, pix_row * img_array);
-void finish_image(image * img);
-
-#endif

+ 0 - 56
libpng/png.c

@@ -1,56 +0,0 @@
-
-#define _XOPEN_SOURCE
-#include "img.h"
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(int argc, char** argv){
-
-	if (argc != 4)
-	{
-		fprintf(stderr, "\nusage: %s count width height", argv[0]);
-		exit(EXIT_FAILURE);
-	}
-
-	int width, height, c;
-
-	c = atoi(argv[1]);
-	width = atoi(argv[2]);
-	height = atoi(argv[3]);
-
-	pix_row rows[width];
-	pix p;
-	char filename[60];
-	int i,j,x,y,count;
-	image img;
-	p.r = 200;
-	p.g = 200;
-	p.b = 200;
-
-	srand48(1000);
-	
-	for (count=0; count<(c);count++){
-
-		for (i=0;i<width;i++){
-			rows[i].p = malloc (height * sizeof(pix));
-			for (j=0 ;j<height; j++){
-				p.r = (int) (drand48() * 255);
-				p.g = (int) (drand48() * 255);
-				p.b = (int) (drand48() * 255);
-				rows[i].p[j] = p;
-			}
-		}
-		sprintf(filename, "images/image%05d.png", count);
-		img = initialize_png("image010", filename, width, height);
-		write_image(&img, rows);
-		finish_image(&img);
-
-		for (i=0; i<width;i++){
-
-			free(rows[i].p);
-		}
-	}
-
-	return (0);
-
-}

+ 0 - 18
pthread/Makefile

@@ -1,18 +0,0 @@
-CC=/usr/bin/gcc
-CFLAGS=-Wall -Werror -fno-builtin -O0 -g -pthread -m64 -std=iso9899:1999
-OBJ=bin/obj
-OBJS=$(OBJ)/example.o $(OBJ)/example2.o $(OBJ)/foo.o
-
-all: bin/example
-
-bin/example: $(OBJS)
-	$(CC) $(CFLAGS) -o bin/example $(OBJS)
-
-bin/obj/foo.o: example.h example2.h
-	$(CC) $(CFLAGS) -c -o $(OBJ)/foo.o foo.c
-
-bin/obj/example.o: example.c
-	$(CC) $(CFLAGS) -c -o $(OBJ)/example.o example.c
-
-bin/obj/example2.o: example2.c
-	$(CC) $(CFLAGS) -c -o $(OBJ)/example2.o example2.c

+ 0 - 19
pthread/example.c

@@ -1,19 +0,0 @@
-#include "example.h"
-
-int fib(int n)
-{
-	int i = 0;
-	if (n == 1 || n == 0)
-		return 1;
-	for (i = 0; i < n; i++) {
-		return (fib(n - 1) + fib(n - 2));
-	}
-	return (-1);
-}
-
-void *runner1(void *arg)
-{
-	struct fibo *fibl = (struct fibo *) arg;
-	fibl->answer = fib(fibl->limit);
-	pthread_exit(0);
-}

+ 0 - 12
pthread/example.h

@@ -1,12 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <pthread.h>
-
-struct fibo {
-	int limit;
-	int answer;
-};
-
-int fib(int n);
-
-void *runner1(void *arg);

+ 0 - 60
pthread/example2.c

@@ -1,60 +0,0 @@
-#include "example2.h"
-
-void *produce(void *buf)
-{
-	buffer *buff;
-	buff = (buffer *) buf;
-	printf("\nproducer started");
-
-	for (int i = 0; i < 20; i++) {
-		sem_wait(&buff->full);
-		pthread_mutex_lock(&buff->mutex);
-
-		buff->data[buff->pos] = 0;
-		buff->pos = buff->pos + 1;
-		printf("\nproduced");
-
-		sem_post(&buff->empty);
-		pthread_mutex_unlock(&buff->mutex);
-
-		sleep(1);
-	}
-
-	pthread_exit(0);
-}
-
-void *consume(void *buf)
-{
-
-	buffer *buff;
-	buff = (buffer *) buf;
-	printf("\nconsumer started");
-	for (int i = 0; i < 20; i++) {
-		sem_wait(&buff->empty);
-		pthread_mutex_lock(&buff->mutex);
-
-		buff->pos = buff->pos - 1;
-		printf("\ngot value %d", buff->data[buff->pos]);
-
-		printf("\nconsumed");
-
-		sem_post(&buff->full);
-
-		pthread_mutex_unlock(&buff->mutex);
-		sleep(2);
-	}
-	pthread_exit(0);
-}
-
-void init_buffer(buffer * buf)
-{
-
-	sem_init(&buf->empty, 0, 0);
-	sem_init(&buf->full, 0, BUFFER_SIZE);
-	pthread_mutex_init(&buf->mutex, NULL);
-	buf->pos = 0;
-
-	for (int i = 0; i < BUFFER_SIZE; i++) {
-		buf->data[i] = -1;
-	}
-}

+ 0 - 21
pthread/example2.h

@@ -1,21 +0,0 @@
-#include <pthread.h>
-#include <stdlib.h>
-#include <semaphore.h>
-#include <stdio.h>
-#include <stdio.h>
-#include <unistd.h>
-
-#define BUFFER_SIZE 10
-
-typedef struct buffer {
-	sem_t empty;
-	sem_t full;
-	pthread_mutex_t mutex;
-	int pos;
-	int data[BUFFER_SIZE];
-
-} buffer;
-
-void *produce(void *buf);
-void *consume(void *buf);
-void init_buffer(buffer * buf);

+ 0 - 69
pthread/foo.c

@@ -1,69 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <pthread.h>
-
-#include "example.h"
-#include "example2.h"
-
-int example1(int argc, char **argv)
-{
-
-	int i;
-	pthread_t id[argc];
-	pthread_attr_t attr;
-	struct fibo d[argc];
-
-	if (argc < 2) {
-		printf("usage: %s num1 num2 num3... numN", argv[0]);
-		return (1);
-	}
-
-
-
-	pthread_attr_init(&attr);
-	for (i = 1; i < argc; i++) {
-		d[i - 1].limit = atoi(argv[i]);
-		d[i - 1].answer = 0;
-		pthread_create(&id[i - 1], &attr, runner1, &d[i - 1]);
-	}
-
-	for (i = 0; i < argc - 1; i++) {
-
-		pthread_join(id[i], NULL);
-		printf("\n\nFib of limit: %d is %d", d[i].limit,
-		       d[i].answer);
-
-	}
-
-	return (0);
-
-}
-
-
-int example2(int argc, char **argv)
-{
-
-	pthread_t id[2];
-	pthread_attr_t attr;
-	buffer buf;
-
-	init_buffer(&buf);
-
-	pthread_attr_init(&attr);
-	pthread_create(&id[0], &attr, produce, &buf);
-	pthread_create(&id[1], &attr, consume, &buf);
-
-	pthread_join(id[0], NULL);
-	pthread_join(id[1], NULL);
-
-	sem_destroy(&buf.empty);
-	pthread_mutex_destroy(&buf.mutex);
-
-	printf("\nProgram finished");
-	return (EXIT_SUCCESS);
-}
-
-int main(int argc, char **argv)
-{
-	example2(argc, argv);
-}

+ 0 - 1
queue_thread_model/.gitignore

@@ -1 +0,0 @@
-bin/*

+ 0 - 17
queue_thread_model/Makefile

@@ -1,17 +0,0 @@
-CC=/usr/bin/gcc
-CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD}
-OUT=bin
-OBJ=$(OUT)/obj
-OBJS=$(OBJ)/queue.o
-
-
-all: $(OBJS)
-	$(CC) $(CFLAGS) model.c $(OBJS) -o $(OUT)/model
-
-$(OBJ)/queue.o:
-	$(CC) $(CFLAGS) -c queue.c -o $(OBJ)/queue.o
-
-
-clean:
-	rm -rf *.o model
-

+ 0 - 27
queue_thread_model/model.c

@@ -1,27 +0,0 @@
-#include <stdio.h>
-#include "queue.h"
-
-int main(int argc, char **argv)
-{
-
-	queue *q;
-	int i = -1;
-
-	q = initialize_queue();
-
-	enqueue(q, 3);
-	enqueue(q, 4);
-	enqueue(q, 5);
-
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
-
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
-
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
-
-	return (0);
-
-}

+ 0 - 57
queue_thread_model/queue.c

@@ -1,57 +0,0 @@
-#include "queue.h"
-#include <stdlib.h>
-#include <stdio.h>
-
-queue *initialize_queue()
-{
-	queue *q;
-
-	q = (queue *) malloc(sizeof(queue *));
-
-	return q;
-}
-
-void enqueue(queue * q, int value)
-{
-	struct node* aux;
-	if (q->head == NULL) {
-		q->head = (struct node *) malloc(sizeof(struct node *));
-		q->head->next = NULL;
-		q->head->value = value;
-		q->tail = q->head;
-		return;
-	} else {
-		aux = (struct node *) malloc(sizeof(struct node *));
-		aux->next = NULL;
-		aux->value = value;
-		q->tail->next = aux;
-		q->tail = aux;
-	}
-}
-
-int dequeue(queue * q)
-{
-	struct node *aux;
-	int ret_val;
-
-	ret_val = q->head->value;
-	aux = q->head;
-	q->head = q->head->next;
-	free(aux);
-	return (ret_val);
-}
-
-void destroy_queue(queue * q)
-{
-	struct node *aux, *next;
-
-	aux = q->head;
-
-	while (aux != NULL) {
-		next = aux->next;
-		free(aux);
-		if (next != NULL)
-			aux = next;
-	}
-	free(q);
-}

+ 0 - 19
queue_thread_model/queue.h

@@ -1,19 +0,0 @@
-#ifndef QUEUE_H
-#define QUEUE_H
-
-struct node {
-	int value;
-	struct node *next;
-};
-
-typedef struct queue {
-	struct node *head;
-	struct node *tail;
-} queue;
-
-queue *initialize_queue();
-void enqueue(queue * q, int value);
-int dequeue(queue * q);
-void destroy_queue(queue * q);
-
-#endif

+ 0 - 117
tun/netw.c

@@ -1,117 +0,0 @@
-#include <linux/if.h>
-#include <linux/if_tun.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#define MTU 1500
-
-typedef struct ipv4 {
-    u_int16_t tun_flags; //tun specific 16 bits
-    u_int16_t tun_proto; //tun specific 16 bits
-    u_int8_t version; // 4 bits
-    u_int8_t header_length; // 4 bits
-    u_int8_t type_of_service; // 8 bits
-    u_int16_t total_length; //16 bits
-    u_int16_t identification; // 16 bits
-    u_int8_t flags; // 3 bits
-
-} ipv4_t;
-
-int create_tun(char *dev_name, int flags){
-    struct ifreq ifr;
-    int fd, err;
-    char *clonedev = "/dev/net/tun";
-
-    if ((fd = open(clonedev, O_RDWR)) < 0)
-    {
-        return fd;
-    }
-
-    memset(&ifr, 0, sizeof(ifr));
-    ifr.ifr_ifru.ifru_flags = flags;
-
-    if(*dev_name) {
-        strncpy(ifr.ifr_ifrn.ifrn_name, dev_name, IFNAMSIZ);
-    }
-
-    if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0)
-    {
-        close(fd);
-        return err;
-    }
-
-    strcpy(dev_name, ifr.ifr_ifrn.ifrn_name);
-
-    return fd;
-}
-
-void print_packet(u_int8_t* buf, int buf_size) {
-    int i;
-
-    printf("received %d bytes: ", buf_size);
-
-    for (i = 0; i < buf_size; i++)
-    {
-        if (i > 0) printf(":");
-        printf("%02X", buf[i]);
-    }
-    printf("\n");
-}
-
-
-// 0100 0110 46
-// 1111 0000 
-// 0100 0000 40
-// 46 - 40 = 6
-
-// 0100 0110 46
-// 0000 1111 
-// 0000 0110 6
-// 46 - 6 = 40
-
-
-ipv4_t parse_packet(u_int8_t *buf, int buf_size) {
-    
-    ipv4_t p;
-    p.tun_flags = buf[1]<<8 | buf[0];
-    p.tun_proto = buf[2]<<8 | buf[3];
-    p.version =  buf[4] - (buf[4] & 00001111);
-    p.header_length = buf[4] - (buf[4] & 11110000);
-    p.type_of_service = buf[5];
-    p.total_length = buf[6]<<8 | buf[7];
-    p.identification = buf[8]<<8 | buf[9];
-    p.flags = buf[10] - (buf[10] & 00011111);
-    return p;
-}
-
-int main (int argc, char** argv) {
-    char tun_name[IFNAMSIZ];
-    u_int8_t buf[MTU];
-    int ret;
-    ipv4_t curr_packet;
-
-    sprintf(tun_name, "tun01");
-    int tunfd = create_tun(tun_name, (IFF_TUN));
-    if (tunfd < 0) {
-        fprintf(stderr, "could not allocate device tun.\n");
-        return(EXIT_FAILURE);
-    }
-    
-    for (;;) {
-        ret = read(tunfd, buf, MTU);
-        curr_packet = parse_packet(buf, ret);
-
-        if (curr_packet.tun_proto == 0x800) {
-            printf("proto: %02X\n", curr_packet.tun_proto);
-            printf("version: %02X\n", curr_packet.version);
-            printf("header length %02X\n", curr_packet.header_length);
-            print_packet(buf, ret);
-        }
-    }
-   
-    return (0);
-}

+ 0 - 17
tun/run.sh

@@ -1,17 +0,0 @@
-#!/bin/bash
-
-gcc -o netw netw.c
-
-ext=$?
-if [[ $ext -ne 0 ]]; then
-	exit $ext
-fi
-
-sudo setcap cap_net_admin=eip netw
-./netw &
-
-pid=$!
-sudo ip addr add 192.168.99.1/24 dev tun01
-sudo ip link set up dev tun01
-trap "kill $pid" INT TERM
-wait $pid

+ 0 - 35
unchat/client.go

@@ -1,35 +0,0 @@
-package unchat
-
-import (
-	"bufio"
-	"log"
-	"net"
-)
-
-//A Client is a entity which can be connected to/by a server
-type Client struct {
-	client net.Conn
-	alias  string
-	server *Server
-}
-
-//SendMessage write to the tcp body and sends to the client
-func (c *Client) SendMessage(message string) error {
-	_, err := c.client.Write([]byte(message))
-	if err != nil {
-		return err
-	}
-	return nil
-}
-
-func (c *Client) ReadInput() error {
-	for {
-		msg, err := bufio.NewReader(c.client).ReadString('\n')
-		if err != nil {
-			return err
-		}
-		log.Printf("msg: %s", msg)
-
-	}
-
-}

+ 0 - 77
unchat/crypt-unchat.go

@@ -1,77 +0,0 @@
-package unchat
-
-import (
-	"crypto/rand"
-	"crypto/rsa"
-	"crypto/sha256"
-	"crypto/x509"
-	"encoding/pem"
-	"fmt"
-	"log"
-)
-
-func main() {
-	key_pk, err := rsa.GenerateKey(rand.Reader, 2048)
-	if err != nil {
-		log.Fatalf("could not generate keypair %s", err)
-	}
-
-	pub_key := &key_pk.PublicKey
-
-	msg := []byte("this is a new message to be encrypted")
-	label := []byte("")
-	hash := sha256.New()
-
-	cipher_text, err := rsa.EncryptOAEP(
-		hash,
-		rand.Reader,
-		pub_key,
-		msg,
-		label)
-
-	if err != nil {
-		log.Fatalf("could not encrypt message %s", err)
-	}
-
-	fmt.Printf("message encrypted: \n%x\n", cipher_text)
-
-	plain_text, err := rsa.DecryptOAEP(
-		hash,
-		rand.Reader,
-		key_pk,
-		cipher_text,
-		label)
-	if err != nil {
-		log.Fatalf("could not decrypt message %s", err)
-	}
-	fmt.Printf("message decrypted: \n%s\n", plain_text)
-
-	pem := ExportPrivKeyAsPEM(key_pk)
-	fmt.Printf("PEM Priv Key \n%s\n", pem)
-
-	ParsePrivKeyFromPEM(pem)
-
-}
-
-func ExportPrivKeyAsPEM(privKey *rsa.PrivateKey) string {
-	privkey_bytes := x509.MarshalPKCS1PrivateKey(privKey)
-	privkey_pem := pem.EncodeToMemory(
-		&pem.Block{
-			Type:  "RSA PRIVATE KEY",
-			Bytes: privkey_bytes,
-		},
-	)
-	return string(privkey_pem)
-}
-
-func ParsePrivKeyFromPEM(pemKey string) (*rsa.PrivateKey, error) {
-	block, _ := pem.Decode([]byte(pemKey))
-	if block == nil {
-		return nil, fmt.Errorf("could not read pem string")
-	}
-	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
-	if err != nil {
-		return nil, err
-	}
-	return priv, nil
-}

+ 0 - 3
unchat/go.mod

@@ -1,3 +0,0 @@
-module unchat
-
-go 1.14

+ 0 - 74
unchat/server.go

@@ -1,74 +0,0 @@
-package unchat
-
-import (
-	"fmt"
-	"log"
-	"net"
-)
-
-//A Server is a proposed type to hold details about itself and connected clients
-type Server struct {
-	alias    string
-	version  string
-	port     int32
-	clients  map[string]Client
-	listener net.Listener
-}
-
-//NewServer initialize a standard server with common attributes
-func NewServer(alias string) Server {
-	return Server{
-		alias:   alias,
-		port:    9999,
-		version: "default",
-	}
-}
-
-//Open expects the port and the ip address which a TCP listener will be started
-func (s *Server) Open(listenAddress string) {
-
-	var server net.Listener
-	address := fmt.Sprintf("%s:%d", listenAddress, s.port)
-	server, err := net.Listen("TCPV4", address)
-	s.listener = server
-	if err != nil {
-		log.Fatalf("could not listen on %s %s", address, err)
-	}
-	defer s.listener.Close()
-	for {
-		conn, err := s.listener.Accept()
-		if err != nil {
-			log.Fatalf("could not accept connection %s", err)
-		}
-
-		go s.handleConnection(conn)
-		defer conn.Close()
-
-	}
-}
-
-func (s *Server) handleConnection(conn net.Conn) {
-	c := &Client{
-		client: conn,
-		alias:  "undefined",
-		server: s,
-	}
-
-	if s.clients == nil {
-		s.clients = make(map[string]Client)
-	}
-	s.clients[c.alias] = *c
-	s.Broadcast(fmt.Sprintf("> %s has joined the server", c.alias))
-	c.ReadInput()
-}
-
-//Broadcast sends a message to all connected clients
-func (s *Server) Broadcast(message string) error {
-	for _, v := range s.clients {
-		err := v.SendMessage(message)
-		if err != nil {
-			log.Printf("count not broadcast to %s: %s", s.alias, err)
-		}
-	}
-	return nil
-}

+ 0 - 1
x11-random/.gitignore

@@ -1 +0,0 @@
-bin/*

+ 0 - 18
x11-random/Makefile

@@ -1,18 +0,0 @@
-CC=/usr/bin/gcc
-CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD} -lX11 -lm
-OUT=bin
-OBJ=$(OUT)/obj
-OBJS=$(OBJ)/worley_noise.o
-
-
-all: bin/worley_noise
-
-bin/worley_noise: $(OBJS)
-	$(CC) $(CFLAGS) $(OBJS) -o $(OUT)/worley_noise
-
-bin/obj/worley_noise.o: worley_noise.c
-	$(CC) $(CFLAGS) -c worley_noise.c -o $(OBJ)/worley_noise.o
-
-clean:
-	find ./bin -type f -exec rm {} \;
-

+ 0 - 159
x11-random/worley_noise.c

@@ -1,159 +0,0 @@
-#define _XOPEN_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <X11/Xlib.h>
-#include <math.h>
-
-#define WIDTH 1280
-#define HEIGHT 720
-#define SEED 200
-
-#define POINTS 15
-
-static int seed_offset = 1;
-
-struct point {
-	int x;
-	int y;
-};
-
-
-double dist_p1_p2(int p1x, int p1y, int p2x, int p2y)
-{
-	double pA, pB;
-	pA = p1x - p2x;
-	pB = p1y - p2y;
-
-	return sqrt(pA * pA + pB * pB);
-
-}
-
-void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color)
-{
-	XSetForeground(di, gc, color);
-	XDrawPoint(di, wi, gc, x, y);
-}
-
-float rand_0_1()
-{
-	return drand48();
-}
-
-void sort(double *arr)
-{
-	int arr_size = POINTS;
-	int i, j, aux;
-	for (i = 0; i < arr_size; i++) {
-		for (j = i + 1; j < arr_size; j++) {
-			if (arr[i] > arr[j]) {
-				aux = arr[j];
-				arr[j] = arr[i];
-				arr[i] = aux;
-			}
-		}
-	}
-
-}
-
-int map(int value, int min, int max, int map_min, int map_max)
-{
-	double R = (double) (map_max - map_min) / (double) (max - min);
-	double y = map_min + (value * R) + R;
-	return (int) y;
-}
-
-void worley_noise(Display * di, Window wi, GC gc)
-{
-	int x, y, i;
-	int c;
-	int noise_space[WIDTH][HEIGHT];
-	double dist[POINTS], sdist[POINTS];
-	struct point p[POINTS];
-
-
-	for (i = 0; i < POINTS; i++) {
-		p[i].x = (int) (rand_0_1() * WIDTH);
-		p[i].y = (int) (rand_0_1() * HEIGHT);
-	}
-
-	for (x = 0; x < WIDTH; x++) {
-		for (y = 0; y < HEIGHT; y++) {
-			for (i = 0; i < POINTS; i++) {
-				dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
-				sort(dist);
-			}
-			int d1 = map(dist[0], 0, WIDTH/2, 200, 0);
-			int d2 = map(dist[1], 0, WIDTH/2, 200, 0);
-			int d3 = map(dist[2], 0, WIDTH/2, 200, 0);
-			
-			/* 0x000000 rgb value where 
-			 * 16 r
-			 *  8 g
-			 *  4 b*/
-			c = (d1 << 16) + (d2 << 8) + d3;
-			noise_space[x][y] = c;
-		}
-	}
-
-	for (x = 0; x < WIDTH; x++) {
-		for (y = 0; y < HEIGHT; y++) {
-			draw_pixel(di, wi, gc, x, y, noise_space[x][y]);
-		}
-	}
-}
-
-void redraw(Display * di, Window wi, GC gc) {
-	
-	XClearWindow(di, wi);
-	worley_noise(di, wi, gc);
-	XFlush(di);
-}
-
-int main()
-{
-	Display *di = XOpenDisplay(getenv("DISPLAY"));
-	if (di == NULL) {
-		fprintf(stderr, "ERROR: No display");
-		exit(EXIT_FAILURE);
-	}
-	int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
-	int sc = DefaultScreen(di);
-	Window ro = DefaultRootWindow(di);
-	Window wi = XCreateSimpleWindow(di,
-					ro,
-					x,
-					y,
-					width,
-					height,
-					border_width,
-					BlackPixel(di, sc),
-					WhitePixel(di, sc));
-
-	XMapWindow(di, wi);
-	XStoreName(di, wi, "Default Window");
-
-	GC gc = XCreateGC(di, ro, 0, NULL);
-
-	XSelectInput(di, wi, KeyPressMask | ExposureMask);
-	XEvent ev;
-	int quit = 0;
-	while (!quit) {
-		if (XPending(di) > 0) {
-			int a = XNextEvent(di, &ev);
-			if (ev.type == KeyPress) {
-				printf("KeyPress %x\n", ev.xkey.keycode);
-				if (ev.xkey.keycode == 0x18)
-					quit = 1;
-			}
-			if (ev.type == Expose) {
-				redraw(di, wi, gc);
-			}
-		}
-		redraw(di, wi, gc);
-	}
-
-	XFreeGC(di, gc);
-	XDestroyWindow(di, wi);
-	XCloseDisplay(di);
-	return (EXIT_SUCCESS);
-}