1 #include <geekos/socket.h>
2 #include <geekos/malloc.h>
3 #include <geekos/ne2k.h>
5 #include <uip/uip_arp.h>
6 #include <geekos/int.h>
7 #include <geekos/vmm_stubs.h>
8 #include <geekos/queue.h>
11 // for some reason, there are compile warnings without these
13 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
15 #define MAX_SOCKS 1024
17 struct socket sockets[MAX_SOCKS];
19 struct gen_queue in_packets;
26 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt) ;
31 init_queue(&in_packets);
33 for (i = 0; i < MAX_SOCKS; i++) {
34 sockets[i].in_use = 0;
35 init_queue(&(sockets[i].send_queue));
36 init_queue(&(sockets[i].recv_queue));
43 // set up interrupt handler
44 // set up device driver
46 Init_Ne2k(&Packet_Received);
51 static int allocate_socket_fd() {
54 for (i = 0; i < MAX_SOCKS; i++) {
55 if (sockets[i].in_use == 0) {
56 sockets[i].in_use = 1;
64 static int release_socket_fd(int sockfd){
65 sockets[sockfd].in_use = 0;
70 struct socket * get_socket_from_fd(int fd) {
71 return &(sockets[fd]);
77 int connect(const uchar_t ip_addr[4], ushort_t port) {
79 sockfd = allocate_socket_fd();
86 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
88 sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
90 if (sockets[sockfd].con == NULL){
91 release_socket_fd(sockfd);
99 void timer_int_Handler(struct Interrupt_State * state){
101 //handle the periodic calls of uIP
102 for(i = 0; i < UIP_CONNS; ++i) {
105 //devicedriver_send();
106 NE2K_Transmit(uip_len);
109 for(i = 0; i < UIP_UDP_CONNS; i++) {
112 //devicedriver_send();
113 NE2K_Transmit(uip_len);
118 // a series of utilities to handle conncetion states
119 static void connected(int sockfd){
123 static void closed(int sockfd){
127 static void acked(int sockfd){
131 static void newdata(int sockfd){
136 static void senddata(int sockfd){
140 bufptr = uip_appdata;
142 if(len < uip_mss()) {
143 // memcpy(bufptr, data, len);
147 //uip_send(uip_appdata,len);
152 //get the socket id by the local tcp port
153 static int get_socket_from_port(ushort_t lport) {
156 for (i = 0; i<MAX_SOCKS; i++){
157 if (sockets[i].con->lport == lport) {
168 void socket_appcall(void) {
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) {
204 struct sock_packet next;
207 next.size = info->size;
208 next.data = (uchar_t *)Malloc(next.size);
210 if (next.data == NULL) {
214 //uip_len = info->size;
216 for(i = 0; i < info->size; i++) {
217 *((next.data) + i) = *(pkt + i);
221 Disable_Interrupts();
222 enqueue(&in_packets, &next);
225 //triger_receiver_interrupt();
231 void int_handler_packet_receive(struct Interrupt_State * state){
232 //device driver got a incoming packet and enqueue that packet to the receive queue
233 struct sock_packet * next_packet;
238 //currently disable interrupt because no lock for the queue
239 Disable_Interrupts();
240 pkt = dequeue(&in_packets);
247 //there are new packets in the receiver queue
248 next_packet = (struct sock_packet *)pkt;
249 uip_len = next_packet->size;
251 for(i = 0; i < uip_len; i++) {
252 uip_buf[i] = *((next_packet->data) + i);
255 Free(next_packet->data);
258 if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
261 //ethernet_devicedriver_send();
262 NE2K_Transmit(uip_len);
269 //ethernet_devicedriver_send();
270 NE2K_Transmit(uip_len);