2025年goahead5.1.1移植arm Linux开发板

goahead5.1.1移植arm Linux开发板1 goahead 源码获取 链接 https pan baidu com s 1JH60KeTF3Ao 提取码 1111 2 修改源码 2 1 修改 IP 地址 在 http c 中 setLocalHost 中将以下所有类似代码注释掉 struct hostent hp if

大家好,我是讯享网,很高兴认识大家。

1.goahead源码获取

2.修改源码

2.1修改IP地址

在http.c中setLocalHost()中将以下所有类似代码注释掉

struct hostent *hp; if ((hp = gethostbyname(host)) == NULL) { error("Cannot get host address for host %s: errno %d", host, errno); return -1; } memcpy((char*) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length); ipaddr = inet_ntoa(intaddr); 

讯享网

在注释代码后加入以下代码

讯享网/*struct hostent *hp; if ((hp = gethostbyname(host)) == NULL) { error("Cannot get host address for host %s: errno %d", host, errno); return -1; } memcpy((char*) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length); ipaddr = inet_ntoa(intaddr);*/ ipaddr="0.0.0.0"; 

2.2上传文件相关代码修改

(2) 文件大小限制,goahead-linux-default-me.h里(goahead-linux-default-me.h会替换me.h)ME_GOAHEAD_LIMIT_POST的大小调大,否则上传大文件无法通过。

(3) 临时文件存放路径 #define ME_GOAHEAD_UPLOAD_DIR "tmp"该为#define ME_GOAHEAD_UPLOAD_DIR “/tmp”,上传后的文件会保存在/tmp目录下,路径可以自主调整。

3.交叉编译

本人交叉编译器:arm-linux-gcc 

make CC=arm-linux-gcc LD=arm-linux-ld

注意:当编译提示/home/coidea/goahead-5.1.1/src/goahead.c:196:对‘__stack_chk_fail’未定义的引用
/home/coidea/goahead-5.1.1/src/goahead.c:196:对‘__stack_chk_guard’未定义的引用时需要修改projects/goahead-linux-default.mk,修改标红处,关闭堆栈保护


讯享网

拷贝goahead和libgo.so到开发板

 拷贝self.key  self.crt route.txt   auth.txt到开发板

 把goahead self.key self.crt route.txt auth.txt 放在开发板同一目录下

把libgo.so放在开发板/lib目录下

在存放goahead的目录下新建一个tmp目录,用于存放上传文件的临时文件夹,例如如图所示

 我的www目录存放的是html文件里面有一个cgi-bin目录用于存放cgi程序

4.修改route.txt

 5.启动goahaed

6.goahead之文件传输

1.system.html

<style>
  form {
    margin: 0 auto;
    width: 400px;
  }
</style>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>led control</title>
  <script>
  function fileForm() {
      var fileInput = document.getElementById("file_path");
      if (fileInput.files.length === 0) {
        alert("请选择一个文件");
        return false;
      }
      return true;
    }
  </script>
</head>

<body>
 

  <h2 align="center">远程升级</h2>
  <!--新建一个表单,动作链接到开发板的cgi-bin/upgrade.cgi,采用的方法为POST-->
  <form action="cgi-bin/aa.cgi" method="post" enctype="multipart/form-data" onsubmit="return fileForm()">
    <p align="center">固件存储路径:<input type="file" name="file_path" id="file_path" required/></p>
    <p align="center"><input type="submit" name="upgrade" value="启动" /></p>
  </form>

</body>
</html>

 2.aa.c交叉编译为aa.cgi

讯享网#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UPLOAD_DIR "/app/goahead/"  // 设置文件保存的目录

int main(int argc, char argv)
{
	// 获取请求方法
	printf("Content-Type: text/html\n\n");
	char *requestMethod = getenv("REQUEST_METHOD");
	if (requestMethod == NULL || strcmp(requestMethod, "POST") != 0) {
		printf("Status: 405 Method Not Allowed\n\n");
		printf("Method Not Allowed");
		return 0;
	}

	// 获取上传的文件数据
	char *contentLengthStr = getenv("CONTENT_LENGTH");
	printf("contentLengthStr:%s\n\n",contentLengthStr);
	if (contentLengthStr == NULL) {
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		return 0;
	}

	long contentLength = strtol(contentLengthStr, NULL, 10);//把长度转换为long类型
	printf("contentLength:%ld\n\n<br>",contentLength );
	if (contentLength <= 0) {
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		return 0;
	}

	char *fileData = malloc(contentLength);
	
	// 获取文件名
	//	char *filename = getenv("HTTP_X_FILENAME");
	char *filename =getenv("FILE_CLIENT_FILENAME_file_path");
	printf("file_name=%s\n\n<br>",filename);
	if (filename == NULL) {
		printf("Status: 400 Bad Request\n\n<br>");
		printf("Bad Request");
		free(fileData);
		return 0;
	}
    //读取临时文件的文件名
	char *tmp = getenv("FILE_FILENAME_file_path");
	printf("tmp:%s\n\n",tmp);
	if (tmp == NULL) {
		printf("    the tmp is null\n\n<br>");
		printf("Status: 400 Bad Request\n\n");
		printf("Bad Request");
		free(fileData);
		return 0;
	}
	char *tempfile[128];
	sprintf(tempfile,"/app/goahead/%s",tmp);
	printf("tempfile:%s\n\n",tempfile);
	
	// 保存文件到指定目录
	char destPath[128];
	char cmd[128];
	snprintf(destPath, sizeof(destPath), "%s%s", UPLOAD_DIR, filename);
	sleep(3);
	sprintf(cmd,"cp %s %s",tempfile,destPath);
	system(cmd);
	FILE *file = fopen(destPath, "r");
	if(file == NULL){
		printf("the file is null<br>");
		return 0;
	}
	
	fseek(file,0,SEEK_END);
	int fileSize = ftell(file);
	printf("the file size is %d<br>",fileSize);
	fseek(file,0,SEEK_SET);
	fread(fileData,1,fileSize,file);
	printf("<br>context:<br>%s<br>",fileData);
	

	printf("Content-Type: text/html\n\n");
	printf("<html><body>");
	printf("File uploaded successfully!");
	printf("</body></html>");

	return 0;
}

网页上传上来的文件在一个临时文件夹里,我的临时文件夹是/app/goahead/tmp/

 FILE_FILENAME_file_path环境变量存储了临时文件的路径

只需要用getenv解析 FILE_FILENAME_file_path即可

小讯
上一篇 2025-03-18 19:39
下一篇 2025-01-29 18:04

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/30096.html