commit 46f2ebf533f0afd10fb2563fc98fb343abba25b5
parent 6c4769c337712a30c1617b006d89d91ee5122135
Author: dsp <dsp@2f30.org>
Date: Thu, 14 Jun 2012 15:14:22 +0300
more styling by lostd@
Diffstat:
M | nbeng.c | | | 68 | +++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 37 insertions(+), 31 deletions(-)
diff --git a/nbeng.c b/nbeng.c
@@ -12,13 +12,13 @@
#include <fcntl.h>
#include <err.h>
-/*SSL flag*/
+/* SSL flag */
int fssl = 0;
-/*connection context */
+/* connection context */
typedef struct concontxt_t {
- int clifd; /*client fd*/
- int srvfd; /*server fd*/
+ int clifd; /* client fd */
+ int srvfd; /* server fd */
struct addrinfo *clinfo;
struct addrinfo *srvinfo;
} concontxt;
@@ -26,18 +26,18 @@ typedef struct concontxt_t {
static void
usage(const char *s)
{
- fprintf(stderr,
- "usage: %s [OPTIONS] <remote-addr> <remote-port> <local-port>\n", s);
+ fprintf(stderr, "usage: %s [OPTIONS] "
+ "<remote-addr> <remote-port> <local-port>\n", s);
fprintf(stderr, " -s\tSSL on\n");
fprintf(stderr, " -h\tThis help screen\n");
}
-/*connect to in port port and store the fds in the context*/
+/* connect to in port port and store the fds in the context */
static void
-connectit(concontxt *c, char *to, char *port){
+connectit(concontxt *c, char *to, char *port) {
struct addrinfo cli_hints, *cli_servinfo, *p0;
int rv, cli_sockfd;
- if( (c == NULL) || (to == NULL) || (port == NULL))
+ if ((c == NULL) || (to == NULL) || (port == NULL))
errx(1, "conectit was passed a null arg");
memset(&cli_hints, 0, sizeof(cli_hints));
cli_hints.ai_family = AF_INET;
@@ -55,24 +55,24 @@ connectit(concontxt *c, char *to, char *port){
}
if (!p0)
errx(1, "failed to bind socket");
- /*all was ok , so we register the socket to the context*/
+ /* all was ok, so we register the socket to the context */
c->clifd = cli_sockfd;
c->clinfo = cli_servinfo;
}
int
-main(int argc,char *argv[])
+main(int argc, char *argv[])
{
- /*our input is the stdin for now, we record lines and send them*/
+ /* our input is the stdin for now, we record lines and send them */
int recfd = STDIN_FILENO;
char *prog = *argv;
int c;
fd_set rsocks;
int highsock, readsocks;
struct timeval timeout;
- char *inbuf=NULL, *linbuf=NULL;
+ char *inbuf = NULL, *linbuf = NULL;
ssize_t inbufln;
- concontxt *con = (concontxt *) malloc(sizeof(concontxt));
+ concontxt *con = (concontxt *) malloc(sizeof (concontxt));
while ((c = getopt(argc, argv, "hs")) != -1) {
switch (c) {
case 'h':
@@ -94,34 +94,37 @@ main(int argc,char *argv[])
usage(prog);
goto freex;
}
- /*connect to the host*/
+ /* connect to the host */
connectit(con, argv[0], argv[1]);
- /*XXX: this should change when we add the srv fd*/
+ /* XXX: this should change when we add the srv fd */
highsock = recfd;
- /*these are the sockets for reading (stdin + server fd)*/
- FD_ZERO(&rsocks);
- FD_SET(recfd, &rsocks);
+ /* these are the sockets for reading (stdin + server fd) */
while (1) {
FD_ZERO(&rsocks);
FD_SET(recfd, &rsocks);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
- readsocks = select(highsock + 1, &rsocks, (fd_set *) 0, (fd_set *) 0, &timeout);
- if(readsocks < 0)
+ readsocks = select(highsock + 1, &rsocks, (fd_set *)0,
+ (fd_set *)0, &timeout);
+ if (readsocks < 0) {
+ warnx("select error");
goto freex;
+ }
if (readsocks == 0) {
- /*show that we are alive*/
+ /* show that we are alive */
printf(".");
fflush(stdout);
} else {
- if (FD_ISSET(recfd, &rsocks)){
- /*deal with input data*/
+ if (FD_ISSET(recfd, &rsocks)) {
+ /* deal with input data */
inbuf = fgetln(stdin, &inbufln);
if (inbuf[inbufln - 1] == '\n')
inbuf[inbufln - 1] = '\0';
else {
- /* EOF without EOL, copy and add the NUL */
- if ((linbuf = malloc(inbufln + 1)) == NULL){
+ /* EOF without EOL,
+ copy and add the NULL */
+ if ((linbuf = malloc(inbufln + 1))
+ == NULL) {
warnx("linbuf alloc failed");
goto freex;
}
@@ -131,21 +134,24 @@ main(int argc,char *argv[])
}
if ((strncmp(inbuf, "Q", 2) == 0))
goto freex;
- printf("got msg: %s , sending it \n", inbuf);
- sendto(con->clifd, inbuf, inbufln, 0, con->clinfo->ai_addr, con->clinfo->ai_addrlen);
+ printf("got msg: %s , sending it\n", inbuf);
+ sendto(con->clifd, inbuf, inbufln, 0,
+ con->clinfo->ai_addr,
+ con->clinfo->ai_addrlen);
fflush(stdout);
- /*free the buf*/
+ /* free the buf */
if(linbuf != NULL)
free(linbuf);
}
}
}
-freex: if (con != NULL){
+freex:
+ if (con != NULL) {
close(con->clifd);
free(con);
}
if (inbuf != NULL)
free(inbuf);
- return 0;
+ return (0);
}