root / trunk / code / projects / colonet / ColonetServer / initialization.c @ 11
History | View | Annotate | Download (3.56 KB)
1 | 11 | emarinel | /**
|
---|---|---|---|
2 | * @file initialization.c
|
||
3 | *
|
||
4 | * @author Jason Knichel
|
||
5 | * @author Eugene Marinelli
|
||
6 | */
|
||
7 | |||
8 | #include <sys/socket.h> |
||
9 | #include <sys/types.h> |
||
10 | #include <netinet/in.h> |
||
11 | #include <string.h> |
||
12 | #include <fcntl.h> |
||
13 | #include <unistd.h> |
||
14 | #include <stdio.h> |
||
15 | #include <stdlib.h> |
||
16 | |||
17 | #include "includes/initialization.h" |
||
18 | #include "includes/client.h" |
||
19 | #include "includes/options.h" |
||
20 | #include "includes/Logging.h" |
||
21 | |||
22 | extern int listenSocket; |
||
23 | extern Log * logger;
|
||
24 | |||
25 | int initializeServer(int argc, char** argv) |
||
26 | { |
||
27 | printf("Initializing Server...\n");
|
||
28 | |||
29 | parseCmdLine(argc, argv); |
||
30 | |||
31 | //TODO: make it so the log file name is passed in on command line and default it to something if it isn't
|
||
32 | logger = new Log("logFile.txt");
|
||
33 | |||
34 | if (initConnection(optionsG.listen_port) < 0) |
||
35 | return -1; |
||
36 | |||
37 | if (initWireless() < 0) { |
||
38 | fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
|
||
39 | return -1; |
||
40 | } |
||
41 | |||
42 | return 0; |
||
43 | } |
||
44 | |||
45 | /*
|
||
46 | int parseCommandLine(int argc, char * argv[], commandParams_t * params)
|
||
47 | {
|
||
48 | printf("Parsing Command Line...\n");
|
||
49 | if (!params)
|
||
50 | return -1;
|
||
51 | |||
52 | //if no command line parameters were specified, set to defaults
|
||
53 | if (argc == 1) {
|
||
54 | printf("No port was specified for listening for client connections."
|
||
55 | " Defaulting to %d\n", DEFAULTPORT);
|
||
56 | params->listenPort = DEFAULTPORT;
|
||
57 | }
|
||
58 | |||
59 | if (!argv)
|
||
60 | return -1;
|
||
61 | |||
62 | int i;
|
||
63 | for (i = 1; i < argc; i++) {
|
||
64 | char * temp = argv[i];
|
||
65 | if (temp[0] != '-')
|
||
66 | {
|
||
67 | printUsage(argv[0]);
|
||
68 | return -1;
|
||
69 | }
|
||
70 | |||
71 | switch(temp[1])
|
||
72 | {
|
||
73 | case 'h': printUsage(argv[0]);
|
||
74 | break;
|
||
75 | case 'p':
|
||
76 | {
|
||
77 | if (i >= argc-1)
|
||
78 | {
|
||
79 | printUsage(argv[0]);
|
||
80 | return -1;
|
||
81 | }
|
||
82 | i++;
|
||
83 | char * portString = argv[i];
|
||
84 | char * endptr = NULL;
|
||
85 | int port = strtol(portString, &endptr, 10);
|
||
86 | if (*endptr != '\0')
|
||
87 | {
|
||
88 | printf("Invalid port specified...%s, %d\n", endptr, port);
|
||
89 | return -1;
|
||
90 | }
|
||
91 | if (port < SMALLEST_POSS_LISTEN_PORT)
|
||
92 | {
|
||
93 | printf("You cannot listen on a port less than %d.\n",
|
||
94 | SMALLEST_POSS_LISTEN_PORT);
|
||
95 | return -1;
|
||
96 | }
|
||
97 | params->listenPort = port;
|
||
98 | printf("Setting port to listen on to %d.\n", params->listenPort);
|
||
99 | }
|
||
100 | break;
|
||
101 | default: printUsage(argv[0]);
|
||
102 | return -1;
|
||
103 | break;
|
||
104 | }
|
||
105 | }
|
||
106 | |||
107 | return 0;
|
||
108 | }
|
||
109 | */
|
||
110 | |||
111 | int initConnection(int port) |
||
112 | { |
||
113 | printf("Initializing connection that will be used to listen for "
|
||
114 | "clients...\n");
|
||
115 | int opts = 1; |
||
116 | struct sockaddr_in my_addr;
|
||
117 | |||
118 | //get a socket fd
|
||
119 | if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
||
120 | printf("\tThere was an error creating a socket\n");
|
||
121 | return -1; |
||
122 | } |
||
123 | |||
124 | //set up the address struct
|
||
125 | memset(&my_addr,'\0',sizeof(my_addr)); |
||
126 | my_addr.sin_family = AF_INET; |
||
127 | my_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
||
128 | my_addr.sin_port = htons(port); |
||
129 | |||
130 | setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
|
||
131 | |||
132 | //get the current socket options
|
||
133 | if ((opts = fcntl(listenSocket, F_GETFL)) < 0) { |
||
134 | printf("\tThere was an error getting the socket options.\n");
|
||
135 | return -1; |
||
136 | } |
||
137 | |||
138 | //set the socket to non blocking
|
||
139 | opts = (opts | O_NONBLOCK); |
||
140 | if (fcntl(listenSocket, F_SETFL, opts) < 0) { |
||
141 | printf("\tThere was an error setting the socket to be non blocking.\n");
|
||
142 | return -1; |
||
143 | } |
||
144 | |||
145 | //bind the socket to listen on the specified port
|
||
146 | if (bind(listenSocket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) { |
||
147 | printf("\tThere was an error binding the socket\n");
|
||
148 | return -1; |
||
149 | } |
||
150 | |||
151 | return 0; |
||
152 | } |