Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


----------------------------------------------------------------------
[palacios.git] / palacios / src / geekos / socket.c
1 #include <geekos/socket.h>
2 #include <geekos/malloc.h>
3 #include <palacios/vmm_types.h>
4 #include <geekos/ne2k.h>
5 #include <uip/uip.h>
6 #include <uip/uip_arp.h>
7 #include <geekos/int.h>
8 #include <geekos/vmm_stubs.h>
9 #include <geekos/queue.h>
10
11 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
12
13 #define MAX_SOCKS 1024
14
15 struct socket sockets[MAX_SOCKS];
16
17
18 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) ;
19
20 void init_network() {
21    int i = 0;
22    
23    for (i = 0; i < MAX_SOCKS; i++) {
24       sockets[i].in_use = 0;
25       init_queue(&(sockets[i].send_queue));
26       init_queue(&(sockets[i].recv_queue));
27    }
28
29   //initiate uIP
30     uip_init();
31     uip_arp_init();
32
33   // set up interrupt handler
34   // set up device driver
35
36     Init_Ne2k(&Packet_Received);
37
38
39 }
40
41 static int allocate_socket_fd() {
42   int i = 0;
43   
44   for (i = 0; i < MAX_SOCKS; i++) {
45     if (sockets[i].in_use == 0) {
46       sockets[i].in_use = 1;
47       return i;
48     }
49   }
50
51   return -1;
52 }
53
54 static int release_socket_fd(int sockfd){
55        if (sockfd >= 0 && sockfd < MAX_SOCKS)
56                 sockets[sockfd].in_use = 0;
57
58         return 0;
59 }
60
61
62 struct socket * get_socket_from_fd(int fd) {
63   return &(sockets[fd]);
64 }
65
66
67 int connect(const uchar_t ip_addr[4], ushort_t port) {
68   int sockfd = -1;
69   sockfd = allocate_socket_fd();
70   uip_ipaddr_t ipaddr;
71   
72   if (sockfd == -1) {
73     return -1;
74   }
75
76   uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);  
77   
78   sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
79
80   if (sockets[sockfd].con == NULL){
81         release_socket_fd(sockfd);
82         return -1;
83   }
84
85   return sockfd;
86 }
87
88
89 void timer_int_Handler(struct Interrupt_State * state){
90         int i;
91         //handle the periodic calls of uIP
92         for(i = 0; i < UIP_CONNS; ++i) {
93                uip_periodic(i);
94                 if(uip_len > 0) {
95                      //devicedriver_send();
96                      NE2K_Transmit(uip_len);
97                 }
98         }
99         for(i = 0; i < UIP_UDP_CONNS; i++) {
100                  uip_udp_periodic(i);
101                  if(uip_len > 0) {
102                      //devicedriver_send();
103                      NE2K_Transmit(uip_len);
104                 }
105         }
106 }
107
108 // a series of utilities to handle conncetion states
109 static void connected(int sockfd){
110
111 }
112
113 static void closed(int sockfd){
114
115 }
116
117 static void acked(int sockfd){
118
119 }
120
121 static void newdata(int sockfd){
122
123 }
124
125 // not finished yet
126 static void
127 senddata(int sockfd){
128     /*uchar_t *bufptr;
129     int len = 0;
130     addr_t pkt;
131
132     struct sockets *sock = get_socket_from_fd(sockfd);
133
134     pkt = dequeue(sock->send_queue);
135     if (pkt == 0)  // no packet for send
136                 return;
137
138     bufptr = uip_appdata;
139   
140     if(len < uip_mss()) {
141       // memcpy(bufptr, data, len);
142     } else {
143
144     }*/
145   //uip_send(uip_appdata,len);
146 }
147
148
149
150 //get the socket id by the local tcp port
151 static int  get_socket_from_port(ushort_t lport) {
152   int i;
153   
154   for (i = 0; i<MAX_SOCKS; i++){
155         if (sockets[i].con->lport == lport) 
156                 return i;
157   }
158   
159   return -1;
160 }
161
162
163
164
165 void
166 socket_appcall(void)
167 {
168   int sockfd; 
169
170   sockfd = get_socket_from_port(uip_conn->lport);
171
172   if (sockfd == -1) return;
173     
174   if(uip_connected()) {
175         connected(sockfd);
176   }
177   
178   if(uip_closed() ||uip_aborted() ||uip_timedout()) {
179      closed(sockfd);
180      return;
181   }
182   
183   if(uip_acked()) {
184      acked(sockfd);
185   }
186   
187   if(uip_newdata()) {
188      newdata(sockfd);
189   }
190   
191   if(uip_rexmit() ||
192      uip_newdata() ||
193      uip_acked() ||
194      uip_connected() ||
195      uip_poll()) {
196      senddata(sockfd);
197   }
198 }
199
200
201
202 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) 
203 {
204           int i;
205
206           uip_len = info->size; 
207           for(i = 0; i < info->size; i++) {
208             uip_buf[i] = *(pkt+i);
209           }
210           Free(pkt);
211           if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
212                 uip_arp_arpin();
213                 if (uip_len > 0){
214                         //ethernet_devicedriver_send();
215                         NE2K_Transmit(uip_len);
216                 }                                       
217            } else {
218                 uip_arp_ipin();
219                 uip_input();
220                 if(uip_len > 0) {
221                        uip_arp_out();
222                      //ethernet_devicedriver_send();
223                         NE2K_Transmit(uip_len);
224                }
225         }                         
226           return 0;
227 }