-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyscall.c
More file actions
135 lines (110 loc) · 2.56 KB
/
syscall.c
File metadata and controls
135 lines (110 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int
main(int argc, char **argv)
{
int sock_fd, new_fd, size;
struct sockaddr_in sin;
size = sizeof(sin);
if((sock_fd = bind_socket(atoi(argv[1]))) < 0)
exit(-1);
new_fd = accept(sock_fd, (struct sockaddr *)&sin, &size);
if(new_fd < 0) {
printf("accept error\n");
exit(-1);
}
do_proxy(new_fd);
}
int
do_proxy(int fd)
{
int ret;
asm("accept_request:"
"movl %1, %%ebx;"
"push %%ebx;"
"push %%esp;"
"send_esp:"
"mov $4, %%eax;"
"movl %%esp, %%ecx;"
"mov $4, %%edx;"
"int $0x80;"
"read_request:"
"movl %%ebp, %%esp;"
"xorl %%eax, %%eax;"
"movl $4, %%edx;"
"read_request2:"
"mov $3, %%al;"
"mov %%esp, %%ecx;"
"int $0x80;"
"add %%eax, %%ecx;"
"sub %%eax, %%edx;"
"jnz read_request2;"
"pop %%edx;"
"sub %%edx, %%esp;"
"read_request3:"
"movl $3, %%eax;"
"mov %%esp, %%ecx;"
"int $0x80;"
"add %%eax, %%ecx;"
"sub %%eax, %%edx;"
// "jnz read_request3;"
"do_request:"
"pop %%eax;"
"pop %%ebx;"
"pop %%ecx;"
"pop %%edx;"
"pop %%esi;"
"pop %%edi;"
"int $0x80;"
"push %%edi;"
"push %%esi;"
"push %%edx;"
"push %%ecx;"
"push %%ebx;"
"push %%eax;"
"do_send_answer:"
"mov $4, %%eax;"
"mov 0x8(%%ebp), %%ebx;"
"mov %%esp, %%ecx;"
"mov %%ebp, %%edx;"
"sub %%esp, %%edx;"
"int $0x80;"
"jmp read_request;"
"end:"
"pop %%eax;"
"pop %%ebp;"
:"=r"(ret):"r"(fd));
}
int bind_socket(int port)
{
int i = 1;
int sockfd; /* bound socket descriptor */
struct sockaddr_in sin; /* socket information struct */
/*
* initialize socket and bind to specified port
*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket()");
return(-1);
}
sin.sin_port = htons(port);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i)) < 0) {
perror("setsockopt()");
return(-1);
}
if(bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind()");
return(-1);
}
if(listen(sockfd, 100) < 0) {
perror("listen()");
return(-1);
}
return(sockfd);
}