Last active
December 24, 2023 00:52
-
-
Save zdimension/183966c604a0a5c64bf1eba4402705a6 to your computer and use it in GitHub Desktop.
libserver / libclient (softsec)
This file contains 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 <netinet/ip.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <stdio.h> | |
const char leng[] = "1025"; | |
/* Sends a *null-terminated* string to the specified port. | |
* Returns 0 on success, -1 on failure. | |
*/ | |
int sndmsg(char msg[1024], int port) | |
{ | |
struct sockaddr_in addr; | |
char dest[1032]; | |
strncpy(dest, msg, 1024); | |
dest[1024] = '\0'; | |
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); | |
if ( fd >= 0 ) | |
{ | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(port); | |
if ( inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) > 0 ) | |
{ | |
if ( connect(fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) >= 0 ) | |
{ | |
send(fd, leng, sizeof(leng), 0); | |
send(fd, dest, strlen(dest), 0); | |
close(fd); | |
return 0; | |
} | |
else | |
{ | |
puts("\nConnection Failed "); | |
return -1; | |
} | |
} | |
else | |
{ | |
puts("\nInvalid address/ Address not supported "); | |
return -1; | |
} | |
} | |
else | |
{ | |
puts("\n Socket creation error "); | |
return -1; | |
} | |
} |
This file contains 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 <stdint.h> | |
#include <sys/socket.h> | |
#include <stdbool.h> | |
#include <netinet/ip.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
int server_fd; | |
struct sockaddr_in address; | |
/* Starts the server on the specified port | |
* Exits on failure. | |
* Returns 0 on success. | |
*/ | |
int startserver(uint16_t port) | |
{ | |
server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); | |
if ( server_fd < 0 ) | |
{ | |
perror("socket failed"); | |
exit(1); | |
} | |
int reuseport = true; | |
if ( setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(reuseport)) ) | |
{ | |
perror("setsockopt"); | |
exit(1); | |
} | |
address.sin_family = AF_INET; | |
address.sin_addr.s_addr = 0; | |
address.sin_port = htons(port); | |
if ( bind(server_fd, (const struct sockaddr *)&address, sizeof(struct sockaddr_in)) < 0 ) | |
{ | |
perror("bind failed"); | |
exit(1); | |
} | |
int result = listen(server_fd, 3); | |
if ( result < 0 ) | |
{ | |
perror("listen"); | |
exit(1); | |
} | |
return result; | |
} | |
ssize_t valread; | |
int new_socket; | |
socklen_t addrlen = sizeof(struct sockaddr_in); | |
/* Blocks until a message is received and write it to the buffer. | |
* The message must be a null-terminated string at most 1024 characters long (null character excluded). | |
* Exits on failure. | |
* Returns 0 on success. | |
*/ | |
int getmsg(char msg_read[1024]) | |
{ | |
char buf[1024]; | |
memset(buf, 0, sizeof(buf)); | |
new_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen); | |
if ( new_socket < 0 ) | |
{ | |
perror("accept"); | |
exit(1); | |
} | |
valread = read(new_socket, buf, 5); | |
int len = atoi(buf); | |
valread = read(new_socket, buf, len); | |
close(new_socket); | |
strcpy(msg_read, buf); | |
return 0; | |
} | |
/* Stops the server. | |
* Returns 0. | |
*/ | |
int stopserver() | |
{ | |
close(server_fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment