summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/socket.c392
-rw-r--r--src/socket.h65
-rw-r--r--src/thread.c177
-rw-r--r--src/thread.h70
4 files changed, 0 insertions, 704 deletions
diff --git a/src/socket.c b/src/socket.c
deleted file mode 100644
index b276864..0000000
--- a/src/socket.c
+++ /dev/null
@@ -1,392 +0,0 @@
-/*
- * socket.c
- *
- * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
- * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdio.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <sys/stat.h>
-#ifdef WIN32
-#include <winsock2.h>
-#include <windows.h>
-static int wsa_init = 0;
-#else
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <arpa/inet.h>
-#endif
-#include "socket.h"
-
-#define RECV_TIMEOUT 20000
-
-static int verbose = 0;
-
-void socket_set_verbose(int level)
-{
- verbose = level;
-}
-
-#ifndef WIN32
-int socket_create_unix(const char *filename)
-{
- struct sockaddr_un name;
- int sock;
- size_t size;
-
- // remove if still present
- unlink(filename);
-
- /* Create the socket. */
- sock = socket(PF_LOCAL, SOCK_STREAM, 0);
- if (sock < 0) {
- perror("socket");
- return -1;
- }
-
- /* Bind a name to the socket. */
- name.sun_family = AF_LOCAL;
- strncpy(name.sun_path, filename, sizeof(name.sun_path));
- name.sun_path[sizeof(name.sun_path) - 1] = '\0';
-
- /* The size of the address is
- the offset of the start of the filename,
- plus its length,
- plus one for the terminating null byte.
- Alternatively you can just do:
- size = SUN_LEN (&name);
- */
- size = (offsetof(struct sockaddr_un, sun_path)
- + strlen(name.sun_path) + 1);
-
- if (bind(sock, (struct sockaddr *) &name, size) < 0) {
- perror("bind");
- socket_close(sock);
- return -1;
- }
-
- if (listen(sock, 10) < 0) {
- perror("listen");
- socket_close(sock);
- return -1;
- }
-
- return sock;
-}
-
-int socket_connect_unix(const char *filename)
-{
- struct sockaddr_un name;
- int sfd = -1;
- size_t size;
- struct stat fst;
-
- // check if socket file exists...
- if (stat(filename, &fst) != 0) {
- if (verbose >= 2)
- fprintf(stderr, "%s: stat '%s': %s\n", __func__, filename,
- strerror(errno));
- return -1;
- }
- // ... and if it is a unix domain socket
- if (!S_ISSOCK(fst.st_mode)) {
- if (verbose >= 2)
- fprintf(stderr, "%s: File '%s' is not a socket!\n", __func__,
- filename);
- return -1;
- }
- // make a new socket
- if ((sfd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
- if (verbose >= 2)
- fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno));
- return -1;
- }
- // and connect to 'filename'
- name.sun_family = AF_LOCAL;
- strncpy(name.sun_path, filename, sizeof(name.sun_path));
- name.sun_path[sizeof(name.sun_path) - 1] = 0;
-
- size = (offsetof(struct sockaddr_un, sun_path)
- + strlen(name.sun_path) + 1);
-
- if (connect(sfd, (struct sockaddr *) &name, size) < 0) {
- socket_close(sfd);
- if (verbose >= 2)
- fprintf(stderr, "%s: connect: %s\n", __func__,
- strerror(errno));
- return -1;
- }
-
- return sfd;
-}
-#endif
-
-int socket_create(uint16_t port)
-{
- int sfd = -1;
- int yes = 1;
-#ifdef WIN32
- WSADATA wsa_data;
- if (!wsa_init) {
- if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) {
- fprintf(stderr, "WSAStartup failed!\n");
- ExitProcess(-1);
- }
- wsa_init = 1;
- }
-#endif
- struct sockaddr_in saddr;
-
- if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
- perror("socket()");
- return -1;
- }
-
- if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) {
- perror("setsockopt()");
- socket_close(sfd);
- return -1;
- }
-
- memset((void *) &saddr, 0, sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = htonl(INADDR_ANY);
- saddr.sin_port = htons(port);
-
- if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) {
- perror("bind()");
- socket_close(sfd);
- return -1;
- }
-
- if (listen(sfd, 1) == -1) {
- perror("listen()");
- socket_close(sfd);
- return -1;
- }
-
- return sfd;
-}
-
-int socket_connect(const char *addr, uint16_t port)
-{
- int sfd = -1;
- int yes = 1;
- struct hostent *hp;
- struct sockaddr_in saddr;
-#ifdef WIN32
- WSADATA wsa_data;
- if (!wsa_init) {
- if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) {
- fprintf(stderr, "WSAStartup failed!\n");
- ExitProcess(-1);
- }
- wsa_init = 1;
- }
-#endif
-
- if (!addr) {
- errno = EINVAL;
- return -1;
- }
-
- if ((hp = gethostbyname(addr)) == NULL) {
- if (verbose >= 2)
- fprintf(stderr, "%s: unknown host '%s'\n", __func__, addr);
- return -1;
- }
-
- if (!hp->h_addr) {
- if (verbose >= 2)
- fprintf(stderr, "%s: gethostbyname returned NULL address!\n",
- __func__);
- return -1;
- }
-
- if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
- perror("socket()");
- return -1;
- }
-
- if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) {
- perror("setsockopt()");
- socket_close(sfd);
- return -1;
- }
-
- memset((void *) &saddr, 0, sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = *(uint32_t *) hp->h_addr;
- saddr.sin_port = htons(port);
-
- if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
- perror("connect");
- socket_close(sfd);
- return -2;
- }
-
- return sfd;
-}
-
-int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout)
-{
- fd_set fds;
- int sret;
- int eagain;
- struct timeval to;
- struct timeval *pto;
-
- if (fd <= 0) {
- if (verbose >= 2)
- fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd);
- return -1;
- }
-
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
-
- if (timeout > 0) {
- to.tv_sec = (time_t) (timeout / 1000);
- to.tv_usec = (time_t) ((timeout - (to.tv_sec * 1000)) * 1000);
- pto = &to;
- } else {
- pto = NULL;
- }
-
- sret = -1;
-
- do {
- eagain = 0;
- switch (fdm) {
- case FDM_READ:
- sret = select(fd + 1, &fds, NULL, NULL, pto);
- break;
- case FDM_WRITE:
- sret = select(fd + 1, NULL, &fds, NULL, pto);
- break;
- case FDM_EXCEPT:
- sret = select(fd + 1, NULL, NULL, &fds, pto);
- break;
- default:
- return -1;
- }
-
- if (sret < 0) {
- switch (errno) {
- case EINTR:
- // interrupt signal in select
- if (verbose >= 2)
- fprintf(stderr, "%s: EINTR\n", __func__);
- eagain = 1;
- break;
- case EAGAIN:
- if (verbose >= 2)
- fprintf(stderr, "%s: EAGAIN\n", __func__);
- break;
- default:
- if (verbose >= 2)
- fprintf(stderr, "%s: select failed: %s\n", __func__,
- strerror(errno));
- return -1;
- }
- }
- } while (eagain);
-
- return sret;
-}
-
-int socket_accept(int fd, uint16_t port)
-{
-#ifdef WIN32
- int addr_len;
-#else
- socklen_t addr_len;
-#endif
- int result;
- struct sockaddr_in addr;
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_port = htons(port);
-
- addr_len = sizeof(addr);
- result = accept(fd, (struct sockaddr*)&addr, &addr_len);
-
- return result;
-}
-
-int socket_shutdown(int fd, int how)
-{
- return shutdown(fd, how);
-}
-
-int socket_close(int fd) {
-#ifdef WIN32
- return closesocket(fd);
-#else
- return close(fd);
-#endif
-}
-
-int socket_receive(int fd, void *data, size_t length)
-{
- return socket_receive_timeout(fd, data, length, 0, RECV_TIMEOUT);
-}
-
-int socket_peek(int fd, void *data, size_t length)
-{
- return socket_receive_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT);
-}
-
-int socket_receive_timeout(int fd, void *data, size_t length, int flags,
- unsigned int timeout)
-{
- int res;
- int result;
-
- // check if data is available
- res = socket_check_fd(fd, FDM_READ, timeout);
- if (res <= 0) {
- return res;
- }
- // if we get here, there _is_ data available
- result = recv(fd, data, length, flags);
- if (res > 0 && result == 0) {
- // but this is an error condition
- if (verbose >= 3)
- fprintf(stderr, "%s: fd=%d recv returned 0\n", __func__, fd);
- return -EAGAIN;
- }
- if (result < 0) {
- return -errno;
- }
- return result;
-}
-
-int socket_send(int fd, void *data, size_t length)
-{
- return send(fd, data, length, 0);
-}
diff --git a/src/socket.h b/src/socket.h
deleted file mode 100644
index 81ee083..0000000
--- a/src/socket.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * socket.h
- *
- * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
- * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef __SOCKET_SOCKET_H
-#define __SOCKET_SOCKET_H
-
-#include <stdlib.h>
-#include <stdint.h>
-
-enum fd_mode {
- FDM_READ,
- FDM_WRITE,
- FDM_EXCEPT
-};
-typedef enum fd_mode fd_mode;
-
-#ifdef WIN32
-#include <winsock2.h>
-#define SHUT_RD SD_READ
-#define SHUT_WR SD_WRITE
-#define SHUT_RDWR SD_BOTH
-#else
-#include <sys/socket.h>
-#endif
-
-#ifndef WIN32
-int socket_create_unix(const char *filename);
-int socket_connect_unix(const char *filename);
-#endif
-int socket_create(uint16_t port);
-int socket_connect(const char *addr, uint16_t port);
-int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout);
-int socket_accept(int fd, uint16_t port);
-
-int socket_shutdown(int fd, int how);
-int socket_close(int fd);
-
-int socket_receive(int fd, void *data, size_t size);
-int socket_peek(int fd, void *data, size_t size);
-int socket_receive_timeout(int fd, void *data, size_t size, int flags,
- unsigned int timeout);
-
-int socket_send(int fd, void *data, size_t size);
-
-void socket_set_verbose(int level);
-
-#endif /* __SOCKET_SOCKET_H */
diff --git a/src/thread.c b/src/thread.c
deleted file mode 100644
index 8ccdd7f..0000000
--- a/src/thread.c
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * thread.c
- *
- * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
- * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "thread.h"
-
-int thread_new(thread_t *thread, thread_func_t thread_func, void* data)
-{
-#ifdef WIN32
- HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL);
- if (th == NULL) {
- return -1;
- }
- *thread = th;
- return 0;
-#else
- int res = pthread_create(thread, NULL, thread_func, data);
- return res;
-#endif
-}
-
-void thread_free(thread_t thread)
-{
-#ifdef WIN32
- CloseHandle(thread);
-#endif
-}
-
-void thread_join(thread_t thread)
-{
- /* wait for thread to complete */
-#ifdef WIN32
- WaitForSingleObject(thread, INFINITE);
-#else
- pthread_join(thread, NULL);
-#endif
-}
-
-int thread_alive(thread_t thread)
-{
- if (!thread)
- return 0;
-#ifdef WIN32
- return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT;
-#else
- return pthread_kill(thread, 0) == 0;
-#endif
-}
-
-void mutex_init(mutex_t* mutex)
-{
-#ifdef WIN32
- InitializeCriticalSection(mutex);
-#else
- pthread_mutex_init(mutex, NULL);
-#endif
-}
-
-void mutex_destroy(mutex_t* mutex)
-{
-#ifdef WIN32
- DeleteCriticalSection(mutex);
-#else
- pthread_mutex_destroy(mutex);
-#endif
-}
-
-void mutex_lock(mutex_t* mutex)
-{
-#ifdef WIN32
- EnterCriticalSection(mutex);
-#else
- pthread_mutex_lock(mutex);
-#endif
-}
-
-void mutex_unlock(mutex_t* mutex)
-{
-#ifdef WIN32
- LeaveCriticalSection(mutex);
-#else
- pthread_mutex_unlock(mutex);
-#endif
-}
-
-void thread_once(thread_once_t *once_control, void (*init_routine)(void))
-{
-#ifdef WIN32
- while (InterlockedExchange(&(once_control->lock), 1) != 0) {
- Sleep(1);
- }
- if (!once_control->state) {
- once_control->state = 1;
- init_routine();
- }
- InterlockedExchange(&(once_control->lock), 0);
-#else
- pthread_once(once_control, init_routine);
-#endif
-}
-
-void cond_init(cond_t* cond)
-{
-#ifdef WIN32
- cond->sem = CreateSemaphore(NULL, 0, 32767, NULL);
-#else
- pthread_cond_init(cond, NULL);
-#endif
-}
-
-void cond_destroy(cond_t* cond)
-{
-#ifdef WIN32
- CloseHandle(cond->sem);
-#else
- pthread_cond_destroy(cond);
-#endif
-}
-
-int cond_signal(cond_t* cond)
-{
-#ifdef WIN32
- int result = 0;
- if (!ReleaseSemaphore(cond->sem, 1, NULL)) {
- result = -1;
- }
- return result;
-#else
- return pthread_cond_signal(cond);
-#endif
-}
-
-int cond_wait(cond_t* cond, mutex_t* mutex)
-{
-#ifdef WIN32
- mutex_unlock(mutex);
- WaitForSingleObject(cond->sem, INFINITE);
-#else
- return pthread_cond_wait(cond, mutex);
-#endif
-}
-
-int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms)
-{
-#ifdef WIN32
- mutex_unlock(mutex);
- WaitForSingleObject(cond->sem, timeout_ms);
-#else
- struct timespec ts;
- struct timeval now;
- gettimeofday(&now, NULL);
-
- ts.tv_sec = now.tv_sec + timeout_ms / 1000;
- ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000);
- ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
- ts.tv_nsec %= (1000 * 1000 * 1000);
-
- return pthread_cond_timedwait(cond, mutex, &ts);
-#endif
-}
diff --git a/src/thread.h b/src/thread.h
deleted file mode 100644
index 69c6632..0000000
--- a/src/thread.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * thread.h
- *
- * Copyright (c) 2012 Martin Szulecki All Rights Reserved.
- * Copyright (c) 2012 Nikias Bassen All Rights Reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef __THREAD_H
-#define __THREAD_H
-
-#ifdef WIN32
-#include <windows.h>
-typedef HANDLE thread_t;
-typedef CRITICAL_SECTION mutex_t;
-typedef struct {
- HANDLE sem;
-} cond_t;
-typedef volatile struct {
- LONG lock;
- int state;
-} thread_once_t;
-#define THREAD_ONCE_INIT {0, 0}
-#define THREAD_ID GetCurrentThreadId()
-#else
-#include <pthread.h>
-#include <signal.h>
-#include <sys/time.h>
-typedef pthread_t thread_t;
-typedef pthread_mutex_t mutex_t;
-typedef pthread_cond_t cond_t;
-typedef pthread_once_t thread_once_t;
-#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
-#define THREAD_ID pthread_self()
-#endif
-
-typedef void* (*thread_func_t)(void* data);
-
-int thread_new(thread_t* thread, thread_func_t thread_func, void* data);
-void thread_free(thread_t thread);
-void thread_join(thread_t thread);
-int thread_alive(thread_t thread);
-
-void mutex_init(mutex_t* mutex);
-void mutex_destroy(mutex_t* mutex);
-void mutex_lock(mutex_t* mutex);
-void mutex_unlock(mutex_t* mutex);
-
-void thread_once(thread_once_t *once_control, void (*init_routine)(void));
-
-void cond_init(cond_t* cond);
-void cond_destroy(cond_t* cond);
-int cond_signal(cond_t* cond);
-int cond_wait(cond_t* cond, mutex_t* mutex);
-int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms);
-
-#endif