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;
63 struct socket * get_socket_from_fd(int fd) {
64 return &(sockets[fd]);
68 int connect(const uchar_t ip_addr[4], ushort_t port) {
70 sockfd = allocate_socket_fd();
77 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
79 sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
81 if (sockets[sockfd].con == NULL){
82 release_socket_fd(sockfd);
90 void timer_int_Handler(struct Interrupt_State * state){
92 //handle the periodic calls of uIP
93 for(i = 0; i < UIP_CONNS; ++i) {
96 //devicedriver_send();
97 NE2K_Transmit(uip_len);
100 for(i = 0; i < UIP_UDP_CONNS; i++) {
103 //devicedriver_send();
104 NE2K_Transmit(uip_len);
109 // a series of utilities to handle conncetion states
110 static void connected(int sockfd){
114 static void closed(int sockfd){
118 static void acked(int sockfd){
122 static void newdata(int sockfd){
128 senddata(int sockfd){
133 struct sockets *sock = get_socket_from_fd(sockfd);
135 pkt = dequeue(sock->send_queue);
136 if (pkt == 0) // no packet for send
139 bufptr = uip_appdata;
141 if(len < uip_mss()) {
142 // memcpy(bufptr, data, len);
146 //uip_send(uip_appdata,len);
151 //get the socket id by the local tcp port
152 static int get_socket_from_port(ushort_t lport) {
155 for (i = 0; i<MAX_SOCKS; i++){
156 if (sockets[i].con->lport == lport)
171 sockfd = get_socket_from_port(uip_conn->lport);
173 if (sockfd == -1) return;
175 if(uip_connected()) {
179 if(uip_closed() ||uip_aborted() ||uip_timedout()) {
203 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);