当前位置: 技术问答>linux和unix
linux下的代理服务网络编程
来源: 互联网 发布时间:2015-02-10
本文导语: TC 在编写S/C程序的时候 发现一些头文件没有 如unp.h config.h inet.h 这些都是要自己编的么 另外 有知道关于linux下的 代理服务器程序的编写的大虾么 大家也可以说说自己的想法呢 比如 主机的listen 客机的connect ...
TC 在编写S/C程序的时候 发现一些头文件没有
如unp.h config.h inet.h
这些都是要自己编的么
另外 有知道关于linux下的 代理服务器程序的编写的大虾么
大家也可以说说自己的想法呢
比如 主机的listen 客机的connect 协议的分析 对于http的 文件传输 ftp的文件传输 。。。。。。。。。。。。。。。。。。。。。。
如unp.h config.h inet.h
这些都是要自己编的么
另外 有知道关于linux下的 代理服务器程序的编写的大虾么
大家也可以说说自己的想法呢
比如 主机的listen 客机的connect 协议的分析 对于http的 文件传输 ftp的文件传输 。。。。。。。。。。。。。。。。。。。。。。
|
简易Proxy程序(源代码)
上次贴出了一篇和之后,有不少网友来信询问下文何时出现。我却一直忙于工作,忘记了将下文贴上来,非常抱歉。现在在这里把源代码贴出来。我申明这个源代码基于GNU GPL,目的在于希望大家能够有时间去更加完善它。你也可以按照你自己的希望去改变它。不过,如果你做了任何大的改动,请通知我.
TODO:
1、使程序能够监听多个端口,并且连接多个远程服务。
2、使程序能够从配置文件中读取设定监听端口和所要连接的远程服务地址以及端口以满足多种服务并存
的需要。
sp.c
/*******************************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *******************************************************************************/
/*******************************************************************************
* Program: sp.c
* Description: a smart proxy
* Author: Alan Chen (ariesram@may10.ca)
* Date: July 18, 2001
* *******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#define ERRLOG "./sp.log"
int do_proxy(int infd, char* addr, int port);
int max(int i, int j);
void waitchild(int);
void version();
void usage();
void daemonize();
void p_error(const char * err_msg);
int main(int argc, char** argv) {
struct sockaddr_in servaddr, clientaddr;
int listenfd, connfd;
int clientlen;
pid_t chpid;
int service_port = 0, remote_port = 0;
char remote_addr[17];
const char optstring[] = "s:r:p:vh";
int opt;
extern char *optarg;
extern int optind, opterr, optopt;
extern FILE *stderr;
memset(remote_addr, 0, 17);
if( argc == 2 ) {
while( (opt = getopt(argc, argv, optstring)) != -1 ) {
if( opt == 'v' ) {
version();
exit(0);
}
else if ( opt == 'h' ) {
usage();
exit(0);
}
else {
printf("type sp -h for help message ");
usage();
exit(0);
}
}
}
else {
while( (opt = getopt(argc, argv, optstring)) != -1 ) {
switch(opt) {
case 's':
service_port = atoi(optarg);
break;
case 'r':
memcpy(remote_addr, optarg, strlen(optarg));
remote_addr[strlen(remote_addr)] = '';
break;
case 'p':
remote_port = atoi(optarg);
break;
default:
usage();
exit(0);
}
}
}
if( service_port == 0' 'remote_port == 0' 'remote_addr[0] == '') {
usage();
exit(0);
}
daemonize();
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(service_port);
servaddr.sin_addr.s_addr = INADDR_ANY;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd = 0; i --)
#endif
#ifdef _DEBUG
for (i = 256; i >= 3; i --)
#endif
close(i);
}
void p_error(const char * err_msg)
{
FILE * fp;
#ifdef _DEBUG
printf("%s ", err_msg);
#endif
fp = fopen(ERRLOG, "a");
if (fp == NULL)
return;
fprintf(fp, "%s ", err_msg);
fclose(fp);
}
上次贴出了一篇和之后,有不少网友来信询问下文何时出现。我却一直忙于工作,忘记了将下文贴上来,非常抱歉。现在在这里把源代码贴出来。我申明这个源代码基于GNU GPL,目的在于希望大家能够有时间去更加完善它。你也可以按照你自己的希望去改变它。不过,如果你做了任何大的改动,请通知我.
TODO:
1、使程序能够监听多个端口,并且连接多个远程服务。
2、使程序能够从配置文件中读取设定监听端口和所要连接的远程服务地址以及端口以满足多种服务并存
的需要。
sp.c
/*******************************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *******************************************************************************/
/*******************************************************************************
* Program: sp.c
* Description: a smart proxy
* Author: Alan Chen (ariesram@may10.ca)
* Date: July 18, 2001
* *******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#define ERRLOG "./sp.log"
int do_proxy(int infd, char* addr, int port);
int max(int i, int j);
void waitchild(int);
void version();
void usage();
void daemonize();
void p_error(const char * err_msg);
int main(int argc, char** argv) {
struct sockaddr_in servaddr, clientaddr;
int listenfd, connfd;
int clientlen;
pid_t chpid;
int service_port = 0, remote_port = 0;
char remote_addr[17];
const char optstring[] = "s:r:p:vh";
int opt;
extern char *optarg;
extern int optind, opterr, optopt;
extern FILE *stderr;
memset(remote_addr, 0, 17);
if( argc == 2 ) {
while( (opt = getopt(argc, argv, optstring)) != -1 ) {
if( opt == 'v' ) {
version();
exit(0);
}
else if ( opt == 'h' ) {
usage();
exit(0);
}
else {
printf("type sp -h for help message ");
usage();
exit(0);
}
}
}
else {
while( (opt = getopt(argc, argv, optstring)) != -1 ) {
switch(opt) {
case 's':
service_port = atoi(optarg);
break;
case 'r':
memcpy(remote_addr, optarg, strlen(optarg));
remote_addr[strlen(remote_addr)] = '';
break;
case 'p':
remote_port = atoi(optarg);
break;
default:
usage();
exit(0);
}
}
}
if( service_port == 0' 'remote_port == 0' 'remote_addr[0] == '') {
usage();
exit(0);
}
daemonize();
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(service_port);
servaddr.sin_addr.s_addr = INADDR_ANY;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd = 0; i --)
#endif
#ifdef _DEBUG
for (i = 256; i >= 3; i --)
#endif
close(i);
}
void p_error(const char * err_msg)
{
FILE * fp;
#ifdef _DEBUG
printf("%s ", err_msg);
#endif
fp = fopen(ERRLOG, "a");
if (fp == NULL)
return;
fprintf(fp, "%s ", err_msg);
fclose(fp);
}