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