1 /*****************************************************************************
2 * Copyright 2005 Daniel Ferullo *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
16 *****************************************************************************/
19 * @file natblaster_helper.c
20 * @author Daniel Ferullo (ferullo@cmu.edu)
22 * @brief contains entrypoint for a helper into natblaster
25 #include "natblaster_helper.h"
29 #include "errorcodes.h"
30 #include "berkeleyapi.h"
32 #include "helpercon.h"
34 int natblaster_server(port_t listen_port
) {
40 struct sockaddr_in peer_con
;
43 /* The return type is "int" and the return codes are "errorcodes". Even
44 * though there aren't strictly the same type, I know they are, and using
45 * the errorcodes makes the code more readable and debug-able. */
47 CHECK_FAILED(bindSocket(listen_port
,&listen_sd
),ERROR_BIND
);
49 /* initalize the list for connection information */
50 CHECK_FAILED(connlist_init(&list
),ERROR_INIT
);
52 if (listen(listen_sd
,5)!=0)
53 return ERROR_TCP_LISTEN
;
55 peer_con_size
= sizeof(peer_con
);
58 this_sd
= accept(listen_sd
,(struct sockaddr
*)&peer_con
,
60 data
.ip
= peer_con
.sin_addr
.s_addr
;
61 data
.port
= peer_con
.sin_port
;
62 DEBUG(DBG_NETWORK
,"NETWORK:recieved a connection!\n");
63 DEBUG(DBG_LIST
, "LIST:list size: %d\n",
64 connlist_count(&list
));
65 CHECK_FAILED(create_new_handler(&list
,&data
,this_sd
),ERROR_1
);
69 /* should never happen */