Ok, I'll give you the necessary patches, that should make it clearer :
--- /usr/src/linux/include/linux/net.h 2006-09-20 05:42:06.000000000 +0200
+++ net.h 2007-03-01 11:15:56.000000000 +0100
@@ -306,3 +309,8 @@ extern int net_msg_burst;
#endif /* __KERNEL__ */
#endif /* _LINUX_NET_H */
+
+extern int (*autodiscovery_connection_callback)(struct socket *sock,
+ struct sockaddr *uaddr, int addr_len, int flags);
--- /usr/src/linux/net/ipv4/af_inet.c 2006-09-20 05:42:06.000000000 +0200
+++ af_inet.c 2007-03-01 11:15:07.000000000 +0100
@@ -116,6 +116,11 @@
#include <linux/mroute.h>
#endif
+int (*autodiscovery_connection_callback)(struct socket *sock,
+ struct sockaddr *uaddr, int addr_len, int flags) = NULL;+
DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
extern void ip_mc_drop_socket(struct sock *sk);
@@ -522,6 +527,14 @@ int inet_stream_connect(struct socket *s
int err;
long timeo;
+ if (autodiscovery_connection_callback != NULL) {
+ err = (*autodiscovery_connection_callback)(sock, uaddr, addr_len, flags);
+ if (err)
+ goto out;
+ }
+
lock_sock(sk);
if (uaddr->sa_family == AF_UNSPEC) {
--- /usr/src/linux/net/socket.c 2006-09-20 05:42:06.000000000 +0200
+++ socket.c 2007-03-01 11:15:07.000000000 +0100
@@ -2177,3 +2177,6 @@ EXPORT_SYMBOL(sock_wake_async);
EXPORT_SYMBOL(sockfd_lookup);
EXPORT_SYMBOL(kernel_sendmsg);
EXPORT_SYMBOL(kernel_recvmsg);
+EXPORT_SYMBOL(autodiscovery_connection_callback);
Now, the main part of my module consist of this file:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/socket.h>
#include <linux/config.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("acp");
MODULE_DESCRIPTION("Autodiscovery module");
extern void *autodiscovery_connection_callback;
static int _connection_callback(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags) {
/* This is the function that will be called upon each attempt of connection using an AF_INET socket (IPv4 only) */
}
static int autodiscovery_init(void) {
printk(KERN_ALERT "Hello, this is autodiscovery module speaking.\n");
autodiscovery_connection_callback = _connection_callback;
return 0;
}
static void autodiscovery_exit(void) {
autodiscovery_connection_callback = NULL;
printk(KERN_ALERT "Goodbye from autodiscovery module.\n");
}
module_init(autodiscovery_init);
module_exit(autodiscovery_exit);
You should get the point by now I think. All I do in the kernel is to define a pointer to a function that I initialize to NULL. When my module loads it sets this pointer to one of its functions. Then whenever I want this function called, I just insert a block
if (pointer != NULL)
(*pointer)(parameters)
BTW, the kernel used is 2.6.18.8 from
www.kernel.org
Hope that helped,
acp