5748287 [rkeene@sledge /home/rkeene/devel/old/rutil_tcpcgi-0.1.17]$ cat -n tcpnet.c
  1 /*
  2  * Copyright (C) 2001  Roy Keene
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 17  *
 18  *      email: tcpcgi@rkeene.org
 19  */
 20 
 21 
 22 /*
 23  *  Network related functions
 24  */
 25 #include <sys/types.h>
 26 #include <sys/stat.h>
 27 #include <stdarg.h>
 28 #include <unistd.h>
 29 #include <fcntl.h>
 30 #include "tcpnet.h"
 31 
 32 #ifndef NO_NETWORK
 33 #include <sys/socket.h>
 34 #include <netinet/in.h>
 35 #include <arpa/inet.h>
 36 #include <stdlib.h>
 37 #include <string.h>
 38 #include <netdb.h>
 39 #include <errno.h>
 40 #include <stdio.h>
 41 
 42 extern int errno;
 43 
 44 /*
 45  *  Create a listening port on tcp port PORT
 46  */
 47 int createlisten(int port, int localonly,int type) {
 48     struct sockaddr_in localname;
 49     int sockFd;
 50     sockFd=socket(AF_INET,type,IPPROTO_IP);
 51     localname.sin_family=AF_INET;
 52     localname.sin_port=htons(port);
 53     localname.sin_addr.s_addr=htonl((localonly)?(INADDR_LOOPBACK):(INADDR_ANY));
 54     if (bind(sockFd,(struct sockaddr *) & localname,sizeof(localname))==-1) { close(sockFd); perror("bind");return(-1);
	}
 55     if (type!=SOCK_DGRAM) {
 56         if (listen(sockFd,1024)==-1) { close(sockFd); perror("listen"); return(-1); }
 57     }
 58     return(sockFd);
 59 }
 60 
 61 
 62 /*
 63  *  Close that socket, yeah.
 64  */
 65 void closeconnection(int sockfd) {
 66     shutdown(sockfd, 1);
 67     close(sockfd);
 68 }
 69 
 70 int createconnection(char *host, int port, int type) {
 71     static char *old_host=NULL;
 72     static uint32_t old_addr=0;
 73     struct hostent *hostinfo;
 74     struct sockaddr_in sock;
 75     int needresolve=1;
 76     int sockid;
 77 
 78     if (old_host) {
 79         if (strcmp(old_host,host)==0) needresolve=0;
 80     }
 81     if (old_addr==0) needresolve=1;
 82 
 83     if (needresolve) {
 84         if ((hostinfo=gethostbyname(host))==NULL) {
 85 #ifdef HAVE_INET_ATON
 86             if (!inet_aton(host,&sock.sin_addr))
 87 #else
 88             if ( (sock.sin_addr.s_addr=inet_addr(host) )==-1)
 89 #endif
 90                 return(-1);
 91         } else {
 92             memcpy(&sock.sin_addr.s_addr,hostinfo->h_addr_list[0],hostinfo->h_length);
 93         }
 94         if (old_host) free(old_host);
 95         old_addr=sock.sin_addr.s_addr;
 96         old_host=strdup(host);
 97     } else {
 98         sock.sin_addr.s_addr=old_addr;
 99     }
100     sock.sin_family=AF_INET;
101     sock.sin_port=htons(port);
102     if ((sockid=socket(AF_INET, type, 0))<0)
103         return(-1);
104     if (connect(sockid, (struct sockaddr *) &sock, sizeof(sock))<0) {
105         close(sockid);
106         return(-1);
107     }
108     return(sockid);
109 }
110 int createconnection_tcp(char *host, int port) {
111     return(createconnection(host,port,SOCK_STREAM));
112 }
113 
114 #else
115 int createlisten(int port, int localonly) { return(-1); }
116 void closeconnection(int sockfd) { return; }
117 int createconnection(char *host, int port, int type) { return(-1); }
118 int createconnection_tcp(char *host, int port) { return(-1); }
119 #endif
5748288 [rkeene@sledge /home/rkeene/devel/old/rutil_tcpcgi-0.1.17]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-01-01 16:22:42