diff options
Diffstat (limited to 'src/net/isocket.cc')
| -rw-r--r-- | src/net/isocket.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/net/isocket.cc b/src/net/isocket.cc new file mode 100644 index 0000000..af94189 --- /dev/null +++ b/src/net/isocket.cc | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <net/isocket.h> | ||
| 21 | #include <errno.h> | ||
| 22 | |||
| 23 | isocket::isocket(const in_addr& ia,int port,int backlog): | ||
| 24 | pipe_stream(-1),rd(-1) | ||
| 25 | { | ||
| 26 | if(port) { | ||
| 27 | fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); | ||
| 28 | if(fd<0) THROW("isocket: Could not create"); | ||
| 29 | reuse(1); | ||
| 30 | } | ||
| 31 | else fd=0; | ||
| 32 | sa.sin_family=2; | ||
| 33 | sa.sin_port=htons(port); | ||
| 34 | sa.sin_addr=ia; | ||
| 35 | if(port) { | ||
| 36 | if(bind(fd,(sockaddr*)&sa,sizeof(sa))==-1) THROW("isocket: Could not bind"); | ||
| 37 | } | ||
| 38 | if(listen(fd,backlog)==-1) THROW("isocket: Could not listen"); | ||
| 39 | rd.setfd(fd); | ||
| 40 | } | ||
| 41 | |||
| 42 | isocket::isocket(int f,const sockaddr_in& ia): | ||
| 43 | pipe_stream(f),rd(f) | ||
| 44 | { | ||
| 45 | sa=ia; | ||
| 46 | setflag(O_NONBLOCK|O_NDELAY); | ||
| 47 | nodelay(1); | ||
| 48 | } | ||
| 49 | |||
| 50 | isocket* isocket::accept() throw(merror_t) | ||
| 51 | { | ||
| 52 | sockaddr_in tsa; socklen_t tsalen; | ||
| 53 | memset(&tsa,0,tsalen=sizeof(tsa)); | ||
| 54 | int tfd=::accept(fd,(sockaddr*)&tsa,&tsalen); | ||
| 55 | if(tfd>0) return new isocket(tfd,tsa); | ||
| 56 | if(errno!=EAGAIN) THROW("isocket: could not accept"); | ||
| 57 | return 0; | ||
| 58 | } | ||
