91{
93
94 int fd =
socket(AF_INET, SOCK_DGRAM, 0);
95
96 if (fd < 0) {
97 cm_msg(
MERROR,
"open_udp_socket",
"socket(AF_INET,SOCK_DGRAM) returned %d, errno %d (%s)", fd, errno, strerror(errno));
98 return -1;
99 }
100
101 int opt = 1;
102 status = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(opt));
103
105 cm_msg(
MERROR,
"open_udp_socket",
"setsockopt(SOL_SOCKET,SO_REUSEADDR) returned %d, errno %d (%s)",
status, errno, strerror(errno));
106 return -1;
107 }
108
109 int bufsize = 8*1024*1024;
110
111
112 status = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize,
sizeof(bufsize));
113
115 cm_msg(
MERROR,
"open_udp_socket",
"setsockopt(SOL_SOCKET,SO_RCVBUF) returned %d, errno %d (%s)",
status, errno, strerror(errno));
116 return -1;
117 }
118
119 struct sockaddr_in local_addr;
120 memset(&local_addr, 0, sizeof(local_addr));
121
122 local_addr.sin_family = AF_INET;
123 local_addr.sin_port = htons(server_port);
124 local_addr.sin_addr.s_addr = INADDR_ANY;
125
126 status = bind(fd, (
struct sockaddr *)&local_addr,
sizeof(local_addr));
127
129 cm_msg(
MERROR,
"open_udp_socket",
"bind(port=%d) returned %d, errno %d (%s)", server_port,
status, errno, strerror(errno));
130 return -1;
131 }
132
133 int xbufsize = 0;
134 unsigned size = sizeof(xbufsize);
135
136 status = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &xbufsize, &size);
137
138
139
141 cm_msg(
MERROR,
"open_udp_socket",
"getsockopt(SOL_SOCKET,SO_RCVBUF) returned %d, errno %d (%s)",
status, errno, strerror(errno));
142 return -1;
143 }
144
145 cm_msg(
MINFO,
"open_udp_socket",
"UDP port %d socket receive buffer size is %d", server_port, xbufsize);
146
147 return fd;
148}