Compare commits
5 commits
fa8bbd7001
...
d5829f14e8
Author | SHA1 | Date | |
---|---|---|---|
|
d5829f14e8 | ||
5ad3b0ef04 | |||
|
120db2838c | ||
|
c9c662dd00 | ||
|
70022ae1f6 |
4 changed files with 318 additions and 1 deletions
2
Makefile
2
Makefile
|
@ -9,7 +9,7 @@ TEST_SRC = $(wildcard test/*.c)
|
|||
TEST_BINS = $(patsubst test/%,bin/%,$(patsubst %.c,%.test,$(TEST_SRC)))
|
||||
|
||||
# CFLAGS = -std=c99 -Ideps -Wall -Wno-unused-function -U__STRICT_ANSI__
|
||||
CFLAGS = -ggdb -std=c99 -Ideps -Ilib -Wall -Wno-unused-function -pedantic
|
||||
CFLAGS = -ggdb -std=c99 -Ideps -Ilib -Wall -Wno-unused-function -pedantic -D_POSIX_C_SOURCE=200112L
|
||||
|
||||
|
||||
all: $(BINS) $(TEST_BINS)
|
||||
|
|
154
src/hashtable-simple.c
Normal file
154
src/hashtable-simple.c
Normal file
|
@ -0,0 +1,154 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct hashcell_t {
|
||||
char key[128];
|
||||
void * value;
|
||||
struct hashcell_t* next;
|
||||
} HASHCELL;
|
||||
|
||||
typedef struct {
|
||||
int capacity;
|
||||
int size;
|
||||
HASHCELL** data;
|
||||
} HASHTABLE;
|
||||
|
||||
int hash_of_key(HASHTABLE* hashtable, char * key) {
|
||||
int val=0;
|
||||
int idx=0;
|
||||
while (key[idx] != '\0') {
|
||||
val ^= key[idx];
|
||||
idx++;
|
||||
}
|
||||
return (val % hashtable->capacity);
|
||||
}
|
||||
|
||||
HASHTABLE* hashtable_create(int capacity) {
|
||||
HASHTABLE * hashtable;
|
||||
printf("hashtable_create -- %d\n", capacity);
|
||||
|
||||
hashtable = malloc(sizeof(HASHTABLE));
|
||||
if (!hashtable) return NULL;
|
||||
memset(hashtable, 0, sizeof(HASHTABLE));
|
||||
|
||||
hashtable->data = malloc(capacity * sizeof(HASHCELL*));
|
||||
if (!hashtable->data) return NULL;
|
||||
memset(hashtable->data, 0, capacity * sizeof(HASHCELL*));
|
||||
|
||||
hashtable->capacity = capacity;
|
||||
hashtable->size = 0;
|
||||
|
||||
printf("hashtable_create -- done\n");
|
||||
return hashtable;
|
||||
}
|
||||
|
||||
void hashtable_destroy(HASHTABLE* hashtable) {
|
||||
printf("hashtable_destroy(%p) -- \n", (void*)hashtable);
|
||||
if (!hashtable) return;
|
||||
if (!hashtable->data) return;
|
||||
free(hashtable->data);
|
||||
free(hashtable);
|
||||
printf("hashtable_destroy(%p) -- done\n", (void*)hashtable);
|
||||
}
|
||||
|
||||
void hashtable_rehash(HASHTABLE* hashtable) {
|
||||
return;
|
||||
}
|
||||
|
||||
int hashtable_set(HASHTABLE* hashtable, char* key, void * value) {
|
||||
int idx;
|
||||
HASHCELL* cell;
|
||||
|
||||
printf("hashtable_set(%s, %p) -- \n", key, (void*)value);
|
||||
// get position in hashtable
|
||||
idx = hash_of_key(hashtable, key);
|
||||
hashtable->size += 1;
|
||||
|
||||
printf("hashtable_set -- index [%d/%d]\n", idx, hashtable->capacity);
|
||||
// search value
|
||||
cell = hashtable->data[idx];
|
||||
while(cell) {
|
||||
if (strcmp(cell->key, key) == 0) { break; }
|
||||
cell = cell->next;
|
||||
}
|
||||
|
||||
if (cell) {
|
||||
// if cell exists, then change inner value
|
||||
printf("hashtable_set -- update value\n");
|
||||
cell->value = value;
|
||||
} else {
|
||||
printf("hashtable_set -- create value\n");
|
||||
// create cell & prepend it to the chain
|
||||
cell = malloc(sizeof(HASHCELL));
|
||||
if (!cell) return -1;
|
||||
memset(cell, 0, sizeof(HASHCELL));
|
||||
|
||||
cell->next = hashtable->data[idx];
|
||||
cell->value = value;
|
||||
strcpy(cell->key, key);
|
||||
hashtable->data[idx] = cell;
|
||||
printf("hashtable_set -- cell@%p{key=%s, value=%p, next=%p}\n",
|
||||
(void*)cell,
|
||||
cell->key,
|
||||
(void*)cell->value,
|
||||
(void*)cell->next);
|
||||
}
|
||||
printf("hashtable_set(...) -- done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void * hashtable_get(HASHTABLE* hashtable, char* key) {
|
||||
int idx;
|
||||
HASHCELL* cell;
|
||||
printf("hashtable_get(%p, %p) -- \n", hashtable, key);
|
||||
idx = hash_of_key(hashtable, key);
|
||||
printf("hashtable_get(...) -- index [%d/%d]\n", idx, hashtable->capacity);
|
||||
cell = hashtable->data[idx];
|
||||
while(cell) {
|
||||
printf("hashtable_get(...) -- cell@%p{key=%s, value=%p, next=%p}\n",
|
||||
(void*)cell,
|
||||
cell->key,
|
||||
cell->value,
|
||||
cell->next);
|
||||
if (strcmp(cell->key, key) == 0) { break; }
|
||||
cell = cell->next;
|
||||
}
|
||||
if (cell) {
|
||||
return cell->value;
|
||||
} else {
|
||||
printf("hashtable_get(...) -- cell not found\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
HASHTABLE * hashtable;
|
||||
int x=1, y=2, z=3;
|
||||
int* value;
|
||||
|
||||
printf("Run !\n");
|
||||
hashtable = hashtable_create(1000);
|
||||
hashtable_destroy(hashtable);
|
||||
|
||||
hashtable = hashtable_create(100);
|
||||
hashtable_set(hashtable, "alice", &x);
|
||||
printf("h['alice'] <- %d\n", x);
|
||||
hashtable_set(hashtable, "bob", &y);
|
||||
printf("h['bob'] <- %d\n", y);
|
||||
hashtable_set(hashtable, "charlie", &z);
|
||||
printf("h['charlie'] <- %d\n", z);
|
||||
hashtable_set(hashtable, "alice", &y);
|
||||
printf("h['alice'] <- %d\n", y);
|
||||
|
||||
value = hashtable_get(hashtable, "alice");
|
||||
printf("h['alice'] ? %d\n", *value);
|
||||
value = hashtable_get(hashtable, "bob");
|
||||
printf("h['bob'] ? %d\n", *value);
|
||||
value = hashtable_get(hashtable, "charlie");
|
||||
printf("h['charlie'] ? %d\n", *value);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
68
src/udp-client.c
Normal file
68
src/udp-client.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
** talker.c -- a datagram "client" demo
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define SERVERPORT "9119" // the port users will be connecting to
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sockfd;
|
||||
struct addrinfo *servinfo, *p;
|
||||
struct addrinfo hints;
|
||||
int rv;
|
||||
int numbytes;
|
||||
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,"usage: talker hostname message\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// loop through all the results and make a socket
|
||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||
p->ai_protocol)) == -1) {
|
||||
perror("talker: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "talker: failed to create socket\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
|
||||
p->ai_addr, p->ai_addrlen)) == -1) {
|
||||
perror("talker: sendto");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
|
||||
printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
95
src/udp-server.c
Normal file
95
src/udp-server.c
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
** listener.c -- a datagram sockets "server" demo
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define MYPORT "9119" // the port users will be connecting to
|
||||
|
||||
#define MAXBUFLEN 100
|
||||
|
||||
// get sockaddr, IPv4 or IPv6:
|
||||
void *get_in_addr(struct sockaddr *sa)
|
||||
{
|
||||
if (sa->sa_family == AF_INET) {
|
||||
return &(((struct sockaddr_in*)sa)->sin_addr);
|
||||
}
|
||||
|
||||
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int sockfd;
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
int rv;
|
||||
int numbytes;
|
||||
struct sockaddr_storage their_addr;
|
||||
char buf[MAXBUFLEN];
|
||||
socklen_t addr_len;
|
||||
char s[INET6_ADDRSTRLEN];
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_flags = AI_PASSIVE; // use my IP
|
||||
|
||||
if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// loop through all the results and bind to the first we can
|
||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||
p->ai_protocol)) == -1) {
|
||||
perror("listener: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
|
||||
close(sockfd);
|
||||
perror("listener: bind");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "listener: failed to bind socket\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
|
||||
printf("listener: waiting to recvfrom...\n");
|
||||
|
||||
addr_len = sizeof their_addr;
|
||||
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
|
||||
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
|
||||
perror("recvfrom");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("listener: got packet from %s\n",
|
||||
inet_ntop(their_addr.ss_family,
|
||||
get_in_addr((struct sockaddr *)&their_addr),
|
||||
s, sizeof s));
|
||||
printf("listener: packet is %d bytes long\n", numbytes);
|
||||
buf[numbytes] = '\0';
|
||||
printf("listener: packet contains \"%s\"\n", buf);
|
||||
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue