1+ #include <stdlib.h>
2+ #include <sys/select.h>
3+ #include <stdio.h>
14#include <lcthw/ringbuffer.h>
2- #include <lcthw/bstrlib.h>
35#include <lcthw/dbg.h>
6+ #include <sys/socket.h>
7+ #include <sys/types.h>
8+ #include <sys/uio.h>
9+ #include <arpa/inet.h>
10+ #include <netdb.h>
411#include <unistd.h>
512#include <fcntl.h>
613#include "net.h"
714
815struct tagbstring NL = bsStatic ("\n" );
916struct tagbstring CRLF = bsStatic ("\r\n" );
1017
11-
1218int nonblock (int fd )
1319{
1420 int flags = fcntl (fd , F_GETFL , 0 );
@@ -22,6 +28,31 @@ int nonblock(int fd)
2228 return -1 ;
2329}
2430
31+ int client_connect (char * host , char * port )
32+ {
33+ int rc = 0 ;
34+ struct addrinfo * addr = NULL ;
35+
36+ rc = getaddrinfo (host , port , NULL , & addr );
37+ check (rc == 0 , "Failed to lookup %s:%s" , host , port );
38+
39+ int sock = socket (AF_INET , SOCK_STREAM , 0 );
40+ check (sock >= 0 , "Cannot create a socket." );
41+
42+ rc = connect (sock , addr -> ai_addr , addr -> ai_addrlen );
43+ check (rc == 0 , "Connect failed." );
44+
45+ rc = nonblock (sock );
46+ check (rc == 0 , "Can't set nonblocking." );
47+
48+ freeaddrinfo (addr );
49+ return sock ;
50+
51+ error :
52+ freeaddrinfo (addr );
53+ return -1 ;
54+ }
55+
2556int read_some (RingBuffer * buffer , int fd , int is_socket )
2657{
2758 int rc = 0 ;
@@ -73,23 +104,28 @@ int write_some(RingBuffer * buffer, int fd, int is_socket)
73104 return -1 ;
74105}
75106
76-
77107int attempt_listen (struct addrinfo * info )
78108{
79- int sockfd = 0 ;
109+ int sockfd = -1 ; // default fail
80110 int rc = -1 ;
81111 int yes = 1 ;
82112
113+ check (info != NULL , "Invalid addrinfo." );
114+
115+ // create a socket with the addrinfo
83116 sockfd = socket (info -> ai_family , info -> ai_socktype ,
84117 info -> ai_protocol );
85- check_debug (sockfd != -1 , "Failed to bind to address result . Trying more." );
118+ check_debug (sockfd != -1 , "Failed to bind to address. Trying more." );
86119
120+ // set the SO_REUSEADDR option on the socket
87121 rc = setsockopt (sockfd , SOL_SOCKET , SO_REUSEADDR , & yes , sizeof (int ));
88- check_debug (rc == 0 , "Failed to set SO_REISEADDR ." );
122+ check_debug (rc == 0 , "Failed to set SO_REUSADDR ." );
89123
124+ // attempt to bind to it
90125 rc = bind (sockfd , info -> ai_addr , info -> ai_addrlen );
91- check_debug (rc == 0 , "Failed to bind socket." );
92-
126+ check_debug (rc == 0 , "Failed to find socket." );
127+
128+ // finally listen with a backlog
93129 rc = listen (sockfd , BACKLOG );
94130 check_debug (rc == 0 , "Failed to listen to socket." );
95131
@@ -99,10 +135,11 @@ int attempt_listen(struct addrinfo *info)
99135 return -1 ;
100136}
101137
138+
102139int server_listen (const char * host , const char * port )
103140{
104141 int rc = 0 ;
105- int sockfd = -1 ;
142+ int sockfd = -1 ; // default fail value
106143 struct addrinfo * info = NULL ;
107144 struct addrinfo * next_p = NULL ;
108145 struct addrinfo addr = {
@@ -111,51 +148,26 @@ int server_listen(const char *host, const char *port)
111148 .ai_flags = AI_PASSIVE
112149 };
113150
114- check (host != NULL , "Must give a valid host." );
115- check (port != NULL , "Must have a valid port." );
151+ check (host != NULL , "Invalid host." );
152+ check (port != NULL , "Invalid port." );
116153
154+ // get the address info for host and port
117155 rc = getaddrinfo (NULL , port , & addr , & info );
118156 check (rc == 0 , "Failed to get address info for connect." );
119-
157+
158+ // cycle through the available list to find one
120159 for (next_p = info ; next_p != NULL ; next_p = next_p -> ai_next )
121160 {
161+ // attempt to listen to each one
122162 sockfd = attempt_listen (next_p );
123163 if (sockfd != -1 ) break ;
124164 }
125165
166+ // either we found one and were able to listen or nothing.
126167 check (sockfd != -1 , "All possible addresses failed." );
127168
169+ error : //fallthrough
128170 if (info ) freeaddrinfo (info );
171+ // this gets set by the above to either -1 or valid
129172 return sockfd ;
130-
131- error : // fallthrough
132- if (info ) freeaddrinfo (info );
133- return -1 ;
134- }
135-
136-
137- int client_connect (char * host , char * port )
138- {
139- int rc = 0 ;
140- struct addrinfo * addr = NULL ;
141-
142- rc = getaddrinfo (host , port , NULL , & addr );
143- check (rc == 0 , "Failed to lookup %s:%s" , host , port );
144-
145- int sock = socket (AF_INET , SOCK_STREAM , 0 );
146- check (sock >= 0 , "Cannot create a socket." );
147-
148- rc = connect (sock , addr -> ai_addr , addr -> ai_addrlen );
149- check (rc == 0 , "Connect failed." );
150-
151- rc = nonblock (sock );
152- check (rc == 0 , "Can't set nonblocking." );
153-
154- freeaddrinfo (addr );
155- return sock ;
156-
157- error :
158- freeaddrinfo (addr );
159- return -1 ;
160173}
161-
0 commit comments