366{
367
368
369
370 struct hostent* host;
372 if (host == NULL) {
373 char temp_buf[500];
374 sprintf(temp_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n",
376 print_err.PrintError(temp_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
377 exit(1);
378 }
379
380
381
382
383 int fd_listen;
384 struct sockaddr_in sock_listen;
385 sock_listen.sin_family = AF_INET;
386 sock_listen.sin_addr.s_addr = *reinterpret_cast<unsigned int*>(host->h_addr_list[0]);
387
388 socklen_t addrlen = sizeof(sock_listen);
390 fd_listen = socket(PF_INET, SOCK_STREAM, 0);
391
392 int flags = 1;
393 int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags));
394 if (ret < 0) {
395 perror("Failed to set REUSEADDR");
396 }
397
398 if (bind(fd_listen, reinterpret_cast<struct sockaddr*>(&sock_listen), sizeof(struct sockaddr)) < 0) {
399 printf("[FATAL] Failed to bind. Maybe other programs have already occupied this port(%d). Exiting...\n",
401
402 FILE* fp;
403 char buf[256];
404 char cmdline[500];
405 sprintf(cmdline,
"/usr/sbin/ss -ap | grep %d",
m_port_to);
406 if ((fp = popen(cmdline, "r")) == NULL) {
407 printf("[WARNING] Failed to run %s\n", cmdline);
408 } else {
409 while (fgets(buf, 256, fp) != NULL) {
410 printf(
"[ERROR] Failed to bind. output of ss(port %d) : %s\n",
m_port_to, buf); fflush(stdout);
411 }
412 pclose(fp);
413 }
414
415 char temp_char[500];
416 sprintf(temp_char, "[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...",
418 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
419 exit(1);
420 }
421
422 int val1 = 0;
423 setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)sizeof(val1));
424 int backlog = 1;
425 if (listen(fd_listen, backlog) < 0) {
426 char err_buf[500];
427 sprintf(err_buf, "Failed in listen(%s). Exting...", strerror(errno));
428 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
429 exit(-1);
430 }
431
432
433
434
435 int fd_accept;
436 struct sockaddr_in sock_accept;
438 fflush(stdout);
439
440 if ((fd_accept = accept(fd_listen, reinterpret_cast<struct sockaddr*>(& (sock_accept)), &addrlen)) == 0) {
441 char err_buf[500];
442 sprintf(err_buf, "[FATAL] Failed to accept(%s). Exiting...", strerror(errno));
443 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
444 exit(-1);
445 } else {
446
447 printf("[DEBUG] Done.\n"); fflush(stdout);
448
449
450 struct timeval timeout;
451 timeout.tv_sec = 1;
452 timeout.tv_usec = 0;
453 ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)sizeof(timeout));
454 if (ret < 0) {
455 const char temp_char[100] = "[FATAL] Failed to set TIMEOUT. Exiting...";
456 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
457 exit(-1);
458 }
459 }
460
461 if (close_listen) {
462 close(fd_listen);
463 }
464
465
466
468
470 m_status.setOutputPort(ntohs(sock_listen.sin_port));
471 m_status.setOutputAddress(sock_listen.sin_addr.s_addr);
472
473 printf("Accepted. port %d address %d\n", (int)ntohs(sock_listen.sin_port), (int)sock_listen.sin_addr.s_addr); fflush(stdout);
474 }
475
476 return;
477
478}
int m_port_to
Destination port.
RunInfoBuffer m_status
Run info buffer.
std::string m_hostname_local
Destination Host.
int m_socket_send
Reciever Socket.
CprErrorMessage print_err
wrapper for B2LOG system