the error connection of socket in kernel(new)

Robert Watson rwatson at FreeBSD.org
Tue May 31 04:20:06 PDT 2005


On Tue, 31 May 2005, shiner chen wrote:

> In order to test the process of connection of socket in kernel ,I wrote 
> a kld . When i load the kld ,the code try to connect the server of 
> ftp,but i foud the code does not work right . the function , connect 
> ,which return is right ,but ,when i see the status of connection by 
> "netstat -a" , i found the connection didn't establish!  the detailed 
> code is attached the following code have mistake!!  The platform is 
> freebsd 5.3
>
>  /* create socket */
> td=curthread;
>
> sock.domain = AF_INET;
> sock.type = SOCK_STREAM;
> sock.protocol = 0;
> st = socket(td, &sock);
> /* does create ok? */
> if( st != 0 )
> { /* create error */
>  log(LOG_DEBUG,"create socket error!\n");
>  goto main_shutdown;
> }
>  printf("the fd of socket is%d\n",control_fd);

How do you initialize the value of "control_fd"?  You need to load it out 
of td->td_reval[0].

What thread is this running in?

> /* has create one ok socket */
> printf("the socket is created!");
> /////////////////////////////////////////////////////////////////
> there are some mistake here!!!!
> /*connect*/
> bzero(&sa_in, sizeof sa_in);
> sa_in.sin_family = AF_INET;
> /*this is port of ftp*/
>       sa_in.sin_port = htons(21);
>       /*this is my computer ip:172.19.11.8*/
>       sa_in.sin_addr.s_addr = htonl((((((172 << 8) | 19) << 8) | 11) << 8) | 48);
>
>       conn.s=control_fd ;
>       conn.name=(char*) &sa_in;
>       conn.namelen=sizeof(sa_in);
>        /*why do not i connect the server of ftp*/
>       nError=connect(td,&conn);
>
>       if (!nError){

Shouldn't you check for "nError != 0" here?  If this is the real code, 
it's probably the case that you're getting EBADF back because you didn't 
set control_fd to the real file descriptor number.

Robert N M Watson

>        printf("connect failed!");
>        goto main_shutdown;
>       } ;
>      printf("connect is secusess!");
>
>
>
>
>
> ---------------------------------
> DO YOU YAHOO!?
>  ÑÅ»¢Ãâ·ÑGÓÊÏ䣭ÖйúµÚÒ»¾øÎÞÀ¬»øÓʼþɧÈų¬´óÓÊÏä


More information about the freebsd-hackers mailing list