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_ring_buffer(&(sockets[i].send_queue), 65535);
26 init_ring_buffer(&(sockets[i].recv_queue), 65535);
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) {
72 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
94 for(i = 0; i < UIP_CONNS; ++i) {
98 NE2K_Transmit(uip_len);
103 for(i = 0; i < UIP_UDP_CONNS; i++) {
107 NE2K_Transmit(uip_len);
112 // a series of utilities to handle conncetion states
113 static void connected(int sockfd){
117 static void closed(int sockfd){
121 static void acked(int sockfd){
125 static void newdata(int sockfd){
130 static void senddata(int sockfd) {
131 struct socket * sock = get_socket_from_fd(sockfd);
133 int pending_bytes = rb_data_len(&(sock->send_queue));
134 int len = (mss < pending_bytes) ? mss: pending_bytes;
136 uchar_t * send_buf = uip_appdata;
138 bytes_read = rb_peek(&(sock->send_queue), send_buf, len);
140 if (bytes_read == 0) {
141 // no packet for send
145 uip_send(send_buf, 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);
176 if(uip_connected()) {
180 if(uip_closed() ||uip_aborted() ||uip_timedout()) {
204 int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
207 uip_len = info->size;
209 for (i = 0; i < info->size; i++) {
210 uip_buf[i] = *(pkt+i);
215 if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
219 NE2K_Transmit(uip_len);
229 NE2K_Transmit(uip_len);