| 1 |
/*---------------------------------------------------------------------------------------------- |
| 2 |
Copyright (c) 2004, Noboru Iwata |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
| 9 |
this list of conditions and the following disclaimer. |
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
this list of conditions and the following disclaimer in the documentation |
| 12 |
and/or other materials provided with the distribution. |
| 13 |
* Neither the name of the <ORGANIZATION> nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without |
| 15 |
specific prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 19 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 21 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 22 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
program : pmxfilter |
| 29 |
|
| 30 |
---------------------------------------------------------------------------------------------*/ |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <sys/time.h> |
| 34 |
#include <netinet/in.h> |
| 35 |
#include <netdb.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <string.h> |
| 38 |
#include <unistd.h> |
| 39 |
#include <errno.h> |
| 40 |
#include "pmxfilter.h" |
| 41 |
|
| 42 |
#define INVALID_SOCKET -1 |
| 43 |
#define SOCKET_ERROR -1 |
| 44 |
|
| 45 |
static short wVerReq; |
| 46 |
static struct sockaddr_in addr; |
| 47 |
static struct sockaddr_in addrc; |
| 48 |
static SOCKET s; |
| 49 |
static SOCKET sc; |
| 50 |
fd_set Fdset_ss,Fdset_sc; //select |
| 51 |
|
| 52 |
static void ErrorHandler(char *message,DWORD err); |
| 53 |
static ULONG GetHostAddress(LPSTR szBuff); |
| 54 |
|
| 55 |
char rcvMsg[MSGLEN]; |
| 56 |
char sndMsg[MSGLEN]; |
| 57 |
|
| 58 |
static ULONG GetHostAddress(LPSTR szBuff) |
| 59 |
{ |
| 60 |
struct hostent *phe; |
| 61 |
phe = gethostbyname(szBuff); |
| 62 |
if (phe == NULL) |
| 63 |
return(0xffffffff); |
| 64 |
return *((unsigned long *)phe->h_addr); |
| 65 |
} |
| 66 |
|
| 67 |
static void ErrorHandler(char *message,DWORD err) |
| 68 |
{ |
| 69 |
char m[32]; |
| 70 |
|
| 71 |
memset(m,0,sizeof(m)); |
| 72 |
sprintf(m,"%s:[%ld]\n",message,err); |
| 73 |
printf(m); |
| 74 |
} |
| 75 |
|
| 76 |
BOOL sub_SocInit(char *host,u_short port) |
| 77 |
{ |
| 78 |
int err; |
| 79 |
BOOL st; |
| 80 |
|
| 81 |
s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); |
| 82 |
if (s == INVALID_SOCKET){ |
| 83 |
ErrorHandler("SOCKET CREATE FAIL",0); |
| 84 |
st = FALSE; |
| 85 |
return(st); |
| 86 |
} |
| 87 |
memset(&addr,0x00,sizeof(addr)); |
| 88 |
addr.sin_family = AF_INET; |
| 89 |
addr.sin_addr.s_addr = GetHostAddress(host); |
| 90 |
addr.sin_port = htons(port); |
| 91 |
if (addr.sin_addr.s_addr == 0xffffffff){ |
| 92 |
ErrorHandler("HOSTNAME ERROR",0); |
| 93 |
close(s); |
| 94 |
st = FALSE; |
| 95 |
return(st); |
| 96 |
} |
| 97 |
if (connect(s,(struct sockaddr *)&addr,sizeof(addr)) == SOCKET_ERROR){ |
| 98 |
ErrorHandler("CONNECT FAIL��",errno); |
| 99 |
close(s); |
| 100 |
st = FALSE; |
| 101 |
return(st); |
| 102 |
} |
| 103 |
FD_ZERO(&Fdset_sc); |
| 104 |
FD_SET(s,&Fdset_sc); |
| 105 |
|
| 106 |
st = TRUE; |
| 107 |
return(st); |
| 108 |
} |
| 109 |
|
| 110 |
void sub_SocEnd(void) |
| 111 |
{ |
| 112 |
close(s); |
| 113 |
} |
| 114 |
|
| 115 |
int sub_cRecv(long t) |
| 116 |
{ |
| 117 |
int len; |
| 118 |
fd_set Rdset; //RCV CHECK |
| 119 |
struct timeval tv; |
| 120 |
|
| 121 |
Rdset = Fdset_sc; //SOCKET |
| 122 |
tv.tv_sec = t; //TIMEOUT (SE) |
| 123 |
tv.tv_usec = 0; |
| 124 |
if (select(FD_SETSIZE,&Rdset,NULL,NULL,&tv) <= 0) { |
| 125 |
ErrorHandler("RCV TIMEOUT",0); |
| 126 |
close(sc); |
| 127 |
return(-2); |
| 128 |
} |
| 129 |
|
| 130 |
memset(rcvMsg,0,sizeof(rcvMsg)); |
| 131 |
len = recv(s,rcvMsg,sizeof(rcvMsg),0); |
| 132 |
if (len == 0){ |
| 133 |
ErrorHandler("SOCKET CLOSE",errno); |
| 134 |
close(s); |
| 135 |
return(0); |
| 136 |
} |
| 137 |
else if(len == SOCKET_ERROR){ |
| 138 |
ErrorHandler("RCV ERROR C",errno); |
| 139 |
close(s); |
| 140 |
return(-1); |
| 141 |
} |
| 142 |
return(len); |
| 143 |
} |
| 144 |
|
| 145 |
int sub_cSend(const char *str,int len) |
| 146 |
{ |
| 147 |
memcpy(sndMsg,str,len); |
| 148 |
int slen = send(s,sndMsg,len,0); |
| 149 |
if (slen == SOCKET_ERROR){ |
| 150 |
close(s); |
| 151 |
ErrorHandler("SEND ERROR C",errno); |
| 152 |
return(-1); |
| 153 |
} |
| 154 |
return(slen); |
| 155 |
} |
| 156 |
|