Skip to content

Commit

Permalink
Minor IPv6 tweaks and credit to original author
Browse files Browse the repository at this point in the history
  • Loading branch information
samhocevar committed Oct 19, 2021
1 parent a7355c4 commit d5c7775
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

\section news0_99_beta20 Changes between 0.99.beta20 and 0.99.beta19

- IPv6 support in cacaserver
- fixed a bug from 2004 that caused PDF documentation generation to fail
- memory allocation functions are now more robust
- numerous fixes for memory leaks and invalid memory accesses:
Expand Down
1 change: 1 addition & 0 deletions THANKS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Gildas Bazin <[email protected]> - win32 driver improvements
- Jari Komppa <jari.komppa at gmail> - win32 speed improvements
- Bastian Märkisch <[email protected]> - bugfixes and win32 driver improvements
- Denis Fondras <ledeuns at github> - IPv6 support in cacaserver

\section thanks2 Reused code

Expand Down
23 changes: 11 additions & 12 deletions src/cacaserver.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* cacaserver Colour ASCII-Art library
* Copyright © 2006-2021 Sam Hocevar <[email protected]>
* 2016 Denis Fondras <ledeuns at github>
* 2006 Jean-Yves Lamoureux <[email protected]>
* All Rights Reserved
*
Expand Down Expand Up @@ -100,7 +101,7 @@ struct client
struct sock {
int sockfd;
struct sockaddr_in my_addr;
};
};

struct server
{
Expand All @@ -125,7 +126,7 @@ struct server
void (*sigpipe_handler)(int);
};

void print_ip(struct sockaddr *ai);
void fprint_ip(FILE *stream, struct sockaddr *ai);
static void manage_connections(struct server *server, int sockfd);
static int send_data(struct server *server, struct client *c);
ssize_t nonblock_write(int fd, void *buf, size_t len);
Expand Down Expand Up @@ -209,7 +210,7 @@ int main(void)
server->socks[server->sock_count].sockfd = fd;
server->sock_count++;
fprintf(stderr, "listening on ");
print_ip(res->ai_addr);
fprint_ip(stderr, res->ai_addr);
fprintf(stderr, "\n");
}
freeaddrinfo(ai);
Expand Down Expand Up @@ -337,23 +338,21 @@ int main(void)
* XXX: The following functions are local
*/

void print_ip(struct sockaddr *ai)
void fprint_ip(FILE *stream, struct sockaddr *ai)
{
char buffer[INET6_ADDRSTRLEN];
socklen_t len = sizeof(struct sockaddr_in6);

if (ai->sa_family == AF_INET)
len = sizeof(struct sockaddr_in);

int err = getnameinfo(ai, len, buffer, sizeof(buffer), NULL, 0,
NI_NUMERICHOST);
if (err != 0) {
fprintf(stderr, "n/a");
}
fprintf(stderr, "%s",buffer);
int err = getnameinfo(ai, len, buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST);
if (err != 0)
fprintf(stream, "n/a");
else
fprintf(stream, "%s", buffer);
}


static void manage_connections(struct server *server, int sockfd)
{
int fd, flags;
Expand All @@ -365,7 +364,7 @@ static void manage_connections(struct server *server, int sockfd)
return;

fprintf(stderr, "[%i] connected from ", fd);
print_ip((struct sockaddr*)&remote_addr);
fprint_ip(stderr, (struct sockaddr*)&remote_addr);
fprintf(stderr, "\n");

/* Non blocking socket */
Expand Down

0 comments on commit d5c7775

Please sign in to comment.