29 #include <arpa/inet.h>
32 #include <netinet/in.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
41 #define MAXBUFLEN 5000
43 #define __MF_SUBJECT__ "mfReceiveAndForward"
48 #define __SHORTFILE__ \
49 (strstr(&__FILE__[0], "/srcs/") ? strstr(&__FILE__[0], "/srcs/") + 6 : __FILE__)
52 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
54 #define __E__ std::endl
56 #define __COUT_HDR_F__ __SHORTFILE__ << "\t"
57 #define __COUT_HDR_L__ "[" << std::dec << __LINE__ << "]\t"
58 #define __COUT_HDR_P__ __PRETTY_FUNCTION__ << "\t"
59 #define __COUT_HDR_FL__ __SHORTFILE__ << " " << __COUT_HDR_L__
60 #define __COUT_HDR_FP__ __SHORTFILE__ << " : " << __COUT_HDR_P__
61 #define __COUT_HDR__ __COUT_HDR_FL__
63 #define __COUT_TYPE__(X) std::cout << QUOTE(X) << ":" << __MF_SUBJECT__ << ":"
65 #define __COUT_ERR__ __COUT_TYPE__(LogError) << __COUT_HDR__
66 #define __COUT_WARN__ __COUT_TYPE__(LogWarning) << __COUT_HDR__
67 #define __COUT_INFO__ __COUT_TYPE__(LogInfo) << __COUT_HDR__
68 #define __COUT__ __COUT_TYPE__(LogDebug) << __COUT_HDR__
69 #define __COUTV__(X) __COUT__ << QUOTE(X) << " = " << X << __E__
72 void* get_in_addr(
struct sockaddr* sa)
74 if(sa->sa_family == AF_INET)
76 return &(((
struct sockaddr_in*)sa)->sin_addr);
79 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
82 int makeSocket(
const char* ip,
int port,
struct addrinfo*& p)
85 struct addrinfo hints, *servinfo;
92 memset(&hints, 0,
sizeof hints);
93 hints.ai_family = AF_UNSPEC;
94 hints.ai_socktype = SOCK_DGRAM;
96 sprintf(portStr,
"%d", port);
97 if((rv = getaddrinfo(ip, portStr, &hints, &servinfo)) != 0)
99 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
104 for(p = servinfo; p != NULL; p = p->ai_next)
106 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
108 perror(
"sw: socket");
117 fprintf(stderr,
"sw: failed to create socket\n");
121 freeaddrinfo(servinfo);
124 unsigned int socketReceiveBufferSize = 0xA00000;
125 if(setsockopt(sockfd,
128 (
char*)&socketReceiveBufferSize,
129 sizeof(socketReceiveBufferSize)) < 0)
130 __COUT_ERR__ <<
"Failed to set socket receive size to 0x" << std::hex
131 << socketReceiveBufferSize << std::dec
132 <<
". Attempting to revert to default." << std::endl;
134 __COUT__ <<
"set socket receive size to 0x" << std::hex << socketReceiveBufferSize
135 << std::dec <<
"." << __E__;
140 int main(
int argc,
char** argv)
142 __COUT__ <<
"Starting...\n\n" << __E__;
144 std::string myPort_(
"3000");
145 std::string myFwdPort_(
"3001");
146 std::string myFwdIP_(
"127.0.0.1");
149 __COUT__ <<
"port parameter file:" << argv[1] <<
"\n\n" << __E__;
150 FILE* fp = fopen(argv[1],
"r");
154 char tmpParamStr[100];
156 sscanf(tmp,
"%*s %s", tmpParamStr);
157 myPort_ = tmpParamStr;
159 sscanf(tmp,
"%*s %s", tmpParamStr);
160 myFwdPort_ = tmpParamStr;
162 sscanf(tmp,
"%*s %s", tmpParamStr);
163 myFwdIP_ = tmpParamStr;
167 __COUT__ <<
"port parameter file failed to open: " << argv[1] <<
"\n\n"
170 __COUT__ <<
"Forwarding from: " << myPort_ <<
" to: " << myFwdIP_ <<
":" << myFwdPort_
175 sscanf(myFwdPort_.c_str(),
"%d", &myFwdPort);
180 struct addrinfo hints, *servinfo, *p;
183 struct sockaddr_storage their_addr;
184 char buff[MAXBUFLEN];
188 memset(&hints, 0,
sizeof hints);
189 hints.ai_family = AF_UNSPEC;
190 hints.ai_socktype = SOCK_DGRAM;
191 hints.ai_flags = AI_PASSIVE;
193 if((rv = getaddrinfo(NULL, myPort_.c_str(), &hints, &servinfo)) != 0)
195 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
200 for(p = servinfo; p != NULL; p = p->ai_next)
202 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
204 __COUT__ <<
"listener: socket...\n\n" << __E__;
205 perror(
"listener: socket");
209 if(bind(sockfd, p->ai_addr, p->ai_addrlen) < 0 )
212 __COUT__ <<
"listener: bind.\n\n" << __E__;
213 perror(
"listener: bind");
222 __COUT__ <<
"listener: failed to bind socket...\n\n" << __E__;
223 fprintf(stderr,
"listener: failed to bind socket\n");
227 freeaddrinfo(servinfo);
243 unsigned int pingCounter = 0;
247 fd_set readfds, masterfds;
251 FD_SET(sockfd, &masterfds);
255 int mf_p, mf_i, mf_j;
256 const int MF_POS_OF_TYPE = 5;
257 const int MF_POS_OF_MSG = 11;
258 bool firstPartPresent;
261 sendSockfd = makeSocket(myFwdIP_.c_str(), myFwdPort, p);
266 select(sockfd + 1, &readfds, NULL, NULL, &tv);
268 if(FD_ISSET(sockfd, &readfds))
275 addr_len =
sizeof their_addr;
276 if((numbytes = recvfrom(sockfd,
280 (
struct sockaddr*)&their_addr,
283 __COUT__ <<
"error: recvfrom...\n\n" << __E__;
305 buff[numbytes] =
'\0';
324 for(mf_p = 0, mf_i = 0; mf_i < numbytes && mf_p < MF_POS_OF_TYPE; ++mf_i)
325 if(buff[mf_i] ==
'|')
328 for(mf_j = mf_i; mf_j < numbytes && mf_p < MF_POS_OF_TYPE + 1; ++mf_j)
329 if(buff[mf_j] ==
'|')
333 if(mf_i < mf_j && mf_j < numbytes)
335 char sav = buff[mf_j - 1];
336 buff[mf_j - 1] =
'\0';
337 std::cout << &buff[mf_i - 1];
338 buff[mf_j - 1] = sav;
341 if(strcmp(&buff[mf_i - 1],
"|Warning") != 0)
344 firstPartPresent =
true;
347 firstPartPresent =
false;
349 for(mf_i = mf_j; mf_i < numbytes && mf_p < MF_POS_OF_MSG; ++mf_i)
350 if(buff[mf_i] ==
'|')
355 std::cout << &buff[mf_i - 1] << __E__;
356 else if(firstPartPresent)
361 if((numbytes = sendto(
362 sendSockfd, buff, numbytes, 0, p->ai_addr, p->ai_addrlen)) == -1)
364 __COUT__ <<
"error: sendto...\n\n" << __E__;
365 perror(
"hw: sendto");
373 if(++pingCounter > 2 * 60)
377 sendto(sendSockfd, buff, 1, 0, p->ai_addr, p->ai_addrlen)) == -1)
379 __COUT__ <<
"error: ping sendto...\n\n" << __E__;
380 perror(
"hw: ping sendto");
391 __COUT__ <<
"Exited.\n\n" << __E__;