Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyitc committed Jun 7, 2021
0 parents commit 9fe1956
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2021, hzyitc
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SRCS=$(wildcard *.c)
DEPS=$(wildcard *.h)
TARGET=mnh_server

FLAGS += -g -fsanitize=address

.PHONY: all
all: $(TARGET)

.PHONY: clean
clean:
$(RM) $(TARGET)

$(TARGET): $(SRCS) $(DEPS)
$(CC) $(FLAGS) -o $@ $(SRCS)
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# mnh_server
[![GitHub release](https://img.shields.io/github/v/tag/hzyitc/mnh_server?label=release)](https://github.com/hzyitc/mnh_server/releases)

[README](README.md) | [中文文档](README_zh.md)

## Introduction

`mnh_server` is a server implement `mnhv1` protocol.

`mnhv1` is a protocol which help to punch nat hole.

## Usage

### Clone and build

```
> git clone https://github.com/hzyitc/mnh_server
> cd mnh_server
> make
```

### Run mnh_server

```
> ./mnh_server <port>
```

## Protocol Details

`mnh_server` will handle `mnhv1` and `HTTP` protocol in same port.

All messages are text.

### mnhv1

Client:
```
mnhv1 {id}\n
```

Server:
```
{ip}:{port}\n
```

Then,

Every 10s, client send a heartbeat to server:
```
heartbeat\n
```

Every time when server recieves a packet, it needs to send a heartbeat back:
```
.
```

### HTTP

Client:
```
GET /{id} {Don't care the following data}
```

Server:
```
HTTP/1.1 200 OK\r\n
Server: mnhv1\r\n
Content-Length: {len}\r\n
\r\n
{ip}:{port}
```

Then, server closes the connection.
74 changes: 74 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# mnh_server
[![GitHub release](https://img.shields.io/github/v/tag/hzyitc/mnh_server?label=release)](https://github.com/hzyitc/mnh_server/releases)

[README](README.md) | [中文文档](README_zh.md)

## 介绍

`mnh_server`是一个实现了`mnhv1`协议的服务器。

`mnhv1`是一种用来协助打NAT洞的协议。

## 使用指南

### 克隆并编译

```
> git clone https://github.com/hzyitc/mnh_server
> cd mnh_server
> make
```

### 运行mnh_server

```
> ./mnh_server <端口>
```

## 协议详解

`mnh_server`会使用同个端口处理`mnhv1``HTTP`协议。

所有消息都是文本格式。

### mnhv1

客户端:
```
mnhv1 {id}\n
```

服务端:
```
{IP}:{端口号}\n
```

然后,

每10s,客户端向服务器发送心跳包:
```
heartbeat\n
```

当服务器收到包后,需要返回一个心跳包:
```
.
```

### HTTP

客户端:
```
GET /{id} {不关心后面的数据}
```

服务端:
```
HTTP/1.1 200 OK\r\n
Server: mnhv1\r\n
Content-Length: {长度}\r\n
\r\n
{IP}:{端口号}
```

然后,服务器关闭连接。
155 changes: 155 additions & 0 deletions client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include "client.h"
#include "epoll.h"
#include "log.h"

#include <unistd.h>
#include <string.h>
#include <errno.h>

#define TIMEOUT 30
#define HTTP_PREFIX "GET /"
#define MNH_PREFIX "mnhv1 "

int str_cmp_head(const char *str, const char *head) {
int ca, cb;
do {
ca = tolower(*str++);
cb = tolower(*head++);
} while (ca == cb && cb != '\0');
return cb ? (ca - cb) : 0;
}

void client_close(int epoll_fd, NODE *node) {
int fd = node->sock;
log_i("clean %d(%s)", fd, node->id);

if(epoll_del(epoll_fd, fd) < 0)
log_e("delete %d from epoll error %d: %s", fd, errno, strerror(errno));

close(fd);
node_free(node);
}

int client_http_sendResponse(int fd, int code, const char *body) {
char *codeMsg;
switch(code) {
case 200: codeMsg = "OK"; break;
case 404: codeMsg = "Not Found"; break;
default: codeMsg = "Unknown"; break;
}

char buffer[2048];
sprintf(buffer,
"HTTP/1.1 %d %s\r\n"
"Server: mnhv1\r\n"
"Content-Length: %lu\r\n"
"\r\n%s",
code, codeMsg,
strlen(body),
body
);
return send(fd, buffer, strlen(buffer), 0);
}

void client_http_handleHandshake(int epoll_fd, NODE *node, void *buf, int len) {
char *p = buf + strlen(HTTP_PREFIX);
char *q = strstr(p, " ");
if(q != NULL) {
*q = 0;

NODE *n = node_queryById(p);
if(n != NULL) {
char body[1024];
sprintf(body, "%s:%d", inet_ntoa(n->address.sin_addr), ntohs(n->address.sin_port));
client_http_sendResponse(node->sock, 200, body);
log_i("%s:%d query \"%s\": %s",
inet_ntoa(node->address.sin_addr), ntohs(node->address.sin_port),
p,
body
);
} else {
// not found
client_http_sendResponse(node->sock, 404, "Not found");
log_i("%s:%d query \"%s\": Not found",
inet_ntoa(node->address.sin_addr), ntohs(node->address.sin_port),
p
);
}
}

client_close(epoll_fd, node);
return;
}

void client_mnh_handleHandshake(int epoll_fd, NODE *node, void *buf, int len) {
char *p = buf + strlen(MNH_PREFIX);
char *q = strstr(p, "\n");
if(q == NULL) {
client_close(epoll_fd, node);
return;
}

*q = 0;
if(strlen(p) > (sizeof(node->id) - 1))
p[sizeof(node->id) - 1] = 0;
strcpy(node->id, p);

log_i("%s:%d register \"%s\"",
inet_ntoa(node->address.sin_addr), ntohs(node->address.sin_port),
p
);

char buffer[1024];
sprintf(buffer, "%s:%d", inet_ntoa(node->address.sin_addr), ntohs(node->address.sin_port));
send(node->sock, buffer, strlen(buffer), 0);

node->state = STATE_MNHV1_HEARTBEAT;
}

void client_handle(int epoll_fd, NODE *node, void *buf, int len) {
node_update(node);

if(node->state == STATE_HANDSHAKE) {
((char *) buf)[len] = 0;
if(str_cmp_head(buf, HTTP_PREFIX) == 0)
client_http_handleHandshake(epoll_fd, node, buf, len);
else if(str_cmp_head(buf, MNH_PREFIX) == 0)
client_mnh_handleHandshake(epoll_fd, node, buf, len);
else
client_close(epoll_fd, node);
} else {
//log_d("%d(%s) recv %dB: %s", node->sock, node->id, len, buf);
log_d("%d(%s) recv %dB: %s", node->sock, node->id, len, "");

send(node->sock, ".", 1, 0);
}
}

void client_handleTCP(int epoll_fd, NODE *node) {
char buf[9001];
int len = read(node->sock, buf, 9000);
if(len < 0) {
log_e("sock %d recv header fail: ret=%d", node->sock, len);
return;
}

client_handle(epoll_fd, node, buf, len);
}

time_t client_getMinAfter() {
NODE *first = node_getFirst();
if(first == NULL)
return -1;

time_t t = TIMEOUT - node_getPast(first);
return (t > 0) ? t : 0;
}

void client_cleanTimeouts(int epoll_fd) {
NODE *node;
while(((node = node_getFirst()) != NULL)
&& (node_getPast(node) > TIMEOUT)) {
log_i("%d(%s) timeout: %lds", node->sock, node->id, node_getPast(node));
client_close(epoll_fd, node);
}
}
13 changes: 13 additions & 0 deletions client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "node.h"

#ifndef __client_h__
#define __client_h__

void client_close(int epoll_fd, NODE *node);
void client_handle(int epoll_fd, NODE *node, void *buf, int len);
void client_handleTCP(int epoll_fd, NODE *node);

time_t client_getMinAfter();
void client_cleanTimeouts(int epoll_fd);

#endif
15 changes: 15 additions & 0 deletions epoll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "epoll.h"

#include <string.h>

int epoll_add(int epoll_fd, int fd, void *data) {
struct epoll_event ev;
memset(&ev, 0, sizeof(struct epoll_event));
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR;
ev.data.ptr = data;
return epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
}

int epoll_del(int epoll_fd, int fd) {
return epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL);
}
9 changes: 9 additions & 0 deletions epoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <sys/epoll.h>

#ifndef __epoll_h__
#define __epoll_h__

int epoll_add(int epoll_fd, int fd, void *data);
int epoll_del(int epoll_fd, int fd);

#endif
Loading

0 comments on commit 9fe1956

Please sign in to comment.