Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 4010aa4

Browse files
author
Gunnar Guðvarðarson
committed
Add getopt for parsing arguments, add listen address
1 parent 6b015ba commit 4010aa4

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

proxy.c

+59-15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define _GNU_SOURCE
2929
#include <assert.h>
3030
#include <errno.h>
31+
#include <getopt.h>
3132
#include <inttypes.h>
3233
#include <netdb.h>
3334
#include <netinet/in.h>
@@ -172,7 +173,7 @@ static bool proxy_client_process (struct proxy_connection *conn);
172173
static void *proxy_client_work (void *ud);
173174

174175
/* proxy functions */
175-
static int proxy_init (struct proxy *proxy, int port);
176+
static int proxy_init (struct proxy *proxy, const char *listen_address, const char *port);
176177
static void proxy_free (struct proxy *proxy);
177178
static void proxy_accept (struct proxy *proxy);
178179

@@ -1212,9 +1213,11 @@ proxy_client_work (void *ud)
12121213
* connections.
12131214
*/
12141215
static int
1215-
proxy_init (struct proxy *proxy, int port)
1216+
proxy_init (struct proxy *proxy, const char *listen_address, const char *port)
12161217
{
1217-
struct sockaddr_in6 addr;
1218+
struct addrinfo hints;
1219+
struct addrinfo *res = NULL;
1220+
int gai;
12181221

12191222
proxy_log_init (&proxy->log);
12201223

@@ -1237,12 +1240,26 @@ proxy_init (struct proxy *proxy, int port)
12371240
return -1;
12381241
}
12391242

1240-
proxy->socket = socket (PF_INET6, SOCK_STREAM, 0);
1243+
memset (&hints, 0, sizeof (hints));
1244+
hints.ai_family = AF_UNSPEC;
1245+
hints.ai_socktype = SOCK_STREAM;
1246+
1247+
gai = getaddrinfo (listen_address, port, &hints, &res);
1248+
1249+
if (gai != 0)
1250+
{
1251+
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (gai));
1252+
1253+
return -1;
1254+
}
1255+
1256+
proxy->socket = socket (res->ai_family, SOCK_STREAM, 0);
12411257

12421258
if (proxy->socket == -1)
12431259
{
12441260
perror ("socket");
12451261

1262+
freeaddrinfo (res);
12461263
proxy_log_free (&proxy->log);
12471264

12481265
if (pthread_attr_destroy (&proxy->attr) != 0)
@@ -1259,6 +1276,7 @@ proxy_init (struct proxy *proxy, int port)
12591276
{
12601277
perror("setsockopt");
12611278

1279+
freeaddrinfo (res);
12621280
proxy_log_free (&proxy->log);
12631281

12641282
if (pthread_attr_destroy (&proxy->attr) != 0)
@@ -1274,16 +1292,11 @@ proxy_init (struct proxy *proxy, int port)
12741292
return -1;
12751293
}
12761294

1277-
memset (&addr, 0, sizeof (addr));
1278-
1279-
addr.sin6_family = AF_INET6;
1280-
addr.sin6_port = htons (port);
1281-
addr.sin6_addr = in6addr_any;
1282-
1283-
if (bind (proxy->socket, (struct sockaddr *) &addr, sizeof (addr)) == -1)
1295+
if (bind (proxy->socket, res->ai_addr, res->ai_addrlen) == -1)
12841296
{
12851297
perror ("bind");
12861298

1299+
freeaddrinfo (res);
12871300
proxy_log_free (&proxy->log);
12881301

12891302
if (pthread_attr_destroy (&proxy->attr) != 0)
@@ -1299,6 +1312,8 @@ proxy_init (struct proxy *proxy, int port)
12991312
return -1;
13001313
}
13011314

1315+
freeaddrinfo (res);
1316+
13021317
if (listen (proxy->socket, SOMAXCONN) == -1)
13031318
{
13041319
perror ("listen");
@@ -1390,16 +1405,45 @@ proxy_accept (struct proxy *proxy)
13901405
int main(int argc, char **argv)
13911406
{
13921407
struct proxy proxy;
1408+
const char *listen_address = "::";
1409+
const char *listen_port = "8080";
13931410

13941411
/* Check arguments */
1395-
if (argc != 2)
1412+
while (1)
13961413
{
1397-
fprintf (stderr, "Usage: %s <port number>\n", argv[0]);
1414+
int option_index = 0;
13981415

1399-
return EXIT_FAILURE;
1416+
static const struct option long_options[] = {
1417+
{ "listen", required_argument, 0, 'l' },
1418+
{ "port", required_argument, 0, 'p' },
1419+
{ "help", no_argument, 0, 'h' },
1420+
{ 0, 0, 0, 0 }
1421+
};
1422+
1423+
int c = getopt_long (argc, argv, "l:p:h", long_options, &option_index);
1424+
1425+
if (c == -1)
1426+
break;
1427+
1428+
switch (c)
1429+
{
1430+
case 'l':
1431+
listen_address = optarg;
1432+
break;
1433+
case 'p':
1434+
listen_port = optarg;
1435+
break;
1436+
default:
1437+
printf ("Usage: %s [options]\n", argv[0]);
1438+
printf ("Options:\n");
1439+
printf ("\t-l, --listen Listen address (::)\n");
1440+
printf ("\t-p, --port Listen port (8080)\n");
1441+
1442+
return EXIT_SUCCESS;
1443+
}
14001444
}
14011445

1402-
if (proxy_init (&proxy, atoi (argv[1])) != 0)
1446+
if (proxy_init (&proxy, listen_address, listen_port) != 0)
14031447
{
14041448
return EXIT_FAILURE;
14051449
}

0 commit comments

Comments
 (0)