Last active
July 14, 2024 11:51
-
-
Save agebhar1/0896d0d63fbbfeef4c0d04414f2470cf to your computer and use it in GitHub Desktop.
nmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CFLAGS := -Wall -Werror | |
| server: server.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <arpa/inet.h> | |
| #include <errno.h> | |
| #include <libgen.h> | |
| #include <netdb.h> | |
| #include <netinet/in.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/socket.h> | |
| #include <unistd.h> | |
| int main(int argc, char const *argv[]) | |
| { | |
| if (argc < 2) | |
| { | |
| fprintf(stderr, "error: too few arguments\n"); | |
| fprintf(stderr, "usage: %s port\n", basename((char *)argv[0])); | |
| exit(1); | |
| } | |
| int port = atoi(argv[1]); | |
| if (((port == 0) && (argv[1][0] != '0')) || (port < 0) || (port > 65536)) | |
| { | |
| fprintf(stderr, "error: invalid port %s\n", argv[1]); | |
| exit(1); | |
| } | |
| int fd, peer_fd; | |
| struct sockaddr_in addr, bind_addr, peer_addr; | |
| socklen_t sockaddr_len = sizeof(struct sockaddr_in); | |
| char buf[1024]; | |
| char peer_name[INET6_ADDRSTRLEN + 1 + 5]; // <ip>:65536 | |
| if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) | |
| { | |
| perror("socket"); | |
| exit(1); | |
| } | |
| memset(&addr, 0, sizeof(addr)); | |
| addr.sin_family = AF_INET; | |
| addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
| addr.sin_port = htons(port); | |
| if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | |
| { | |
| perror("bind"); | |
| exit(1); | |
| } | |
| if (listen(fd, 10) == -1) | |
| { | |
| perror("listen"); | |
| exit(1); | |
| } | |
| if (getsockname(fd, (struct sockaddr *)&bind_addr, &sockaddr_len) == -1) | |
| { | |
| perror("getsockname"); | |
| exit(1); | |
| } | |
| fprintf(stdout, "listen of port: %d\n", ntohs(bind_addr.sin_port)); | |
| fflush(stdout); | |
| while (1) | |
| { | |
| if ((peer_fd = accept(fd, (struct sockaddr *)NULL, NULL)) == -1) | |
| { | |
| perror("accept"); | |
| exit(1); | |
| } | |
| memset(&peer_name, 0, sizeof(peer_name)); | |
| if (getpeername(peer_fd, (struct sockaddr *)&peer_addr, &sockaddr_len) == 0) | |
| { | |
| switch (peer_addr.sin_family) | |
| { | |
| case AF_INET: | |
| { | |
| inet_ntop(AF_INET, &peer_addr.sin_addr, peer_name, INET_ADDRSTRLEN); | |
| break; | |
| } | |
| case AF_INET6: | |
| { | |
| inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)&peer_addr)->sin6_addr), peer_name, INET6_ADDRSTRLEN); | |
| break; | |
| } | |
| default: | |
| break; | |
| } | |
| } | |
| else | |
| { | |
| sprintf(peer_name, "[unknown]"); | |
| } | |
| fprintf(stdout, "accepted peer: %s:%d\n", peer_name, ntohs(peer_addr.sin_port)); | |
| fflush(stdout); | |
| ssize_t count = 0; | |
| do | |
| { | |
| count = read(peer_fd, buf, sizeof(buf)); | |
| if (count == 0) | |
| { | |
| fprintf(stdout, "peer closed stdin\n"); | |
| fflush(stdout); | |
| } | |
| if (count > 0) | |
| { | |
| fprintf(stdout, "read %ld byte(s) from peer: [", count); | |
| { | |
| fprintf(stdout, "%d", buf[0]); | |
| for (int i = 1; i < count; i++) | |
| { | |
| fprintf(stdout, ",%hhu", buf[i]); | |
| } | |
| fprintf(stdout, "]\n"); | |
| } | |
| fflush(stdout); | |
| } | |
| else | |
| { | |
| fprintf(stderr, "errno: %d, strerror: %s\n", errno, strerror(errno)); | |
| } | |
| } while (count > 0); | |
| if (close(peer_fd) == -1) | |
| { | |
| perror("close"); | |
| exit(1); | |
| } | |
| fprintf(stdout, "closed\n"); | |
| fflush(stdout); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment