1 #include <geekos/socket.h>
2 #include <geekos/malloc.h>
3 #include <palacios/vmm_types.h>
4 #include <geekos/ne2k.h>
6 #include <uip/uip_arp.h>
7 #include <geekos/int.h>
8 #include <geekos/vmm_stubs.h>
9 #include <geekos/queue.h>
11 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
13 #define MAX_SOCKS 1024
15 struct socket sockets[MAX_SOCKS];
18 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) ;
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));
33 // set up interrupt handler
34 // set up device driver
36 Init_Ne2k(&Packet_Received);
41 static int allocate_socket_fd() {
44 for (i = 0; i < MAX_SOCKS; i++) {
45 if (sockets[i].in_use == 0) {
46 sockets[i].in_use = 1;
54 static int release_socket_fd(int sockfd){
55 if (sockfd >= 0 && sockfd < MAX_SOCKS)
56 sockets[sockfd].in_use = 0;
62 struct socket * get_socket_from_fd(int fd) {
63 return &(sockets[fd]);
67 int connect(const uchar_t ip_addr[4], ushort_t port) {
69 sockfd = allocate_socket_fd();
76 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
78 sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
80 if (sockets[sockfd].con == NULL){
81 release_socket_fd(sockfd);
89 void timer_int_Handler(struct Interrupt_State * state){
91 //handle the periodic calls of uIP
92 for(i = 0; i < UIP_CONNS; ++i) {
95 //devicedriver_send();
96 NE2K_Transmit(uip_len);
99 for(i = 0; i < UIP_UDP_CONNS; i++) {
102 //devicedriver_send();
103 NE2K_Transmit(uip_len);
108 // a series of utilities to handle conncetion states
109 static void connected(int sockfd){
113 static void closed(int sockfd){
117 static void acked(int sockfd){
121 static void newdata(int sockfd){
127 senddata(int sockfd){
132 struct sockets *sock = get_socket_from_fd(sockfd);
134 pkt = dequeue(sock->send_queue);
135 if (pkt == 0) // no packet for send
138 bufptr = uip_appdata;
140 if(len < uip_mss()) {
141 // memcpy(bufptr, data, len);
145 //uip_send(uip_appdata,len);
150 //get the socket id by the local tcp port
151 static int get_socket_from_port(ushort_t lport) {
154 for (i = 0; i<MAX_SOCKS; i++){
155 if (sockets[i].con->lport == lport)
170 sockfd = get_socket_from_port(uip_conn->lport);
172 if (sockfd == -1) return;
174 if(uip_connected()) {
178 if(uip_closed() ||uip_aborted() ||uip_timedout()) {
202 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt)
206 uip_len = info->size;
207 for(i = 0; i < info->size; i++) {
208 uip_buf[i] = *(pkt+i);
211 if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
214 //ethernet_devicedriver_send();
215 NE2K_Transmit(uip_len);
222 //ethernet_devicedriver_send();
223 NE2K_Transmit(uip_len);