/*
 * Simple TCP getpeercon() test
 *
 * Paul Moore <paul.moore@hp.com>
 *
 */

/*
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <selinux/selinux.h>
#include <selinux/context.h>

#define LISTEN_QUEUE 1
#define RECV_BUF_LEN 1024

/**
 * main
 */
int main(int argc, char *argv[])
{
  int ret_val;
  int srv_sock, cli_sock;
  struct sockaddr_in srv_sock_addr, cli_sock_addr;
  socklen_t cli_sock_addr_len;
  short srv_sock_port;
  char buffer[RECV_BUF_LEN];
  security_context_t ctx;
  char *ctx_str;

  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    return 1;
  }
  srv_sock_port = atoi(argv[1]);

  printf("-> creating socket ... ");
  srv_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (srv_sock < 0) {
    printf("error: %d\n", srv_sock);
    return 1;
  } else
    printf("ok\n");

  printf("-> listening on TCP port %d ... ", srv_sock_port);
  memset(&srv_sock_addr, 0, sizeof(srv_sock_addr));
  srv_sock_addr.sin_family = AF_INET;
  srv_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  srv_sock_addr.sin_port = htons(srv_sock_port);
  ret_val = bind(srv_sock,
		 (struct sockaddr *)&srv_sock_addr,
		 sizeof(srv_sock_addr));
  if (ret_val < 0) {
    printf("bind error: %d\n", ret_val);
    return 1;
  }
  ret_val = listen(srv_sock, LISTEN_QUEUE);
  if (ret_val < 0) {
    printf("listen error: %d\n", ret_val);
    return 1;
  } else
    printf("ok\n");

  /* loop forever */
  for (;;) {
    printf("-> waiting ... ", srv_sock_port);
    fflush(stdout);
    memset(&cli_sock_addr, 0, sizeof(cli_sock_addr));
    cli_sock_addr_len = sizeof(cli_sock_addr);
    cli_sock = accept(srv_sock,
		      (struct sockaddr *)&cli_sock_addr,
		      &cli_sock_addr_len);
    if (cli_sock < 0) {
      printf("error: %d\n", cli_sock);
      continue;
    }
    ret_val = getpeercon(cli_sock, &ctx);
    if (ret_val < 0)
      ctx_str = strdup("NO_CONTEXT");
    else
      ctx_str = strdup(ctx);
    printf("connect(%s,%s)\n", inet_ntoa(cli_sock_addr.sin_addr), ctx_str);
    free(ctx_str);

    do {
      ret_val = recv(cli_sock, buffer, RECV_BUF_LEN, 0);
      if (ret_val < 0)
	printf("error: %d\n", ret_val);
      else {
	buffer[ret_val] = '\0';
	printf("%s", buffer);
      }
    } while (ret_val > 0);
    close(cli_sock);
    printf("-> connection closed\n");
  }

  return 0;
}
