File: server.c

package info (click to toggle)
dcap 2.47.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: ansic: 14,604; makefile: 312; python: 75; sh: 58
file content (125 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * $Id: server.c,v 1.1 2002-10-14 10:31:36 cvs Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


#include "gssTunnel.h"

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif

int
create_socket(int *dataFd, int *cbPort)
{

	struct sockaddr_in me, him;
#if defined(sun) || defined(sgi) || defined (__alpha)
	size_t          addrSize;
#else
	socklen_t       addrSize;
#endif
	int             bindResult;
	int             newFd;
	struct hostent *he;

	char           *hostName;



	hostName = (char *) malloc(MAXHOSTNAMELEN + 1);
	if (hostName == NULL) {
		*dataFd = -1;
		return -1;

	}
	hostName[MAXHOSTNAMELEN] = '\0';

	if (gethostname(hostName, MAXHOSTNAMELEN) < 0) {
		printf("Failed to get local host name.\n");
		*dataFd = -1;
		return -1;
	}
	/* trying to get full fully-qualified domain name. */
	he = (struct hostent *) gethostbyname((const char *) hostName);

	if (he != NULL) {
		/* if we successed to get it, let use it */
		free(hostName);
		hostName = (char *) strdup(he->h_name);
	} else {
		printf("Unable to get FQDN for host %s.\n", hostName);
	}

	printf("Setting hostname to %s.\n", hostName);


	*dataFd = socket(AF_INET, SOCK_STREAM, 0);
	if (*dataFd < 0) {
		return *dataFd;
	}
	memset((char *) &me, 0, sizeof(me));
	me.sin_family = AF_INET;
	me.sin_addr.s_addr = htonl(INADDR_ANY);
	me.sin_port = htons(*cbPort);

	addrSize = sizeof(me);
	bindResult = bind(*dataFd, (struct sockaddr *) & me, addrSize);
	if (bindResult < 0) {
		close(*dataFd);
		*dataFd = -1;
		return -1;
	}
	if (*cbPort == 0) {
		/* get our TCP port number */
#if defined(sun) || defined(sgi) || defined(__alpha)
		getsockname(*dataFd, (struct sockaddr *) & me, (int *) &addrSize);
#else
		getsockname(*dataFd, (struct sockaddr *) & me, (socklen_t *) & addrSize);
#endif
		*cbPort = ntohs(me.sin_port);
	}
	listen(*dataFd, 512);
	addrSize = sizeof(him);
	newFd = accept(*dataFd, (struct sockaddr *) & him, (int *) &addrSize);
	close(*dataFd);
	*dataFd = newFd;
	return *cbPort;

}



int
main(int argc, char *argv[])
{
	int             rc;
	int             port;
	int             sock;

	if (argc != 2) {
		printf("Usage %s <port>\n", argv[0]);
		exit(1);
	}
	port = atoi(argv[1]);

	create_socket(&sock, &port);


	gss_check(sock );

	close(sock);
	return 0;

}