Page MenuHomePhabricator

Missing #include makes that compiling example fails
Closed, ResolvedPublic

Description

This happens when I try to compile libssh 0.9.3 on NetBSD 8.1:

...
Scanning dependencies of target samplesftp                                                                                                                                                                                                   
[ 62%] Built target samplesshd-cb                                                                                                                                                                                                            
[ 62%] Building C object examples/CMakeFiles/samplesftp.dir/samplesftp.c.o                                                                                                                                                                   
[ 63%] Linking C executable proxy                                                                                                                                                                                                            
[ 63%] Building C object examples/CMakeFiles/samplesftp.dir/authentication.c.o                                                                                                                                                               
[ 63%] Building C object examples/CMakeFiles/scp_download.dir/connect_ssh.c.o                                                                                                                                                                
[ 64%] Linking C executable scp_download                                                                                                                                                                                                     
[ 64%] Built target proxy                                                                                                                                                                                                                    
Scanning dependencies of target sshd_direct-tcpip                                                                                                                                                                                            
Scanning dependencies of target samplesshd-kbdint                                                                                                                                                                                            
[ 64%] Building C object examples/CMakeFiles/samplesftp.dir/knownhosts.c.o                                                                                                                                                                   
[ 65%] Building C object examples/CMakeFiles/sshd_direct-tcpip.dir/sshd_direct-tcpip.c.o                                                                                                                                                     
[ 65%] Building C object examples/CMakeFiles/samplesshd-kbdint.dir/samplesshd-kbdint.c.o                                                                                                                                                     
[ 65%] Built target scp_download                                                                                                                                                                                                             
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c: In function 'my_channel_data_function':                                                                                                              
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:283:13: error: implicit declaration of function 'send'; did you mean 'srand'? [-Werror=implicit-function-declaration]                                 
         i = send(*event_fd_data->p_fd, data, len, 0);                                                                                                                                                                                       
             ^~~~                                                                                                                                                                                                                            
             srand                                                                                                                                                                                                                           
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c: In function 'my_fd_data_function':                                                                                                                   
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:350:11: error: implicit declaration of function 'recv'; did you mean 'execv'? [-Werror=implicit-function-declaration]                                 
     len = recv(*event_fd_data->p_fd, buf, sizeof(buf), 0);                                                                                                                                                                                  
           ^~~~                                                                                                                                                                                                                              
           execv                                                                                                                                                                                                                             
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:382:9: error: implicit declaration of function 'shutdown'; did you mean 'chown'? [-Werror=implicit-function-declaration]                              
         shutdown(*event_fd_data->p_fd, SHUT_RD);                                                                                                                                                                                            
         ^~~~~~~~                                                                                                                                                                                                                            
         chown                                                                                                                                                                                                                               
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:382:40: error: 'SHUT_RD' undeclared (first use in this function)                                                                                      
         shutdown(*event_fd_data->p_fd, SHUT_RD);                                                                                                                                                                                            
                                        ^~~~~~~                                                                                                                                                                                              
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:382:40: note: each undeclared identifier is reported only once for each function it appears in                                                        
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c: In function 'open_tcp_socket':                                                                                                                       
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:391:24: error: storage size of 'sin' isn't known                                                                                                      
     struct sockaddr_in sin;                                                                                                                                                                                                                 
                        ^~~                                                                                                                                                                                                                  
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:397:19: error: implicit declaration of function 'socket'; did you mean 'setkey'? [-Werror=implicit-function-declaration]                              
     forwardsock = socket(AF_INET, SOCK_STREAM, 0);                                                                                                                                                                                          
                   ^~~~~~                                                                                                                                                                                                                    
                   setkey                                                                                                                                                                                                                    
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:397:26: error: 'AF_INET' undeclared (first use in this function)                                                                                      
     forwardsock = socket(AF_INET, SOCK_STREAM, 0);                                                                                                                                                                                          
                          ^~~~~~~                                                                                                                                                                                                            
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:397:35: error: 'SOCK_STREAM' undeclared (first use in this function)                                                                                  
     forwardsock = socket(AF_INET, SOCK_STREAM, 0);                                                                                                                                                                                          
                                   ^~~~~~~~~~~                                                                                                                                                                                               
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:420:9: error: implicit declaration of function 'connect'; did you mean 'cgetnext'? [-Werror=implicit-function-declaration]                            
     if (connect(forwardsock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {                                                                                                                                                                   
         ^~~~~~~                                                                                                                                                                                                                             
         cgetnext                                                                                                                                                                                                                            
/pkg_comp/obj/pkgsrc/security/libssh/default/libssh-0.9.3/examples/sshd_direct-tcpip.c:391:24: warning: unused variable 'sin' [-Wunused-variable]                                                                                            
     struct sockaddr_in sin;                                                                                                                                                                                                                 
                        ^~~                                                                                                                                                                                                                  
cc1: some warnings being treated as errors                                                                                                                                                                                                   
--- examples/CMakeFiles/sshd_direct-tcpip.dir/sshd_direct-tcpip.c.o ---                                                                                                                                                                      
*** [examples/CMakeFiles/sshd_direct-tcpip.dir/sshd_direct-tcpip.c.o] Error code 1                                                                                                                                                           
                                                                                                                                                                                                                                             
make[2]: stopped in /pkg_comp/obj/pkgsrc/security/libssh/default/build                                                                                                                                                                       
1 error

Looking at the source, it is obvious that <sys/socket.h> is not included.

The manpage for send(2) (and recv(2)) clearly states

SEND(2)                       System Calls Manual                      SEND(2)

NAME
     send, sendto, sendmsg, sendmmsg - send a message from a socket

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <sys/socket.h>

     ssize_t
     send(int s, const void *msg, size_t len, int flags);

     ssize_t
     sendto(int s, const void *msg, size_t len, int flags,
         const struct sockaddr *to, socklen_t tolen);

     ssize_t
     sendmsg(int s, const struct msghdr *msg, int flags);

     int
     sendmmsg(int s, struct mmsghdr *mmsg, unsigned int vlen,
         unsigned int flags);

Note the #include.

Even the Linux manual page, which tends to be notoriously sloppy regarding includes, reads

SEND(2)                    Linux Programmer's Manual                   SEND(2)

NAME
       send, sendto, sendmsg - send a message on a socket

SYNOPSIS
       #include <sys/types.h>
       #include <sys/socket.h>

       ssize_t send(int sockfd, const void *buf, size_t len, int flags);

       ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
                      const struct sockaddr *dest_addr, socklen_t addrlen);

       ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);

also listing <sys/types.h> which is indeed often required before including other system headers.

Fix:

add the mentioned include statements.

Event Timeline

rhialto created this task.Apr 8 2020, 1:59 PM
Jakuje claimed this task.Apr 9 2020, 11:49 AM
Jakuje triaged this task as Normal priority.
Jakuje added a project: Restricted Project.
Jakuje added a subscriber: Jakuje.

Can you check the following commit solves your problems:

https://gitlab.com/jjelen/libssh-mirror/-/commit/de01f164f62c865e54367321a65897c1c8d6f000

If so, I will push the changes.

Yes, that compiles perfectly. I just cloned the whole tree.

Thanks!

Jakuje closed this task as Resolved.May 21 2020, 11:29 AM

Fixed in previously mentioned commits.