-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketoptions.c
208 lines (181 loc) · 5.59 KB
/
socketoptions.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Write a program that reads and prints to the screen the default values for following options for a socket (use the appropriate socket if the semantic is specific to one protocol).
//
//
// For the options that are available for the two protocols use the following format:
//
//
//
// SO_RCVLOWAT_TCP=size
// SO_RCVLOWAT_UDP=size
// Always print the TCP value first and use the order given above, also one option per line with all values related to that option.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
void printResult(char*, int, int);
void printBits(size_t const, void const * const);
int main(){
int sockfdTCP, sockfdUDP, optlen, value;
struct linger linger_timer;
sockfdTCP = socket(AF_INET, SOCK_STREAM, 0);
sockfdUDP = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfdTCP==-1){
printf("Error creating TCP Socket\n");
return -1;
}
if(sockfdUDP==-1){
printf("Error creating UDP Socket\n");
return -1;
}
optlen = sizeof(value);
value = fcntl (sockfdUDP, F_GETFL, 0);
int val = value & O_NONBLOCK;
if(val == O_NONBLOCK){
printf("O_NONBLOCK= true\n");
}
else{
printf("O_NONBLOCK= false\n");
}
value = 0;
val = 0;
value = fcntl (sockfdUDP, F_GETFL, 0);
val = value & O_ASYNC;
if(val == O_ASYNC){
printf("O_ASYNC= true\n");
}
else{
printf("O_ASYNC= false\n");
}
/******UDP********/
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_BROADCAST, &value, &optlen)==-1){
printf("Error getsockopt(SO_BROADCAST)\n");
}
printResult("SO_BROADCAST_UDP", value, 1);
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_DONTROUTE, &value, &optlen)==-1){
printf("Error getsockopt(SO_DONTROUTE)\n");
}
printResult("SO_DONTROUTE_UDP", value, 1);
/******TCP*********/
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_KEEPALIVE, &value, &optlen)==-1){
printf("Error getsockopt(SO_KEEPALIVE)\n");
}
printResult("SO_KEEPALIVE_TCP", value, 1);
printf("SO_KEEPALIVE_TCP timer=? \n");
//
// value = 0;
// if(getsockopt (sockfdTCP, IPPROTO_TCP, TCP_KEEPALIVE, &value, &optlen)==-1){
// printf("Error getsockopt(TCP_KEEPALIVE)\n");
// }
// printf("TCP_KEEPALIVE_TCP=", value);
//
// value = 0;
// struct timeval tval;
// if(getsockopt (sockfdTCP, IPPROTO_TCP, TCP_KEEPALIVE, &tval, &optlen)==-1){
// printf("Error getsockopt(TCP_KEEPALIVE)\n");
// }
// printf("TCP_KEEPALIVE_TCP timer=", tval.tv_sec);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_LINGER, &value, &optlen)==-1){
printf("Error getsockopt(SO_LINGER)\n");
}
printResult("SO_LINGER_TCP", value, 1);
int t = sizeof(linger_timer);
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_LINGER, &linger_timer, &t)==-1){
printf("Error getsockopt(SO_LINGER)\n");
}
printf("SO_LINGER_TCP timer = %d\n", linger_timer.l_linger);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_RCVBUF, &value, &optlen)==-1){
printf("Error getsockopt(SO_RCVBUF)\n");
}
printResult("SO_RCVBUF_TCP", value, 0);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_SNDBUF, &value, &optlen)==-1){
printf("Error getsockopt(SO_SNDBUF)\n");
}
printResult("SO_SNDBUF_TCP", value, 0);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_RCVLOWAT, &value, &optlen)==-1){
printf("Error getsockopt(SO_RCVLOWAT)\n");
}
printResult("SO_RCVLOWAT_TCP", value, 0);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_SNDLOWAT, &value, &optlen)==-1){
printf("Error getsockopt(SO_SNDLOWAT)\n");
}
printResult("SO_SNDLOWAT_TCP", value, 0);
value = 0;
if(getsockopt (sockfdTCP, SOL_SOCKET, SO_REUSEADDR, &value, &optlen)==-1){
printf("Error getsockopt(SO_REUSEADDR)\n");
}
printResult("SO_REUSEADDR_TCP", value, 1);
value = 0;
if(getsockopt (sockfdTCP, IPPROTO_TCP, TCP_MAXSEG, &value, &optlen)==-1){
printf("Error getsockopt(TCP_MAXSEG)\n");
}
printResult("TCP_MAXSEG_TCP", value, 0);
value = 0;
if(getsockopt (sockfdTCP, IPPROTO_TCP, TCP_NODELAY, &value, &optlen)==-1){
printf("Error getsockopt(TCP_NODELAY)\n");
}
printResult("TCP_NODELAY_TCP", value, 1);
/*****UDP******/
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_RCVBUF, &value, &optlen)==-1){
printf("Error getsockopt(SO_RCVBUF)\n");
}
printResult("SO_RCVBUF_UDP", value, 0);
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_SNDBUF, &value, &optlen)==-1){
printf("Error getsockopt(SO_SNDBUF)\n");
}
printResult("SO_SNDBUF_UDP", value, 0);
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_RCVLOWAT, &value, &optlen)==-1){
printf("Error getsockopt(SO_RCVLOWAT)\n");
}
printResult("SO_RCVLOWAT_UDP", value, 0);
value = 0;
if(getsockopt (sockfdUDP, SOL_SOCKET, SO_SNDLOWAT, &value, &optlen)==-1){
printf("Error getsockopt(SO_SNDLOWAT)\n");
}
printResult("SO_SNDLOWAT_UDP", value, 0);
}
void printResult(char* str, int value, int bool){
// bool = 0 : pinrint int, 1: print boolean
if(bool==0){
printf("%s= %d\n", str, value);
}
else if(bool==1 && value==0){
printf("%s= false\n", str);
}
else if(bool==1 && value==1){
printf("%s= true\n", str);
}
else{
printf("error\n");
}
}
// function from https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}