Commit e25a3bdc authored by Simon Morlat's avatar Simon Morlat
Browse files

work in progress

parent 0571004c
......@@ -29,6 +29,7 @@ lib_LTLIBRARIES=libbellesip.la
libbellesip_la_SOURCES= \
clock_gettime.c clock_gettime.h \
port.c port.h \
belle_sip_uri_impl.c \
belle_sip_headers_impl.c \
belle_sip_utils.c belle_sip_internal.h \
......
......@@ -24,28 +24,11 @@
#include <sys/types.h>
#include <errno.h>
#ifndef WIN32
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#else
#include <ws2tcpip.h>
#include <winsock2.h>
#include <pthread.h>
#endif
/* include all public headers*/
#include "belle-sip/belle-sip.h"
#include "port.h"
#ifdef PACKAGE
#undef PACKAGE
#endif
......@@ -72,42 +55,6 @@
#include "config.h"
#endif
#if defined(WIN32) || defined(WIN32_WCE)
static inline void close_socket(belle_sip_socket_t s){
closesocket(s);
}
static inline int get_socket_error(void){
return WSAGetLastError();
}
const char *getSocketErrorString();
#define belle_sip_get_socket_error_string() getSocketErrorString()
#define belle_sip_get_socket_error_string_from_code(code) getSocketErrorString()
#define usleep(us) Sleep((us)/1000)
static inline int inet_aton(const char *ip, struct in_addr *p){
*(long*)p=inet_addr(ip);
return 0;
}
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#else
static inline void close_socket(belle_sip_socket_t s){
close(s);
}
static inline int get_socket_error(void){
return errno;
}
#define belle_sip_get_socket_error_string() strerror(errno)
#define belle_sip_get_socket_error_string_from_code(code) strerror(code)
#endif
/*etc*/
#define BELLE_SIP_INTERFACE_GET_METHODS(obj,interface) \
......
......@@ -51,17 +51,21 @@ void belle_sip_resolver_context_destroy(belle_sip_resolver_context_t *ctx){
if (ctx->thread!=0){
if (!ctx->exited){
ctx->cancelled=1;
pthread_cancel(ctx->thread);
belle_sip_thread_cancel(ctx->thread);
}
pthread_join(ctx->thread,NULL);
belle_sip_thread_join(ctx->thread,NULL);
}
if (ctx->name)
belle_sip_free(ctx->name);
if (ctx->ai){
freeaddrinfo(ctx->ai);
}
#ifndef WIN32
close(ctx->ctlpipe[0]);
close(ctx->ctlpipe[1]);
#else
CloseEvent(ctx->ctlevent);
#endif
}
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_resolver_context_t);
......@@ -71,17 +75,23 @@ static int resolver_callback(belle_sip_resolver_context_t *ctx){
char tmp;
ctx->cb(ctx->cb_data, ctx->name, ctx->ai);
ctx->ai=NULL;
#ifndef WIN32
if (read(ctx->source.fd,&tmp,1)!=1){
belle_sip_fatal("Unexpected read from resolver_callback");
}
#else
#endif
return BELLE_SIP_STOP;
}
belle_sip_resolver_context_t *belle_sip_resolver_context_new(){
belle_sip_resolver_context_t *ctx=belle_sip_object_new(belle_sip_resolver_context_t);
#ifndef WIN32
if (pipe(ctx->ctlpipe)==-1){
belle_sip_fatal("pipe() failed: %s",strerror(errno));
}
#else
#endif
belle_sip_fd_source_init(&ctx->source,(belle_sip_source_func_t)resolver_callback,ctx,ctx->ctlpipe[0],BELLE_SIP_EVENT_READ,-1);
return ctx;
}
......@@ -106,10 +116,13 @@ static void *belle_sip_resolver_thread(void *ptr){
belle_sip_message("%s has address %s.",ctx->name,host);
ctx->ai=res;
}
#ifndef WIN32
if (write(ctx->ctlpipe[1],"q",1)==-1){
belle_sip_error("belle_sip_resolver_thread(): Fail to write on pipe.");
}
#else
SetEvent(ctx->ctlevent);
#endif
return NULL;
}
......@@ -125,7 +138,7 @@ unsigned long belle_sip_resolve(const char *name, int port, unsigned int hints,
ctx->hints=hints;
belle_sip_main_loop_add_source(ml,(belle_sip_source_t*)ctx);
belle_sip_object_unref(ctx);
pthread_create(&ctx->thread,NULL,belle_sip_resolver_thread,ctx);
belle_sip_thread_create(&ctx->thread,NULL,belle_sip_resolver_thread,ctx);
return ctx->source.id;
}else{
cb(data,name,res);
......
......@@ -22,9 +22,6 @@
#include "belle_sip_internal.h"
#define BELLE_SIP_RESOLVER_HINT_IPV6 (1)
#define BELLE_SIP_RESOLVER_HINT_SRV (1<<1)
typedef struct belle_sip_resolver_context belle_sip_resolver_context_t;
......@@ -43,8 +40,12 @@ struct belle_sip_resolver_context{
int port;
struct addrinfo *ai;
unsigned int hints;
pthread_t thread;
belle_sip_thread_t thread;
#ifndef WIN32
int ctlpipe[2];
#else
HANDLE ctlevent;
#endif
uint8_t cancelled;
uint8_t exited;
};
......
/*
belle-sip - SIP (RFC3261) library.
Copyright (C) 2010 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "belle_sip_internal.h"
#ifdef WIN32
typedef struct thread_param{
void * (*func)(void *);
void * arg;
}thread_param_t;
static unsigned WINAPI thread_starter(void *data){
thread_param_t *params=(thread_param_t*)data;
void *ret=params->func(params->arg);
belle_sip_free(data);
return (DWORD)ret;
}
int belle_sip_thread_create(belle_sip_thread_t *th, void *attr, void * (*func)(void *), void *data)
{
thread_param_t *params=belle_sip_new(thread_param_t);
params->func=func;
params->arg=data;
*th=(HANDLE)_beginthreadex( NULL, 0, thread_starter, params, 0, NULL);
return 0;
}
int belle_sip_thread_join(belle_sip_thread_t thread_h, void **unused)
{
if (thread_h!=NULL)
{
WaitForSingleObject(thread_h, INFINITE);
CloseHandle(thread_h);
}
return 0;
}
#endif
/*
belle-sip - SIP (RFC3261) library.
Copyright (C) 2010 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef belle_sip_port_h
#define belle_sip_port_h
#ifndef WIN32
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#else
#include <ws2tcpip.h>
#include <winsock2.h>
/*AI_NUMERICSERV is not defined for windows XP. Since it is not essential, we define it to 0 (does nothing)*/
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
#endif
/*
* Socket abstraction layer
*/
#if defined(WIN32)
static inline void close_socket(belle_sip_socket_t s){
closesocket(s);
}
static inline int get_socket_error(void){
return WSAGetLastError();
}
const char *getSocketErrorString();
#define belle_sip_get_socket_error_string() getSocketErrorString()
#define belle_sip_get_socket_error_string_from_code(code) getSocketErrorString()
#define usleep(us) Sleep((us)/1000)
static inline int inet_aton(const char *ip, struct in_addr *p){
*(long*)p=inet_addr(ip);
return 0;
}
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#else
static inline void close_socket(belle_sip_socket_t s){
close(s);
}
static inline int get_socket_error(void){
return errno;
}
#define belle_sip_get_socket_error_string() strerror(errno)
#define belle_sip_get_socket_error_string_from_code(code) strerror(code)
#endif
/*
* Thread abstraction layer
*/
#ifdef WIN32
typedef HANDLE belle_sip_thread_t;
int belle_sip_thread_join(belle_sip_thread_t thread, void **retptr);
int belle_sip_thread_create(belle_sip_thread_t *thread, void *attr, void * (*routine)(void*), void *arg);
#else
#include <pthread.h>
typedef pthread_t belle_sip_thread_t;
#define belle_sip_thread_join(thread,retptr) pthread_join(thread,retptr)
#define belle_sip_thread_create(thread,attr,routine,arg) pthread_create(thread,attr,routine,arg)
#endif
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment