@@ -86,18 +86,7 @@ using namespace oxt;
8686static WritevFunction writevFunction = syscalls::writev;
8787
8888
89- bool
90- purgeStdio (FILE *f) {
91- #if defined(HAVE_FPURGE)
92- fpurge (f);
93- return true ;
94- #elif defined(HAVE___FPURGE)
95- __fpurge (f);
96- return true ;
97- #else
98- return false ;
99- #endif
100- }
89+ /* ***** Server address types support ******/
10190
10291ServerAddressType
10392getSocketAddressType (const StaticString &address) {
@@ -182,67 +171,8 @@ isLocalSocketAddress(const StaticString &address) {
182171 }
183172}
184173
185- void
186- setBlocking (int fd) {
187- int flags, ret;
188174
189- do {
190- flags = fcntl (fd, F_GETFL);
191- } while (flags == -1 && errno == EINTR);
192- if (flags == -1 ) {
193- int e = errno;
194- throw SystemException (" Cannot set socket to blocking mode: "
195- " cannot get socket flags" ,
196- e);
197- }
198- do {
199- ret = fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);
200- } while (ret == -1 && errno == EINTR);
201- if (ret == -1 ) {
202- int e = errno;
203- throw SystemException (" Cannot set socket to blocking mode: "
204- " cannot set socket flags" ,
205- e);
206- }
207- }
208-
209- void
210- setNonBlocking (int fd) {
211- int flags, ret;
212-
213- do {
214- flags = fcntl (fd, F_GETFL);
215- } while (flags == -1 && errno == EINTR);
216- if (flags == -1 ) {
217- int e = errno;
218- throw SystemException (" Cannot set socket to non-blocking mode: "
219- " cannot get socket flags" ,
220- e);
221- }
222- do {
223- ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
224- } while (ret == -1 && errno == EINTR);
225- if (ret == -1 ) {
226- int e = errno;
227- throw SystemException (" Cannot set socket to non-blocking mode: "
228- " cannot set socket flags" ,
229- e);
230- }
231- }
232-
233- int
234- callAccept4 (int sock, struct sockaddr *addr, socklen_t *addr_len, int options) {
235- #if defined(HAVE_ACCEPT4)
236- int ret;
237- do {
238- ret = ::accept4 (sock, addr, addr_len, options);
239- } while (ret == -1 && errno == EINTR);
240- return ret;
241- #else
242- errno = ENOSYS;
243- return -1 ;
244- #endif
245- }
175+ /* ***** Server socket creation ******/
246176
247177int
248178createServer (const StaticString &address, unsigned int backlogSize, bool autoDelete,
@@ -408,6 +338,9 @@ createTcpServer(const char *address, unsigned short port, unsigned int backlogSi
408338 return fd;
409339}
410340
341+
342+ /* ***** Socket connection establishment (blocking) ******/
343+
411344int
412345connectToServer (const StaticString &address, const char *file, unsigned int line) {
413346 TRACE_POINT ();
@@ -486,54 +419,6 @@ connectToUnixServer(const StaticString &filename, const char *file,
486419 abort (); // Never reached.
487420}
488421
489- void
490- setupNonBlockingUnixSocket (NUnix_State &state, const StaticString &filename,
491- const char *file, unsigned int line)
492- {
493- state.fd .assign (syscalls::socket (PF_UNIX, SOCK_STREAM, 0 ), file, line);
494- if (state.fd == -1 ) {
495- int e = errno;
496- throw SystemException (" Cannot create a Unix socket file descriptor" , e);
497- }
498-
499- state.filename = filename;
500- setNonBlocking (state.fd );
501- }
502-
503- bool
504- connectToUnixServer (NUnix_State &state) {
505- struct sockaddr_un addr;
506- int ret;
507-
508- if (state.filename .size () > sizeof (addr.sun_path ) - 1 ) {
509- string message = " Cannot connect to Unix socket '" ;
510- message.append (state.filename .data (), state.filename .size ());
511- message.append (" ': filename is too long." );
512- throw RuntimeException (message);
513- }
514-
515- addr.sun_family = AF_UNIX;
516- memcpy (addr.sun_path , state.filename .data (), state.filename .size ());
517- addr.sun_path [state.filename .size ()] = ' \0 ' ;
518-
519- ret = syscalls::connect (state.fd , (const sockaddr *) &addr, sizeof (addr));
520- if (ret == -1 ) {
521- if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
522- return false ;
523- } else if (errno == EISCONN) {
524- return true ;
525- } else {
526- int e = errno;
527- string message = " Cannot connect to Unix socket '" ;
528- message.append (state.filename .data (), state.filename .size ());
529- message.append (" '" );
530- throw SystemException (message, e);
531- }
532- } else {
533- return true ;
534- }
535- }
536-
537422int
538423connectToTcpServer (const StaticString &hostname, unsigned int port,
539424 const char *file, unsigned int line)
@@ -591,6 +476,57 @@ connectToTcpServer(const StaticString &hostname, unsigned int port,
591476 return fd;
592477}
593478
479+
480+ /* ***** Socket connection establishment (non-blocking) ******/
481+
482+ void
483+ setupNonBlockingUnixSocket (NUnix_State &state, const StaticString &filename,
484+ const char *file, unsigned int line)
485+ {
486+ state.fd .assign (syscalls::socket (PF_UNIX, SOCK_STREAM, 0 ), file, line);
487+ if (state.fd == -1 ) {
488+ int e = errno;
489+ throw SystemException (" Cannot create a Unix socket file descriptor" , e);
490+ }
491+
492+ state.filename = filename;
493+ setNonBlocking (state.fd );
494+ }
495+
496+ bool
497+ connectToUnixServer (NUnix_State &state) {
498+ struct sockaddr_un addr;
499+ int ret;
500+
501+ if (state.filename .size () > sizeof (addr.sun_path ) - 1 ) {
502+ string message = " Cannot connect to Unix socket '" ;
503+ message.append (state.filename .data (), state.filename .size ());
504+ message.append (" ': filename is too long." );
505+ throw RuntimeException (message);
506+ }
507+
508+ addr.sun_family = AF_UNIX;
509+ memcpy (addr.sun_path , state.filename .data (), state.filename .size ());
510+ addr.sun_path [state.filename .size ()] = ' \0 ' ;
511+
512+ ret = syscalls::connect (state.fd , (const sockaddr *) &addr, sizeof (addr));
513+ if (ret == -1 ) {
514+ if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
515+ return false ;
516+ } else if (errno == EISCONN) {
517+ return true ;
518+ } else {
519+ int e = errno;
520+ string message = " Cannot connect to Unix socket '" ;
521+ message.append (state.filename .data (), state.filename .size ());
522+ message.append (" '" );
523+ throw SystemException (message, e);
524+ }
525+ } else {
526+ return true ;
527+ }
528+ }
529+
594530void
595531setupNonBlockingTcpSocket (NTCP_State &state, const StaticString &hostname, int port,
596532 const char *file, unsigned int line)
@@ -719,6 +655,84 @@ NConnect_State::asNTCP_State() {
719655 return s_tcp;
720656}
721657
658+
659+ /* ***** Other ******/
660+
661+ bool
662+ purgeStdio (FILE *f) {
663+ #if defined(HAVE_FPURGE)
664+ fpurge (f);
665+ return true ;
666+ #elif defined(HAVE___FPURGE)
667+ __fpurge (f);
668+ return true ;
669+ #else
670+ return false ;
671+ #endif
672+ }
673+
674+ void
675+ setBlocking (int fd) {
676+ int flags, ret;
677+
678+ do {
679+ flags = fcntl (fd, F_GETFL);
680+ } while (flags == -1 && errno == EINTR);
681+ if (flags == -1 ) {
682+ int e = errno;
683+ throw SystemException (" Cannot set socket to blocking mode: "
684+ " cannot get socket flags" ,
685+ e);
686+ }
687+ do {
688+ ret = fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);
689+ } while (ret == -1 && errno == EINTR);
690+ if (ret == -1 ) {
691+ int e = errno;
692+ throw SystemException (" Cannot set socket to blocking mode: "
693+ " cannot set socket flags" ,
694+ e);
695+ }
696+ }
697+
698+ void
699+ setNonBlocking (int fd) {
700+ int flags, ret;
701+
702+ do {
703+ flags = fcntl (fd, F_GETFL);
704+ } while (flags == -1 && errno == EINTR);
705+ if (flags == -1 ) {
706+ int e = errno;
707+ throw SystemException (" Cannot set socket to non-blocking mode: "
708+ " cannot get socket flags" ,
709+ e);
710+ }
711+ do {
712+ ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
713+ } while (ret == -1 && errno == EINTR);
714+ if (ret == -1 ) {
715+ int e = errno;
716+ throw SystemException (" Cannot set socket to non-blocking mode: "
717+ " cannot set socket flags" ,
718+ e);
719+ }
720+ }
721+
722+ int
723+ callAccept4 (int sock, struct sockaddr *addr, socklen_t *addr_len, int options) {
724+ #if defined(HAVE_ACCEPT4)
725+ int ret;
726+ do {
727+ ret = ::accept4 (sock, addr, addr_len, options);
728+ } while (ret == -1 && errno == EINTR);
729+ return ret;
730+ #else
731+ errno = ENOSYS;
732+ return -1 ;
733+ #endif
734+ }
735+
722736bool
723737pingTcpServer (const StaticString &host, unsigned int port, unsigned long long *timeout) {
724738 TRACE_POINT ();
0 commit comments