linux version yippee
This commit is contained in:
parent
37f4a98791
commit
71fff37a40
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1 @@
|
||||||
*.exe
|
*.exe
|
||||||
|
|
32
README.md
32
README.md
|
@ -1,12 +1,20 @@
|
||||||
# meownet 😺
|
# meownet 😺
|
||||||
an awesome web server that meows at you
|
an awesome web server that meows at you
|
||||||
|
|
||||||
## okay some more detail
|
## okay some more detail
|
||||||
i built this in c using `winsock2`. why? because i wanted to learn sockets! and my windows machine was the closest subject at the time.
|
i originally built this in c using `winsock2`. why? because i wanted to learn sockets! and my windows machine was the closest subject at the time. now this runs the regular linux socket API by default (i left the old windows-specific code in `src/windows.c` if you want to use that!)
|
||||||
|
|
||||||
**Q:** is it very good? **A:** no! but i had fun making it, so idrc
|
**Q:** is it very good? **A:** no! but i had fun making it, so idrc
|
||||||
|
|
||||||
a fun little foire into getting even more familiar with how web services work. how fun!!
|
a fun little foire into getting even more familiar with how web services work. how fun!!
|
||||||
|
|
||||||
### building
|
### building
|
||||||
oh yeah you can compile this with `gcc` and a little `gcc -o ./meow.exe ./src/main.c -lws2_32`. i might make this work for mac/linux at some point if i somehow get even more bored :]
|
oh yeah you can compile this with `gcc` and a little
|
||||||
|
```sh
|
||||||
|
# linux/macos (untested on macos)
|
||||||
|
gcc -o ./meow ./src/main.c
|
||||||
|
```
|
||||||
|
```powershell
|
||||||
|
# windows
|
||||||
|
gcc -o ./meow.exe ./src/windows.c -lws2_32
|
||||||
|
```
|
||||||
|
|
301
src/main.c
301
src/main.c
|
@ -1,166 +1,135 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <winsock2.h>
|
#include <stdlib.h>
|
||||||
#include <ws2tcpip.h>
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
#define BUFFER_SIZE (1024 * 1024)
|
#include <unistd.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
void simple_http_ok(char *response, size_t *response_len);
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
int main() {
|
|
||||||
|
#define BUFFER_SIZE (1024 * 1024)
|
||||||
printf("hello world!~\n");
|
|
||||||
|
void simple_http_ok(char *response, size_t *response_len);
|
||||||
int result = 0;
|
|
||||||
WSADATA wsaData = {0};
|
int main() {
|
||||||
struct sockaddr_in service;
|
|
||||||
|
printf("hello world!~\n");
|
||||||
const char* address = "127.0.0.1";
|
|
||||||
int port = 8080;
|
int result = 0;
|
||||||
|
|
||||||
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
const char* address = "0.0.0.0";
|
||||||
if (result != 0) {
|
int port = 8080;
|
||||||
printf("WSAStartup failed: %d\n", result);
|
|
||||||
return 1;
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
}
|
if (sock == -1) {
|
||||||
|
fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
|
||||||
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
return 1;
|
||||||
if (sock == INVALID_SOCKET) {
|
}
|
||||||
printf("failed to create socket! (%d)\n", WSAGetLastError());
|
printf("socket created successfully!\n");
|
||||||
WSACleanup();
|
|
||||||
return 1;
|
struct in_addr in_address;
|
||||||
}
|
inet_aton(address, &in_address);
|
||||||
|
struct sockaddr_in service = {AF_INET, htons(port), in_address};
|
||||||
printf("socket created successfully!\n");
|
if (bind(sock, (struct sockaddr*) &service, sizeof(struct sockaddr_in)) == -1) {
|
||||||
|
close(sock);
|
||||||
service.sin_family = AF_INET;
|
fprintf(stderr, "failed to bind to %s:%d: %s\n", address, port, strerror(errno));
|
||||||
service.sin_addr.s_addr = inet_addr(address);
|
return 1;
|
||||||
service.sin_port = htons(port);
|
}
|
||||||
|
|
||||||
if (bind(sock, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) {
|
if (listen(sock, 1) == -1) {
|
||||||
closesocket(sock);
|
close(sock);
|
||||||
printf("failed to bind to %s:%d: %ld\n", address, port, WSAGetLastError());
|
fprintf(stderr, "failed to bind to %s:%d: %s\n", address, port, strerror(errno));
|
||||||
WSACleanup();
|
return 1;
|
||||||
return 1;
|
}
|
||||||
}
|
|
||||||
|
printf("now listening on %s:%d!\n", address, port);
|
||||||
/*
|
|
||||||
result = connect(sock, (SOCKADDR*) &service, sizeof(client_service));
|
char running = 1;
|
||||||
if (result == SOCKET_ERROR) {
|
|
||||||
closesocket(sock);
|
do {
|
||||||
printf("unable to connect to the server: %ld\n", WSAGetLastError());
|
int accept_sock = accept(sock, NULL, NULL);
|
||||||
WSACleanup();
|
if (accept_sock == -1) {
|
||||||
return 1;
|
close(sock);
|
||||||
}
|
fprintf(stderr, "failed to accept socket connection: %s\n", strerror(errno));
|
||||||
*/
|
return 1;
|
||||||
|
} else {
|
||||||
if (listen(sock, 1) == SOCKET_ERROR) {
|
printf("client connected!\n");
|
||||||
closesocket(sock);
|
}
|
||||||
printf("failed to bind to %s:%d: %ld\n", address, port, WSAGetLastError());
|
|
||||||
WSACleanup();
|
char recvbuf[512];
|
||||||
return 1;
|
int received = 0;
|
||||||
}
|
do {
|
||||||
|
received = recv(accept_sock, recvbuf, 512, 0);
|
||||||
/*
|
char end[] = "\r\n\r\n";
|
||||||
char address[INET_ADDRSTRLEN];
|
|
||||||
InetNtop(AF_INET, &(service.sin_addr.s_addr), address, INET_ADDRSTRLEN);
|
if (received > 0) {
|
||||||
ntohs(service.sin_port)
|
printf("bytes received: %d\n", received);
|
||||||
*/
|
printf("< ");
|
||||||
printf("now listening on %s:%d!\n", address, port);
|
for (int i = 0; i < received; i++) {
|
||||||
|
printf("%c", recvbuf[i]);
|
||||||
char running = 1;
|
if (recvbuf[i] == '\n') {
|
||||||
|
printf("< ");
|
||||||
do {
|
}
|
||||||
SOCKET accept_sock = accept(sock, NULL, NULL);
|
}
|
||||||
if (accept_sock == INVALID_SOCKET) {
|
|
||||||
closesocket(sock);
|
if (received >= 4) {
|
||||||
printf("failed to accept socket connection: %ld\n", WSAGetLastError());
|
char matches = 1;
|
||||||
WSACleanup();
|
for (int i = 0; i < 4; i++) {
|
||||||
return 1;
|
if (recvbuf[received - 4 + i] != end[i]) {
|
||||||
} else {
|
matches = 0;
|
||||||
printf("client connected!\n");
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
char recvbuf[512];
|
if (matches) break;
|
||||||
int received = 0;
|
}
|
||||||
do {
|
} else if (received == 0) {
|
||||||
received = recv(accept_sock, recvbuf, 512, 0);
|
printf("connection closed.\n");
|
||||||
char end[] = "\r\n\r\n";
|
} else {
|
||||||
char lastfour[4];
|
printf("recv failed: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
if (received > 0) {
|
} while (received > 0);
|
||||||
printf("bytes received: %d\n", received);
|
|
||||||
printf("< ");
|
char *response = (char *)malloc(BUFFER_SIZE * 2 * sizeof(char));
|
||||||
for (int i = 0; i < received; i++) {
|
size_t response_len;
|
||||||
printf("%c", recvbuf[i]);
|
simple_http_ok(response, &response_len);
|
||||||
if (recvbuf[i] == '\n') {
|
|
||||||
printf("< ");
|
send(accept_sock, response, response_len, 0);
|
||||||
}
|
|
||||||
for (int i = 0; i < 3; i++) {
|
free(response);
|
||||||
lastfour[i] = lastfour[i + 1];
|
|
||||||
}
|
result = close(accept_sock);
|
||||||
lastfour[3] = recvbuf[i];
|
if (result == -1) {
|
||||||
}
|
fprintf(stderr, "failed to close socket properly: %s\n", strerror(errno));
|
||||||
|
close(sock);
|
||||||
char matches = 1;
|
return 1;
|
||||||
|
}
|
||||||
for (int i = 0; i < 4; i++) {
|
} while (running);
|
||||||
if (lastfour[i] != end[i]) {
|
|
||||||
matches = 0;
|
// close responsibly! <3
|
||||||
break;
|
result = close(sock);
|
||||||
}
|
if (result == -1) {
|
||||||
}
|
fprintf(stderr, "failed to close socket properly: %s\n", strerror(errno));
|
||||||
|
return 1;
|
||||||
if (matches) break;
|
}
|
||||||
} else if (received == 0) {
|
|
||||||
printf("connection closed.\n");
|
return 0;
|
||||||
} else {
|
|
||||||
printf("recv failed: %d\n", WSAGetLastError());
|
}
|
||||||
}
|
|
||||||
} while (received > 0);
|
void simple_http_ok(char *response, size_t *response_len) {
|
||||||
|
// build header
|
||||||
char *response = (char *)malloc(BUFFER_SIZE * 2 * sizeof(char));
|
char *header = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
||||||
size_t response_len;
|
snprintf(header, BUFFER_SIZE,
|
||||||
simple_http_ok(response, &response_len);
|
"HTTP/1.1 200 OK\r\n"
|
||||||
|
"Content-Type: text/plain; charset=utf-8\r\n"
|
||||||
send(accept_sock, response, response_len, 0);
|
"Content-Length: 10\r\n"
|
||||||
|
"\r\n"
|
||||||
free(response);
|
"meow 😺\n");
|
||||||
|
|
||||||
result = closesocket(accept_sock);
|
*response_len = 0;
|
||||||
if (result == SOCKET_ERROR) {
|
memcpy(response, header, strlen(header));
|
||||||
printf("failed to close socket properly: %d\n", WSAGetLastError());
|
*response_len += strlen(header);
|
||||||
closesocket(sock);
|
|
||||||
WSACleanup();
|
free(header);
|
||||||
return 1;
|
}
|
||||||
}
|
|
||||||
} while (running);
|
|
||||||
|
|
||||||
// close responsibly! <3
|
|
||||||
result = closesocket(sock);
|
|
||||||
if (result == SOCKET_ERROR) {
|
|
||||||
printf("failed to close socket properly: %d\n", WSAGetLastError());
|
|
||||||
WSACleanup();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void simple_http_ok(char *response, size_t *response_len) {
|
|
||||||
// build header
|
|
||||||
char *header = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
|
||||||
snprintf(header, BUFFER_SIZE,
|
|
||||||
"HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Type: text/plain; charset=utf-8\r\n"
|
|
||||||
"Content-Length: 9\r\n"
|
|
||||||
"\r\n"
|
|
||||||
"meow 😺");
|
|
||||||
|
|
||||||
*response_len = 0;
|
|
||||||
memcpy(response, header, strlen(header));
|
|
||||||
*response_len += strlen(header);
|
|
||||||
|
|
||||||
free(header);
|
|
||||||
}
|
|
||||||
|
|
166
src/windows.c
Normal file
166
src/windows.c
Normal file
|
@ -0,0 +1,166 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE (1024 * 1024)
|
||||||
|
|
||||||
|
void simple_http_ok(char *response, size_t *response_len);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
printf("hello world!~\n");
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
WSADATA wsaData = {0};
|
||||||
|
struct sockaddr_in service;
|
||||||
|
|
||||||
|
const char* address = "127.0.0.1";
|
||||||
|
int port = 8080;
|
||||||
|
|
||||||
|
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
if (result != 0) {
|
||||||
|
printf("WSAStartup failed: %d\n", result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (sock == INVALID_SOCKET) {
|
||||||
|
printf("failed to create socket! (%d)\n", WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("socket created successfully!\n");
|
||||||
|
|
||||||
|
service.sin_family = AF_INET;
|
||||||
|
service.sin_addr.s_addr = inet_addr(address);
|
||||||
|
service.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (bind(sock, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR) {
|
||||||
|
closesocket(sock);
|
||||||
|
printf("failed to bind to %s:%d: %ld\n", address, port, WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
result = connect(sock, (SOCKADDR*) &service, sizeof(client_service));
|
||||||
|
if (result == SOCKET_ERROR) {
|
||||||
|
closesocket(sock);
|
||||||
|
printf("unable to connect to the server: %ld\n", WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (listen(sock, 1) == SOCKET_ERROR) {
|
||||||
|
closesocket(sock);
|
||||||
|
printf("failed to bind to %s:%d: %ld\n", address, port, WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
char address[INET_ADDRSTRLEN];
|
||||||
|
InetNtop(AF_INET, &(service.sin_addr.s_addr), address, INET_ADDRSTRLEN);
|
||||||
|
ntohs(service.sin_port)
|
||||||
|
*/
|
||||||
|
printf("now listening on %s:%d!\n", address, port);
|
||||||
|
|
||||||
|
char running = 1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
SOCKET accept_sock = accept(sock, NULL, NULL);
|
||||||
|
if (accept_sock == INVALID_SOCKET) {
|
||||||
|
closesocket(sock);
|
||||||
|
printf("failed to accept socket connection: %ld\n", WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
printf("client connected!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
char recvbuf[512];
|
||||||
|
int received = 0;
|
||||||
|
do {
|
||||||
|
received = recv(accept_sock, recvbuf, 512, 0);
|
||||||
|
char end[] = "\r\n\r\n";
|
||||||
|
char lastfour[4];
|
||||||
|
|
||||||
|
if (received > 0) {
|
||||||
|
printf("bytes received: %d\n", received);
|
||||||
|
printf("< ");
|
||||||
|
for (int i = 0; i < received; i++) {
|
||||||
|
printf("%c", recvbuf[i]);
|
||||||
|
if (recvbuf[i] == '\n') {
|
||||||
|
printf("< ");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
lastfour[i] = lastfour[i + 1];
|
||||||
|
}
|
||||||
|
lastfour[3] = recvbuf[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
char matches = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
if (lastfour[i] != end[i]) {
|
||||||
|
matches = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches) break;
|
||||||
|
} else if (received == 0) {
|
||||||
|
printf("connection closed.\n");
|
||||||
|
} else {
|
||||||
|
printf("recv failed: %d\n", WSAGetLastError());
|
||||||
|
}
|
||||||
|
} while (received > 0);
|
||||||
|
|
||||||
|
char *response = (char *)malloc(BUFFER_SIZE * 2 * sizeof(char));
|
||||||
|
size_t response_len;
|
||||||
|
simple_http_ok(response, &response_len);
|
||||||
|
|
||||||
|
send(accept_sock, response, response_len, 0);
|
||||||
|
|
||||||
|
free(response);
|
||||||
|
|
||||||
|
result = closesocket(accept_sock);
|
||||||
|
if (result == SOCKET_ERROR) {
|
||||||
|
printf("failed to close socket properly: %d\n", WSAGetLastError());
|
||||||
|
closesocket(sock);
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} while (running);
|
||||||
|
|
||||||
|
// close responsibly! <3
|
||||||
|
result = closesocket(sock);
|
||||||
|
if (result == SOCKET_ERROR) {
|
||||||
|
printf("failed to close socket properly: %d\n", WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void simple_http_ok(char *response, size_t *response_len) {
|
||||||
|
// build header
|
||||||
|
char *header = (char *)malloc(BUFFER_SIZE * sizeof(char));
|
||||||
|
snprintf(header, BUFFER_SIZE,
|
||||||
|
"HTTP/1.1 200 OK\r\n"
|
||||||
|
"Content-Type: text/plain; charset=utf-8\r\n"
|
||||||
|
"Content-Length: 9\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"meow 😺");
|
||||||
|
|
||||||
|
*response_len = 0;
|
||||||
|
memcpy(response, header, strlen(header));
|
||||||
|
*response_len += strlen(header);
|
||||||
|
|
||||||
|
free(header);
|
||||||
|
}
|
Loading…
Reference in a new issue