43 #include <linux/kingcobra.h>
46 kingcobra_kernelspace_init(
void)
48 printk(KERN_INFO
"kingcobra_kernelspace_init(): initializing KingCobra kernel module\n");
54 if(kingcobra_disk_persistence==1)
56 printk(KERN_INFO
"kingcobra_kernelspace_init(): Initializing disk persistence file for KingCobra\n");
57 request_reply_queue=filp_open(
"/var/log/kingcobra/REQUEST_REPLY.queue", O_WRONLY|O_APPEND, 0777);
61 EXPORT_SYMBOL(kingcobra_kernelspace_init);
65 kingcobra_kernelspace_exit(
void)
67 printk(KERN_INFO
"kingcobra_kernelspace_exit(): exiting KingCobra kernel module \n");
68 filp_close(request_reply_queue,NULL);
72 EXPORT_SYMBOL(kingcobra_kernelspace_exit);
75 void kingcobra_servicerequest_kernelspace(
void* args)
77 printk(KERN_INFO
"kingcobra_servicerequest_kernelspace(): KingCobra service request received from kernel KingCobra workqueue: %s\n",(
char*)args);
78 if(kingcobra_disk_persistence==1)
82 sprintf(buf,
"%s $$\n",(
char*)args);
83 printk(KERN_INFO
"kingcobra_servicerequest_kernelspace(): disk persistence enabled, writing incoming request to KingCobra disk file:%s\n",buf);
84 vfs_write(request_reply_queue, buf, 256, &request_reply_queue_pos);
85 request_reply_queue_pos+=256;
87 long client_ip_l=parse_ip_address((
char*)args);
88 char* logicaltimestamp=parse_timestamp((
char*)args);
89 char* response=kmalloc(KCOBRA_BUF_SIZE,GFP_ATOMIC);
90 sprintf(response,
"REPLY#%x#%s :--- from kingcobra_servicerequest_kernelspace() to client",client_ip_l,logicaltimestamp);
91 reply_to_publisher(client_ip_l,response);
93 EXPORT_SYMBOL(kingcobra_servicerequest_kernelspace);
95 char* parse_timestamp(
char* request)
99 char* request_dup=kstrdup(request,GFP_ATOMIC);
100 strsep(&request_dup,delim);
101 strsep(&request_dup,delim);
102 timestamp=kstrdup(strsep(&request_dup,delim),GFP_ATOMIC);
103 printk(KERN_INFO
"parse_timestamp(): timestamp parsed from request header = %s\n",timestamp);
107 long parse_ip_address(
char* request)
111 char* request_dup=kstrdup(request,GFP_ATOMIC);
112 printk(KERN_INFO
"parse_ip_address(): request_dup = %s\n",request_dup);
113 char* request_header=strsep(&request_dup,delim);
114 printk(KERN_INFO
"parse_ip_address(): request_header= %s\n", request_header);
115 token=strsep(&request_dup,delim);
116 printk(KERN_INFO
"parse_ip_address(): token = %s\n",token);
118 kstrtoll(token,16,&ll2);
119 printk(KERN_INFO
"parse_ip_address(): ll2=%d\n",ll2);
123 void reply_to_publisher(
long client_ip_l,
char *response)
131 struct sockaddr_in sin;
135 char buf[KCOBRA_BUF_SIZE];
144 sin.sin_family=AF_INET;
146 sin.sin_addr.s_addr=client_ip_l;
147 sin.sin_port=htons(10000);
150 iov.iov_len=
sizeof(buf);
151 msg.msg_name = (
struct sockaddr *) &sin;
152 msg.msg_namelen =
sizeof(
struct sockaddr);
153 msg.msg_iov = (
struct iovec *) &iov;
155 msg.msg_control = NULL;
156 msg.msg_controllen = 0;
161 strcpy(iov.iov_base, response);
162 error = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
163 printk(KERN_INFO
"reply_to_publisher() : created client kernel socket\n");
164 kernel_connect(sock, (
struct sockaddr*)&sin,
sizeof(sin) , 0);
165 printk(KERN_INFO
"reply_to_publisher() : connected kernel client to virgo cloudexec kernel service\n ");
166 kernel_sendmsg(sock, &msg, &iov, nr, KCOBRA_BUF_SIZE);
167 printk(KERN_INFO
"reply_to_publisher() : sent message: %s \n", buf);
168 len = kernel_recvmsg(sock, &msg, &iov, nr, KCOBRA_BUF_SIZE, msg.msg_flags);
169 printk(KERN_INFO
"reply_to_publisher() : received message: %s \n", buf);
171 printk(KERN_INFO
"reply_to_publisher() : le32_to_cpus(buf): %s \n", buf);
173 printk(KERN_INFO
"reply_to_publisher() : reply_to_publisher() client socket_release() invoked\n");
179 MODULE_LICENSE(
"GPL");
180 module_init(kingcobra_kernelspace_init);
181 module_exit(kingcobra_kernelspace_exit);