1 #include <geekos/socket.h>
2 #include <geekos/malloc.h>
3 #include <geekos/ne2k.h>
5 #include <uip/uip_arp.h>
6 #include <geekos/vmm_stubs.h>
7 #include <geekos/debug.h>
11 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
13 #define MAX_SOCKS 1024
16 struct socket sockets[MAX_SOCKS];
20 int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt);
25 for (i = 0; i < MAX_SOCKS; i++) {
26 sockets[i].in_use = 0;
27 sockets[i].send_buf = NULL;
28 sockets[i].recv_buf = NULL;
29 sockets[i].state = CLOSED;
38 Init_Ne2k(&Packet_Received);
47 void set_ip_addr(uchar_t addr[4]) {
49 uip_ipaddr(ipaddr, addr[0], addr[1], addr[2], addr[3]); /* Local IP address */
50 uip_sethostaddr(ipaddr);
54 static int allocate_socket_fd() {
57 for (i = 0; i < MAX_SOCKS; i++) {
58 if (sockets[i].in_use == 0) {
59 sockets[i].in_use = 1;
60 sockets[i].send_buf = create_ring_buffer(BUF_SIZE);
61 if (sockets[i].send_buf == NULL)
63 sockets[i].recv_buf = create_ring_buffer(BUF_SIZE);
64 if (sockets[i].recv_buf == NULL){
65 free_ring_buffer(sockets[i].send_buf);
75 static int release_socket_fd(int sockfd){
76 if (sockfd >= 0 && sockfd < MAX_SOCKS){
77 sockets[sockfd].in_use = 0;
78 free_ring_buffer(sockets[sockfd].send_buf);
79 free_ring_buffer(sockets[sockfd].recv_buf);
80 sockets[sockfd].send_buf = NULL;
81 sockets[sockfd].recv_buf = NULL;
82 sockets[sockfd].state = CLOSED;
89 struct socket * get_socket_from_fd(int fd) {
90 return &(sockets[fd]);
94 int connect(const uchar_t ip_addr[4], ushort_t port) {
96 sockfd = allocate_socket_fd();
103 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
105 sockets[sockfd].state = WAITING;
107 sockets[sockfd].con = uip_connect((uip_ipaddr_t *)&ip_addr, htons(port));
110 if (sockets[sockfd].con == NULL){
111 release_socket_fd(sockfd);
116 PrintBoth("Connection start\n");
118 while(sockets[sockfd].state == WAITING) {
119 timer_int_handler(NULL);
123 PrintBoth("Connected\n");
125 if (sockets[sockfd].state != ESTABLISHED) {
126 release_socket_fd(sockfd);
134 int recv(int sockfd, void * buf, uint_t len){
137 struct socket *sock = get_socket_from_fd(sockfd);
140 // here we need some lock mechnism, just disable interrupt may not work properly because recv() will be run as a kernel thread
142 recvlen = rb_read(sock->recv_buf, buf, len);
145 Wait(&(sock->recv_wait_queue));
152 void timer_int_Handler(struct Interrupt_State * state){
154 //handle the periodic calls of uIP
155 for(i = 0; i < UIP_CONNS; ++i) {
158 //devicedriver_send();
159 NE2K_Transmit(uip_len);
162 for(i = 0; i < UIP_UDP_CONNS; i++) {
165 //devicedriver_send();
166 NE2K_Transmit(uip_len);
171 // a series of utilities to handle conncetion states
172 static void connected(int sockfd) {
173 struct socket * sock = get_socket_from_fd(sockfd);
175 PrintBoth("Connected Interrupt\n");
177 sock->state = ESTABLISHED;
179 Wake_Up(&(sock->recv_wait_queue));
182 static void closed(int sockfd){
186 static void acked(int sockfd){
190 static void newdata(int sockfd){
197 dataptr = (char *)uip_appdata;
202 sock = get_socket_from_fd(sockfd);
204 wrlen = rb_write(sock->recv_buf, dataptr, len);
206 if (wrlen < len){ //write error, what should I do?
210 Wake_Up(&(sock->recv_wait_queue));
217 static void send_to_driver(int sockfd) {
219 PrintBoth("Sending data to driver\n");
225 int send(int sockfd, void * buf, uint_t len) {
226 struct socket * sock = get_socket_from_fd(sockfd);
227 //int mss = uip_mss();
228 //int pending_bytes = rb_data_len(sock->send_buf);
229 //int len = (mss < pending_bytes) ? mss: pending_bytes;
231 uchar_t * send_buf = uip_appdata;
233 bytes_read = rb_peek(sock->send_buf, send_buf, len);
235 if (bytes_read == 0) {
236 // no packet for send
240 uip_send(send_buf, len);
247 //get the socket id by the local tcp port
248 static int get_socket_from_port(ushort_t lport) {
252 for (i = 0; i < MAX_SOCKS; i++){
253 if (sockets[i].con->lport == lport) {
263 void socket_appcall(void) {
267 sockfd = get_socket_from_port(uip_conn->lport);
269 PrintBoth("Appcall\n");
276 if (uip_connected()) {
281 if (uip_closed() ||uip_aborted() ||uip_timedout()) {
299 send_to_driver(sockfd);
306 int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
309 uip_len = info->size;
311 // for (i = 0; i < info->size; i++) {
312 // uip_buf[i] = *(pkt + i);
315 memcpy(uip_buf, pkt, uip_len);
320 if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
324 NE2K_Transmit(uip_len);
334 NE2K_Transmit(uip_len);