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>
8 #include <geekos/timer.h>
11 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
13 #define MAX_SOCKS 1024
16 struct socket sockets[MAX_SOCKS];
18 void socket_appcall(void);
20 #define UIP_APPCALL socket_appcall
21 #endif /* UIP_APPCALL */
26 static int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt);
27 static void periodic_caller(int timer_id);
34 for (i = 0; i < MAX_SOCKS; i++) {
35 sockets[i].in_use = 0;
36 sockets[i].send_buf = NULL;
37 sockets[i].recv_buf = NULL;
38 sockets[i].state = CLOSED;
48 Init_Ne2k(&Packet_Received);
50 iflag = Begin_Int_Atomic();
51 Start_Timer(2, periodic_caller);
52 End_Int_Atomic(iflag);
59 void set_ip_addr(uchar_t addr[4]) {
61 uip_ipaddr(ipaddr, addr[0], addr[1], addr[2], addr[3]); /* Local IP address */
62 uip_sethostaddr(ipaddr);
66 static int allocate_socket_fd() {
69 for (i = 0; i < MAX_SOCKS; i++) {
70 if (sockets[i].in_use == 0) {
71 sockets[i].in_use = 1;
72 sockets[i].send_buf = create_ring_buffer(BUF_SIZE);
73 if (sockets[i].send_buf == NULL)
75 sockets[i].recv_buf = create_ring_buffer(BUF_SIZE);
76 if (sockets[i].recv_buf == NULL){
77 free_ring_buffer(sockets[i].send_buf);
87 static int release_socket_fd(int sockfd){
88 if (sockfd >= 0 && sockfd < MAX_SOCKS){
89 sockets[sockfd].in_use = 0;
90 free_ring_buffer(sockets[sockfd].send_buf);
91 free_ring_buffer(sockets[sockfd].recv_buf);
92 sockets[sockfd].send_buf = NULL;
93 sockets[sockfd].recv_buf = NULL;
94 sockets[sockfd].state = CLOSED;
101 struct socket * get_socket_from_fd(int fd) {
102 return &(sockets[fd]);
107 static void periodic_caller(int timer_id) {
109 //handle the periodic calls of uIP
111 //PrintBoth("Timer CALLBACK handler\n");
113 for(i = 0; i < UIP_CONNS; ++i) {
116 //devicedriver_send();
117 PrintBoth("Sending Packet\n");
118 NE2K_Transmit(uip_len);
121 for(i = 0; i < UIP_UDP_CONNS; i++) {
124 //devicedriver_send();
125 NE2K_Transmit(uip_len);
131 int connect(const uchar_t ip_addr[4], ushort_t port) {
133 sockfd = allocate_socket_fd();
134 static uip_ipaddr_t ipaddr;
140 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
142 sockets[sockfd].con = uip_connect(&ipaddr, htons(port));
145 if (sockets[sockfd].con == NULL){
146 release_socket_fd(sockfd);
151 PrintBoth("Connection start\n");
152 Wait(&(sock->recv_wait_queue));
155 PrintBoth("Connected\n");
163 int recv(int sockfd, void * buf, uint_t len){
166 struct socket *sock = get_socket_from_fd(sockfd);
169 // here we need some lock mechnism, just disable interrupt may not work properly because recv() will be run as a kernel thread
171 recvlen = rb_read(sock->recv_buf, buf, len);
174 Wait(&(sock->recv_wait_queue));
183 // a series of utilities to handle conncetion states
184 static void connected(int sockfd) {
185 struct socket * sock = get_socket_from_fd(sockfd);
187 PrintBoth("Connected Interrupt\n");
189 sock->state = ESTABLISHED;
191 Wake_Up(&(sock->recv_wait_queue));
194 static void closed(int sockfd){
198 static void acked(int sockfd){
202 static void newdata(int sockfd){
209 dataptr = (char *)uip_appdata;
214 sock = get_socket_from_fd(sockfd);
216 wrlen = rb_write(sock->recv_buf, dataptr, len);
218 if (wrlen < len){ //write error, what should I do?
222 Wake_Up(&(sock->recv_wait_queue));
229 static void send_to_driver(int sockfd) {
231 PrintBoth("Sending data to driver\n");
237 int send(int sockfd, void * buf, uint_t len) {
238 struct socket * sock = get_socket_from_fd(sockfd);
239 //int mss = uip_mss();
240 //int pending_bytes = rb_data_len(sock->send_buf);
241 //int len = (mss < pending_bytes) ? mss: pending_bytes;
243 uchar_t * send_buf = uip_appdata;
245 bytes_read = rb_peek(sock->send_buf, send_buf, len);
247 if (bytes_read == 0) {
248 // no packet for send
252 uip_send(send_buf, len);
259 //get the socket id by the local tcp port
260 static int get_socket_from_port(ushort_t lport) {
264 for (i = 0; i < MAX_SOCKS; i++){
265 if (sockets[i].con->lport == lport) {
275 void socket_appcall(void) {
279 sockfd = get_socket_from_port(uip_conn->lport);
281 PrintBoth("Appcall\n");
288 if (uip_connected()) {
293 if (uip_closed() ||uip_aborted() ||uip_timedout()) {
311 send_to_driver(sockfd);
318 static int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
320 uip_len = info->size;
322 // for (i = 0; i < info->size; i++) {
323 // uip_buf[i] = *(pkt + i);
326 PrintBoth("Packet REceived\n");
328 memcpy(uip_buf, pkt, uip_len);
333 if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
334 PrintBoth("ARP PACKET\n");
340 PrintBoth("Transmitting\n");
341 NE2K_Transmit(uip_len);
346 PrintBoth("Data PACKET\n");
352 PrintBoth("Transmitting\n");
354 NE2K_Transmit(uip_len);