2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #ifndef __VMM_SOCKET_H__
22 #define __VMM_SOCKET_H__
24 #include <palacios/vmm.h>
26 #define V3_SOCK void *
30 #define V3_Create_UDP_Socket() ({ \
31 extern struct v3_socket_hooks * sock_hooks; \
33 if ((sock_hooks) && (sock_hooks)->udp_socket) { \
34 sock = (sock_hooks)->udp_socket(0,0); \
41 #define V3_Create_TCP_Socket() ({ \
42 extern struct v3_socket_hooks * sock_hooks; \
44 if ((sock_hooks) && (sock_hooks)->tcp_socket) { \
45 sock = (sock_hooks)->tcp_socket(0,0,0); \
51 #define V3_Close_Socket(sock) \
53 extern struct v3_socket_hooks * sock_hooks; \
54 if ((sock_hooks) && (sock_hooks)->close) { \
55 (sock_hooks)->close(sock); \
61 #define V3_Bind_Socket(sock, port) ({ \
62 extern struct v3_socket_hooks * sock_hooks; \
64 if ((sock_hooks) && (sock_hooks)->bind) { \
65 ret = (sock_hooks)->bind(sock, port); \
71 #define V3_Accept_Socket(sock, ip_ptr) ({ \
72 extern struct v3_socket_hooks * sock_hooks; \
74 if ((sock_hooks) && (sock_hooks)->accept) { \
75 ret = (sock_hooks)->accept(sock, ip_ptr); \
81 #define V3_Select_Socket(rset,wset,eset,tv) ({ \
82 extern struct v3_socket_hooks * sock_hooks; \
84 if ((sock_hooks) && (sock_hooks)->select) { \
85 ret = (sock_hooks)->select(rset, wset, eset, tv); \
92 #define V3_Connect_To_IP(sock, ip, port) ({ \
93 extern struct v3_socket_hooks * sock_hooks; \
95 if ((sock_hooks) && (sock_hooks)->connect_to_ip) { \
96 ret = (sock_hooks)->connect_to_ip(sock, ip, port); \
102 #define V3_Connect_To_Host(sock, hostname, port) ({ \
103 extern struct v3_socket_hooks * sock_hooks; \
105 if ((sock_hooks) && (sock_hooks)->connect_to_host) { \
106 ret = (sock_hooks)->connect_to_host(sock, hostname, port); \
112 #define V3_Send(sock, buf, len) ({ \
113 extern struct v3_socket_hooks * sock_hooks; \
115 if ((sock_hooks) && (sock_hooks)->send) { \
116 ret = (sock_hooks)->send(sock, buf, len); \
121 #define V3_Recv(sock, buf, len) ({ \
122 extern struct v3_socket_hooks * sock_hooks; \
124 if ((sock_hooks) && (sock_hooks)->recv) { \
125 ret = (sock_hooks)->recv(sock, buf, len); \
130 #define V3_SendTo_Host(sock, hostname, port, buf, len) ({ \
131 extern struct v3_socket_hooks * sock_hooks; \
133 if ((sock_hooks) && (sock_hooks)->sendto_host) { \
134 ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
140 #define V3_SendTo_IP(sock, ip, port, buf, len) ({ \
141 extern struct v3_socket_hooks * sock_hooks; \
143 if ((sock_hooks) && (sock_hooks)->sendto_ip) { \
144 ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len); \
150 #define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({ \
151 extern struct v3_socket_hooks * sock_hooks; \
153 if ((sock_hooks) && (sock_hooks)->recvfrom_host) { \
154 ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
160 #define V3_RecvFrom_IP(sock, ip, port, buf, len) ({ \
161 extern struct v3_socket_hooks * sock_hooks; \
163 if ((sock_hooks) && (sock_hooks)->recvfrom_ip) { \
164 ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len); \
174 long tv_sec; /* seconds */
175 long tv_usec; /* and microseconds */
178 struct v3_sock_entry {
181 struct v3_sock_entry * next;
185 unsigned int num_socks;
186 struct v3_sock_entry * socks;
190 void v3_init_sock_set(struct v3_sock_set * sock_set);
192 void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // adds socket to the sockset
193 void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // deletes socket from sockset
194 int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // checks is_set vairable
195 void v3_zero_sockset(struct v3_sock_set * sock_set); // clears all is_set variables.
199 #define v3_foreach_sock(/* (struct v3_sock_set *) */ sock_set, /* (struct v3_sock_entry *) */ iter) \
200 for (iter = sock_set->socks; iter != NULL; iter = iter->next)
204 struct v3_socket_hooks {
205 // Socket creation routines
206 xsxsxsxsxs V3_SOCK (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
207 V3_SOCK (*udp_socket)(const int bufsize, const int nonblocking);
209 // Socket Destruction
210 void (*close)(V3_SOCK sock);
212 // Network Server Calls
213 int (*bind_socket)(const V3_SOCK sock, const int port);
215 int (*listen)(const V3_SOCK sock, int backlog);
217 V3_SOCK (*accept)(const V3_SOCK const sock, unsigned int * remote_ip, unsigned int * port);
218 // This going to suck
219 int (*select)(struct v3_sock_set * rset, \
220 struct v3_sock_set * wset, \
221 struct v3_sock_set * eset, \
222 struct v3_timeval tv);
225 int (*connect_to_ip)(const V3_SOCK sock, const int hostip, const int port);
226 int (*connect_to_host)(const V3_SOCK sock, const char * hostname, const int port);
229 int (*send)(const V3_SOCK sock, const char * buf, const int len);
230 int (*recv)(const V3_SOCK sock, char * buf, const int len);
233 int (*sendto_host)(const V3_SOCK sock, const char * hostname, const int port,
234 const char * buf, const int len);
235 int (*sendto_ip)(const V3_SOCK sock, const int ip_addr, const int port,
236 const char * buf, const int len);
238 int (*recvfrom_host)(const V3_SOCK sock, const char * hostname, const int port,
239 char * buf, const int len);
240 int (*recvfrom_ip)(const V3_SOCK sock, const int ip_addr, const int port,
241 char * buf, const int len);
245 extern void V3_Init_Sockets(struct v3_socket_hooks * hooks);