Index: uspace/lib/c/arch/abs32le/include/fibril.h
===================================================================
--- uspace/lib/c/arch/abs32le/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/abs32le/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -44,4 +44,5 @@
 		(ctx)->pc = (uintptr_t) (_pc); \
 		(ctx)->sp = ((uintptr_t) (stack)) + (size) - SP_DELTA; \
+		(ctx)->fp = 0; \
 		(ctx)->tls = ((uintptr_t) (ptls)) + sizeof(tcb_t); \
 	} while (0)
@@ -53,7 +54,14 @@
 typedef struct {
 	uintptr_t sp;
+	uintptr_t fp;
 	uintptr_t pc;
 	uintptr_t tls;
 } context_t;
+
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	/* On real hardware, this function returns the frame pointer. */
+	return ctx->fp;
+}
 
 #endif
Index: uspace/lib/c/arch/amd64/include/fibril.h
===================================================================
--- uspace/lib/c/arch/amd64/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/amd64/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -56,17 +56,22 @@
  */
 typedef struct {
-    uint64_t sp;
-    uint64_t pc;
-    
-    uint64_t rbx;
-    uint64_t rbp;
+	uint64_t sp;
+	uint64_t pc;
 
-    uint64_t r12;
-    uint64_t r13;
-    uint64_t r14;
-    uint64_t r15;
+	uint64_t rbx;
+	uint64_t rbp;
 
-    uint64_t tls;
+	uint64_t r12;
+	uint64_t r13;
+	uint64_t r14;
+	uint64_t r15;
+
+	uint64_t tls;
 } context_t;
+
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->rbp;
+}
 
 #endif
Index: uspace/lib/c/arch/arm32/include/fibril.h
===================================================================
--- uspace/lib/c/arch/arm32/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/arm32/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -86,4 +86,9 @@
 } context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->fp;
+}
+
 
 #endif
Index: uspace/lib/c/arch/ia32/include/fibril.h
===================================================================
--- uspace/lib/c/arch/ia32/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/ia32/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -67,4 +67,9 @@
 } context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->ebp;
+}
+
 #endif
 
Index: uspace/lib/c/arch/ia64/include/fibril.h
===================================================================
--- uspace/lib/c/arch/ia64/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/ia64/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -130,4 +130,9 @@
 } context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return 0;	/* FIXME */
+}
+
 #endif
 
Index: uspace/lib/c/arch/mips32/include/fibril.h
===================================================================
--- uspace/lib/c/arch/mips32/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/mips32/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -85,4 +85,9 @@
 } context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->sp;
+}
+
 #endif
 
Index: uspace/lib/c/arch/ppc32/include/fibril.h
===================================================================
--- uspace/lib/c/arch/ppc32/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/ppc32/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -78,4 +78,9 @@
 } __attribute__ ((packed)) context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->sp;
+}
+
 #endif
 
Index: uspace/lib/c/arch/sparc64/include/fibril.h
===================================================================
--- uspace/lib/c/arch/sparc64/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/arch/sparc64/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -77,4 +77,9 @@
 } context_t;
 
+static inline uintptr_t context_get_fp(context_t *ctx)
+{
+	return ctx->sp + STACK_BIAS;
+}
+
 #endif
 
Index: uspace/lib/c/generic/fibril.c
===================================================================
--- uspace/lib/c/generic/fibril.c	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/generic/fibril.c	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -275,4 +275,6 @@
 	fibril->func = func;
 	fibril->arg = arg;
+
+	fibril->waits_for = NULL;
 	
 	context_save(&fibril->ctx);
Index: uspace/lib/c/generic/fibril_synch.c
===================================================================
--- uspace/lib/c/generic/fibril_synch.c	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/generic/fibril_synch.c	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -42,4 +42,6 @@
 #include <errno.h>
 #include <assert.h>
+#include <stacktrace.h>
+#include <stdlib.h>
 
 static void optimize_execution_power(void)
@@ -56,6 +58,42 @@
 }
 
+static bool check_for_deadlock(fibril_owner_info_t *oi)
+{
+	while (oi && oi->owned_by) {
+		if (oi->owned_by == (fibril_t *) fibril_get_id())
+			return true;
+		oi = oi->owned_by->waits_for;
+	}
+
+	return false;
+}
+
+static void print_deadlock(fibril_owner_info_t *oi)
+{
+	fibril_t *f = (fibril_t *) fibril_get_id();
+
+	printf("Deadlock detected.\n");
+	stacktrace_print();
+
+	printf("Fibril %p waits for primitive %p.\n", f, oi);
+
+	while (oi && oi->owned_by) {
+		printf("Primitive %p is owned by fibril %p.\n",
+		    oi, oi->owned_by);
+		if (oi->owned_by == f)
+			break;
+		stacktrace_print_fp_pc(context_get_fp(&oi->owned_by->ctx),
+		    oi->owned_by->ctx.pc);
+		printf("Fibril %p waits for primitive %p.\n",
+		     oi->owned_by, oi->owned_by->waits_for);
+		oi = oi->owned_by->waits_for;
+	}
+
+	abort();
+}
+
 void fibril_mutex_initialize(fibril_mutex_t *fm)
 {
+	fm->oi.owned_by = NULL;
 	fm->counter = 1;
 	list_initialize(&fm->waiters);
@@ -64,4 +102,6 @@
 void fibril_mutex_lock(fibril_mutex_t *fm)
 {
+	fibril_t *f = (fibril_t *) fibril_get_id();
+
 	futex_down(&async_futex);
 	if (fm->counter-- <= 0) {
@@ -73,6 +113,12 @@
 		link_initialize(&wdata.wu_event.link);
 		list_append(&wdata.wu_event.link, &fm->waiters);
+
+		if (check_for_deadlock(&fm->oi))
+			print_deadlock(&fm->oi);
+		f->waits_for = &fm->oi;
+
 		fibril_switch(FIBRIL_TO_MANAGER);
 	} else {
+		fm->oi.owned_by = f;
 		futex_up(&async_futex);
 	}
@@ -86,4 +132,5 @@
 	if (fm->counter > 0) {
 		fm->counter--;
+		fm->oi.owned_by = (fibril_t *) fibril_get_id();
 		locked = true;
 	}
@@ -99,4 +146,5 @@
 		link_t *tmp;
 		awaiter_t *wdp;
+		fibril_t *f;
 	
 		assert(!list_empty(&fm->waiters));
@@ -105,7 +153,14 @@
 		wdp->active = true;
 		wdp->wu_event.inlist = false;
+
+		f = (fibril_t *) wdp->fid;
+		fm->oi.owned_by = f;
+		f->waits_for = NULL;
+
 		list_remove(&wdp->wu_event.link);
 		fibril_add_ready(wdp->fid);
 		optimize_execution_power();
+	} else {
+		fm->oi.owned_by = NULL;
 	}
 }
@@ -120,4 +175,5 @@
 void fibril_rwlock_initialize(fibril_rwlock_t *frw)
 {
+	frw->oi.owned_by = NULL;
 	frw->writers = 0;
 	frw->readers = 0;
Index: uspace/lib/c/include/fibril.h
===================================================================
--- uspace/lib/c/include/fibril.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/include/fibril.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -48,4 +48,10 @@
 #define FIBRIL_WRITER      2
 
+struct fibril;
+
+typedef struct {
+	struct fibril *owned_by;
+} fibril_owner_info_t;
+
 typedef enum {
 	FIBRIL_PREEMPT,
@@ -68,4 +74,6 @@
 	int retval;
 	int flags;
+
+	fibril_owner_info_t *waits_for;
 } fibril_t;
 
Index: uspace/lib/c/include/fibril_synch.h
===================================================================
--- uspace/lib/c/include/fibril_synch.h	(revision f4c8a83fcccd1bda6e8735965144672a29bb8336)
+++ uspace/lib/c/include/fibril_synch.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -43,4 +43,5 @@
 
 typedef struct {
+	fibril_owner_info_t oi;		/* Keep this the first thing. */
 	int counter;
 	link_t waiters;
@@ -49,4 +50,7 @@
 #define FIBRIL_MUTEX_INITIALIZER(name) \
 	{ \
+		.oi = { \
+			.owned_by = NULL \
+		}, \
 		.counter = 1, \
 		.waiters = { \
@@ -60,4 +64,5 @@
 
 typedef struct {
+	fibril_owner_info_t oi;	/* Keep this the first thing. */
 	unsigned writers;
 	unsigned readers;
@@ -67,4 +72,7 @@
 #define FIBRIL_RWLOCK_INITIALIZER(name) \
 	{ \
+		.oi = { \
+			.owned_by = NULL \
+		}, \
 		.readers = 0, \
 		.writers = 0, \
Index: uspace/lib/packet/include/netdb.h
===================================================================
--- uspace/lib/packet/include/netdb.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
+++ uspace/lib/packet/include/netdb.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup netdb
+ *  @{
+ */
+
+/** @file
+ *  Structures and interfaces according to the BSD netdb.h file.
+ */
+
+#ifndef __NET_NETDB_H__
+#define __NET_NETDB_H__
+
+#include <sys/types.h>
+
+/** Structure returned by network data base library.
+ *  All addresses are supplied in host order, and returned in network order (suitable for use in system calls).
+ */
+struct	hostent {
+	/** Official host name.
+	 */
+	char * h_name;
+	/** Alias list.
+	 */
+	char **	h_aliases;
+	/** Host address type.
+	 */
+	int h_addrtype;
+	/** Address length.
+	 */
+	int h_length;
+	/** List of addresses from name server.
+	 */
+	char **	h_addr_list;
+	/** Address, for backward compatiblity.
+	 */
+#define	h_addr	h_addr_list[0]
+};
+
+/** @name Host entry address types definitions.
+ */
+/*@{*/
+
+/** Authoritative Answer Host not found address type.
+ */
+#define	HOST_NOT_FOUND	1
+
+/** Non-Authoritive Host not found, or SERVERFAIL address type.
+ */
+#define	TRY_AGAIN	2
+
+/** Non recoverable errors, FORMERR, REFUSED, NOTIMP address type.
+ */
+#define	NO_RECOVERY	3
+
+/** Valid name, no data record of requested type address type.
+ */
+#define	NO_DATA		4
+
+/** No address, look for MX record address type.
+ */
+#define	NO_ADDRESS	NO_DATA
+
+/*@}*/
+
+/** Returns host entry by the host address.
+ *  @param[in] address The host address.
+ *  @param[in] len The address length.
+ *  @param[in] type The address type.
+ *  @returns Host entry information.
+ */
+//struct hostent *	gethostbyaddr(const void * address, int len, int type);
+
+/** Returns host entry by the host name.
+ *  @param[in] name The host name.
+ *  @returns Host entry information.
+ */
+//struct hostent *	gethostbyname(const char * name);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/packet/include/tcp_codes.h
===================================================================
--- uspace/lib/packet/include/tcp_codes.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
+++ uspace/lib/packet/include/tcp_codes.h	(revision f2d2c6049aba8476777148ca9a6e2daaf5b9a771)
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2009 Lukas Mejdrech
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup tcp
+ *  @{
+ */
+
+/** @file
+ *  TCP options definitions.
+ */
+
+#ifndef __NET_TCP_CODES_H__
+#define __NET_TCP_CODES_H__
+
+/** End of list TCP option.
+ */
+#define TCPOPT_END_OF_LIST				0x0
+
+/** No operation TCP option.
+ */
+#define TCPOPT_NO_OPERATION				0x1
+
+/** Maximum segment size TCP option.
+ */
+#define TCPOPT_MAX_SEGMENT_SIZE			0x2
+
+/** Maximum segment size TCP option length.
+ */
+#define TCPOPT_MAX_SEGMENT_SIZE_LENGTH	4
+
+/** Window scale TCP option.
+ */
+#define TCPOPT_WINDOW_SCALE				0x3
+
+/** Window scale TCP option length.
+ */
+#define TCPOPT_WINDOW_SCALE_LENGTH		3
+
+/** Selective acknowledgement permitted TCP option.
+ */
+#define TCPOPT_SACK_PERMITTED			0x4
+
+/** Selective acknowledgement permitted TCP option length.
+ */
+#define TCPOPT_SACK_PERMITTED_LENGTH	2
+
+/** Selective acknowledgement TCP option.
+ *  Has variable length.
+ */
+#define TCPOPT_SACK						0x5
+
+/** Timestamp TCP option.
+ */
+#define TCPOPT_TIMESTAMP				0x8
+
+/** Timestamp TCP option length.
+ */
+#define TCPOPT_TIMESTAMP_LENGTH			10
+
+#endif
+
+/** @}
+ */
