3232
3333int accept (int s , struct sockaddr * addr , socklen_t * addrlen )
3434{
35+ int new_client = -1 ;
3536 int sock = dfs_lwip_getsocket (s );
3637
37- return lwip_accept (sock , addr , addrlen );
38+ new_client = lwip_accept (sock , addr , addrlen );
39+ if (new_client != -1 )
40+ {
41+ /* this is a new socket, create it in file system fd */
42+ int fd ;
43+ struct dfs_fd * d ;
44+
45+ /* allocate a fd */
46+ fd = fd_new ();
47+ if (fd < 0 )
48+ {
49+ rt_set_errno (- DFS_STATUS_ENOMEM );
50+ lwip_close (sock );
51+
52+ printf ("no fd yet!\n" );
53+ return -1 ;
54+ }
55+ d = fd_get (fd );
56+
57+ /* this is a socket fd */
58+ d -> type = FT_SOCKET ;
59+ d -> path = RT_NULL ;
60+
61+ d -> fs = dfs_lwip_get_fs ();
62+
63+ d -> flags = DFS_O_RDWR ; /* set flags as read and write */
64+ d -> size = 0 ;
65+ d -> pos = 0 ;
66+
67+ /* set socket to the data of dfs_fd */
68+ d -> data = (void * ) new_client ;
69+
70+ /* release the ref-count of fd */
71+ fd_put (d );
72+
73+ return fd ;
74+ }
75+
76+ return new_client ;
3877}
3978
4079int bind (int s , const struct sockaddr * name , socklen_t namelen )
@@ -46,9 +85,28 @@ int bind(int s, const struct sockaddr *name, socklen_t namelen)
4685
4786int shutdown (int s , int how )
4887{
49- int sock = dfs_lwip_getsocket (s );
88+ int sock ;
89+ struct dfs_fd * d ;
90+
91+ d = fd_get (s );
92+ if (d == RT_NULL )
93+ {
94+ rt_set_errno (- DFS_STATUS_EBADF );
95+
96+ return -1 ;
97+ }
98+
99+ sock = dfs_lwip_getsocket (s );
100+ if (lwip_shutdown (sock , how ) == 0 )
101+ {
102+ /* socket has been closed, delete it from file system fd */
103+ fd_put (d );
104+ fd_put (d );
105+
106+ return 0 ;
107+ }
50108
51- return lwip_shutdown ( s , how ) ;
109+ return -1 ;
52110}
53111
54112int getpeername (int s , struct sockaddr * name , socklen_t * namelen )
@@ -163,3 +221,4 @@ int socket(int domain, int type, int protocol)
163221
164222 return fd ;
165223}
224+
0 commit comments