1 /* 2 * Copyright (C) 2005 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 * Author Information 19 * Roy Keene 20 * Planning Systems Inc 21 * Slidell, LA 22 * backuppcd-bugs@psislidell.com 23 */ 24 25 /* 26 * Network related functions 27 */ 28 #include "compat.h" 29 #include "net.h" 30 31 #ifdef HAVE_SYS_TYPES_H 32 #include <sys/types.h> 33 #endif 34 #ifdef HAVE_SYS_STAT_H 35 #include <sys/stat.h> 36 #endif 37 #include <stdarg.h> 38 #ifdef HAVE_UNISTD_H 39 #include <unistd.h> 40 #endif 41 #include <fcntl.h> 42 43 #ifdef HAVE_SYS_SOCKET_H 44 #include <sys/socket.h> 45 #endif 46 #ifdef HAVE_NETINET_IN_H 47 #include <netinet/in.h> 48 #endif 49 #ifdef HAVE_ARPA_INET_H 50 #include <arpa/inet.h> 51 #endif 52 #ifdef HAVE_STDLIB_H 53 #include <stdlib.h> 54 #endif 55 #ifdef HAVE_STRING_H 56 #include <string.h> 57 #endif 58 #ifdef HAVE_NETDB_H 59 #include <netdb.h> 60 #endif 61 #ifdef HAVE_ERRNO_H 62 #include <errno.h> 63 #endif 64 #ifdef HAVE_STDIO_H 65 #include <stdio.h> 66 #endif 67 68 #ifndef SHUT_RDWR 69 #define SHUT_RDWR 2 70 #endif 71 72 /* Do things required for network access. */ 73 static int net_init(void) { 74 #ifdef _USE_WIN32_ 75 static int net_init_called = 0; 76 WSADATA wsaData; 77 78 if (net_init_called) { 79 return(0); 80 } 81 82 if (WSAStartup(MAKEWORD(2, 0), &wsaData)!=0) { 83 return(-1); 84 } 85 if (wsaData.wVersion!=MAKEWORD(2, 0)) { 86 /* Cleanup Winsock stuff */ 87 WSACleanup(); 88 return(-1); 89 } 90 91 net_init_called = 1; 92 #endif 93 return(0); 94 } 95 96 97 /* 98 * Create a listening port on tcp port PORT 99 */ 100 int net_listen(const int port) { 101 struct sockaddr_in localname; 102 int sockFd; 103 104 net_init(); 105 106 sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); 107 localname.sin_family = AF_INET; 108 localname.sin_port = htons(port); 109 localname.sin_addr.s_addr = INADDR_ANY; 110 111 if (sockFd < 0) { 112 return(-1); 113 } 114 115 if (bind(sockFd, (struct sockaddr *) &localname, sizeof(localname)) < 0) { 116 return(-1); 117 } 118 119 if (listen(sockFd, 1024) < 0) { 120 return(-1); 121 } 122 123 return(sockFd); 124 } 125 126 127 int net_connect_tcp(const char *host, const int port) { 128 int sockid; 129 struct hostent *hostinfo; 130 struct sockaddr_in sock; 131 132 net_init(); 133 134 #ifdef HAVE_INET_ATON 135 if (!inet_aton(host,&sock.sin_addr)) { 136 #else 137 #ifdef HAVE_INET_ADDR 138 if ( (sock.sin_addr.s_addr=inet_addr(host) )==-1) { 139 #else 140 { 141 #endif 142 #endif 143 if ((hostinfo=gethostbyname(host))==NULL) return(-EPERM); 144 memcpy(&sock.sin_addr.s_addr,hostinfo->h_addr_list[0],hostinfo->h_length); 145 } 146 147 sock.sin_family=AF_INET; 148 sock.sin_port=htons(port); 149 if ((sockid=socket(AF_INET, SOCK_STREAM, 0))<0) { 150 return(-EIO); 151 } 152 if (connect(sockid, (struct sockaddr *) &sock, sizeof(sock))<0) { 153 close(sockid); 154 return(-EIO); 155 } 156 return(sockid); 157 } 158 159 int net_close(const int sockid) { 160 shutdown(sockid, SHUT_RDWR); 161 return(close(sockid)); 162 } |